mgbam commited on
Commit
8171765
Β·
verified Β·
1 Parent(s): 0472dbd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -36
app.py CHANGED
@@ -1,4 +1,5 @@
1
- import os, tempfile, streamlit as st, inspect
 
2
  from agents.analytics_pipeline import analytics_coordinator
3
  from db_connector import fetch_data_from_db, list_tables, SUPPORTED_ENGINES
4
 
@@ -7,11 +8,11 @@ st.title("πŸ“Š BizIntel AI Ultra – Business Intelligence Agent")
7
 
8
  TEMP_DIR = tempfile.gettempdir()
9
 
10
- # ── 1. Data source ───────────────────────────────────────────
11
- source = st.radio("Select data source", ["Upload CSV", "Connect to SQL Database"])
12
  csv_path = None
13
 
14
- if source == "Upload CSV":
15
  up = st.file_uploader("Upload CSV", ["csv"])
16
  if up:
17
  csv_path = os.path.join(TEMP_DIR, up.name)
@@ -20,49 +21,31 @@ if source == "Upload CSV":
20
  st.success("CSV saved βœ…")
21
 
22
  else:
23
- engine = st.selectbox("DB engine", SUPPORTED_ENGINES)
24
  conn = st.text_input("SQLAlchemy connection string")
25
  if conn:
26
- tables = list_tables(conn)
27
- table = st.selectbox("Table", tables) if tables else None
28
- if table:
29
- csv_path = fetch_data_from_db(conn, table)
30
- st.success(f"Fetched **{table}** as CSV βœ…")
31
 
32
- # ── 2. Optional image preview ────────────────────────────────
33
  st.markdown("---")
34
  img = st.file_uploader("Optional image (PNG/JPG)", ["png", "jpg", "jpeg"])
35
  if img:
36
- img_p = os.path.join(TEMP_DIR, img.name)
37
- with open(img_p, "wb") as f:
38
  f.write(img.read())
39
- st.image(img_p, use_column_width=True)
40
 
41
- # ── 3. Run agent pipeline ────────────────────────────────────
42
  if csv_path:
43
  st.markdown("---")
44
  st.info("Running analytics pipeline…")
45
 
46
- # Discover an executable method
47
- cand_methods = ["invoke", "run", "execute", "chat", "__call__"]
48
- exec_fn = None
49
- for m in cand_methods:
50
- if hasattr(analytics_coordinator, m) and callable(getattr(analytics_coordinator, m)):
51
- exec_fn = getattr(analytics_coordinator, m)
52
- break
53
-
54
- if exec_fn is None:
55
- st.error("Cannot find an execution method on LlmAgent. Available attrs:\n"
56
- + ", ".join(dir(analytics_coordinator)))
57
- st.stop()
58
-
59
- # Determine if function expects keyword or positional arg
60
- sig = inspect.signature(exec_fn)
61
  try:
62
- if "input" in sig.parameters:
63
- report = exec_fn(input=csv_path)
64
- else:
65
- report = exec_fn(csv_path)
66
  except Exception as e:
67
  st.error(f"Agent execution failed: {e}")
68
  st.stop()
@@ -70,8 +53,9 @@ if csv_path:
70
  st.subheader("πŸ“ Analysis & Strategy Report")
71
  st.text(report)
72
 
73
- # Show charts if any were created
74
- for name, cap in [("sales_plot.png", "Sales Trend"), ("forecast_plot.png", "Forecast")]:
 
75
  p = os.path.join(TEMP_DIR, name)
76
  if os.path.exists(p):
77
  st.image(p, caption=cap, use_column_width=True)
 
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
 
 
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)
 
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()
 
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)