OCR_Smart / app_gradio.py
Mariem-Daha's picture
Upload 10 files
3d7858b verified
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
)