Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,37 @@
|
|
| 1 |
-
# app.py — BizIntel AI Ultra (Gemini 1.5 Pro, selectable date column)
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
import tempfile
|
| 5 |
import pandas as pd
|
| 6 |
import streamlit as st
|
| 7 |
import google.generativeai as genai
|
|
|
|
| 8 |
|
| 9 |
from tools.csv_parser import parse_csv_tool
|
| 10 |
from tools.plot_generator import plot_sales_tool
|
| 11 |
-
from tools.forecaster import forecast_tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
# ───────────────────────────────
|
| 14 |
-
# 1. GEMINI CONFIG (1.5‑Pro, temperature 0.7)
|
| 15 |
-
# ──────────────────────────────────────────────────────────────
|
| 16 |
genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
|
| 17 |
gemini = genai.GenerativeModel(
|
| 18 |
"gemini-1.5-pro-latest",
|
| 19 |
generation_config={
|
| 20 |
"temperature": 0.7,
|
| 21 |
"top_p": 0.9,
|
| 22 |
-
# Allowed types: text/plain, application/json, etc.
|
| 23 |
"response_mime_type": "text/plain",
|
| 24 |
},
|
| 25 |
)
|
| 26 |
|
| 27 |
-
# ───────────────────────────────────────
|
| 28 |
-
# 2. STREAMLIT PAGE SETUP
|
| 29 |
-
# ──────────────────────────────────────────────────────────────
|
| 30 |
st.set_page_config(page_title="BizIntel AI Ultra – Gemini 1.5 Pro", layout="wide")
|
| 31 |
-
st.title("📊 BizIntel AI Ultra – Advanced Analytics
|
| 32 |
|
| 33 |
TEMP_DIR = tempfile.gettempdir()
|
| 34 |
|
| 35 |
-
# ─────────────────────────────────────────────────
|
| 36 |
-
# 3. CSV UPLOAD
|
| 37 |
-
# ──────────────────────────────────────────────────────────────
|
| 38 |
csv_file = st.file_uploader("Upload CSV (≤ 200 MB)", type=["csv"])
|
| 39 |
if csv_file is None:
|
| 40 |
st.info("⬆️ Upload a CSV to begin.")
|
|
@@ -43,34 +40,34 @@ if csv_file is None:
|
|
| 43 |
csv_path = os.path.join(TEMP_DIR, csv_file.name)
|
| 44 |
with open(csv_path, "wb") as f:
|
| 45 |
f.write(csv_file.read())
|
| 46 |
-
st.success("CSV saved
|
| 47 |
|
| 48 |
-
# Preview
|
| 49 |
df_preview = pd.read_csv(csv_path, nrows=5)
|
| 50 |
st.dataframe(df_preview)
|
| 51 |
-
date_col = st.selectbox("Select
|
| 52 |
|
| 53 |
-
# ────────────────
|
| 54 |
-
|
| 55 |
-
# ──────────────────────────────────────────────────────────────
|
| 56 |
-
with st.spinner("🔍 Parsing CSV…"):
|
| 57 |
summary_text = parse_csv_tool(csv_path)
|
| 58 |
|
| 59 |
-
with st.spinner("
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
-
with st.spinner("
|
| 63 |
-
forecast_text = forecast_tool(csv_path, date_col=date_col)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
# ───────────────────────────────────
|
| 66 |
-
# 5. GEMINI 1.5‑PRO STRATEGY (sync call)
|
| 67 |
-
# ─────────────────��────────────────────────────────────────────
|
| 68 |
prompt = (
|
| 69 |
f"You are **BizIntel Strategist AI**.\n\n"
|
| 70 |
-
f"## CSV Summary\n```\n{summary_text}\n```\n\n"
|
| 71 |
-
f"## Forecast Output\n```\n{forecast_text}\n```\n\n"
|
| 72 |
-
"
|
| 73 |
-
"Return **Markdown** with:\n"
|
| 74 |
"1. **Five key insights** (bullet list)\n"
|
| 75 |
"2. **Three actionable strategies** (with expected impact)\n"
|
| 76 |
"3. **Risk factors or anomalies**\n"
|
|
@@ -78,21 +75,40 @@ prompt = (
|
|
| 78 |
)
|
| 79 |
|
| 80 |
st.subheader("🚀 Strategy Recommendations (Gemini 1.5 Pro)")
|
| 81 |
-
with st.spinner("Gemini 1.5 Pro is
|
| 82 |
-
|
| 83 |
|
| 84 |
-
strategy_md = response.text
|
| 85 |
st.markdown(strategy_md)
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
| 90 |
st.markdown("---")
|
| 91 |
-
st.subheader("
|
| 92 |
-
|
|
|
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
| 3 |
import pandas as pd
|
| 4 |
import streamlit as st
|
| 5 |
import google.generativeai as genai
|
| 6 |
+
import plotly.graph_objects as go
|
| 7 |
|
| 8 |
from tools.csv_parser import parse_csv_tool
|
| 9 |
from tools.plot_generator import plot_sales_tool
|
| 10 |
+
from tools.forecaster import forecast_tool
|
| 11 |
+
from tools.visuals import (
|
| 12 |
+
histogram_tool,
|
| 13 |
+
scatter_matrix_tool,
|
| 14 |
+
corr_heatmap_tool,
|
| 15 |
+
)
|
| 16 |
|
| 17 |
+
# ── Gemini 1.5‑Pro configuration ─────────────────────────────
|
|
|
|
|
|
|
| 18 |
genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
|
| 19 |
gemini = genai.GenerativeModel(
|
| 20 |
"gemini-1.5-pro-latest",
|
| 21 |
generation_config={
|
| 22 |
"temperature": 0.7,
|
| 23 |
"top_p": 0.9,
|
|
|
|
| 24 |
"response_mime_type": "text/plain",
|
| 25 |
},
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# ── Streamlit page setup ─────────────────────────────────────
|
|
|
|
|
|
|
| 29 |
st.set_page_config(page_title="BizIntel AI Ultra – Gemini 1.5 Pro", layout="wide")
|
| 30 |
+
st.title("📊 BizIntel AI Ultra – Advanced Analytics")
|
| 31 |
|
| 32 |
TEMP_DIR = tempfile.gettempdir()
|
| 33 |
|
| 34 |
+
# ── CSV upload ───────────────────────────────────────────────
|
|
|
|
|
|
|
| 35 |
csv_file = st.file_uploader("Upload CSV (≤ 200 MB)", type=["csv"])
|
| 36 |
if csv_file is None:
|
| 37 |
st.info("⬆️ Upload a CSV to begin.")
|
|
|
|
| 40 |
csv_path = os.path.join(TEMP_DIR, csv_file.name)
|
| 41 |
with open(csv_path, "wb") as f:
|
| 42 |
f.write(csv_file.read())
|
| 43 |
+
st.success("CSV saved ✅")
|
| 44 |
|
| 45 |
+
# Preview + date column selection
|
| 46 |
df_preview = pd.read_csv(csv_path, nrows=5)
|
| 47 |
st.dataframe(df_preview)
|
| 48 |
+
date_col = st.selectbox("Select date/time column for forecasting", df_preview.columns)
|
| 49 |
|
| 50 |
+
# ── Local tools: summary, sales trend, forecast ──────────────
|
| 51 |
+
with st.spinner("Parsing CSV…"):
|
|
|
|
|
|
|
| 52 |
summary_text = parse_csv_tool(csv_path)
|
| 53 |
|
| 54 |
+
with st.spinner("Generating sales trend chart…"):
|
| 55 |
+
sales_fig = plot_sales_tool(csv_path, date_col=date_col)
|
| 56 |
+
st.plotly_chart(sales_fig, use_container_width=True)
|
| 57 |
|
| 58 |
+
with st.spinner("Forecasting future metrics…"):
|
| 59 |
+
forecast_text = forecast_tool(csv_path, date_col=date_col)
|
| 60 |
+
if os.path.exists("forecast_plot.png"):
|
| 61 |
+
forecast_img = "forecast_plot.png"
|
| 62 |
+
else:
|
| 63 |
+
forecast_img = None
|
| 64 |
|
| 65 |
+
# ── Gemini strategy insights ─────────────────────────────────
|
|
|
|
|
|
|
| 66 |
prompt = (
|
| 67 |
f"You are **BizIntel Strategist AI**.\n\n"
|
| 68 |
+
f"### CSV Summary\n```\n{summary_text}\n```\n\n"
|
| 69 |
+
f"### Forecast Output\n```\n{forecast_text}\n```\n\n"
|
| 70 |
+
"Return Markdown with:\n"
|
|
|
|
| 71 |
"1. **Five key insights** (bullet list)\n"
|
| 72 |
"2. **Three actionable strategies** (with expected impact)\n"
|
| 73 |
"3. **Risk factors or anomalies**\n"
|
|
|
|
| 75 |
)
|
| 76 |
|
| 77 |
st.subheader("🚀 Strategy Recommendations (Gemini 1.5 Pro)")
|
| 78 |
+
with st.spinner("Gemini 1.5 Pro is generating insights…"):
|
| 79 |
+
strategy_md = gemini.generate_content(prompt).text
|
| 80 |
|
|
|
|
| 81 |
st.markdown(strategy_md)
|
| 82 |
|
| 83 |
+
# Display forecast image if exists
|
| 84 |
+
if forecast_img:
|
| 85 |
+
st.image(forecast_img, caption="Sales Forecast", use_column_width=True)
|
| 86 |
+
|
| 87 |
+
# ── Optional exploratory visuals ─────────────────────────────
|
| 88 |
st.markdown("---")
|
| 89 |
+
st.subheader("🔍 Optional Exploratory Visuals")
|
| 90 |
+
|
| 91 |
+
num_cols = df_preview.select_dtypes("number").columns
|
| 92 |
|
| 93 |
+
# Histogram
|
| 94 |
+
if st.checkbox("Show histogram"):
|
| 95 |
+
hist_col = st.selectbox("Histogram variable", num_cols, key="hist")
|
| 96 |
+
fig_hist = histogram_tool(csv_path, hist_col)
|
| 97 |
+
st.plotly_chart(fig_hist, use_container_width=True)
|
| 98 |
|
| 99 |
+
# Scatter‑matrix
|
| 100 |
+
if st.checkbox("Show scatter‑matrix"):
|
| 101 |
+
multi_cols = st.multiselect("Choose up to 5 columns", num_cols, default=num_cols[:3])
|
| 102 |
+
if multi_cols:
|
| 103 |
+
fig_scatter = scatter_matrix_tool(csv_path, multi_cols)
|
| 104 |
+
st.plotly_chart(fig_scatter, use_container_width=True)
|
| 105 |
+
|
| 106 |
+
# Correlation heat‑map
|
| 107 |
+
if st.checkbox("Show correlation heat‑map"):
|
| 108 |
+
fig_corr = corr_heatmap_tool(csv_path)
|
| 109 |
+
st.plotly_chart(fig_corr, use_container_width=True)
|
| 110 |
+
|
| 111 |
+
# ── CSV summary text at bottom ───────────────────────────────
|
| 112 |
+
st.markdown("---")
|
| 113 |
+
st.subheader("📑 CSV Summary (full Stats)")
|
| 114 |
+
st.text(summary_text)
|