mgbam commited on
Commit
10c7dea
Β·
verified Β·
1 Parent(s): 907e177

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -53
app.py CHANGED
@@ -1,61 +1,57 @@
1
- import os, tempfile, streamlit as st
2
- from google.adk.runner import run_agent_sync # ← NEW
3
- from agents.analytics_pipeline import analytics_coordinator
4
- from db_connector import fetch_data_from_db, list_tables, SUPPORTED_ENGINES
 
 
 
 
 
 
5
 
6
  st.set_page_config(page_title="BizIntel AI Ultra", layout="wide")
7
- st.title("πŸ“Š BizIntel AI Ultra – Business Intelligence Agent")
8
 
9
  TEMP_DIR = tempfile.gettempdir()
10
 
11
- # ── Data source ───────────────────────────────────────────────
12
- src = st.radio("Select data source", ["Upload CSV", "Connect to SQL Database"])
13
- csv_path = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- if src == "Upload CSV":
16
- up = st.file_uploader("Upload CSV", ["csv"])
17
- if up:
18
- csv_path = os.path.join(TEMP_DIR, up.name)
19
- with open(csv_path, "wb") as f:
20
- f.write(up.read())
21
- st.success("CSV saved βœ…")
22
 
 
 
23
  else:
24
- eng = st.selectbox("DB engine", SUPPORTED_ENGINES)
25
- conn = st.text_input("SQLAlchemy connection string")
26
- if conn:
27
- tbls = list_tables(conn)
28
- tbl = st.selectbox("Table", tbls) if tbls else None
29
- if tbl:
30
- csv_path = fetch_data_from_db(conn, tbl)
31
- st.success(f"Fetched **{tbl}** as CSV βœ…")
32
-
33
- # ── Optional image preview ────────────────────────────────────
34
- st.markdown("---")
35
- img = st.file_uploader("Optional image (PNG/JPG)", ["png", "jpg", "jpeg"])
36
- if img:
37
- img_path = os.path.join(TEMP_DIR, img.name)
38
- with open(img_path, "wb") as f:
39
- f.write(img.read())
40
- st.image(img_path, use_column_width=True)
41
-
42
- # ── Run pipeline via ADK runner ───────────────────────────────
43
- if csv_path:
44
- st.markdown("---")
45
- st.info("Running analytics pipeline…")
46
-
47
- try:
48
- report = run_agent_sync(agent=analytics_coordinator, input_data=csv_path)
49
- except Exception as e:
50
- st.error(f"Agent execution failed: {e}")
51
- st.stop()
52
-
53
- st.subheader("πŸ“ Analysis & Strategy Report")
54
- st.text(report)
55
-
56
- # Show charts if generated
57
- for name, cap in [("sales_plot.png", "Sales Trend"),
58
- ("forecast_plot.png", "Forecast Chart")]:
59
- p = os.path.join(TEMP_DIR, name)
60
- if os.path.exists(p):
61
- st.image(p, caption=cap, use_column_width=True)
 
1
+ import os, tempfile, streamlit as st, pandas as pd
2
+ import google.generativeai as genai
3
+ from tools.csv_parser import parse_csv_tool
4
+ from tools.plot_generator import plot_sales_tool
5
+ from tools.forecaster import forecast_tool
6
+
7
+ # Configure Gemini
8
+ import os
9
+ genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
10
+ model = genai.GenerativeModel("gemini-pro")
11
 
12
  st.set_page_config(page_title="BizIntel AI Ultra", layout="wide")
13
+ st.title("πŸ“Š BizIntel AI Ultra – Emergency Pipeline")
14
 
15
  TEMP_DIR = tempfile.gettempdir()
16
 
17
+ # 1. CSV upload
18
+ csv = st.file_uploader("Upload CSV", ["csv"])
19
+ if csv:
20
+ csv_path = os.path.join(TEMP_DIR, csv.name)
21
+ with open(csv_path, "wb") as f:
22
+ f.write(csv.read())
23
+ st.success("CSV saved βœ…")
24
+
25
+ # 2. Run local tools
26
+ summary = parse_csv_tool(csv_path)
27
+ plot_msg = plot_sales_tool(csv_path)
28
+ forecast_msg = forecast_tool(csv_path)
29
+
30
+ # 3. Strategy via Gemini
31
+ prompt = f"""
32
+ You are a business strategist AI.
33
+ CSV SUMMARY:
34
+ {summary}
35
+
36
+ FORECAST:
37
+ {forecast_msg}
38
+
39
+ Provide key insights and 3 actionable strategies.
40
+ """
41
+
42
+ strat = model.generate_content(prompt).text
43
+
44
+ # 4. Display results
45
+ st.subheader("πŸ“ Data Summary")
46
+ st.text(summary)
47
+
48
+ if "Generated sales_plot.png" in plot_msg and os.path.exists("sales_plot.png"):
49
+ st.image("sales_plot.png", caption="Sales Trend", use_column_width=True)
50
 
51
+ if "Generated forecast_plot.png" in forecast_msg and os.path.exists("forecast_plot.png"):
52
+ st.image("forecast_plot.png", caption="Forecast Chart", use_column_width=True)
 
 
 
 
 
53
 
54
+ st.subheader("πŸš€ Strategy Recommendations")
55
+ st.markdown(strat)
56
  else:
57
+ st.info("Upload a CSV to begin.")