BizIntel_AI / app.py
mgbam's picture
Update app.py
2c362d2 verified
raw
history blame
4.12 kB
# app.py – BizIntel AI Ultra (GeminiΒ 1.5Β Pro, selectable date column)
import os
import tempfile
import pandas as pd
import streamlit as st
import google.generativeai as genai
from tools.csv_parser import parse_csv_tool
from tools.plot_generator import plot_sales_tool
from tools.forecaster import forecast_tool # must accept date_col param
# ──────────────────────────────────────────────────────────────
# 1. GEMINI CONFIG (1.5‑pro, temperature 0.7)
# ──────────────────────────────────────────────────────────────
genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
gemini = genai.GenerativeModel(
model="gemini-1.5-pro-latest",
temperature=0.7,
top_p=0.9,
response_mime_type="text/markdown",
)
# ──────────────────────────────────────────────────────────────
# 2. STREAMLIT PAGE SETUP
# ──────────────────────────────────────────────────────────────
st.set_page_config(page_title="BizIntel AI Ultra – GeminiΒ 1.5Β Pro", layout="wide")
st.title("πŸ“Š BizIntelΒ AIΒ Ultra – Advanced Analytics Pipeline")
TEMP_DIR = tempfile.gettempdir()
# ──────────────────────────────────────────────────────────────
# 3. CSV UPLOAD
# ──────────────────────────────────────────────────────────────
csv_file = st.file_uploader("Upload a CSV file (≀ 200β€―MB)", type=["csv"])
if not csv_file:
st.info("⬆️ Upload a CSV to begin.")
st.stop()
csv_path = os.path.join(TEMP_DIR, csv_file.name)
with open(csv_path, "wb") as f:
f.write(csv_file.read())
st.success("CSV saved to temporary storage βœ…")
# Preview first rows for column selection
df_preview = pd.read_csv(csv_path, nrows=5)
st.dataframe(df_preview)
# ──────────────────────────────────────────────────────────────
# 4. DATE COLUMN CHOICE
# ──────────────────────────────────────────────────────────────
date_col = st.selectbox("Choose the date/time column for forecasting", df_preview.columns)
# ──────────────────────────────────────────────────────────────
# 5. RUN LOCAL TOOLS
# ──────────────────────────────────────────────────────────────
with st.spinner("Parsing CSV…"):
summary_text = parse_csv_tool(csv_path)
with st.spinner("Generating sales trend chart…"):
trend_msg = plot_sales_tool(csv_path) # creates sales_plot.png
with st.spinner("Forecasting future metrics…"):
forecast_text = forecast_tool(csv_path, date_col=date_col) # ← passes chosen column
# ──────────────────────────────────────────────────────────────
# 6. GEMINI 1.5‑PRO STRATEGY (stream)
# ──────────────────────────────────────────────────────────────
prompt = f"""
You are BizIntel Strategist AI.
**CSV SUMMARY**