Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,8 @@
|
|
1 |
-
# app.py β BizIntelΒ AIΒ UltraΒ
|
2 |
-
# Features: CSV upload, SQL DB fetch, interactive Plotly, GeminiΒ 1.5β―Pro,
|
3 |
-
# optional EDA, download buttons.
|
4 |
|
5 |
import os
|
6 |
import tempfile
|
7 |
-
from io import StringIO
|
8 |
|
9 |
import pandas as pd
|
10 |
import streamlit as st
|
@@ -13,117 +11,100 @@ import plotly.graph_objects as go
|
|
13 |
|
14 |
from tools.csv_parser import parse_csv_tool
|
15 |
from tools.plot_generator import plot_sales_tool
|
16 |
-
from tools.forecaster import forecast_tool
|
17 |
-
from tools.visuals import
|
18 |
-
histogram_tool,
|
19 |
-
scatter_matrix_tool,
|
20 |
-
corr_heatmap_tool,
|
21 |
-
)
|
22 |
from db_connector import fetch_data_from_db, list_tables, SUPPORTED_ENGINES
|
23 |
|
24 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
25 |
-
#
|
26 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
27 |
genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
|
28 |
gemini = genai.GenerativeModel(
|
29 |
"gemini-1.5-pro-latest",
|
30 |
-
generation_config={
|
|
|
|
|
|
|
|
|
31 |
)
|
32 |
|
33 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
34 |
-
#
|
35 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
36 |
st.set_page_config(page_title="BizIntelΒ AIΒ Ultra", layout="wide")
|
37 |
-
st.title("π BizIntelΒ AIΒ UltraΒ β Advanced Analytics
|
38 |
|
39 |
TEMP_DIR = tempfile.gettempdir()
|
40 |
|
41 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
42 |
-
#
|
43 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
44 |
-
|
45 |
-
csv_path
|
46 |
|
47 |
-
if
|
48 |
-
|
49 |
-
if
|
50 |
-
csv_path = os.path.join(TEMP_DIR,
|
51 |
with open(csv_path, "wb") as f:
|
52 |
-
f.write(
|
53 |
st.success("CSV saved β
")
|
54 |
|
55 |
-
|
56 |
engine = st.selectbox("DB engine", SUPPORTED_ENGINES)
|
57 |
-
|
58 |
-
if
|
59 |
try:
|
60 |
-
|
|
|
|
|
|
|
|
|
61 |
except Exception as e:
|
62 |
st.error(f"Connection failed: {e}")
|
63 |
st.stop()
|
64 |
|
65 |
-
table = st.selectbox("Select table", tables)
|
66 |
-
if st.button("Fetch table"):
|
67 |
-
csv_path = fetch_data_from_db(conn_str, table)
|
68 |
-
st.success(f"Fetched β{table}β into CSV β
")
|
69 |
-
|
70 |
-
# Stop if we still donβt have a CSV
|
71 |
if csv_path is None:
|
72 |
st.stop()
|
73 |
|
74 |
-
#
|
75 |
with open(csv_path, "rb") as f:
|
76 |
-
st.download_button("β¬οΈ
|
77 |
|
78 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
79 |
-
#
|
80 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
81 |
df_preview = pd.read_csv(csv_path, nrows=5)
|
82 |
st.dataframe(df_preview)
|
83 |
-
|
84 |
date_col = st.selectbox("Select date/time column for forecasting", df_preview.columns)
|
85 |
|
86 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
87 |
-
#
|
88 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
89 |
with st.spinner("Parsing CSVβ¦"):
|
90 |
summary_text = parse_csv_tool(csv_path)
|
91 |
|
92 |
with st.spinner("Generating sales trendβ¦"):
|
93 |
sales_fig = plot_sales_tool(csv_path, date_col=date_col)
|
94 |
-
|
95 |
if isinstance(sales_fig, go.Figure):
|
96 |
st.plotly_chart(sales_fig, use_container_width=True)
|
97 |
else:
|
98 |
st.warning(sales_fig)
|
99 |
|
100 |
with st.spinner("Forecastingβ¦"):
|
101 |
-
forecast_text = forecast_tool(csv_path, date_col=date_col)
|
102 |
-
|
103 |
-
try:
|
104 |
-
forecast_df = pd.read_csv(StringIO(forecast_text))
|
105 |
-
except Exception:
|
106 |
-
forecast_df = None
|
107 |
-
|
108 |
-
# Forecast plot preview
|
109 |
-
if os.path.exists("forecast_plot.png"):
|
110 |
-
st.image("forecast_plot.png", caption="Sales Forecast", use_column_width=True)
|
111 |
|
112 |
-
|
113 |
-
|
114 |
-
buf = StringIO()
|
115 |
-
forecast_df.to_csv(buf, index=False)
|
116 |
-
st.download_button("β¬οΈ Download Forecast CSV", buf.getvalue(), file_name="forecast.csv")
|
117 |
|
118 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
119 |
-
#
|
120 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
121 |
prompt = (
|
122 |
f"You are **BizIntel Strategist AI**.\n\n"
|
123 |
-
"### CSV Summary\n"
|
124 |
-
f"```\n{
|
125 |
-
"### Forecast Output\n"
|
126 |
-
f"```\n{forecast_text}\n```\n\n"
|
127 |
"Return **Markdown** with:\n"
|
128 |
"1. Five key insights\n"
|
129 |
"2. Three actionable strategies (with expected impact)\n"
|
@@ -135,33 +116,47 @@ st.subheader("π Strategy Recommendations (GeminiΒ 1.5Β Pro)")
|
|
135 |
with st.spinner("Generating insightsβ¦"):
|
136 |
strategy_md = gemini.generate_content(prompt).text
|
137 |
st.markdown(strategy_md)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
141 |
|
142 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
143 |
-
#
|
144 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
145 |
st.markdown("---")
|
146 |
st.subheader("π Optional Exploratory Visuals")
|
147 |
|
148 |
-
|
149 |
|
150 |
if st.checkbox("Histogram"):
|
151 |
-
|
152 |
-
st.plotly_chart(histogram_tool(csv_path,
|
153 |
|
154 |
if st.checkbox("Scatterβmatrix"):
|
155 |
-
|
156 |
-
if
|
157 |
-
st.plotly_chart(scatter_matrix_tool(csv_path,
|
158 |
|
159 |
if st.checkbox("Correlation heatβmap"):
|
160 |
st.plotly_chart(corr_heatmap_tool(csv_path), use_container_width=True)
|
161 |
-
|
162 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
163 |
-
# 6. FULL SUMMARY
|
164 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
165 |
-
st.markdown("---")
|
166 |
-
st.subheader("π CSV Summary (stats)")
|
167 |
-
st.text(summary_text)
|
|
|
1 |
+
# app.py β BizIntelΒ AIΒ Ultra (Geminiβ―1.5Β Pro, CSVβ―+β―DB, interactive Plotly, pro summary)
|
|
|
|
|
2 |
|
3 |
import os
|
4 |
import tempfile
|
5 |
+
from io import StringIO
|
6 |
|
7 |
import pandas as pd
|
8 |
import streamlit as st
|
|
|
11 |
|
12 |
from tools.csv_parser import parse_csv_tool
|
13 |
from tools.plot_generator import plot_sales_tool
|
14 |
+
from tools.forecaster import forecast_tool
|
15 |
+
from tools.visuals import histogram_tool, scatter_matrix_tool, corr_heatmap_tool
|
|
|
|
|
|
|
|
|
16 |
from db_connector import fetch_data_from_db, list_tables, SUPPORTED_ENGINES
|
17 |
|
18 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
19 |
+
# 1. GEMINI CONFIG
|
20 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
21 |
genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
|
22 |
gemini = genai.GenerativeModel(
|
23 |
"gemini-1.5-pro-latest",
|
24 |
+
generation_config={
|
25 |
+
"temperature": 0.7,
|
26 |
+
"top_p": 0.9,
|
27 |
+
"response_mime_type": "text/plain",
|
28 |
+
},
|
29 |
)
|
30 |
|
31 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
32 |
+
# 2. PAGE SETUP
|
33 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
34 |
st.set_page_config(page_title="BizIntelΒ AIΒ Ultra", layout="wide")
|
35 |
+
st.title("π BizIntelΒ AIΒ UltraΒ β Advanced Analytics + GeminiΒ 1.5Β Pro")
|
36 |
|
37 |
TEMP_DIR = tempfile.gettempdir()
|
38 |
|
39 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
40 |
+
# 3. DATA SOURCE (CSV OR DB)
|
41 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
42 |
+
source = st.radio("Select data source", ["Upload CSV", "Connect to SQL Database"])
|
43 |
+
csv_path = None
|
44 |
|
45 |
+
if source == "Upload CSV":
|
46 |
+
up = st.file_uploader("Upload CSV (β€β―200β―MB)", type=["csv"])
|
47 |
+
if up:
|
48 |
+
csv_path = os.path.join(TEMP_DIR, up.name)
|
49 |
with open(csv_path, "wb") as f:
|
50 |
+
f.write(up.read())
|
51 |
st.success("CSV saved β
")
|
52 |
|
53 |
+
else:
|
54 |
engine = st.selectbox("DB engine", SUPPORTED_ENGINES)
|
55 |
+
conn = st.text_input("SQLAlchemy connection string")
|
56 |
+
if conn:
|
57 |
try:
|
58 |
+
tbls = list_tables(conn)
|
59 |
+
tbl = st.selectbox("Table", tbls)
|
60 |
+
if st.button("Fetch table"):
|
61 |
+
csv_path = fetch_data_from_db(conn, tbl)
|
62 |
+
st.success(f"Fetched **{tbl}** as CSV β
")
|
63 |
except Exception as e:
|
64 |
st.error(f"Connection failed: {e}")
|
65 |
st.stop()
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
if csv_path is None:
|
68 |
st.stop()
|
69 |
|
70 |
+
# Download original CSV
|
71 |
with open(csv_path, "rb") as f:
|
72 |
+
st.download_button("β¬οΈΒ Download original CSV", f, file_name=os.path.basename(csv_path))
|
73 |
|
74 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
75 |
+
# 4. PREVIEW & DATE COLUMN
|
76 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
77 |
df_preview = pd.read_csv(csv_path, nrows=5)
|
78 |
st.dataframe(df_preview)
|
|
|
79 |
date_col = st.selectbox("Select date/time column for forecasting", df_preview.columns)
|
80 |
|
81 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
82 |
+
# 5. LOCAL TOOLS: SUMMARY, SALES TREND, FORECAST
|
83 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
84 |
with st.spinner("Parsing CSVβ¦"):
|
85 |
summary_text = parse_csv_tool(csv_path)
|
86 |
|
87 |
with st.spinner("Generating sales trendβ¦"):
|
88 |
sales_fig = plot_sales_tool(csv_path, date_col=date_col)
|
|
|
89 |
if isinstance(sales_fig, go.Figure):
|
90 |
st.plotly_chart(sales_fig, use_container_width=True)
|
91 |
else:
|
92 |
st.warning(sales_fig)
|
93 |
|
94 |
with st.spinner("Forecastingβ¦"):
|
95 |
+
forecast_text = forecast_tool(csv_path, date_col=date_col)
|
96 |
+
forecast_png = "forecast_plot.png" if os.path.exists("forecast_plot.png") else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
+
if forecast_png:
|
99 |
+
st.image(forecast_png, caption="Sales Forecast", use_container_width=True)
|
|
|
|
|
|
|
100 |
|
101 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
102 |
+
# 6. GEMINI STRATEGY
|
103 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
104 |
prompt = (
|
105 |
f"You are **BizIntel Strategist AI**.\n\n"
|
106 |
+
f"### CSV Summary\n```\n{summary_text}\n```\n\n"
|
107 |
+
f"### Forecast Output\n```\n{forecast_text}\n```\n\n"
|
|
|
|
|
108 |
"Return **Markdown** with:\n"
|
109 |
"1. Five key insights\n"
|
110 |
"2. Three actionable strategies (with expected impact)\n"
|
|
|
116 |
with st.spinner("Generating insightsβ¦"):
|
117 |
strategy_md = gemini.generate_content(prompt).text
|
118 |
st.markdown(strategy_md)
|
119 |
+
st.download_button("β¬οΈΒ Download Strategy (.md)", strategy_md, file_name="strategy.md")
|
120 |
+
|
121 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
122 |
+
# 7. PROFESSIONAL CSV SUMMARY
|
123 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
124 |
+
st.markdown("---")
|
125 |
+
st.subheader("π CSV Overview")
|
126 |
+
|
127 |
+
full_df = pd.read_csv(csv_path)
|
128 |
+
total_rows = len(full_df)
|
129 |
+
num_cols = len(full_df.columns)
|
130 |
+
missing_pct = full_df.isna().mean().mean() * 100
|
131 |
+
|
132 |
+
c1, c2, c3 = st.columns(3)
|
133 |
+
c1.metric("Rows", f"{total_rows:,}")
|
134 |
+
c2.metric("Columns", str(num_cols))
|
135 |
+
c3.metric("MissingΒ %", f"{missing_pct:.1f}%")
|
136 |
|
137 |
+
with st.expander("πΒ Detailed descriptive statistics"):
|
138 |
+
stats_df = full_df.describe().T.reset_index().rename(columns={"index": "Feature"})
|
139 |
+
st.dataframe(
|
140 |
+
stats_df.style.format(precision=2).background_gradient(cmap="Blues"),
|
141 |
+
use_container_width=True,
|
142 |
+
)
|
143 |
|
144 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
145 |
+
# 8. OPTIONAL EXPLORATORY VISUALS
|
146 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
147 |
st.markdown("---")
|
148 |
st.subheader("π Optional Exploratory Visuals")
|
149 |
|
150 |
+
num_cols_only = df_preview.select_dtypes("number").columns
|
151 |
|
152 |
if st.checkbox("Histogram"):
|
153 |
+
hcol = st.selectbox("Variable", num_cols_only, key="hist")
|
154 |
+
st.plotly_chart(histogram_tool(csv_path, hcol), use_container_width=True)
|
155 |
|
156 |
if st.checkbox("Scatterβmatrix"):
|
157 |
+
sm_cols = st.multiselect("Choose up to 5 columns", num_cols_only, default=num_cols_only[:3])
|
158 |
+
if sm_cols:
|
159 |
+
st.plotly_chart(scatter_matrix_tool(csv_path, sm_cols), use_container_width=True)
|
160 |
|
161 |
if st.checkbox("Correlation heatβmap"):
|
162 |
st.plotly_chart(corr_heatmap_tool(csv_path), use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|