Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,175 +1,139 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
from
|
4 |
-
|
5 |
-
import
|
6 |
-
from PIL import Image
|
7 |
-
import
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
highlighted_text = buffer
|
141 |
-
yield highlighted_text
|
142 |
-
|
143 |
-
|
144 |
-
css = """
|
145 |
-
#output {
|
146 |
-
height: 500px;
|
147 |
-
overflow: auto;
|
148 |
-
border: 1px solid #ccc;
|
149 |
-
}
|
150 |
-
"""
|
151 |
-
|
152 |
-
with gr.Blocks(css=css) as demo:
|
153 |
-
gr.Markdown(DESCRIPTION)
|
154 |
-
|
155 |
-
with gr.Tab(label="Image/Video Input"):
|
156 |
-
with gr.Row():
|
157 |
-
with gr.Column():
|
158 |
-
input_media = gr.File(
|
159 |
-
label="Upload Image or Video", type="filepath"
|
160 |
-
)
|
161 |
-
search_word = gr.Textbox(
|
162 |
-
label="Search Word", placeholder="Enter word to highlight", lines=1
|
163 |
-
)
|
164 |
-
submit_btn = gr.Button(value="Submit")
|
165 |
-
with gr.Column():
|
166 |
-
# Use HTML component to display highlighted text
|
167 |
-
output_text = gr.HTML(label="Output Text")
|
168 |
-
|
169 |
-
submit_btn.click(
|
170 |
-
qwen_inference,
|
171 |
-
inputs=[input_media, search_word],
|
172 |
-
outputs=[output_text]
|
173 |
-
)
|
174 |
-
|
175 |
-
demo.launch(debug=True)
|
|
|
1 |
+
from byaldi import RAGMultiModalModel
|
2 |
+
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
|
3 |
+
from qwen_vl_utils import process_vision_info
|
4 |
+
import torch
|
5 |
+
import gradio as gr
|
6 |
+
from PIL import Image
|
7 |
+
import re
|
8 |
+
|
9 |
+
|
10 |
+
# Load models
|
11 |
+
def initialize_models():
|
12 |
+
"""Loads and returns the RAG multimodal and Qwen2-VL models along with the processor."""
|
13 |
+
multimodal_rag = RAGMultiModalModel.from_pretrained("vidore/colpali")
|
14 |
+
qwen_model = Qwen2VLForConditionalGeneration.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", trust_remote_code=True, torch_dtype=torch.float32)
|
15 |
+
qwen_processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", trust_remote_code=True)
|
16 |
+
return multimodal_rag, qwen_model, qwen_processor
|
17 |
+
|
18 |
+
multimodal_rag, qwen_model, qwen_processor = initialize_models()
|
19 |
+
|
20 |
+
# Text extraction function
|
21 |
+
def perform_ocr(image):
|
22 |
+
"""Extracts Sanskrit and English text from an image using the Qwen model."""
|
23 |
+
query = "Extract text from the image in original language"
|
24 |
+
|
25 |
+
# Format the request for the model
|
26 |
+
user_input = [
|
27 |
+
{
|
28 |
+
"role": "user",
|
29 |
+
"content": [
|
30 |
+
{"type": "image", "image": image},
|
31 |
+
{"type": "text", "text": query}
|
32 |
+
]
|
33 |
+
}
|
34 |
+
]
|
35 |
+
|
36 |
+
# Preprocess the input
|
37 |
+
input_text = qwen_processor.apply_chat_template(user_input, tokenize=False, add_generation_prompt=True)
|
38 |
+
image_inputs, video_inputs = process_vision_info(user_input)
|
39 |
+
model_inputs = qwen_processor(
|
40 |
+
text=[input_text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt"
|
41 |
+
).to("cpu") # Use CPU for inference
|
42 |
+
|
43 |
+
# Generate output
|
44 |
+
with torch.no_grad():
|
45 |
+
generated_ids = qwen_model.generate(**model_inputs, max_new_tokens=2000)
|
46 |
+
trimmed_ids = [output[len(input_ids):] for input_ids, output in zip(model_inputs.input_ids, generated_ids)]
|
47 |
+
ocr_result = qwen_processor.batch_decode(trimmed_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
48 |
+
|
49 |
+
return ocr_result
|
50 |
+
|
51 |
+
# Keyword search function
|
52 |
+
def highlight_keyword(text, keyword):
|
53 |
+
"""Searches and highlights the keyword in the extracted text."""
|
54 |
+
keyword_lowercase = keyword.lower()
|
55 |
+
sentences = text.split('. ')
|
56 |
+
results = []
|
57 |
+
|
58 |
+
for sentence in sentences:
|
59 |
+
if keyword_lowercase in sentence.lower():
|
60 |
+
highlighted = re.sub(f'({re.escape(keyword)})', r'<mark>\1</mark>', sentence, flags=re.IGNORECASE)
|
61 |
+
results.append(highlighted)
|
62 |
+
|
63 |
+
return results if results else ["No matches found."]
|
64 |
+
|
65 |
+
# Gradio app for text extraction
|
66 |
+
def extract_text(image):
|
67 |
+
"""Extracts text from an uploaded image."""
|
68 |
+
return perform_ocr(image)
|
69 |
+
|
70 |
+
# Gradio app for keyword search
|
71 |
+
def search_in_text(extracted_text, keyword):
|
72 |
+
"""Searches for a keyword in the extracted text and highlights matches."""
|
73 |
+
results = highlight_keyword(extracted_text, keyword)
|
74 |
+
return "<br>".join(results)
|
75 |
+
|
76 |
+
# Updated title with revised phrasing
|
77 |
+
header_html = """
|
78 |
+
<h1 style="text-align: center; color: #4CAF50;"><span class="gradient-text">OCR and Text Search Prototype</span></h1>
|
79 |
+
"""
|
80 |
+
|
81 |
+
# CSS to fix button sizes
|
82 |
+
custom_css = """
|
83 |
+
.gr-button {
|
84 |
+
width: 200px; /* Set a fixed width for the buttons */
|
85 |
+
padding: 12px 20px; /* Add padding to buttons for consistency */
|
86 |
+
}
|
87 |
+
.gr-textbox {
|
88 |
+
max-height: 300px; /* Set a maximum height for the extracted text output */
|
89 |
+
overflow-y: scroll; /* Enable scrolling when text exceeds the height */
|
90 |
+
}
|
91 |
+
"""
|
92 |
+
|
93 |
+
# Gradio Interface
|
94 |
+
with gr.Blocks(css=custom_css) as interface:
|
95 |
+
|
96 |
+
# Header section
|
97 |
+
gr.HTML(header_html)
|
98 |
+
|
99 |
+
# Sidebar section
|
100 |
+
with gr.Row():
|
101 |
+
with gr.Column(scale=1, min_width=200):
|
102 |
+
gr.Markdown("## Instructions")
|
103 |
+
gr.Markdown("""
|
104 |
+
1. Upload an image containing text.
|
105 |
+
2. Extract the text from the image.
|
106 |
+
3. Search for specific keywords within the extracted text.
|
107 |
+
""")
|
108 |
+
gr.Markdown("### Features")
|
109 |
+
gr.Markdown("""
|
110 |
+
- **OCR**: Extract text from images.
|
111 |
+
- **Keyword Search**: Search and highlight keywords in extracted text.
|
112 |
+
""")
|
113 |
+
|
114 |
+
with gr.Column(scale=3):
|
115 |
+
# Main content in tabs
|
116 |
+
with gr.Tabs():
|
117 |
+
|
118 |
+
# First Tab: Text Extraction
|
119 |
+
with gr.Tab("Extract Text"):
|
120 |
+
gr.Markdown("### Upload an image to extract text:")
|
121 |
+
with gr.Row():
|
122 |
+
image_upload = gr.Image(type="pil", label="Upload Image", interactive=True)
|
123 |
+
with gr.Row():
|
124 |
+
extract_btn = gr.Button("Extract Text")
|
125 |
+
extracted_textbox = gr.Textbox(label="Extracted Text")
|
126 |
+
extract_btn.click(extract_text, inputs=image_upload, outputs=extracted_textbox)
|
127 |
+
|
128 |
+
# Second Tab: Keyword Search
|
129 |
+
with gr.Tab("Search in Extracted Text"):
|
130 |
+
gr.Markdown("### Search for a keyword in the extracted text:")
|
131 |
+
with gr.Row():
|
132 |
+
keyword_searchbox = gr.Textbox(label="Enter Keyword", placeholder="Keyword to search")
|
133 |
+
with gr.Row():
|
134 |
+
search_btn = gr.Button("Search")
|
135 |
+
search_results = gr.HTML(label="Results")
|
136 |
+
search_btn.click(search_in_text, inputs=[extracted_textbox, keyword_searchbox], outputs=search_results)
|
137 |
+
|
138 |
+
# Launch the Gradio App
|
139 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|