mgbam commited on
Commit
d826a13
ยท
verified ยท
1 Parent(s): b3a1f0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -15
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py โ€” BizIntelย AIย Ultra (Geminiโ€ฏ1.5ย Pro, selectable date column)
2
 
3
  import os
4
  import tempfile
@@ -8,7 +8,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 # accepts date_col
12
 
13
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
14
  # 1. GEMINI CONFIG (1.5โ€‘Pro, temperature 0.7)
@@ -36,7 +36,7 @@ TEMP_DIR = tempfile.gettempdir()
36
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
37
  csv_file = st.file_uploader("Upload CSV (โ‰คโ€ฏ200โ€ฏMB)", type=["csv"])
38
  if csv_file is None:
39
- st.info("โฌ†๏ธ Upload a CSV to begin.")
40
  st.stop()
41
 
42
  csv_path = os.path.join(TEMP_DIR, csv_file.name)
@@ -44,10 +44,10 @@ with open(csv_path, "wb") as f:
44
  f.write(csv_file.read())
45
  st.success("CSV saved to temporary storage โœ…")
46
 
47
- # Preview first rows and let user pick date column
48
  df_preview = pd.read_csv(csv_path, nrows=5)
49
  st.dataframe(df_preview)
50
- date_col = st.selectbox("Choose the date/time column for forecasting", df_preview.columns)
51
 
52
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
53
  # 4. LOCAL TOOL EXECUTION
@@ -56,20 +56,20 @@ with st.spinner("๐Ÿ” Parsing CSVโ€ฆ"):
56
  summary_text = parse_csv_tool(csv_path)
57
 
58
  with st.spinner("๐Ÿ“ˆ Generating sales trend chartโ€ฆ"):
59
- _ = plot_sales_tool(csv_path) # generates sales_plot.png
60
 
61
  with st.spinner("๐Ÿ”ฎ Forecasting future metricsโ€ฆ"):
62
- forecast_text = forecast_tool(csv_path, date_col=date_col) # pass chosen column
63
 
64
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
65
- # 5. GEMINI 1.5โ€‘PRO STRATEGY (streaming)
66
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
67
  prompt = (
68
  f"You are **BizIntel Strategist AI**.\n\n"
69
  f"## CSV Summary\n```\n{summary_text}\n```\n\n"
70
  f"## Forecast Output\n```\n{forecast_text}\n```\n\n"
71
  "### Task\n"
72
- "Return Markdown with:\n"
73
  "1. **Five key insights** (bullet list)\n"
74
  "2. **Three actionable strategies** (with expected impact)\n"
75
  "3. **Risk factors or anomalies**\n"
@@ -77,13 +77,11 @@ prompt = (
77
  )
78
 
79
  st.subheader("๐Ÿš€ Strategy Recommendations (Geminiย 1.5ย Pro)")
80
- placeholder = st.empty()
81
- strategy_md = ""
82
 
83
- for chunk in gemini.generate_content_stream(prompt):
84
- if chunk.candidate and chunk.candidate.content.parts:
85
- strategy_md += chunk.candidate.content.parts[0].text
86
- placeholder.markdown(strategy_md)
87
 
88
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
89
  # 6. DISPLAY SUMMARY & CHARTS
@@ -94,5 +92,6 @@ st.text(summary_text)
94
 
95
  if os.path.exists("sales_plot.png"):
96
  st.image("sales_plot.png", caption="Sales Trend", use_column_width=True)
 
97
  if os.path.exists("forecast_plot.png"):
98
  st.image("forecast_plot.png", caption="Forecast Chart", use_column_width=True)
 
1
+ # app.py โ€” BizIntelย AIย Ultra (Geminiโ€ฏ1.5ย Pro, dateโ€‘column selector)
2
 
3
  import os
4
  import tempfile
 
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 # accepts date_col argument
12
 
13
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
14
  # 1. GEMINI CONFIG (1.5โ€‘Pro, temperature 0.7)
 
36
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
37
  csv_file = st.file_uploader("Upload CSV (โ‰คโ€ฏ200โ€ฏMB)", type=["csv"])
38
  if csv_file is None:
39
+ st.info("โฌ†๏ธย Upload a CSV to begin.")
40
  st.stop()
41
 
42
  csv_path = os.path.join(TEMP_DIR, csv_file.name)
 
44
  f.write(csv_file.read())
45
  st.success("CSV saved to temporary storage โœ…")
46
 
47
+ # Preview first rows and choose date column
48
  df_preview = pd.read_csv(csv_path, nrows=5)
49
  st.dataframe(df_preview)
50
+ date_col = st.selectbox("Select the date/time column for forecasting", df_preview.columns)
51
 
52
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
53
  # 4. LOCAL TOOL EXECUTION
 
56
  summary_text = parse_csv_tool(csv_path)
57
 
58
  with st.spinner("๐Ÿ“ˆ Generating sales trend chartโ€ฆ"):
59
+ _ = plot_sales_tool(csv_path) # saves sales_plot.png
60
 
61
  with st.spinner("๐Ÿ”ฎ Forecasting future metricsโ€ฆ"):
62
+ forecast_text = forecast_tool(csv_path, date_col=date_col) # uses chosen column
63
 
64
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
65
+ # 5. GEMINI 1.5โ€‘PRO STRATEGY (sync call)
66
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
67
  prompt = (
68
  f"You are **BizIntel Strategist AI**.\n\n"
69
  f"## CSV Summary\n```\n{summary_text}\n```\n\n"
70
  f"## Forecast Output\n```\n{forecast_text}\n```\n\n"
71
  "### Task\n"
72
+ "Return **Markdown** with:\n"
73
  "1. **Five key insights** (bullet list)\n"
74
  "2. **Three actionable strategies** (with expected impact)\n"
75
  "3. **Risk factors or anomalies**\n"
 
77
  )
78
 
79
  st.subheader("๐Ÿš€ Strategy Recommendations (Geminiย 1.5ย Pro)")
80
+ with st.spinner("Geminiย 1.5ย Pro is thinkingโ€ฆ"):
81
+ response = gemini.generate_content(prompt)
82
 
83
+ strategy_md = response.text
84
+ st.markdown(strategy_md)
 
 
85
 
86
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
87
  # 6. DISPLAY SUMMARY & CHARTS
 
92
 
93
  if os.path.exists("sales_plot.png"):
94
  st.image("sales_plot.png", caption="Sales Trend", use_column_width=True)
95
+
96
  if os.path.exists("forecast_plot.png"):
97
  st.image("forecast_plot.png", caption="Forecast Chart", use_column_width=True)