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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -38
app.py CHANGED
@@ -1,68 +1,77 @@
1
- import os
2
- import tempfile
3
- import streamlit as st
4
  from agents.analytics_pipeline import analytics_coordinator
5
  from db_connector import fetch_data_from_db, list_tables, SUPPORTED_ENGINES
6
 
7
- # Page setup
8
  st.set_page_config(page_title="BizIntel AI Ultra", layout="wide")
9
  st.title("πŸ“Š BizIntel AI Ultra – Business Intelligence Agent")
10
 
11
  TEMP_DIR = tempfile.gettempdir()
12
 
13
- # 1. Data source
14
- input_source = st.radio("Select data source", ["Upload CSV", "Connect to SQL Database"])
15
- csv_path: str | None = None
16
 
17
- if input_source == "Upload CSV":
18
- up_csv = st.file_uploader("Upload CSV", type="csv")
19
- if up_csv:
20
- csv_path = os.path.join(TEMP_DIR, up_csv.name)
21
  with open(csv_path, "wb") as f:
22
- f.write(up_csv.read())
23
  st.success("CSV saved βœ…")
24
 
25
- elif input_source == "Connect to SQL Database":
26
  engine = st.selectbox("DB engine", SUPPORTED_ENGINES)
27
  conn = st.text_input("SQLAlchemy connection string")
28
  if conn:
29
  tables = list_tables(conn)
30
- if tables:
31
- table = st.selectbox("Table", tables)
32
- if table:
33
- csv_path = fetch_data_from_db(conn, table)
34
- st.success(f"Fetched **{table}** as CSV βœ…")
35
 
36
- # 2. Optional image preview
37
  st.markdown("---")
38
- st.subheader("πŸ“· Optional image upload")
39
-
40
- img = st.file_uploader("PNG/JPG", type=["png", "jpg", "jpeg"], key="img")
41
  if img:
42
- img_path = os.path.join(TEMP_DIR, img.name)
43
- with open(img_path, "wb") as f:
44
  f.write(img.read())
45
- st.image(img_path, caption="Uploaded Image", use_column_width=True)
46
 
47
- # 3. Run analytics pipeline
48
  if csv_path:
49
  st.markdown("---")
50
- st.info("Running analytics pipeline… ⏳")
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- # Robust call: try invoke() β†’ run() β†’ __call__()
53
- if hasattr(analytics_coordinator, "invoke"):
54
- report = analytics_coordinator.invoke(input=csv_path)
55
- elif hasattr(analytics_coordinator, "run"):
56
- report = analytics_coordinator.run(input=csv_path)
57
- else: # fallback to callable
58
- report = analytics_coordinator(csv_path)
 
 
 
59
 
60
  st.subheader("πŸ“ Analysis & Strategy Report")
61
  st.text(report)
62
 
63
- # Show charts if present
64
- for name, caption in [("sales_plot.png", "Sales Trend"),
65
- ("forecast_plot.png", "Forecast Chart")]:
66
  p = os.path.join(TEMP_DIR, name)
67
  if os.path.exists(p):
68
- st.image(p, caption=caption, use_column_width=True)
 
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
 
 
5
  st.set_page_config(page_title="BizIntel AI Ultra", layout="wide")
6
  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)
18
  with open(csv_path, "wb") as f:
19
+ f.write(up.read())
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()
69
 
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)