Update module_ocr.py
Browse files- module_ocr.py +55 -1
module_ocr.py
CHANGED
|
@@ -7,6 +7,8 @@ Date: 2025-04-06
|
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
import ocr
|
|
|
|
|
|
|
| 10 |
|
| 11 |
#
|
| 12 |
# Process one file
|
|
@@ -16,6 +18,45 @@ def process(input_file: str):
|
|
| 16 |
"""
|
| 17 |
return ocr.process_file(input_file)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
#
|
| 21 |
# User interface
|
|
@@ -24,7 +65,13 @@ with gr.Blocks() as demo:
|
|
| 24 |
|
| 25 |
# Upload file to process
|
| 26 |
with gr.Row():
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
output_text = gr.Textbox(label="OCR output", scale=2)
|
| 29 |
|
| 30 |
# Buttons
|
|
@@ -46,6 +93,13 @@ with gr.Blocks() as demo:
|
|
| 46 |
label="Examples"
|
| 47 |
)
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
# Functions
|
| 50 |
ocr_btn.click(
|
| 51 |
fn=process,
|
|
|
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
import ocr
|
| 10 |
+
import pdf2image
|
| 11 |
+
import tempfile
|
| 12 |
|
| 13 |
#
|
| 14 |
# Process one file
|
|
|
|
| 18 |
"""
|
| 19 |
return ocr.process_file(input_file)
|
| 20 |
|
| 21 |
+
#
|
| 22 |
+
# Preview the document (image or PDF)
|
| 23 |
+
#
|
| 24 |
+
def preview_file(file):
|
| 25 |
+
if file is None:
|
| 26 |
+
return None, None
|
| 27 |
+
|
| 28 |
+
file_path = file.name
|
| 29 |
+
file_extension = file_path.lower().split('.')[-1]
|
| 30 |
+
|
| 31 |
+
if file_extension in ['jpg', 'jpeg', 'png', 'gif', 'bmp']:
|
| 32 |
+
# For images, return the image directly
|
| 33 |
+
return file_path, None
|
| 34 |
+
|
| 35 |
+
elif file_extension == 'pdf':
|
| 36 |
+
# For PDFs, convert first page to image using pdf2image
|
| 37 |
+
try:
|
| 38 |
+
# Convert only the first page for preview
|
| 39 |
+
pages = pdf2image.convert_from_path(
|
| 40 |
+
file_path,
|
| 41 |
+
first_page=1,
|
| 42 |
+
last_page=1,
|
| 43 |
+
dpi=150 # Good quality for preview
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if pages:
|
| 47 |
+
# Save the first page as a temporary image
|
| 48 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp_file:
|
| 49 |
+
pages[0].save(tmp_file.name, 'PNG')
|
| 50 |
+
return tmp_file.name, f"PDF Preview: {os.path.basename(file_path)}"
|
| 51 |
+
else:
|
| 52 |
+
return None, "<p>Could not convert PDF to image</p>"
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return None, f"<p>Error previewing PDF: {str(e)}</p>"
|
| 56 |
+
|
| 57 |
+
else:
|
| 58 |
+
return None, f"<p>Preview not available for {file_extension} files</p>"
|
| 59 |
+
|
| 60 |
|
| 61 |
#
|
| 62 |
# User interface
|
|
|
|
| 65 |
|
| 66 |
# Upload file to process
|
| 67 |
with gr.Row():
|
| 68 |
+
with gr.Column():
|
| 69 |
+
input_file = gr.File(
|
| 70 |
+
label="Upload a PDF or image file",
|
| 71 |
+
file_types=[".pdf", ".jpg", ".jpeg", ".png", ".gif", ".bmp"],
|
| 72 |
+
scale=1)
|
| 73 |
+
preview_image = gr.Image(label="Preview", show_label=True)
|
| 74 |
+
preview_text = gr.HTML(label="Status")
|
| 75 |
output_text = gr.Textbox(label="OCR output", scale=2)
|
| 76 |
|
| 77 |
# Buttons
|
|
|
|
| 93 |
label="Examples"
|
| 94 |
)
|
| 95 |
|
| 96 |
+
# Update preview when file is uploaded
|
| 97 |
+
input_file.change(
|
| 98 |
+
fn=preview_file,
|
| 99 |
+
inputs=[input_file],
|
| 100 |
+
outputs=[preview_image, preview_text]
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
# Functions
|
| 104 |
ocr_btn.click(
|
| 105 |
fn=process,
|