File size: 2,028 Bytes
b6000fa
c0f6ba9
 
ea92b60
02c2338
 
 
76cd12d
1904191
2be5a11
ea92b60
a1a3598
1904191
a1a3598
 
1904191
02c2338
2be5a11
1904191
02c2338
 
 
2be5a11
1904191
02c2338
 
a1a3598
1904191
ea92b60
 
1904191
02c2338
2be5a11
1904191
02c2338
3c316aa
1904191
2be5a11
1904191
 
02c2338
 
a1a3598
1904191
a1a3598
 
2be5a11
02c2338
 
 
1904191
 
02c2338
2be5a11
1904191
d0f6b5d
2be5a11
 
 
c0f6ba9
c196002
2be5a11
 
 
 
 
c0f6ba9
1904191
b6000fa
c196002
02c2338
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
import gradio as gr
import ai_engine
import zoho_client
import time
import sys

sys.stdout.reconfigure(line_buffering=True)

def process_pipeline(file):
    logs = "--- 🚀 STARTING INVOICE SYNC ---\n"
    
    if file is None:
        yield None, "❌ Please upload a file first."
        return

    # 1. OCR
    try:
        logs += "👁️ Scanning Document...\n"
        yield None, logs
        text, img, meta = ai_engine.perform_ocr(file)
        
        if not text:
            logs += "❌ OCR Failed. No text.\n"
            yield None, logs
            return
    except Exception as e:
        logs += f"❌ OCR Error: {e}\n"
        yield None, logs
        return

    # 2. AI Extraction
    try:
        logs += "🧠 AI Extracting Data...\n"
        yield img, logs
        ai_output = ai_engine.extract_intelligent_json(text, meta)
        
        items = ai_output.get('data', {}).get('line_items', [])
        logs += f"   -> Extracted {len(items)} items.\n"
        logs += "----------------------------------\n"
        yield img, logs
        
    except Exception as e:
        logs += f"❌ AI Error: {e}\n"
        yield img, logs
        return

    # 3. Zoho Execution (Linear)
    try:
        for update in zoho_client.route_and_execute(ai_output):
            logs += update
            yield img, logs
            time.sleep(0.1)
    except Exception as e:
        logs += f"\n❌ Execution Error: {e}"
        yield img, logs

with gr.Blocks(title="Zoho Invoice Agent") as demo:
    gr.Markdown("## 🧾 Zoho Invoice Agent (Linear Mode)")
    gr.Markdown("Upload Invoice -> OCR -> AI -> Zoho Books (Invoice). No complex routing.")
    
    with gr.Row():
        f_in = gr.File(label="Upload Invoice")
        btn = gr.Button("Process", variant="primary")
        
    out_img = gr.Image(label="View", height=400)
    out_log = gr.Code(label="Logs", language="shell")
    
    btn.click(process_pipeline, f_in, [out_img, out_log])

if __name__ == "__main__":
    demo.launch(ssr_mode=False)