File size: 3,057 Bytes
6034171
 
 
 
 
 
 
e98e646
 
 
6034171
e98e646
 
6034171
 
 
e98e646
6034171
 
 
9cdbbd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6034171
9cdbbd4
 
 
 
 
 
 
 
 
e98e646
9cdbbd4
 
 
 
 
 
 
 
 
 
 
 
 
6034171
9cdbbd4
 
 
 
 
 
 
 
 
 
 
 
 
6034171
 
 
 
 
 
 
9cdbbd4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import gradio as gr
from ocr_processor import extract_text_from_image
from llm_processor import load_llm_model, generate_json_from_text

# Load LLM model on startup
load_llm_model()

def process_invoice(file_bytes):
    if file_bytes is None:
        return "No file uploaded", {"error": "No file uploaded"}
    
    # Step 1: Extract text
    raw_text = extract_text_from_image(file_bytes)
    if not raw_text or "No text detected" in raw_text:
        return raw_text, {"error": "No text could be extracted from the image."}
    
    # Step 2: JSON generation
    json_data = generate_json_from_text(raw_text)
    return raw_text, json_data

# Custom CSS with Tailwind CDN
custom_css = """
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
body {
    font-family: 'Inter', sans-serif;
    background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%);
}
.gradio-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 2rem;
}
h1 {
    font-size: 2.5rem;
    font-weight: 700;
    color: #1f2937;
    text-align: center;
    margin-bottom: 1rem;
}
h3 {
    font-size: 1.25rem;
    color: #4b5563;
    text-align: center;
    margin-bottom: 2rem;
}
.gr-button {
    background-color: #2563eb;
    color: white;
    padding: 0.75rem 1.5rem;
    border-radius: 0.5rem;
    font-weight: 600;
    transition: background-color 0.3s ease;
}
.gr-button:hover {
    background-color: #1e40af;
}
.gr-textbox, .gr-json {
    border-radius: 0.5rem;
    border: 1px solid #d1d5db;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.gr-file-upload {
    border: 2px dashed #d1d5db;
    border-radius: 0.5rem;
    padding: 1rem;
    background-color: white;
}
"""

# Gradio UI
with gr.Blocks(css=custom_css) as demo:
    gr.Markdown(
        """
        # 🧾 Invoice Processing App
        Upload an invoice image to extract **OCR text** and generate **structured JSON** output.
        """,
        elem_classes="text-center"
    )

    with gr.Row(variant="panel"):
        with gr.Column(scale=1):
            input_file = gr.File(
                label="Upload Invoice Image",
                type="binary",
                file_types=[".png", ".jpg", ".jpeg"],
                elem_classes="gr-file-upload"
            )
            process_btn = gr.Button(
                "Process Invoice",
                variant="primary",
                elem_classes="gr-button mt-4 w-full"
            )
    
    with gr.Row(variant="panel"):
        with gr.Column(scale=1):
            raw_text_output = gr.Textbox(
                label="Extracted OCR Text",
                lines=10,
                placeholder="Extracted text will appear here...",
                elem_classes="gr-textbox"
            )
        with gr.Column(scale=1):
            json_output = gr.JSON(
                label="Structured JSON",
                elem_classes="gr-json"
            )

    process_btn.click(
        process_invoice,
        inputs=input_file,
        outputs=[raw_text_output, json_output]
    )

demo.launch()