Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pytesseract
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# Function to extract text from the uploaded image
|
6 |
+
def extract_text(image):
|
7 |
+
text = pytesseract.image_to_string(image, lang='eng+hin') # Extract text using Tesseract
|
8 |
+
return text
|
9 |
+
|
10 |
+
# Function to extract text and perform keyword search
|
11 |
+
def ocr_and_search(image, keyword):
|
12 |
+
extracted_text = extract_text(image) # Get the extracted text
|
13 |
+
if keyword:
|
14 |
+
# Search for the keyword in the extracted text
|
15 |
+
matching_lines = [line for line in extracted_text.splitlines() if keyword.lower() in line.lower()]
|
16 |
+
return extracted_text, matching_lines # Return both extracted text and matching lines
|
17 |
+
else:
|
18 |
+
return extracted_text, [] # If no keyword, return empty list for matches
|
19 |
+
|
20 |
+
# Create a Gradio interface
|
21 |
+
interface = gr.Interface(
|
22 |
+
fn=ocr_and_search,
|
23 |
+
inputs=[
|
24 |
+
gr.Image(type="pil", label="Upload Image"), # Input for image upload
|
25 |
+
gr.Textbox(label="Enter Keyword") # Input for keyword search
|
26 |
+
],
|
27 |
+
outputs=[
|
28 |
+
gr.Textbox(label="Extracted Text"), # Output for extracted text
|
29 |
+
gr.Textbox(label="Matching Lines") # Output for matching lines based on keyword search
|
30 |
+
],
|
31 |
+
title="OCR with Keyword Search",
|
32 |
+
description="Upload an image and enter a keyword to search within the extracted text."
|
33 |
+
)
|
34 |
+
|
35 |
+
# Launch the app with share=True to create a public link
|
36 |
+
if __name__ == "__main__":
|
37 |
+
interface.launch(share=True) # Set share=True to generate a public URL
|