Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -28,7 +28,7 @@ def load_model():
|
|
28 |
vlm = load_model()
|
29 |
|
30 |
# OCR function to extract text from an image
|
31 |
-
def ocr_image(image, query="Extract text from the image"):
|
32 |
messages = [
|
33 |
{
|
34 |
"role": "user",
|
@@ -59,22 +59,34 @@ def ocr_image(image, query="Extract text from the image"):
|
|
59 |
generated_ids_trimmed = [
|
60 |
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
61 |
]
|
|
|
62 |
output_text = processor.batch_decode(
|
63 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
64 |
-
)
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
# Gradio interface
|
68 |
-
def process_image(image):
|
69 |
-
return ocr_image(image)
|
70 |
|
71 |
-
#
|
72 |
interface = gr.Interface(
|
73 |
fn=process_image,
|
74 |
-
inputs=
|
|
|
|
|
|
|
75 |
outputs="text",
|
76 |
-
title="Hindi & English OCR",
|
77 |
-
description="Upload an image containing text in Hindi or English to extract the text using OCR."
|
78 |
)
|
79 |
|
80 |
# Launch Gradio interface in Colab
|
|
|
28 |
vlm = load_model()
|
29 |
|
30 |
# OCR function to extract text from an image
|
31 |
+
def ocr_image(image, query="Extract text from the image", keyword=""):
|
32 |
messages = [
|
33 |
{
|
34 |
"role": "user",
|
|
|
59 |
generated_ids_trimmed = [
|
60 |
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
61 |
]
|
62 |
+
|
63 |
output_text = processor.batch_decode(
|
64 |
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
65 |
+
)[0]
|
66 |
+
|
67 |
+
if keyword:
|
68 |
+
keyword_lower = keyword.lower()
|
69 |
+
if keyword_lower in output_text.lower():
|
70 |
+
highlighted_text = output_text.replace(keyword, f"**{keyword}**")
|
71 |
+
return f"Keyword '{keyword}' found in the text:\n\n{highlighted_text}"
|
72 |
+
else:
|
73 |
+
return f"Keyword '{keyword}' not found in the text:\n\n{output_text}"
|
74 |
+
else:
|
75 |
+
return output_text
|
76 |
|
77 |
# Gradio interface
|
78 |
+
def process_image(image, keyword=""):
|
79 |
+
return ocr_image(image, keyword=keyword)
|
80 |
|
81 |
+
# Update the Gradio interface:
|
82 |
interface = gr.Interface(
|
83 |
fn=process_image,
|
84 |
+
inputs=[
|
85 |
+
gr.Image(type="pil"),
|
86 |
+
gr.Textbox(label="Enter keyword to search (optional)")
|
87 |
+
],
|
88 |
outputs="text",
|
89 |
+
title="Hindi & English OCR with Keyword Search",
|
|
|
90 |
)
|
91 |
|
92 |
# Launch Gradio interface in Colab
|