# 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 # ────────────────────────────────────────────────────────────── # 1. GEMINI CONFIG (1.5‑Pro, temperature 0.7) # ────────────────────────────────────────────────────────────── genai.configure(api_key=os.getenv("GEMINI_APIKEY")) gemini = genai.GenerativeModel( "gemini-1.5-pro-latest", generation_config={ "temperature": 0.7, "top_p": 0.9, # Allowed types: text/plain, application/json, etc. "response_mime_type": "text/plain", }, ) # ────────────────────────────────────────────────────────────── # 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 CSV (≤ 200 MB)", type=["csv"]) if csv_file is None: 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 and choose date column df_preview = pd.read_csv(csv_path, nrows=5) st.dataframe(df_preview) date_col = st.selectbox("Select the date/time column for forecasting", df_preview.columns) # ────────────────────────────────────────────────────────────── # 4. LOCAL TOOL EXECUTION # ────────────────────────────────────────────────────────────── with st.spinner("🔍 Parsing CSV…"): summary_text = parse_csv_tool(csv_path) with st.spinner("📈 Generating sales trend chart…"): _ = plot_sales_tool(csv_path) # generates sales_plot.png with st.spinner("🔮 Forecasting future metrics…"): forecast_text = forecast_tool(csv_path, date_col=date_col) # uses chosen column # ────────────────────────────────────────────────────────────── # 5. GEMINI 1.5‑PRO STRATEGY (sync call) # ────────────────────────────────────────────────────────────── prompt = ( f"You are **BizIntel Strategist AI**.\n\n" f"## CSV Summary\n```\n{summary_text}\n```\n\n" f"## Forecast Output\n```\n{forecast_text}\n```\n\n" "### Task\n" "Return **Markdown** with:\n" "1. **Five key insights** (bullet list)\n" "2. **Three actionable strategies** (with expected impact)\n" "3. **Risk factors or anomalies**\n" "4. **Suggested additional visuals**\n" ) st.subheader("🚀 Strategy Recommendations (Gemini 1.5 Pro)") with st.spinner("Gemini 1.5 Pro is thinking…"): response = gemini.generate_content(prompt) strategy_md = response.text st.markdown(strategy_md) # ────────────────────────────────────────────────────────────── # 6. DISPLAY SUMMARY & CHARTS # ────────────────────────────────────────────────────────────── st.markdown("---") st.subheader("📑 CSV Summary") st.text(summary_text) if os.path.exists("sales_plot.png"): st.image("sales_plot.png", caption="Sales Trend", use_column_width=True) if os.path.exists("forecast_plot.png"): st.image("forecast_plot.png", caption="Forecast Chart", use_column_width=True)