mgbam commited on
Commit
5cc8a1b
Β·
verified Β·
1 Parent(s): 2683b5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -45
app.py CHANGED
@@ -4,75 +4,65 @@ 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
- # ──────────────────────────────────────────────────────────────
8
- # PAGE CONFIG
9
- # ──────────────────────────────────────────────────────────────
10
  st.set_page_config(page_title="BizIntel AI Ultra", layout="wide")
11
  st.title("πŸ“Š BizIntel AI Ultra – Business Intelligence Agent")
12
 
13
- # Writable directory inside HuggingΒ Face container
14
  TEMP_DIR = tempfile.gettempdir()
15
 
16
- # ──────────────────────────────────────────────────────────────
17
- # 1. DATA SOURCE SELECTION (CSV or DB)
18
- # ──────────────────────────────────────────────────────────────
19
  input_source = st.radio("Select data source", ["Upload CSV", "Connect to SQL Database"])
20
  csv_path: str | None = None
21
 
22
  if input_source == "Upload CSV":
23
- uploaded_csv = st.file_uploader("Upload CSV", type="csv")
24
- if uploaded_csv:
25
- csv_path = os.path.join(TEMP_DIR, uploaded_csv.name)
26
  with open(csv_path, "wb") as f:
27
- f.write(uploaded_csv.read())
28
- st.success("CSV saved to temporary storage βœ…")
29
 
30
  elif input_source == "Connect to SQL Database":
31
- engine = st.selectbox("Database engine", SUPPORTED_ENGINES)
32
- conn_str = st.text_input("SQLAlchemy connection string")
33
- if conn_str:
34
- tables = list_tables(conn_str)
35
  if tables:
36
- table_name = st.selectbox("Choose a table", tables)
37
- if table_name:
38
- csv_path = fetch_data_from_db(conn_str, table_name)
39
- st.success(f"Fetched **{table_name}** as CSV βœ…")
40
 
41
- # ──────────────────────────────────────────────────────────────
42
- # 2. OPTIONAL IMAGE UPLOAD & PREVIEW
43
- # ──────────────────────────────────────────────────────────────
44
  st.markdown("---")
45
- st.subheader("πŸ“· Optional: Upload an Image for Preview / Future Analysis")
46
 
47
- img_file = st.file_uploader(
48
- "Upload an image (PNG/JPG)", type=["png", "jpg", "jpeg"], key="img"
49
- )
50
-
51
- if img_file:
52
- img_path = os.path.join(TEMP_DIR, img_file.name)
53
  with open(img_path, "wb") as f:
54
- f.write(img_file.read())
55
  st.image(img_path, caption="Uploaded Image", use_column_width=True)
56
- # πŸ‘‰Β To analyse with a vision agent later, call it here with `img_path`.
57
 
58
- # ──────────────────────────────────────────────────────────────
59
- # 3. RUN ANALYTICS PIPELINE
60
- # ──────────────────────────────────────────────────────────────
61
  if csv_path:
62
  st.markdown("---")
63
  st.info("Running analytics pipeline… ⏳")
64
 
65
- # Google ADK agents now use .invoke() (sync) instead of .run()
66
- report = analytics_coordinator.invoke(input=csv_path)
 
 
 
 
 
67
 
68
  st.subheader("πŸ“ Analysis & Strategy Report")
69
  st.text(report)
70
 
71
- # Display charts saved by tool functions (if any)
72
- for plot_name, caption in [
73
- ("sales_plot.png", "Sales Trend"),
74
- ("forecast_plot.png", "Forecast Chart"),
75
- ]:
76
- plot_path = os.path.join(TEMP_DIR, plot_name)
77
- if os.path.exists(plot_path):
78
- st.image(plot_path, caption=caption, use_column_width=True)
 
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)