Update module_ocr.py
Browse files6 unmodified lines7891011123 unmodified lines1617181920212 unmodified lines2425262728293015 unmodified lines4647484950516 unmodified linesimport gradio as grimport ocr## Process one file3 unmodified lines"""return ocr.process_file(input_file)## User interface2 unmodified lines# Upload file to processwith gr.Row():input_file = gr.File(label="Upload a PDF file", scale=1)output_text = gr.Textbox(label="OCR output", scale=2)# Buttons15 unmodified lineslabel="Examples")# Functionsocr_btn.click(fn=process,6 unmodified lines78910111213143 unmodified lines1819202122232425262728293031323334353637383940414243444546474849505152535455565758596061622 unmodified lines6566676869707172737475767715 unmodified lines939495969798991001011021031041056 unmodified linesimport gradio as grimport ocrimport pdf2imageimport tempfile## Process one file3 unmodified lines"""return ocr.process_file(input_file)## Preview the document (image or PDF)#def preview_file(file):if file is None:return None, Nonefile_path = file.namefile_extension = file_path.lower().split('.')[-1]if file_extension in ['jpg', 'jpeg', 'png', 'gif', 'bmp']:# For images, return the image directlyreturn file_path, Noneelif file_extension == 'pdf':# For PDFs, convert first page to image using pdf2imagetry:# Convert only the first page for previewpages = pdf2image.convert_from_path(file_path,first_page=1,last_page=1,dpi=150 # Good quality for preview)if pages:# Save the first page as a temporary imagewith tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp_file:pages[0].save(tmp_file.name, 'PNG')return tmp_file.name, f"PDF Preview: {os.path.basename(file_path)}"else:return None, "<p>Could not convert PDF to image</p>"except Exception as e:return None, f"<p>Error previewing PDF: {str(e)}</p>"else:return None, f"<p>Preview not available for {file_extension} files</p>"## User interface2 unmodified lines# Upload file to processwith gr.Row():with gr.Column():input_file = gr.File(label="Upload a PDF or image file",file_types=[".pdf", ".jpg", ".jpeg", ".png", ".gif", ".bmp"],scale=1)preview_image = gr.Image(label="Preview", show_label=True)preview_text = gr.HTML(label="Status")output_text = gr.Textbox(label="OCR output", scale=2)# Buttons15 unmodified lineslabel="Examples")# Update preview when file is uploadedinput_file.change(fn=preview_file,inputs=[input_file],outputs=[preview_image, preview_text])# Functionsocr_btn.click(fn=process,
module_ocr.py+55-1