BizIntel_AI / app.py
mgbam's picture
Update app.py
72a73ff verified
raw
history blame
4.01 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
# ──────────────────────────────────────────────────────────────
# 0. PAGE CONFIG
# ──────────────────────────────────────────────────────────────
st.set_page_config(page_title="BizIntel AI Ultra", layout="wide")
st.title("πŸ“Š BizIntel AI Ultra – Business Intelligence Agent")
# Where we'll store any uploaded files (always writable in HF Spaces)
TEMP_DIR = tempfile.gettempdir()
# ──────────────────────────────────────────────────────────────
# 1. DATA SOURCE SELECTION
# ──────────────────────────────────────────────────────────────
input_source = st.radio("Select data source", ["Upload CSV", "Connect to SQL Database"])
csv_path: str | None = None
if input_source == "Upload CSV":
uploaded_file = st.file_uploader("Upload CSV", type="csv")
if uploaded_file:
csv_path = os.path.join(TEMP_DIR, uploaded_file.name)
with open(csv_path, "wb") as f:
f.write(uploaded_file.read())
st.success("CSV file saved to temporary storage βœ…")
elif input_source == "Connect to SQL Database":
engine = st.selectbox("Database engine", SUPPORTED_ENGINES)
conn_str = st.text_input("SQLAlchemy connection string")
if conn_str:
tables = list_tables(conn_str)
if tables:
table_name = st.selectbox("Choose a table", tables)
if table_name:
csv_path = fetch_data_from_db(conn_str, table_name)
st.success(f"Fetched **{table_name}** as CSV βœ…")
# ──────────────────────────────────────────────────────────────
# 2. OPTIONAL IMAGE UPLOAD & PREVIEW
# ──────────────────────────────────────────────────────────────
st.markdown("---")
st.subheader("πŸ“· Optional: Upload an Image for Preview / Future Analysis")
img_file = st.file_uploader(
"Upload an image (PNG/JPG)", type=["png", "jpg", "jpeg"], key="img"
)
if img_file:
img_path = os.path.join(TEMP_DIR, img_file.name)
with open(img_path, "wb") as f:
f.write(img_file.read())
st.image(img_path, caption="Uploaded Image", use_column_width=True)
# πŸ‘‰ Future hook: pass `img_path` to a vision agent here.
# ──────────────────────────────────────────────────────────────
# 3. RUN ANALYTICS PIPELINE
# ──────────────────────────────────────────────────────────────
if csv_path:
st.markdown("---")
st.info("Running analytics pipeline… ⏳")
report = analytics_coordinator.run(input=csv_path)
st.subheader("πŸ“ Analysis & Strategy Report")
st.text(report)
# Show charts generated by tool functions if present
for plot_name, caption in [
("sales_plot.png", "Sales Trend"),
("forecast_plot.png", "Forecast Chart"),
]:
plot_path = os.path.join(TEMP_DIR, plot_name)
if os.path.exists(plot_path):
st.image(plot_path, caption=caption, use_column_width=True)