BizIntel_AI / app.py
mgbam's picture
Update app.py
8171765 verified
raw
history blame
2.41 kB
import os, tempfile, streamlit as st
from google.adk.runner import run_agent_sync # ← NEW
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()
# ── Data source ───────────────────────────────────────────────
src = st.radio("Select data source", ["Upload CSV", "Connect to SQL Database"])
csv_path = None
if src == "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:
eng = st.selectbox("DB engine", SUPPORTED_ENGINES)
conn = st.text_input("SQLAlchemy connection string")
if conn:
tbls = list_tables(conn)
tbl = st.selectbox("Table", tbls) if tbls else None
if tbl:
csv_path = fetch_data_from_db(conn, tbl)
st.success(f"Fetched **{tbl}** as CSV βœ…")
# ── Optional image preview ────────────────────────────────────
st.markdown("---")
img = st.file_uploader("Optional image (PNG/JPG)", ["png", "jpg", "jpeg"])
if img:
img_path = os.path.join(TEMP_DIR, img.name)
with open(img_path, "wb") as f:
f.write(img.read())
st.image(img_path, use_column_width=True)
# ── Run pipeline via ADK runner ───────────────────────────────
if csv_path:
st.markdown("---")
st.info("Running analytics pipeline…")
try:
report = run_agent_sync(agent=analytics_coordinator, input_data=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 generated
for name, cap in [("sales_plot.png", "Sales Trend"),
("forecast_plot.png", "Forecast Chart")]:
p = os.path.join(TEMP_DIR, name)
if os.path.exists(p):
st.image(p, caption=cap, use_column_width=True)