Update module_ocr2.py
Browse files- module_ocr2.py +56 -1
module_ocr2.py
CHANGED
@@ -11,6 +11,9 @@ import gradio as gr
|
|
11 |
import os
|
12 |
import magic
|
13 |
|
|
|
|
|
|
|
14 |
import ocr2 # OCR with software 2.0 models
|
15 |
|
16 |
#
|
@@ -49,7 +52,46 @@ def process(input_file: str):
|
|
49 |
return "Unsupported file type. Please upload a PDF, or an image file."
|
50 |
return ocr2.process(input_file)
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
#
|
54 |
# User interface
|
55 |
#
|
@@ -57,7 +99,14 @@ with gr.Blocks() as demo:
|
|
57 |
|
58 |
# Upload file to process
|
59 |
with gr.Row():
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
output_text = gr.Textbox(label="OCR output", scale=2)
|
62 |
|
63 |
# Buttons
|
@@ -78,6 +127,12 @@ with gr.Blocks() as demo:
|
|
78 |
cache_examples=False,
|
79 |
label="Examples"
|
80 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
# Functions
|
83 |
ocr_btn.click(
|
|
|
11 |
import os
|
12 |
import magic
|
13 |
|
14 |
+
import pdf2image
|
15 |
+
import tempfile
|
16 |
+
|
17 |
import ocr2 # OCR with software 2.0 models
|
18 |
|
19 |
#
|
|
|
52 |
return "Unsupported file type. Please upload a PDF, or an image file."
|
53 |
return ocr2.process(input_file)
|
54 |
|
55 |
+
#
|
56 |
+
# Preview the document (image or PDF)
|
57 |
+
#
|
58 |
+
def preview_file(file):
|
59 |
+
if file is None:
|
60 |
+
return None, None
|
61 |
+
|
62 |
+
file_path = file.name
|
63 |
+
file_extension = file_path.lower().split('.')[-1]
|
64 |
+
|
65 |
+
if file_extension in ['jpg', 'jpeg', 'png', 'gif', 'bmp']:
|
66 |
+
# For images, return the image directly
|
67 |
+
return file_path, None
|
68 |
|
69 |
+
elif file_extension == 'pdf':
|
70 |
+
# For PDFs, convert first page to image using pdf2image
|
71 |
+
try:
|
72 |
+
# Convert only the first page for preview
|
73 |
+
pages = pdf2image.convert_from_path(
|
74 |
+
file_path,
|
75 |
+
first_page=1,
|
76 |
+
last_page=1,
|
77 |
+
dpi=150 # Good quality for preview
|
78 |
+
)
|
79 |
+
|
80 |
+
if pages:
|
81 |
+
# Save the first page as a temporary image
|
82 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp_file:
|
83 |
+
pages[0].save(tmp_file.name, 'PNG')
|
84 |
+
return tmp_file.name, f"PDF Preview: {os.path.basename(file_path)}"
|
85 |
+
else:
|
86 |
+
return None, "<p>Could not convert PDF to image</p>"
|
87 |
+
|
88 |
+
except Exception as e:
|
89 |
+
return None, f"<p>Error previewing PDF: {str(e)}</p>"
|
90 |
+
|
91 |
+
else:
|
92 |
+
return None, f"<p>Preview not available for {file_extension} files</p>"
|
93 |
+
|
94 |
+
|
95 |
#
|
96 |
# User interface
|
97 |
#
|
|
|
99 |
|
100 |
# Upload file to process
|
101 |
with gr.Row():
|
102 |
+
with gr.Column():
|
103 |
+
input_file = gr.File(
|
104 |
+
label="Upload a PDF or an image file",
|
105 |
+
file_types=[".pdf", ".jpg", ".jpeg", ".png", ".gif", ".bmp"],
|
106 |
+
scale=1)
|
107 |
+
preview_image = gr.Image(label="Preview", show_label=True)
|
108 |
+
preview_text = gr.HTML(label="Status")
|
109 |
+
|
110 |
output_text = gr.Textbox(label="OCR output", scale=2)
|
111 |
|
112 |
# Buttons
|
|
|
127 |
cache_examples=False,
|
128 |
label="Examples"
|
129 |
)
|
130 |
+
# Update preview when file is uploaded
|
131 |
+
input_file.change(
|
132 |
+
fn=preview_file,
|
133 |
+
inputs=[input_file],
|
134 |
+
outputs=[preview_image, preview_text]
|
135 |
+
)
|
136 |
|
137 |
# Functions
|
138 |
ocr_btn.click(
|