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)