Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
# app.py β BizIntel AI Ultra
|
2 |
# Supports: CSV/Excel/DB ingestion, date+metric plotting, ARIMA forecasting,
|
3 |
-
# safe Plotly writes, Gemini 1.5 Pro strategy, KPI cards, optional EDA.
|
4 |
|
5 |
import os
|
6 |
import tempfile
|
7 |
-
from typing import Literal
|
8 |
|
9 |
import pandas as pd
|
10 |
import streamlit as st
|
@@ -12,24 +11,22 @@ import google.generativeai as genai
|
|
12 |
import plotly.graph_objects as go
|
13 |
|
14 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
15 |
-
# 0)
|
16 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
17 |
TMP = tempfile.gettempdir()
|
18 |
-
|
19 |
-
|
20 |
def _safe_write(self, path, *args, **kwargs):
|
21 |
filename = os.path.basename(path)
|
22 |
safe_path = os.path.join(TMP, filename)
|
23 |
-
return
|
24 |
-
|
25 |
go.Figure.write_image = _safe_write
|
26 |
|
27 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
28 |
-
# 1)
|
29 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
30 |
from tools.csv_parser import parse_csv_tool
|
31 |
-
from tools.plot_generator import plot_metric_tool
|
32 |
-
from tools.forecaster import forecast_metric_tool
|
33 |
from tools.visuals import histogram_tool, scatter_matrix_tool, corr_heatmap_tool
|
34 |
from db_connector import fetch_data_from_db, list_tables, SUPPORTED_ENGINES
|
35 |
|
@@ -71,7 +68,7 @@ if source == "Upload CSV / Excel":
|
|
71 |
else:
|
72 |
try:
|
73 |
df_xl = pd.read_excel(tmp_file, sheet_name=0)
|
74 |
-
csv_path =
|
75 |
df_xl.to_csv(csv_path, index=False)
|
76 |
except Exception as e:
|
77 |
st.error(f"Excel parsing failed: {e}")
|
@@ -100,14 +97,21 @@ with open(csv_path, "rb") as f:
|
|
100 |
st.download_button("β¬οΈ Download working CSV", f, file_name=os.path.basename(csv_path))
|
101 |
|
102 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
103 |
-
# 5) Show
|
104 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
105 |
df_head = pd.read_csv(csv_path, nrows=5)
|
106 |
st.dataframe(df_head)
|
107 |
|
108 |
-
|
|
|
|
|
|
|
109 |
numeric_cols = df_head.select_dtypes("number").columns.tolist()
|
110 |
-
|
|
|
|
|
|
|
|
|
111 |
|
112 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
113 |
# 6) Local analysis: summary, trend chart, forecast
|
@@ -156,22 +160,29 @@ st.download_button("β¬οΈ Download Strategy (.md)", strategy_md, file_name="str
|
|
156 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
157 |
# 8) KPI cards + detailed Stats
|
158 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
159 |
-
full_df
|
160 |
-
total_rows
|
161 |
-
|
162 |
missing_pct = full_df.isna().mean().mean() * 100
|
163 |
|
164 |
st.markdown("---")
|
165 |
st.subheader("π Dataset Overview")
|
166 |
c1, c2, c3 = st.columns(3)
|
167 |
-
c1.metric("Rows",
|
168 |
-
c2.metric("Columns",
|
169 |
c3.metric("Missing %", f"{missing_pct:.1f}%")
|
170 |
|
171 |
with st.expander("π Detailed descriptive statistics"):
|
172 |
-
stats_df =
|
173 |
-
|
174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
177 |
# 9) Optional Exploratory Visuals
|
@@ -183,10 +194,10 @@ if st.checkbox("Histogram"):
|
|
183 |
hcol = st.selectbox("Variable", numeric_cols, key="hist")
|
184 |
st.plotly_chart(histogram_tool(csv_path, hcol), use_container_width=True)
|
185 |
|
186 |
-
if st.checkbox("Scatter
|
187 |
sel = st.multiselect("Choose columns", numeric_cols, default=numeric_cols[:3])
|
188 |
if sel:
|
189 |
st.plotly_chart(scatter_matrix_tool(csv_path, sel), use_container_width=True)
|
190 |
|
191 |
-
if st.checkbox("Correlation
|
192 |
st.plotly_chart(corr_heatmap_tool(csv_path), use_container_width=True)
|
|
|
1 |
# app.py β BizIntel AI Ultra
|
2 |
# Supports: CSV/Excel/DB ingestion, date+metric plotting, ARIMA forecasting,
|
3 |
+
# safe Plotly writes into /tmp, Gemini 1.5 Pro strategy, KPI cards, optional EDA.
|
4 |
|
5 |
import os
|
6 |
import tempfile
|
|
|
7 |
|
8 |
import pandas as pd
|
9 |
import streamlit as st
|
|
|
11 |
import plotly.graph_objects as go
|
12 |
|
13 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
14 |
+
# 0) Monkeyβpatch Plotly to write images into /tmp (writable)
|
15 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
16 |
TMP = tempfile.gettempdir()
|
17 |
+
_orig_write = go.Figure.write_image
|
|
|
18 |
def _safe_write(self, path, *args, **kwargs):
|
19 |
filename = os.path.basename(path)
|
20 |
safe_path = os.path.join(TMP, filename)
|
21 |
+
return _orig_write(self, safe_path, *args, **kwargs)
|
|
|
22 |
go.Figure.write_image = _safe_write
|
23 |
|
24 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
25 |
+
# 1) Tool & DB imports
|
26 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
27 |
from tools.csv_parser import parse_csv_tool
|
28 |
+
from tools.plot_generator import plot_metric_tool
|
29 |
+
from tools.forecaster import forecast_metric_tool
|
30 |
from tools.visuals import histogram_tool, scatter_matrix_tool, corr_heatmap_tool
|
31 |
from db_connector import fetch_data_from_db, list_tables, SUPPORTED_ENGINES
|
32 |
|
|
|
68 |
else:
|
69 |
try:
|
70 |
df_xl = pd.read_excel(tmp_file, sheet_name=0)
|
71 |
+
csv_path = tmp_file.rsplit(".", 1)[0] + ".csv"
|
72 |
df_xl.to_csv(csv_path, index=False)
|
73 |
except Exception as e:
|
74 |
st.error(f"Excel parsing failed: {e}")
|
|
|
97 |
st.download_button("β¬οΈ Download working CSV", f, file_name=os.path.basename(csv_path))
|
98 |
|
99 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
100 |
+
# 5) Show head & pick date + metric (but never the same column)
|
101 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
102 |
df_head = pd.read_csv(csv_path, nrows=5)
|
103 |
st.dataframe(df_head)
|
104 |
|
105 |
+
# a) Date dropdown over all columns
|
106 |
+
date_col = st.selectbox("Select date/time column", df_head.columns)
|
107 |
+
|
108 |
+
# b) Metric dropdown only numeric columns, excluding the chosen date_col
|
109 |
numeric_cols = df_head.select_dtypes("number").columns.tolist()
|
110 |
+
metric_options = [c for c in numeric_cols if c != date_col]
|
111 |
+
if not metric_options:
|
112 |
+
st.error(f"No numeric columns available once we exclude '{date_col}'.")
|
113 |
+
st.stop()
|
114 |
+
metric_col = st.selectbox("Select numeric metric column", metric_options)
|
115 |
|
116 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
117 |
# 6) Local analysis: summary, trend chart, forecast
|
|
|
160 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
161 |
# 8) KPI cards + detailed Stats
|
162 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
163 |
+
full_df = pd.read_csv(csv_path, low_memory=False)
|
164 |
+
total_rows = len(full_df)
|
165 |
+
num_columns = full_df.shape[1]
|
166 |
missing_pct = full_df.isna().mean().mean() * 100
|
167 |
|
168 |
st.markdown("---")
|
169 |
st.subheader("π Dataset Overview")
|
170 |
c1, c2, c3 = st.columns(3)
|
171 |
+
c1.metric("Rows", f"{total_rows:,}")
|
172 |
+
c2.metric("Columns", str(num_columns))
|
173 |
c3.metric("Missing %", f"{missing_pct:.1f}%")
|
174 |
|
175 |
with st.expander("π Detailed descriptive statistics"):
|
176 |
+
stats_df = (
|
177 |
+
full_df.describe()
|
178 |
+
.T
|
179 |
+
.reset_index()
|
180 |
+
.rename(columns={"index":"Feature"})
|
181 |
+
)
|
182 |
+
st.dataframe(
|
183 |
+
stats_df.style.format(precision=2).background_gradient(cmap="Blues"),
|
184 |
+
use_container_width=True
|
185 |
+
)
|
186 |
|
187 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
188 |
# 9) Optional Exploratory Visuals
|
|
|
194 |
hcol = st.selectbox("Variable", numeric_cols, key="hist")
|
195 |
st.plotly_chart(histogram_tool(csv_path, hcol), use_container_width=True)
|
196 |
|
197 |
+
if st.checkbox("Scatter Matrix"):
|
198 |
sel = st.multiselect("Choose columns", numeric_cols, default=numeric_cols[:3])
|
199 |
if sel:
|
200 |
st.plotly_chart(scatter_matrix_tool(csv_path, sel), use_container_width=True)
|
201 |
|
202 |
+
if st.checkbox("Correlation Heatmap"):
|
203 |
st.plotly_chart(corr_heatmap_tool(csv_path), use_container_width=True)
|