mgbam commited on
Commit
e490e03
ยท
verified ยท
1 Parent(s): 3acbc9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -32
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import os
2
  import tempfile
3
  import pandas as pd
@@ -6,32 +8,38 @@ import google.generativeai as genai
6
  import plotly.graph_objects as go
7
 
8
  from tools.csv_parser import parse_csv_tool
9
- from tools.plot_generator import plot_sales_tool
10
- from tools.forecaster import forecast_tool
11
  from tools.visuals import (
12
  histogram_tool,
13
  scatter_matrix_tool,
14
  corr_heatmap_tool,
15
  )
16
 
17
- # โ”€โ”€ Gemini 1.5โ€‘Pro configuration โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 
 
18
  genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
19
  gemini = genai.GenerativeModel(
20
  "gemini-1.5-pro-latest",
21
  generation_config={
22
  "temperature": 0.7,
23
  "top_p": 0.9,
24
- "response_mime_type": "text/plain",
25
  },
26
  )
27
 
28
- # โ”€โ”€ Streamlit page setup โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 
 
29
  st.set_page_config(page_title="BizIntel AI Ultra โ€“ Geminiย 1.5ย Pro", layout="wide")
30
  st.title("๐Ÿ“Š BizIntelย AIย Ultraย โ€“ Advanced Analytics")
31
 
32
  TEMP_DIR = tempfile.gettempdir()
33
 
34
- # โ”€โ”€ CSV upload โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 
 
35
  csv_file = st.file_uploader("Upload CSV (โ‰คโ€ฏ200โ€ฏMB)", type=["csv"])
36
  if csv_file is None:
37
  st.info("โฌ†๏ธย Upload a CSV to begin.")
@@ -42,49 +50,55 @@ with open(csv_path, "wb") as f:
42
  f.write(csv_file.read())
43
  st.success("CSV saved โœ…")
44
 
45
- # Preview + date column selection
46
  df_preview = pd.read_csv(csv_path, nrows=5)
47
  st.dataframe(df_preview)
48
  date_col = st.selectbox("Select date/time column for forecasting", df_preview.columns)
49
 
50
- # โ”€โ”€ Local tools: summary, sales trend, forecast โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
51
- with st.spinner("Parsing CSVโ€ฆ"):
 
 
52
  summary_text = parse_csv_tool(csv_path)
53
 
54
- with st.spinner("Generating sales trend chartโ€ฆ"):
55
  sales_fig = plot_sales_tool(csv_path, date_col=date_col)
 
 
 
56
  st.plotly_chart(sales_fig, use_container_width=True)
 
 
57
 
58
- with st.spinner("Forecasting future metricsโ€ฆ"):
59
  forecast_text = forecast_tool(csv_path, date_col=date_col)
60
- if os.path.exists("forecast_plot.png"):
61
- forecast_img = "forecast_plot.png"
62
- else:
63
- forecast_img = None
64
 
65
- # โ”€โ”€ Gemini strategy insights โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 
 
 
 
 
 
66
  prompt = (
67
  f"You are **BizIntel Strategist AI**.\n\n"
68
  f"### CSV Summary\n```\n{summary_text}\n```\n\n"
69
  f"### Forecast Output\n```\n{forecast_text}\n```\n\n"
70
- "Return Markdown with:\n"
71
- "1. **Five key insights** (bullet list)\n"
72
- "2. **Three actionable strategies** (with expected impact)\n"
73
- "3. **Risk factors or anomalies**\n"
74
- "4. **Suggested additional visuals**\n"
75
  )
76
 
77
  st.subheader("๐Ÿš€ Strategy Recommendations (Geminiย 1.5ย Pro)")
78
  with st.spinner("Geminiย 1.5ย Pro is generating insightsโ€ฆ"):
79
  strategy_md = gemini.generate_content(prompt).text
80
-
81
  st.markdown(strategy_md)
82
 
83
- # Display forecast image if exists
84
- if forecast_img:
85
- st.image(forecast_img, caption="Sales Forecast", use_column_width=True)
86
-
87
- # โ”€โ”€ Optional exploratory visuals โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
88
  st.markdown("---")
89
  st.subheader("๐Ÿ” Optional Exploratory Visuals")
90
 
@@ -98,9 +112,11 @@ if st.checkbox("Show histogram"):
98
 
99
  # Scatterโ€‘matrix
100
  if st.checkbox("Show scatterโ€‘matrix"):
101
- multi_cols = st.multiselect("Choose up to 5 columns", num_cols, default=num_cols[:3])
102
- if multi_cols:
103
- fig_scatter = scatter_matrix_tool(csv_path, multi_cols)
 
 
104
  st.plotly_chart(fig_scatter, use_container_width=True)
105
 
106
  # Correlation heatโ€‘map
@@ -108,7 +124,9 @@ if st.checkbox("Show correlation heatโ€‘map"):
108
  fig_corr = corr_heatmap_tool(csv_path)
109
  st.plotly_chart(fig_corr, use_container_width=True)
110
 
111
- # โ”€โ”€ CSV summary text at bottom โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 
 
112
  st.markdown("---")
113
- st.subheader("๐Ÿ“‘ CSV Summary (full Stats)")
114
  st.text(summary_text)
 
1
+ # app.py โ€” BizIntelย AIย Ultra (Geminiโ€ฏ1.5ย Pro, interactive Plotly visuals)
2
+
3
  import os
4
  import tempfile
5
  import pandas as pd
 
8
  import plotly.graph_objects as go
9
 
10
  from tools.csv_parser import parse_csv_tool
11
+ from tools.plot_generator import plot_sales_tool # accepts date_col, returns Figure or str
12
+ from tools.forecaster import forecast_tool # accepts date_col
13
  from tools.visuals import (
14
  histogram_tool,
15
  scatter_matrix_tool,
16
  corr_heatmap_tool,
17
  )
18
 
19
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
20
+ # 1. GEMINI CONFIG (1.5โ€‘Pro, temperature 0.7)
21
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
22
  genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
23
  gemini = genai.GenerativeModel(
24
  "gemini-1.5-pro-latest",
25
  generation_config={
26
  "temperature": 0.7,
27
  "top_p": 0.9,
28
+ "response_mime_type": "text/plain", # must be allowed type
29
  },
30
  )
31
 
32
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
33
+ # 2. STREAMLIT PAGE SETUP
34
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
35
  st.set_page_config(page_title="BizIntel AI Ultra โ€“ Geminiย 1.5ย Pro", layout="wide")
36
  st.title("๐Ÿ“Š BizIntelย AIย Ultraย โ€“ Advanced Analytics")
37
 
38
  TEMP_DIR = tempfile.gettempdir()
39
 
40
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
41
+ # 3. CSV UPLOAD
42
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
43
  csv_file = st.file_uploader("Upload CSV (โ‰คโ€ฏ200โ€ฏMB)", type=["csv"])
44
  if csv_file is None:
45
  st.info("โฌ†๏ธย Upload a CSV to begin.")
 
50
  f.write(csv_file.read())
51
  st.success("CSV saved โœ…")
52
 
53
+ # Preview & date column selection
54
  df_preview = pd.read_csv(csv_path, nrows=5)
55
  st.dataframe(df_preview)
56
  date_col = st.selectbox("Select date/time column for forecasting", df_preview.columns)
57
 
58
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
59
+ # 4. LOCAL TOOL EXECUTION
60
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
61
+ with st.spinner("๐Ÿ” Parsing CSVโ€ฆ"):
62
  summary_text = parse_csv_tool(csv_path)
63
 
64
+ with st.spinner("๐Ÿ“ˆ Generating sales trend chartโ€ฆ"):
65
  sales_fig = plot_sales_tool(csv_path, date_col=date_col)
66
+
67
+ # Show chart or warn
68
+ if isinstance(sales_fig, go.Figure):
69
  st.plotly_chart(sales_fig, use_container_width=True)
70
+ else: # returned error message
71
+ st.warning(sales_fig)
72
 
73
+ with st.spinner("๐Ÿ”ฎ Forecasting future metricsโ€ฆ"):
74
  forecast_text = forecast_tool(csv_path, date_col=date_col)
 
 
 
 
75
 
76
+ # Display forecast PNG if created
77
+ if os.path.exists("forecast_plot.png"):
78
+ st.image("forecast_plot.png", caption="Sales Forecast", use_column_width=True)
79
+
80
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
81
+ # 5. GEMINI 1.5โ€‘PRO STRATEGY
82
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
83
  prompt = (
84
  f"You are **BizIntel Strategist AI**.\n\n"
85
  f"### CSV Summary\n```\n{summary_text}\n```\n\n"
86
  f"### Forecast Output\n```\n{forecast_text}\n```\n\n"
87
+ "Return **Markdown** with:\n"
88
+ "1. Five key insights (bullets)\n"
89
+ "2. Three actionable strategies (with expected impact)\n"
90
+ "3. Risk factors or anomalies\n"
91
+ "4. Suggested additional visuals\n"
92
  )
93
 
94
  st.subheader("๐Ÿš€ Strategy Recommendations (Geminiย 1.5ย Pro)")
95
  with st.spinner("Geminiย 1.5ย Pro is generating insightsโ€ฆ"):
96
  strategy_md = gemini.generate_content(prompt).text
 
97
  st.markdown(strategy_md)
98
 
99
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
100
+ # 6. OPTIONAL EXPLORATORY VISUALS
101
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 
 
102
  st.markdown("---")
103
  st.subheader("๐Ÿ” Optional Exploratory Visuals")
104
 
 
112
 
113
  # Scatterโ€‘matrix
114
  if st.checkbox("Show scatterโ€‘matrix"):
115
+ mult_cols = st.multiselect(
116
+ "Choose up to 5 columns", num_cols, default=num_cols[:3], key="scatter"
117
+ )
118
+ if mult_cols:
119
+ fig_scatter = scatter_matrix_tool(csv_path, mult_cols)
120
  st.plotly_chart(fig_scatter, use_container_width=True)
121
 
122
  # Correlation heatโ€‘map
 
124
  fig_corr = corr_heatmap_tool(csv_path)
125
  st.plotly_chart(fig_corr, use_container_width=True)
126
 
127
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
128
+ # 7. CSV SUMMARY TEXT
129
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
130
  st.markdown("---")
131
+ st.subheader("๐Ÿ“‘ CSV Summary (full stats)")
132
  st.text(summary_text)