Spaces:
Runtime error
Runtime error
RufusRubin777
commited on
Commit
•
eb0fa3b
1
Parent(s):
5641add
Update app.py
Browse files
app.py
CHANGED
@@ -16,82 +16,75 @@ def load_models():
|
|
16 |
|
17 |
RAG, model, processor = load_models()
|
18 |
|
19 |
-
# Function for OCR
|
20 |
-
def
|
21 |
text_query = "Extract all the text in Sanskrit and English from the image."
|
22 |
-
|
23 |
# Prepare message for Qwen model
|
24 |
-
messages = [
|
25 |
-
{
|
26 |
-
"role": "user",
|
27 |
"content": [
|
28 |
-
{"type": "image", "image": image},
|
29 |
-
{"type": "text", "text": text_query}
|
30 |
-
]
|
31 |
-
}
|
32 |
]
|
33 |
-
|
34 |
# Process the image
|
35 |
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
36 |
image_inputs, video_inputs = process_vision_info(messages)
|
37 |
inputs = processor(
|
38 |
text=[text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt"
|
39 |
).to("cpu") # Use CPU
|
40 |
-
|
41 |
# Generate text
|
42 |
with torch.no_grad():
|
43 |
generated_ids = model.generate(**inputs, max_new_tokens=2000)
|
44 |
generated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]
|
45 |
-
extracted_text = processor.batch_decode(
|
46 |
-
|
47 |
-
)[0]
|
48 |
-
|
49 |
return extracted_text
|
50 |
|
51 |
-
# Function for keyword search
|
52 |
-
def
|
53 |
keyword_lower = keyword.lower()
|
54 |
sentences = extracted_text.split('. ')
|
55 |
matched_sentences = []
|
56 |
-
|
57 |
for sentence in sentences:
|
58 |
if keyword_lower in sentence.lower():
|
59 |
-
highlighted_sentence = re.sub(
|
60 |
-
f'({re.escape(keyword)})', r'<mark>\1</mark>', sentence, flags=re.IGNORECASE
|
61 |
-
)
|
62 |
matched_sentences.append(highlighted_sentence)
|
63 |
-
|
64 |
-
return matched_sentences
|
65 |
|
66 |
# Gradio App
|
67 |
-
def
|
68 |
-
extracted_text =
|
69 |
return extracted_text
|
70 |
|
71 |
-
def
|
72 |
-
search_results =
|
73 |
-
search_results_str = "<br>".join(search_results)
|
74 |
return search_results_str
|
75 |
|
76 |
# Gradio Interface
|
77 |
with gr.Blocks() as iface:
|
78 |
-
extracted_text = gr.State()
|
79 |
-
|
80 |
with gr.Row():
|
81 |
with gr.Column():
|
82 |
image_input = gr.Image(type="pil", label="Upload an Image")
|
83 |
extract_button = gr.Button("Extract Text")
|
84 |
-
|
85 |
-
|
|
|
|
|
86 |
with gr.Column():
|
87 |
keyword_input = gr.Textbox(label="Enter keyword to search in extracted text", placeholder="Keyword")
|
88 |
search_button = gr.Button("Search Keyword")
|
89 |
-
|
90 |
|
91 |
-
|
92 |
-
extract_button.click(fn=extract_text_app, inputs=image_input, outputs=text_output, _js=None)
|
93 |
-
extract_button.click(fn=lambda txt: txt, inputs=text_output, outputs=extracted_text)
|
94 |
-
search_button.click(fn=search_text_app, inputs=[extracted_text, keyword_input], outputs=search_output)
|
95 |
|
96 |
# Launch Gradio App
|
97 |
iface.launch()
|
|
|
16 |
|
17 |
RAG, model, processor = load_models()
|
18 |
|
19 |
+
# Function for OCR
|
20 |
+
def extract_text_from_image(image):
|
21 |
text_query = "Extract all the text in Sanskrit and English from the image."
|
22 |
+
|
23 |
# Prepare message for Qwen model
|
24 |
+
messages = [
|
25 |
+
{
|
26 |
+
"role": "user",
|
27 |
"content": [
|
28 |
+
{"type": "image", "image": image},
|
29 |
+
{"type": "text", "text": text_query}
|
30 |
+
]
|
31 |
+
}
|
32 |
]
|
33 |
+
|
34 |
# Process the image
|
35 |
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
36 |
image_inputs, video_inputs = process_vision_info(messages)
|
37 |
inputs = processor(
|
38 |
text=[text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt"
|
39 |
).to("cpu") # Use CPU
|
40 |
+
|
41 |
# Generate text
|
42 |
with torch.no_grad():
|
43 |
generated_ids = model.generate(**inputs, max_new_tokens=2000)
|
44 |
generated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]
|
45 |
+
extracted_text = processor.batch_decode(generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
46 |
+
|
|
|
|
|
47 |
return extracted_text
|
48 |
|
49 |
+
# Function for keyword search
|
50 |
+
def search_keyword_in_text(extracted_text, keyword):
|
51 |
keyword_lower = keyword.lower()
|
52 |
sentences = extracted_text.split('. ')
|
53 |
matched_sentences = []
|
54 |
+
|
55 |
for sentence in sentences:
|
56 |
if keyword_lower in sentence.lower():
|
57 |
+
highlighted_sentence = re.sub(f'({re.escape(keyword)})', r'<mark>\1</mark>', sentence, flags=re.IGNORECASE)
|
|
|
|
|
58 |
matched_sentences.append(highlighted_sentence)
|
59 |
+
|
60 |
+
return matched_sentences if matched_sentences else ["No matches found."]
|
61 |
|
62 |
# Gradio App
|
63 |
+
def app_extract_text(image):
|
64 |
+
extracted_text = extract_text_from_image(image)
|
65 |
return extracted_text
|
66 |
|
67 |
+
def app_search_keyword(extracted_text, keyword):
|
68 |
+
search_results = search_keyword_in_text(extracted_text, keyword)
|
69 |
+
search_results_str = "<br>".join(search_results)
|
70 |
return search_results_str
|
71 |
|
72 |
# Gradio Interface
|
73 |
with gr.Blocks() as iface:
|
|
|
|
|
74 |
with gr.Row():
|
75 |
with gr.Column():
|
76 |
image_input = gr.Image(type="pil", label="Upload an Image")
|
77 |
extract_button = gr.Button("Extract Text")
|
78 |
+
extracted_text_output = gr.Textbox(label="Extracted Text")
|
79 |
+
|
80 |
+
extract_button.click(app_extract_text, inputs=image_input, outputs=extracted_text_output)
|
81 |
+
|
82 |
with gr.Column():
|
83 |
keyword_input = gr.Textbox(label="Enter keyword to search in extracted text", placeholder="Keyword")
|
84 |
search_button = gr.Button("Search Keyword")
|
85 |
+
search_results_output = gr.HTML(label="Search Results")
|
86 |
|
87 |
+
search_button.click(app_search_keyword, inputs=[extracted_text_output, keyword_input], outputs=search_results_output)
|
|
|
|
|
|
|
88 |
|
89 |
# Launch Gradio App
|
90 |
iface.launch()
|