import os, tempfile, streamlit as st, inspect from agents.analytics_pipeline import analytics_coordinator from db_connector import fetch_data_from_db, list_tables, SUPPORTED_ENGINES st.set_page_config(page_title="BizIntel AI Ultra", layout="wide") st.title("πŸ“Š BizIntel AI Ultra – Business Intelligence Agent") TEMP_DIR = tempfile.gettempdir() # ── 1. Data source ─────────────────────────────────────────── source = st.radio("Select data source", ["Upload CSV", "Connect to SQL Database"]) csv_path = None if source == "Upload CSV": up = st.file_uploader("Upload CSV", ["csv"]) if up: csv_path = os.path.join(TEMP_DIR, up.name) with open(csv_path, "wb") as f: f.write(up.read()) st.success("CSV saved βœ…") else: engine = st.selectbox("DB engine", SUPPORTED_ENGINES) conn = st.text_input("SQLAlchemy connection string") if conn: tables = list_tables(conn) table = st.selectbox("Table", tables) if tables else None if table: csv_path = fetch_data_from_db(conn, table) st.success(f"Fetched **{table}** as CSV βœ…") # ── 2. Optional image preview ──────────────────────────────── st.markdown("---") img = st.file_uploader("Optional image (PNG/JPG)", ["png", "jpg", "jpeg"]) if img: img_p = os.path.join(TEMP_DIR, img.name) with open(img_p, "wb") as f: f.write(img.read()) st.image(img_p, use_column_width=True) # ── 3. Run agent pipeline ──────────────────────────────────── if csv_path: st.markdown("---") st.info("Running analytics pipeline…") # Discover an executable method cand_methods = ["invoke", "run", "execute", "chat", "__call__"] exec_fn = None for m in cand_methods: if hasattr(analytics_coordinator, m) and callable(getattr(analytics_coordinator, m)): exec_fn = getattr(analytics_coordinator, m) break if exec_fn is None: st.error("Cannot find an execution method on LlmAgent. Available attrs:\n" + ", ".join(dir(analytics_coordinator))) st.stop() # Determine if function expects keyword or positional arg sig = inspect.signature(exec_fn) try: if "input" in sig.parameters: report = exec_fn(input=csv_path) else: report = exec_fn(csv_path) except Exception as e: st.error(f"Agent execution failed: {e}") st.stop() st.subheader("πŸ“ Analysis & Strategy Report") st.text(report) # Show charts if any were created for name, cap in [("sales_plot.png", "Sales Trend"), ("forecast_plot.png", "Forecast")]: p = os.path.join(TEMP_DIR, name) if os.path.exists(p): st.image(p, caption=cap, use_column_width=True)