create requirements.txt
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import easyocr
|
2 |
+
import gradio as gr
|
3 |
+
import re
|
4 |
+
|
5 |
+
# Initialize EasyOCR reader with English and Hindi languages
|
6 |
+
reader = easyocr.Reader(['en', 'hi'])
|
7 |
+
|
8 |
+
# Define the OCR function
|
9 |
+
def ocr_text(image):
|
10 |
+
"""
|
11 |
+
Performs OCR on the given image and returns the extracted text.
|
12 |
+
Args:
|
13 |
+
image: The image to perform OCR on.
|
14 |
+
Returns:
|
15 |
+
The extracted text and the original image.
|
16 |
+
"""
|
17 |
+
result = reader.readtext(image)
|
18 |
+
full_text = ' '.join([text for _, text, _ in result])
|
19 |
+
return full_text, image
|
20 |
+
|
21 |
+
# Define the text highlighting function
|
22 |
+
def highlight_text(full_text, search_term):
|
23 |
+
"""
|
24 |
+
Highlights the search term in the given text.
|
25 |
+
Args:
|
26 |
+
full_text: The text to search in.
|
27 |
+
search_term: The term to search for.
|
28 |
+
Returns:
|
29 |
+
The text with the search term highlighted.
|
30 |
+
"""
|
31 |
+
if search_term:
|
32 |
+
highlighted_text = re.sub(f"({search_term})", r"<mark>\1</mark>", full_text, flags=re.IGNORECASE)
|
33 |
+
return highlighted_text
|
34 |
+
return full_text
|
35 |
+
|
36 |
+
# Define the Gradio interface
|
37 |
+
with gr.Blocks() as demo:
|
38 |
+
image_input = gr.Image(type="filepath")
|
39 |
+
extracted_text = gr.Textbox(lines=5, placeholder="Extracted text will appear here...")
|
40 |
+
ocr_button = gr.Button("Extract Text")
|
41 |
+
ocr_button.click(fn=ocr_text, inputs=image_input, outputs=[extracted_text, image_input])
|
42 |
+
|
43 |
+
search_term = gr.Textbox(lines=1, placeholder="Enter search term")
|
44 |
+
highlighted_text = gr.HTML()
|
45 |
+
search_button = gr.Button("Search")
|
46 |
+
search_button.click(fn=highlight_text, inputs=[extracted_text, search_term], outputs=highlighted_text)
|
47 |
+
|
48 |
+
demo.launch(share=True)
|