Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pytesseract
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# Set Tesseract path if needed
|
6 |
+
# pytesseract.pytesseract.tesseract_cmd = "/path/to/tesseract" # Update if needed
|
7 |
+
|
8 |
+
def extract_text_from_image(image_path):
|
9 |
+
img = Image.open(image_path)
|
10 |
+
text = pytesseract.image_to_string(img, lang="eng+hin")
|
11 |
+
return text
|
12 |
+
|
13 |
+
def ocr_and_search(image, keyword):
|
14 |
+
# Extract text from the uploaded image
|
15 |
+
text = extract_text_from_image(image)
|
16 |
+
|
17 |
+
# Highlight the keyword in red if found
|
18 |
+
if keyword.lower() in text.lower():
|
19 |
+
# Use case-insensitive replacement by matching case
|
20 |
+
highlighted_text = text.replace(
|
21 |
+
keyword, f'<span style="color: red; font-weight: bold;">{keyword}</span>'
|
22 |
+
)
|
23 |
+
else:
|
24 |
+
highlighted_text = "Keyword not found"
|
25 |
+
|
26 |
+
return text, highlighted_text
|
27 |
+
|
28 |
+
# Define Gradio interface
|
29 |
+
app = gr.Interface(
|
30 |
+
fn=ocr_and_search,
|
31 |
+
inputs=[gr.Image(type="filepath"), gr.Textbox(label="Keyword")],
|
32 |
+
outputs=[gr.Textbox(label="Extracted Text"), gr.HTML(label="Search Results")]
|
33 |
+
)
|
34 |
+
|
35 |
+
app.launch(share=True)
|