mgbam commited on
Commit
63a9cd3
Β·
verified Β·
1 Parent(s): 5405a02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -24
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) 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
 
@@ -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 = 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}")
@@ -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 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
@@ -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 = 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
@@ -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-matrix"):
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 heat-map"):
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)