mgbam commited on
Commit
c7c64f3
Β·
verified Β·
1 Parent(s): be434c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -74
app.py CHANGED
@@ -1,10 +1,8 @@
1
- # app.py β€” BizIntelΒ AIΒ UltraΒ v2
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, BytesIO
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 # returns text & PNG; we’ll also grab df
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
- # Gemini 1.5‑Pro
26
  # ──────────────────────────────────────────────────────────────
27
  genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
28
  gemini = genai.GenerativeModel(
29
  "gemini-1.5-pro-latest",
30
- generation_config={"temperature": 0.7, "top_p": 0.9, "response_mime_type": "text/plain"},
 
 
 
 
31
  )
32
 
33
  # ──────────────────────────────────────────────────────────────
34
- # Streamlit page
35
  # ──────────────────────────────────────────────────────────────
36
  st.set_page_config(page_title="BizIntelΒ AIΒ Ultra", layout="wide")
37
- st.title("πŸ“Š BizIntelΒ AIΒ Ultra – Advanced AnalyticsΒ + GeminiΒ 1.5Β Pro")
38
 
39
  TEMP_DIR = tempfile.gettempdir()
40
 
41
  # ──────────────────────────────────────────────────────────────
42
- # 1. CHOOSE DATA SOURCE
43
  # ──────────────────────────────────────────────────────────────
44
- data_source = st.radio("Select data source", ["Upload CSV", "Connect to SQL Database"])
45
- csv_path: str | None = None
46
 
47
- if data_source == "Upload CSV":
48
- csv_file = st.file_uploader("Upload CSV (≀ 200β€―MB)", type=["csv"])
49
- if csv_file:
50
- csv_path = os.path.join(TEMP_DIR, csv_file.name)
51
  with open(csv_path, "wb") as f:
52
- f.write(csv_file.read())
53
  st.success("CSV saved βœ…")
54
 
55
- elif data_source == "Connect to SQL Database":
56
  engine = st.selectbox("DB engine", SUPPORTED_ENGINES)
57
- conn_str = st.text_input("SQLAlchemy connection string")
58
- if conn_str:
59
  try:
60
- tables = list_tables(conn_str)
 
 
 
 
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
- # Offer original CSV download
75
  with open(csv_path, "rb") as f:
76
- st.download_button("⬇️ Download original CSV", f, file_name=os.path.basename(csv_path))
77
 
78
  # ──────────────────────────────────────────────────────────────
79
- # 2. PREVIEW + DATE COL
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
- # 3. RUN LOCAL TOOLS
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) # PNG created
102
- # Also capture forecast df from statsmodels if you return it (optional)
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
- # Download forecast CSV if df exists
113
- if forecast_df is not None and not forecast_df.empty:
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
- # 4. GEMINI STRATEGY
120
  # ──────────────────────────────────────────────────────────────
121
  prompt = (
122
  f"You are **BizIntel Strategist AI**.\n\n"
123
- "### CSV Summary\n"
124
- f"```\n{summary_text}\n```\n\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
- # Download strategy as Markdown
140
- st.download_button("⬇️ Download Strategy (.md)", strategy_md, file_name="strategy.md")
 
 
 
 
141
 
142
  # ──────────────────────────────────────────────────────────────
143
- # 5. OPTIONAL EXPLORATORY VISUALS
144
  # ──────────────────────────────────────────────────────────────
145
  st.markdown("---")
146
  st.subheader("πŸ” Optional Exploratory Visuals")
147
 
148
- num_cols = df_preview.select_dtypes("number").columns
149
 
150
  if st.checkbox("Histogram"):
151
- col = st.selectbox("Variable", num_cols, key="hist")
152
- st.plotly_chart(histogram_tool(csv_path, col), use_container_width=True)
153
 
154
  if st.checkbox("Scatter‑matrix"):
155
- cols = st.multiselect("Choose up to 5 columns", num_cols, default=num_cols[:3])
156
- if cols:
157
- st.plotly_chart(scatter_matrix_tool(csv_path, cols), use_container_width=True)
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)