Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import json | |
| from pathlib import Path | |
| from smart_ocr_pipeline_final import main as process_invoice | |
| # Set page title and description | |
| title = "π§ Smart OCR Pipeline - Full AI Processing" | |
| description = """ | |
| **Advanced Invoice OCR with AI Post-Processing** | |
| This service uses: | |
| - DocTR for text extraction | |
| - GPT-4o-mini Vision for structured data extraction (with image) | |
| - Advanced validation and error correction | |
| - Math verification and auto-correction | |
| **Cost:** ~$0.01-$0.05 per invoice | |
| **Best for:** Complex invoices, highest accuracy needed | |
| """ | |
| def process_invoice_gradio(image): | |
| """Process invoice image and return structured data""" | |
| if image is None: | |
| return "Please upload an image first." | |
| try: | |
| # Save uploaded image temporarily | |
| temp_dir = "temp_uploads" | |
| Path(temp_dir).mkdir(exist_ok=True) | |
| temp_path = os.path.join(temp_dir, "temp_invoice.jpg") | |
| image.save(temp_path) | |
| # Process with OCR pipeline | |
| result = process_invoice(temp_path, temp_dir) | |
| # Format output as JSON | |
| output = json.dumps(result, indent=2, ensure_ascii=False) | |
| return output | |
| except Exception as e: | |
| return f"Error processing invoice: {str(e)}" | |
| # Create Gradio interface | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown(f"# {title}") | |
| gr.Markdown(description) | |
| with gr.Row(): | |
| with gr.Column(): | |
| image_input = gr.Image( | |
| type="pil", | |
| label="Upload Invoice Image", | |
| sources=["upload", "clipboard"] | |
| ) | |
| submit_btn = gr.Button("Process Invoice", variant="primary") | |
| with gr.Column(): | |
| output = gr.Textbox( | |
| label="Extracted Data (JSON)", | |
| lines=20, | |
| max_lines=30 | |
| ) | |
| # Examples | |
| gr.Markdown("### π Features:") | |
| gr.Markdown(""" | |
| - β Image preprocessing (deskew, denoise, enhance) | |
| - β DocTR OCR extraction | |
| - β GPT-4o-mini Vision post-processing | |
| - β Automatic validation and error correction | |
| - β Math verification | |
| - β Structured JSON output | |
| """) | |
| # Event handler | |
| submit_btn.click( | |
| fn=process_invoice_gradio, | |
| inputs=image_input, | |
| outputs=output | |
| ) | |
| # Launch with authentication (optional) | |
| if __name__ == "__main__": | |
| demo.launch( | |
| share=False, | |
| server_name="0.0.0.0", | |
| server_port=7860 | |
| ) |