Update module_ocr.py
Browse files- module_ocr.py +11 -12
module_ocr.py
CHANGED
@@ -106,7 +106,9 @@ def process(
|
|
106 |
|
107 |
return output_text, output_pdf
|
108 |
|
109 |
-
|
|
|
|
|
110 |
def preview_file(file):
|
111 |
if file is None:
|
112 |
return None, None
|
@@ -131,12 +133,9 @@ def preview_file(file):
|
|
131 |
|
132 |
if pages:
|
133 |
# Save the first page as a temporary image
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
output_path = os.path.join(output_dir, output_path)
|
138 |
-
pages[0].save(output_path.name, 'PNG')
|
139 |
-
return output_path.name.name, f"PDF Preview: {os.path.basename(file_path)}"
|
140 |
else:
|
141 |
return None, "<p>Could not convert PDF to image</p>"
|
142 |
|
@@ -214,11 +213,11 @@ with gr.Blocks() as demo:
|
|
214 |
""")
|
215 |
|
216 |
# Update preview when file is uploaded
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
|
223 |
# Functions
|
224 |
ocr_btn.click(
|
|
|
106 |
|
107 |
return output_text, output_pdf
|
108 |
|
109 |
+
#
|
110 |
+
# Preview the document (image or PDF)
|
111 |
+
#
|
112 |
def preview_file(file):
|
113 |
if file is None:
|
114 |
return None, None
|
|
|
133 |
|
134 |
if pages:
|
135 |
# Save the first page as a temporary image
|
136 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp_file:
|
137 |
+
pages[0].save(tmp_file.name, 'PNG')
|
138 |
+
return tmp_file.name, f"PDF Preview: {os.path.basename(file_path)}"
|
|
|
|
|
|
|
139 |
else:
|
140 |
return None, "<p>Could not convert PDF to image</p>"
|
141 |
|
|
|
213 |
""")
|
214 |
|
215 |
# Update preview when file is uploaded
|
216 |
+
input_file.change(
|
217 |
+
fn=preview_file,
|
218 |
+
inputs=[input_file],
|
219 |
+
outputs=[preview_image, preview_text]
|
220 |
+
)
|
221 |
|
222 |
# Functions
|
223 |
ocr_btn.click(
|