BizIntel_AI / app.py
mgbam's picture
Update app.py
5cc8a1b verified
raw
history blame
2.42 kB
import os
import tempfile
import streamlit as st
from agents.analytics_pipeline import analytics_coordinator
from db_connector import fetch_data_from_db, list_tables, SUPPORTED_ENGINES
# Page setup
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
input_source = st.radio("Select data source", ["Upload CSV", "Connect to SQL Database"])
csv_path: str | None = None
if input_source == "Upload CSV":
up_csv = st.file_uploader("Upload CSV", type="csv")
if up_csv:
csv_path = os.path.join(TEMP_DIR, up_csv.name)
with open(csv_path, "wb") as f:
f.write(up_csv.read())
st.success("CSV saved ✅")
elif input_source == "Connect to SQL Database":
engine = st.selectbox("DB engine", SUPPORTED_ENGINES)
conn = st.text_input("SQLAlchemy connection string")
if conn:
tables = list_tables(conn)
if tables:
table = st.selectbox("Table", tables)
if table:
csv_path = fetch_data_from_db(conn, table)
st.success(f"Fetched **{table}** as CSV ✅")
# 2. Optional image preview
st.markdown("---")
st.subheader("📷 Optional image upload")
img = st.file_uploader("PNG/JPG", type=["png", "jpg", "jpeg"], key="img")
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, caption="Uploaded Image", use_column_width=True)
# 3. Run analytics pipeline
if csv_path:
st.markdown("---")
st.info("Running analytics pipeline… ⏳")
# Robust call: try invoke() → run() → __call__()
if hasattr(analytics_coordinator, "invoke"):
report = analytics_coordinator.invoke(input=csv_path)
elif hasattr(analytics_coordinator, "run"):
report = analytics_coordinator.run(input=csv_path)
else: # fallback to callable
report = analytics_coordinator(csv_path)
st.subheader("📝 Analysis & Strategy Report")
st.text(report)
# Show charts if present
for name, caption 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=caption, use_column_width=True)