Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
-
# app.py โ BizIntel AI Ultra
|
|
|
|
|
2 |
|
3 |
import os
|
4 |
import tempfile
|
@@ -10,34 +12,29 @@ import google.generativeai as genai
|
|
10 |
import plotly.graph_objects as go
|
11 |
|
12 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
13 |
-
#
|
14 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
15 |
_tmp = tempfile.gettempdir()
|
16 |
-
|
17 |
|
18 |
def _safe_write(self, path, *args, **kwargs):
|
19 |
-
# keep only filename, write into tempdir
|
20 |
fname = os.path.basename(path)
|
21 |
safe_path = os.path.join(_tmp, fname)
|
22 |
-
return
|
23 |
|
24 |
go.Figure.write_image = _safe_write
|
25 |
|
26 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
27 |
-
# TOOL IMPORTS
|
28 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
29 |
-
from tools.csv_parser
|
30 |
-
from tools.plot_generator
|
31 |
-
from tools.forecaster
|
32 |
-
from tools.visuals
|
33 |
-
|
34 |
-
scatter_matrix_tool,
|
35 |
-
corr_heatmap_tool,
|
36 |
-
)
|
37 |
-
from db_connector import fetch_data_from_db, list_tables, SUPPORTED_ENGINES
|
38 |
|
39 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
40 |
-
# 1. GEMINI
|
41 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
42 |
genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
|
43 |
gemini = genai.GenerativeModel(
|
@@ -50,7 +47,7 @@ gemini = genai.GenerativeModel(
|
|
50 |
)
|
51 |
|
52 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
53 |
-
# 2. PAGE
|
54 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
55 |
st.set_page_config(page_title="BizIntel AI Ultra", layout="wide")
|
56 |
st.title("๐ BizIntel AI Ultra โ Advanced Analytics + Gemini 1.5 Pro")
|
@@ -58,22 +55,21 @@ st.title("๐ BizIntel AI Ultra โ Advanced Analytics + Gemini 1.5 Pro")
|
|
58 |
TEMP_DIR = tempfile.gettempdir()
|
59 |
|
60 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
61 |
-
# 3. DATA SOURCE
|
62 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
63 |
source = st.radio("Select data source", ["Upload CSV / Excel", "Connect to SQL Database"])
|
64 |
csv_path: str | None = None
|
65 |
|
66 |
if source == "Upload CSV / Excel":
|
67 |
-
|
68 |
-
if
|
69 |
-
temp_path = os.path.join(TEMP_DIR,
|
70 |
with open(temp_path, "wb") as f:
|
71 |
-
f.write(
|
72 |
|
73 |
-
if
|
74 |
csv_path = temp_path
|
75 |
else:
|
76 |
-
# Excel โ load sheet0 โ write out to CSV
|
77 |
try:
|
78 |
df_xl = pd.read_excel(temp_path, sheet_name=0)
|
79 |
csv_path = os.path.splitext(temp_path)[0] + ".csv"
|
@@ -81,10 +77,9 @@ if source == "Upload CSV / Excel":
|
|
81 |
except Exception as e:
|
82 |
st.error(f"Excel parsing failed: {e}")
|
83 |
st.stop()
|
|
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
else:
|
88 |
engine = st.selectbox("DB engine", SUPPORTED_ENGINES)
|
89 |
conn = st.text_input("SQLAlchemy connection string")
|
90 |
if conn:
|
@@ -101,17 +96,18 @@ else:
|
|
101 |
if csv_path is None:
|
102 |
st.stop()
|
103 |
|
|
|
104 |
with open(csv_path, "rb") as f:
|
105 |
st.download_button("โฌ๏ธ Download working CSV", f, file_name=os.path.basename(csv_path))
|
106 |
|
107 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
108 |
-
# 4. COLUMN SELECTION
|
109 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
110 |
df_head = pd.read_csv(csv_path, nrows=5)
|
111 |
st.dataframe(df_head)
|
112 |
|
113 |
date_col = st.selectbox("Select date/time column", df_head.columns)
|
114 |
-
numeric_cols = df_head.select_dtypes("number").columns
|
115 |
metric_col = st.selectbox("Select numeric metric column", numeric_cols)
|
116 |
|
117 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
@@ -120,7 +116,7 @@ metric_col = st.selectbox("Select numeric metric column", numeric_cols)
|
|
120 |
with st.spinner("Parsing datasetโฆ"):
|
121 |
summary_text = parse_csv_tool(csv_path)
|
122 |
|
123 |
-
with st.spinner("
|
124 |
trend_fig = plot_metric_tool(csv_path, date_col, metric_col)
|
125 |
if isinstance(trend_fig, go.Figure):
|
126 |
st.plotly_chart(trend_fig, use_container_width=True)
|
@@ -130,18 +126,16 @@ else:
|
|
130 |
with st.spinner("Forecastingโฆ"):
|
131 |
forecast_text = forecast_metric_tool(csv_path, date_col, metric_col)
|
132 |
|
133 |
-
#
|
134 |
st.subheader(f"๐ฎ {metric_col} Forecast")
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
else:
|
141 |
-
st.warning("Forecast image not found.")
|
142 |
|
143 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
144 |
-
# 6. GEMINI STRATEGY
|
145 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
146 |
prompt = (
|
147 |
f"You are **BizIntel Strategist AI**.\n\n"
|
@@ -161,7 +155,7 @@ st.markdown(strategy_md)
|
|
161 |
st.download_button("โฌ๏ธ Download Strategy (.md)", strategy_md, file_name="strategy.md")
|
162 |
|
163 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
164 |
-
# 7. KPI CARDS + DETAILED STATS
|
165 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
166 |
full_df = pd.read_csv(csv_path, low_memory=False)
|
167 |
total_rows = len(full_df)
|
@@ -170,10 +164,10 @@ missing_pct = full_df.isna().mean().mean() * 100
|
|
170 |
|
171 |
st.markdown("---")
|
172 |
st.subheader("๐ Dataset Overview")
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
|
178 |
with st.expander("๐ Detailed descriptive statistics"):
|
179 |
stats_df = full_df.describe().T.reset_index().rename(columns={"index": "Feature"})
|
@@ -189,8 +183,8 @@ st.markdown("---")
|
|
189 |
st.subheader("๐ Optional Exploratory Visuals")
|
190 |
|
191 |
if st.checkbox("Histogram"):
|
192 |
-
|
193 |
-
st.plotly_chart(histogram_tool(csv_path,
|
194 |
|
195 |
if st.checkbox("Scatter-matrix"):
|
196 |
sel = st.multiselect("Choose columns", numeric_cols, default=numeric_cols[:3])
|
|
|
1 |
+
# app.py โ BizIntel AI Ultra
|
2 |
+
# Supports: CSV/Excel/DB ingestion, any numeric metric + date, interactive Plotly, Gemini 1.5 Pro,
|
3 |
+
# KPI cards, optional EDA, safe image writes, and updated use_container_width flag.
|
4 |
|
5 |
import os
|
6 |
import tempfile
|
|
|
12 |
import plotly.graph_objects as go
|
13 |
|
14 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
15 |
+
# REDIRECT ALL write_image CALLS TO A WRITABLE TEMP DIRECTORY
|
16 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
17 |
_tmp = tempfile.gettempdir()
|
18 |
+
_original_write = go.Figure.write_image
|
19 |
|
20 |
def _safe_write(self, path, *args, **kwargs):
|
|
|
21 |
fname = os.path.basename(path)
|
22 |
safe_path = os.path.join(_tmp, fname)
|
23 |
+
return _original_write(self, safe_path, *args, **kwargs)
|
24 |
|
25 |
go.Figure.write_image = _safe_write
|
26 |
|
27 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
28 |
+
# TOOL IMPORTS (updated generic versions)
|
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 |
+
# 1. GEMINI 1.5-PRO INITIALIZATION
|
38 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
39 |
genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
|
40 |
gemini = genai.GenerativeModel(
|
|
|
47 |
)
|
48 |
|
49 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
50 |
+
# 2. STREAMLIT PAGE CONFIG
|
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")
|
|
|
55 |
TEMP_DIR = tempfile.gettempdir()
|
56 |
|
57 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
58 |
+
# 3. DATA SOURCE: CSV / EXCEL / SQL DATABASE
|
59 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
60 |
source = st.radio("Select data source", ["Upload CSV / Excel", "Connect to SQL Database"])
|
61 |
csv_path: str | None = None
|
62 |
|
63 |
if source == "Upload CSV / Excel":
|
64 |
+
upload = st.file_uploader("Upload CSV or Excel (โค 500 MB)", type=["csv", "xlsx", "xls"])
|
65 |
+
if upload:
|
66 |
+
temp_path = os.path.join(TEMP_DIR, upload.name)
|
67 |
with open(temp_path, "wb") as f:
|
68 |
+
f.write(upload.read())
|
69 |
|
70 |
+
if upload.name.lower().endswith(".csv"):
|
71 |
csv_path = temp_path
|
72 |
else:
|
|
|
73 |
try:
|
74 |
df_xl = pd.read_excel(temp_path, sheet_name=0)
|
75 |
csv_path = os.path.splitext(temp_path)[0] + ".csv"
|
|
|
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: # SQL Database path
|
|
|
|
|
83 |
engine = st.selectbox("DB engine", SUPPORTED_ENGINES)
|
84 |
conn = st.text_input("SQLAlchemy connection string")
|
85 |
if conn:
|
|
|
96 |
if csv_path is None:
|
97 |
st.stop()
|
98 |
|
99 |
+
# Offer download of the working CSV
|
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 |
+
# 4. COLUMN SELECTION: DATE + METRIC
|
105 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
106 |
df_head = pd.read_csv(csv_path, nrows=5)
|
107 |
st.dataframe(df_head)
|
108 |
|
109 |
date_col = st.selectbox("Select date/time column", df_head.columns)
|
110 |
+
numeric_cols = df_head.select_dtypes("number").columns.tolist()
|
111 |
metric_col = st.selectbox("Select numeric metric column", numeric_cols)
|
112 |
|
113 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
|
116 |
with st.spinner("Parsing datasetโฆ"):
|
117 |
summary_text = parse_csv_tool(csv_path)
|
118 |
|
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)
|
|
|
126 |
with st.spinner("Forecastingโฆ"):
|
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):
|
133 |
+
st.image(forecast_png, use_container_width=True)
|
134 |
+
else:
|
135 |
+
st.warning("Forecast image not found.")
|
|
|
|
|
136 |
|
137 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
138 |
+
# 6. GEMINI STRATEGY RECOMMENDATIONS
|
139 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
140 |
prompt = (
|
141 |
f"You are **BizIntel Strategist AI**.\n\n"
|
|
|
155 |
st.download_button("โฌ๏ธ Download Strategy (.md)", strategy_md, file_name="strategy.md")
|
156 |
|
157 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
158 |
+
# 7. KPI CARDS + DETAILED STATS EXPANDER
|
159 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
160 |
full_df = pd.read_csv(csv_path, low_memory=False)
|
161 |
total_rows = len(full_df)
|
|
|
164 |
|
165 |
st.markdown("---")
|
166 |
st.subheader("๐ Dataset Overview")
|
167 |
+
col1, col2, col3 = st.columns(3)
|
168 |
+
col1.metric("Rows", f"{total_rows:,}")
|
169 |
+
col2.metric("Columns", str(num_cols))
|
170 |
+
col3.metric("Missing %", f"{missing_pct:.1f}%")
|
171 |
|
172 |
with st.expander("๐ Detailed descriptive statistics"):
|
173 |
stats_df = full_df.describe().T.reset_index().rename(columns={"index": "Feature"})
|
|
|
183 |
st.subheader("๐ Optional Exploratory Visuals")
|
184 |
|
185 |
if st.checkbox("Histogram"):
|
186 |
+
hist_col = st.selectbox("Variable", numeric_cols, key="hist")
|
187 |
+
st.plotly_chart(histogram_tool(csv_path, hist_col), use_container_width=True)
|
188 |
|
189 |
if st.checkbox("Scatter-matrix"):
|
190 |
sel = st.multiselect("Choose columns", numeric_cols, default=numeric_cols[:3])
|