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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -44
app.py CHANGED
@@ -1,40 +1,37 @@
1
- # app.py โ€” BizIntelย AIย Ultra (Geminiโ€ฏ1.5ย Pro, selectable date column)
2
-
3
  import os
4
  import tempfile
5
  import pandas as pd
6
  import streamlit as st
7
  import google.generativeai as genai
 
8
 
9
  from tools.csv_parser import parse_csv_tool
10
  from tools.plot_generator import plot_sales_tool
11
- from tools.forecaster import forecast_tool # must accept date_col
 
 
 
 
 
12
 
13
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
14
- # 1. GEMINI CONFIG (1.5โ€‘Pro, temperature 0.7)
15
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
16
  genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
17
  gemini = genai.GenerativeModel(
18
  "gemini-1.5-pro-latest",
19
  generation_config={
20
  "temperature": 0.7,
21
  "top_p": 0.9,
22
- # Allowed types: text/plain, application/json, etc.
23
  "response_mime_type": "text/plain",
24
  },
25
  )
26
 
27
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
28
- # 2. STREAMLIT PAGE SETUP
29
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
30
  st.set_page_config(page_title="BizIntel AI Ultra โ€“ Geminiย 1.5ย Pro", layout="wide")
31
- st.title("๐Ÿ“Š BizIntelย AIย Ultraย โ€“ Advanced Analytics Pipeline")
32
 
33
  TEMP_DIR = tempfile.gettempdir()
34
 
35
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
36
- # 3. CSV UPLOAD
37
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
38
  csv_file = st.file_uploader("Upload CSV (โ‰คโ€ฏ200โ€ฏMB)", type=["csv"])
39
  if csv_file is None:
40
  st.info("โฌ†๏ธย Upload a CSV to begin.")
@@ -43,34 +40,34 @@ if csv_file is None:
43
  csv_path = os.path.join(TEMP_DIR, csv_file.name)
44
  with open(csv_path, "wb") as f:
45
  f.write(csv_file.read())
46
- st.success("CSV saved to temporary storage โœ…")
47
 
48
- # Preview first rows and choose date column
49
  df_preview = pd.read_csv(csv_path, nrows=5)
50
  st.dataframe(df_preview)
51
- date_col = st.selectbox("Select the date/time column for forecasting", df_preview.columns)
52
 
53
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
54
- # 4. LOCAL TOOL EXECUTION
55
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
56
- with st.spinner("๐Ÿ” Parsing CSVโ€ฆ"):
57
  summary_text = parse_csv_tool(csv_path)
58
 
59
- with st.spinner("๐Ÿ“ˆ Generating sales trend chartโ€ฆ"):
60
- _ = plot_sales_tool(csv_path) # generates sales_plot.png
 
61
 
62
- with st.spinner("๐Ÿ”ฎ Forecasting future metricsโ€ฆ"):
63
- forecast_text = forecast_tool(csv_path, date_col=date_col) # uses chosen column
 
 
 
 
64
 
65
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
66
- # 5. GEMINI 1.5โ€‘PRO STRATEGY (sync call)
67
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€๏ฟฝ๏ฟฝโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
68
  prompt = (
69
  f"You are **BizIntel Strategist AI**.\n\n"
70
- f"## CSV Summary\n```\n{summary_text}\n```\n\n"
71
- f"## Forecast Output\n```\n{forecast_text}\n```\n\n"
72
- "### Task\n"
73
- "Return **Markdown** with:\n"
74
  "1. **Five key insights** (bullet list)\n"
75
  "2. **Three actionable strategies** (with expected impact)\n"
76
  "3. **Risk factors or anomalies**\n"
@@ -78,21 +75,40 @@ prompt = (
78
  )
79
 
80
  st.subheader("๐Ÿš€ Strategy Recommendations (Geminiย 1.5ย Pro)")
81
- with st.spinner("Geminiย 1.5ย Pro is thinkingโ€ฆ"):
82
- response = gemini.generate_content(prompt)
83
 
84
- strategy_md = response.text
85
  st.markdown(strategy_md)
86
 
87
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
88
- # 6. DISPLAY SUMMARY & CHARTS
89
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 
 
90
  st.markdown("---")
91
- st.subheader("๐Ÿ“‘ CSV Summary")
92
- st.text(summary_text)
 
93
 
94
- if os.path.exists("sales_plot.png"):
95
- st.image("sales_plot.png", caption="Sales Trend", use_column_width=True)
 
 
 
96
 
97
- if os.path.exists("forecast_plot.png"):
98
- st.image("forecast_plot.png", caption="Forecast Chart", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import tempfile
3
  import pandas as pd
4
  import streamlit as st
5
  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.")
 
40
  csv_path = os.path.join(TEMP_DIR, csv_file.name)
41
  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"
 
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
+
91
+ num_cols = df_preview.select_dtypes("number").columns
92
 
93
+ # Histogram
94
+ if st.checkbox("Show histogram"):
95
+ hist_col = st.selectbox("Histogram variable", num_cols, key="hist")
96
+ fig_hist = histogram_tool(csv_path, hist_col)
97
+ st.plotly_chart(fig_hist, use_container_width=True)
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
107
+ 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)