Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
# app.py โ BizIntel AI Ultra
|
2 |
-
# Supports: CSV/Excel/DB ingestion,
|
3 |
-
#
|
4 |
|
5 |
import os
|
6 |
import tempfile
|
@@ -12,29 +12,29 @@ import google.generativeai as genai
|
|
12 |
import plotly.graph_objects as go
|
13 |
|
14 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
15 |
-
#
|
16 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
17 |
-
|
18 |
_original_write = go.Figure.write_image
|
19 |
|
20 |
def _safe_write(self, path, *args, **kwargs):
|
21 |
-
|
22 |
-
safe_path = os.path.join(
|
23 |
return _original_write(self, safe_path, *args, **kwargs)
|
24 |
|
25 |
go.Figure.write_image = _safe_write
|
26 |
|
27 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
28 |
-
#
|
29 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
30 |
from tools.csv_parser import parse_csv_tool
|
31 |
-
from tools.plot_generator import plot_metric_tool # date_col, metric_col
|
32 |
-
from tools.forecaster import forecast_metric_tool # date_col, metric_col
|
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 |
|
36 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
37 |
-
#
|
38 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
39 |
genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
|
40 |
gemini = genai.GenerativeModel(
|
@@ -47,71 +47,70 @@ gemini = genai.GenerativeModel(
|
|
47 |
)
|
48 |
|
49 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
50 |
-
#
|
51 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
52 |
st.set_page_config(page_title="BizIntel AI Ultra", layout="wide")
|
53 |
st.title("๐ BizIntel AI Ultra โ Advanced Analytics + Gemini 1.5 Pro")
|
54 |
-
|
55 |
TEMP_DIR = tempfile.gettempdir()
|
56 |
|
57 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
58 |
-
#
|
59 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
60 |
source = st.radio("Select data source", ["Upload CSV / Excel", "Connect to SQL Database"])
|
61 |
-
csv_path
|
62 |
|
63 |
if source == "Upload CSV / Excel":
|
64 |
-
upload = st.file_uploader("Upload CSV or Excel (โค 500 MB)", type=["csv",
|
65 |
if upload:
|
66 |
-
|
67 |
-
with open(
|
68 |
f.write(upload.read())
|
69 |
|
70 |
if upload.name.lower().endswith(".csv"):
|
71 |
-
csv_path =
|
72 |
else:
|
73 |
try:
|
74 |
-
df_xl = pd.read_excel(
|
75 |
-
csv_path = os.path.splitext(
|
76 |
df_xl.to_csv(csv_path, index=False)
|
77 |
except Exception as e:
|
78 |
st.error(f"Excel parsing failed: {e}")
|
79 |
st.stop()
|
80 |
st.success(f"{upload.name} saved โ
")
|
81 |
|
82 |
-
else:
|
83 |
engine = st.selectbox("DB engine", SUPPORTED_ENGINES)
|
84 |
conn = st.text_input("SQLAlchemy connection string")
|
85 |
if conn:
|
86 |
try:
|
87 |
tables = list_tables(conn)
|
88 |
-
|
89 |
if st.button("Fetch table"):
|
90 |
-
csv_path = fetch_data_from_db(conn,
|
91 |
-
st.success(f"Fetched **{
|
92 |
except Exception as e:
|
93 |
st.error(f"Connection failed: {e}")
|
94 |
st.stop()
|
95 |
|
96 |
-
if csv_path
|
97 |
st.stop()
|
98 |
|
99 |
-
#
|
100 |
with open(csv_path, "rb") as f:
|
101 |
st.download_button("โฌ๏ธ Download working CSV", f, file_name=os.path.basename(csv_path))
|
102 |
|
103 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
104 |
-
#
|
105 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
106 |
df_head = pd.read_csv(csv_path, nrows=5)
|
107 |
st.dataframe(df_head)
|
108 |
|
109 |
-
date_col
|
110 |
numeric_cols = df_head.select_dtypes("number").columns.tolist()
|
111 |
-
metric_col
|
112 |
|
113 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
114 |
-
#
|
115 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
116 |
with st.spinner("Parsing datasetโฆ"):
|
117 |
summary_text = parse_csv_tool(csv_path)
|
@@ -119,14 +118,14 @@ with st.spinner("Parsing datasetโฆ"):
|
|
119 |
with st.spinner("Generating trend chartโฆ"):
|
120 |
trend_fig = plot_metric_tool(csv_path, date_col, metric_col)
|
121 |
if isinstance(trend_fig, go.Figure):
|
|
|
122 |
st.plotly_chart(trend_fig, use_container_width=True)
|
123 |
else:
|
124 |
st.warning(trend_fig)
|
125 |
|
126 |
-
with st.spinner("
|
127 |
forecast_text = forecast_metric_tool(csv_path, date_col, metric_col)
|
128 |
|
129 |
-
# Display the forecast image saved under /tmp
|
130 |
st.subheader(f"๐ฎ {metric_col} Forecast")
|
131 |
forecast_png = os.path.join(TEMP_DIR, "forecast_plot.png")
|
132 |
if os.path.exists(forecast_png):
|
@@ -135,7 +134,7 @@ else:
|
|
135 |
st.warning("Forecast image not found.")
|
136 |
|
137 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
138 |
-
#
|
139 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
140 |
prompt = (
|
141 |
f"You are **BizIntel Strategist AI**.\n\n"
|
@@ -155,36 +154,34 @@ st.markdown(strategy_md)
|
|
155 |
st.download_button("โฌ๏ธ Download Strategy (.md)", strategy_md, file_name="strategy.md")
|
156 |
|
157 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
158 |
-
#
|
159 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
160 |
full_df = pd.read_csv(csv_path, low_memory=False)
|
161 |
total_rows = len(full_df)
|
162 |
-
num_cols =
|
163 |
missing_pct = full_df.isna().mean().mean() * 100
|
164 |
|
165 |
st.markdown("---")
|
166 |
st.subheader("๐ Dataset Overview")
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
|
172 |
with st.expander("๐ Detailed descriptive statistics"):
|
173 |
-
stats_df = full_df.describe().T.reset_index().rename(columns={"index":
|
174 |
-
st.dataframe(
|
175 |
-
|
176 |
-
use_container_width=True,
|
177 |
-
)
|
178 |
|
179 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
180 |
-
#
|
181 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
182 |
st.markdown("---")
|
183 |
st.subheader("๐ Optional Exploratory Visuals")
|
184 |
|
185 |
if st.checkbox("Histogram"):
|
186 |
-
|
187 |
-
st.plotly_chart(histogram_tool(csv_path,
|
188 |
|
189 |
if st.checkbox("Scatter-matrix"):
|
190 |
sel = st.multiselect("Choose columns", numeric_cols, default=numeric_cols[:3])
|
|
|
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
|
|
|
12 |
import plotly.graph_objects as go
|
13 |
|
14 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
15 |
+
# 0) Redirect all Plotly write_image() into a writable temp dir
|
16 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
17 |
+
TMP = tempfile.gettempdir()
|
18 |
_original_write = go.Figure.write_image
|
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 _original_write(self, safe_path, *args, **kwargs)
|
24 |
|
25 |
go.Figure.write_image = _safe_write
|
26 |
|
27 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
28 |
+
# 1) Imports for tools & DB connector
|
29 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
30 |
from tools.csv_parser import parse_csv_tool
|
31 |
+
from tools.plot_generator import plot_metric_tool # (csv_path, date_col, metric_col) โ Figure or error
|
32 |
+
from tools.forecaster import forecast_metric_tool # (csv_path, date_col, metric_col) โ text + /tmp/forecast_plot.png
|
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 |
|
36 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
37 |
+
# 2) Gemini 1.5 Pro initialization
|
38 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
39 |
genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
|
40 |
gemini = genai.GenerativeModel(
|
|
|
47 |
)
|
48 |
|
49 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
50 |
+
# 3) Streamlit page setup
|
51 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
52 |
st.set_page_config(page_title="BizIntel AI Ultra", layout="wide")
|
53 |
st.title("๐ BizIntel AI Ultra โ Advanced Analytics + Gemini 1.5 Pro")
|
|
|
54 |
TEMP_DIR = tempfile.gettempdir()
|
55 |
|
56 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
57 |
+
# 4) Data source selection: CSV/Excel or SQL Database
|
58 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
59 |
source = st.radio("Select data source", ["Upload CSV / Excel", "Connect to SQL Database"])
|
60 |
+
csv_path = None
|
61 |
|
62 |
if source == "Upload CSV / Excel":
|
63 |
+
upload = st.file_uploader("Upload CSV or Excel (โค 500 MB)", type=["csv","xlsx","xls"])
|
64 |
if upload:
|
65 |
+
tmp_file = os.path.join(TEMP_DIR, upload.name)
|
66 |
+
with open(tmp_file, "wb") as f:
|
67 |
f.write(upload.read())
|
68 |
|
69 |
if upload.name.lower().endswith(".csv"):
|
70 |
+
csv_path = tmp_file
|
71 |
else:
|
72 |
try:
|
73 |
+
df_xl = pd.read_excel(tmp_file, sheet_name=0)
|
74 |
+
csv_path = os.path.splitext(tmp_file)[0] + ".csv"
|
75 |
df_xl.to_csv(csv_path, index=False)
|
76 |
except Exception as e:
|
77 |
st.error(f"Excel parsing failed: {e}")
|
78 |
st.stop()
|
79 |
st.success(f"{upload.name} saved โ
")
|
80 |
|
81 |
+
else:
|
82 |
engine = st.selectbox("DB engine", SUPPORTED_ENGINES)
|
83 |
conn = st.text_input("SQLAlchemy connection string")
|
84 |
if conn:
|
85 |
try:
|
86 |
tables = list_tables(conn)
|
87 |
+
tbl = st.selectbox("Table", tables)
|
88 |
if st.button("Fetch table"):
|
89 |
+
csv_path = fetch_data_from_db(conn, tbl)
|
90 |
+
st.success(f"Fetched **{tbl}** as CSV โ
")
|
91 |
except Exception as e:
|
92 |
st.error(f"Connection failed: {e}")
|
93 |
st.stop()
|
94 |
|
95 |
+
if not csv_path:
|
96 |
st.stop()
|
97 |
|
98 |
+
# Download the working CSV
|
99 |
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 first rows & let user pick date + numeric metric
|
104 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
105 |
df_head = pd.read_csv(csv_path, nrows=5)
|
106 |
st.dataframe(df_head)
|
107 |
|
108 |
+
date_col = st.selectbox("Select date/time column", df_head.columns)
|
109 |
numeric_cols = df_head.select_dtypes("number").columns.tolist()
|
110 |
+
metric_col = st.selectbox("Select numeric metric column", numeric_cols)
|
111 |
|
112 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
113 |
+
# 6) Local analysis: summary, trend chart, forecast
|
114 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
115 |
with st.spinner("Parsing datasetโฆ"):
|
116 |
summary_text = parse_csv_tool(csv_path)
|
|
|
118 |
with st.spinner("Generating trend chartโฆ"):
|
119 |
trend_fig = plot_metric_tool(csv_path, date_col, metric_col)
|
120 |
if isinstance(trend_fig, go.Figure):
|
121 |
+
st.subheader("๐ Trend")
|
122 |
st.plotly_chart(trend_fig, use_container_width=True)
|
123 |
else:
|
124 |
st.warning(trend_fig)
|
125 |
|
126 |
+
with st.spinner("Running forecastโฆ"):
|
127 |
forecast_text = forecast_metric_tool(csv_path, date_col, metric_col)
|
128 |
|
|
|
129 |
st.subheader(f"๐ฎ {metric_col} Forecast")
|
130 |
forecast_png = os.path.join(TEMP_DIR, "forecast_plot.png")
|
131 |
if os.path.exists(forecast_png):
|
|
|
134 |
st.warning("Forecast image not found.")
|
135 |
|
136 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
137 |
+
# 7) Gemini-driven strategy recommendations
|
138 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
139 |
prompt = (
|
140 |
f"You are **BizIntel Strategist AI**.\n\n"
|
|
|
154 |
st.download_button("โฌ๏ธ Download Strategy (.md)", strategy_md, file_name="strategy.md")
|
155 |
|
156 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
157 |
+
# 8) KPI cards + detailed Stats
|
158 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
159 |
full_df = pd.read_csv(csv_path, low_memory=False)
|
160 |
total_rows = len(full_df)
|
161 |
+
num_cols = full_df.shape[1]
|
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", f"{total_rows:,}")
|
168 |
+
c2.metric("Columns", str(num_cols))
|
169 |
+
c3.metric("Missing %", f"{missing_pct:.1f}%")
|
170 |
|
171 |
with st.expander("๐ Detailed descriptive statistics"):
|
172 |
+
stats_df = full_df.describe().T.reset_index().rename(columns={"index":"Feature"})
|
173 |
+
st.dataframe(stats_df.style.format(precision=2).background_gradient(cmap="Blues"),
|
174 |
+
use_container_width=True)
|
|
|
|
|
175 |
|
176 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
177 |
+
# 9) Optional Exploratory Visuals
|
178 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
179 |
st.markdown("---")
|
180 |
st.subheader("๐ Optional Exploratory Visuals")
|
181 |
|
182 |
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-matrix"):
|
187 |
sel = st.multiselect("Choose columns", numeric_cols, default=numeric_cols[:3])
|