Spaces:
Sleeping
Sleeping
| 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) |