Agentic / app.py
vachaspathi's picture
Update app.py
2be5a11 verified
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)