Spaces:
Sleeping
Sleeping
AlirezaF138
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,32 @@
|
|
1 |
import gradio as gr
|
2 |
import pytesseract
|
3 |
from pdf2image import convert_from_path
|
|
|
4 |
import os
|
5 |
|
6 |
# Function to perform OCR and search for a keyword
|
7 |
-
def
|
8 |
-
# Convert PDF to images
|
9 |
-
images = convert_from_path(pdf_file.name)
|
10 |
-
|
11 |
-
# Initialize a variable to hold the extracted text
|
12 |
extracted_text = ""
|
13 |
keyword_found = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
20 |
# Check if the keyword is in the extracted text
|
21 |
if keyword.lower() in text.lower():
|
22 |
keyword_found = True
|
@@ -31,22 +41,29 @@ def ocr_pdf_and_search_keyword(pdf_file, keyword, lang='fas'): # 'fas': Persian
|
|
31 |
# Create Gradio interface
|
32 |
def gradio_interface():
|
33 |
# Define Gradio inputs and outputs
|
34 |
-
|
|
|
35 |
keyword_input = gr.Textbox(label="Enter Keyword", value="ููุณูู") # Default keyword is 'ููุณูู'
|
36 |
output_text = gr.Textbox(label="Extracted Text", interactive=False)
|
37 |
output_message = gr.Textbox(label="Keyword Search Result", interactive=False)
|
38 |
|
39 |
# Function to process the inputs and return the outputs
|
40 |
-
def process(
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
return extracted_text, result_message
|
43 |
|
44 |
# Create and launch Gradio interface
|
45 |
gr.Interface(fn=process,
|
46 |
-
inputs=[
|
47 |
outputs=[output_text, output_message],
|
48 |
-
title="OCR
|
49 |
-
description="Upload a PDF, enter a keyword, and see the OCR results along with a search for the keyword."
|
50 |
).launch()
|
51 |
|
52 |
# Call the function to create the interface
|
|
|
1 |
import gradio as gr
|
2 |
import pytesseract
|
3 |
from pdf2image import convert_from_path
|
4 |
+
from PIL import Image
|
5 |
import os
|
6 |
|
7 |
# Function to perform OCR and search for a keyword
|
8 |
+
def ocr_and_search(input_file, keyword, lang='fas'): # 'fas': Persian language (Farsi)
|
|
|
|
|
|
|
|
|
9 |
extracted_text = ""
|
10 |
keyword_found = False
|
11 |
+
|
12 |
+
# Check if the input file is a PDF or an image
|
13 |
+
if isinstance(input_file, str) and input_file.endswith('.pdf'): # Check if the file is a PDF
|
14 |
+
# Convert PDF to images
|
15 |
+
images = convert_from_path(input_file)
|
16 |
+
|
17 |
+
# Loop through each image and perform OCR
|
18 |
+
for page_number, image in enumerate(images):
|
19 |
+
text = pytesseract.image_to_string(image, lang=lang)
|
20 |
+
extracted_text += text
|
21 |
|
22 |
+
# Check if the keyword is in the extracted text
|
23 |
+
if keyword.lower() in text.lower():
|
24 |
+
keyword_found = True
|
25 |
+
|
26 |
+
elif isinstance(input_file, Image.Image): # If the input is an image
|
27 |
+
text = pytesseract.image_to_string(input_file, lang=lang)
|
28 |
+
extracted_text = text
|
29 |
+
|
30 |
# Check if the keyword is in the extracted text
|
31 |
if keyword.lower() in text.lower():
|
32 |
keyword_found = True
|
|
|
41 |
# Create Gradio interface
|
42 |
def gradio_interface():
|
43 |
# Define Gradio inputs and outputs
|
44 |
+
input_type = gr.Radio(["PDF", "Image"], label="Choose Input Type", value="PDF") # Option to choose file type
|
45 |
+
file_input = gr.File(label="Upload PDF/Image")
|
46 |
keyword_input = gr.Textbox(label="Enter Keyword", value="ููุณูู") # Default keyword is 'ููุณูู'
|
47 |
output_text = gr.Textbox(label="Extracted Text", interactive=False)
|
48 |
output_message = gr.Textbox(label="Keyword Search Result", interactive=False)
|
49 |
|
50 |
# Function to process the inputs and return the outputs
|
51 |
+
def process(input_type, file, keyword):
|
52 |
+
# Handle PDF and image accordingly
|
53 |
+
if input_type == "PDF":
|
54 |
+
extracted_text, result_message = ocr_and_search(file.name, keyword)
|
55 |
+
else: # Handle image input
|
56 |
+
image = Image.open(file.name) # Open image file
|
57 |
+
extracted_text, result_message = ocr_and_search(image, keyword)
|
58 |
+
|
59 |
return extracted_text, result_message
|
60 |
|
61 |
# Create and launch Gradio interface
|
62 |
gr.Interface(fn=process,
|
63 |
+
inputs=[input_type, file_input, keyword_input],
|
64 |
outputs=[output_text, output_message],
|
65 |
+
title="OCR Keyword Search (PDF/Image)",
|
66 |
+
description="Upload a PDF or Image, enter a keyword, and see the OCR results along with a search for the keyword."
|
67 |
).launch()
|
68 |
|
69 |
# Call the function to create the interface
|