mgbam commited on
Commit
72a73ff
Β·
verified Β·
1 Parent(s): 86dbcf0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -27
app.py CHANGED
@@ -1,24 +1,31 @@
 
 
1
  import streamlit as st
2
  from agents.analytics_pipeline import analytics_coordinator
3
- import os
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
  # ──────────────────────────────────────────────────────────────
10
- # ❢ DATA SOURCE SELECTION (CSV or Database)
11
  # ──────────────────────────────────────────────────────────────
12
  input_source = st.radio("Select data source", ["Upload CSV", "Connect to SQL Database"])
13
- file_path = None
14
 
15
  if input_source == "Upload CSV":
16
  uploaded_file = st.file_uploader("Upload CSV", type="csv")
17
  if uploaded_file:
18
- file_path = os.path.join("data", uploaded_file.name)
19
- with open(file_path, "wb") as f:
20
  f.write(uploaded_file.read())
21
- st.success("CSV file uploaded βœ…")
22
 
23
  elif input_source == "Connect to SQL Database":
24
  engine = st.selectbox("Database engine", SUPPORTED_ENGINES)
@@ -28,41 +35,43 @@ elif input_source == "Connect to SQL Database":
28
  if tables:
29
  table_name = st.selectbox("Choose a table", tables)
30
  if table_name:
31
- file_path = fetch_data_from_db(conn_str, table_name)
32
  st.success(f"Fetched **{table_name}** as CSV βœ…")
33
 
34
  # ──────────────────────────────────────────────────────────────
35
- # ❷ OPTIONAL IMAGE UPLOAD + PREVIEW
36
  # ──────────────────────────────────────────────────────────────
37
  st.markdown("---")
38
  st.subheader("πŸ“· Optional: Upload an Image for Preview / Future Analysis")
39
 
40
- uploaded_image = st.file_uploader(
41
- "Upload an image (PNG orΒ JPG)", type=["png", "jpg", "jpeg"], key="img"
42
  )
43
 
44
- if uploaded_image is not None:
45
- image_path = os.path.join("data", uploaded_image.name)
46
- with open(image_path, "wb") as f:
47
- f.write(uploaded_image.read())
48
- st.image(image_path, caption="Uploaded Image", use_column_width=True)
49
-
50
- # πŸ‘‰Β If you later want to pass the image to an agent, you can add code here
51
- # e.g.Β result = image_analysis_agent.run(input=image_path)
52
 
53
  # ──────────────────────────────────────────────────────────────
54
- # ❸ RUN BUSINESS ANALYTICS PIPELINE
55
  # ──────────────────────────────────────────────────────────────
56
- if file_path:
57
  st.markdown("---")
58
  st.info("Running analytics pipeline… ⏳")
59
- result = analytics_coordinator.run(input=file_path)
 
60
 
61
  st.subheader("πŸ“ Analysis & Strategy Report")
62
- st.text(result)
63
 
64
- # Display generated charts if they exist
65
- if os.path.exists("sales_plot.png"):
66
- st.image("sales_plot.png", caption="Sales Trend", use_column_width=True)
67
- if os.path.exists("forecast_plot.png"):
68
- st.image("forecast_plot.png", caption="Forecast Chart", use_column_width=True)
 
 
 
 
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
+ # ──────────────────────────────────────────────────────────────
8
+ # 0. 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
+ # Where we'll store any uploaded files (always writable in HF Spaces)
14
+ TEMP_DIR = tempfile.gettempdir()
15
+
16
  # ──────────────────────────────────────────────────────────────
17
+ # 1. DATA SOURCE SELECTION
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_file = st.file_uploader("Upload CSV", type="csv")
24
  if uploaded_file:
25
+ csv_path = os.path.join(TEMP_DIR, uploaded_file.name)
26
+ with open(csv_path, "wb") as f:
27
  f.write(uploaded_file.read())
28
+ st.success("CSV file saved to temporary storage βœ…")
29
 
30
  elif input_source == "Connect to SQL Database":
31
  engine = st.selectbox("Database engine", SUPPORTED_ENGINES)
 
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
+ # πŸ‘‰ Future hook: pass `img_path` to a vision agent here.
 
 
57
 
58
  # ──────────────────────────────────────────────────────────────
59
+ # 3. RUN ANALYTICS PIPELINE
60
  # ──────────────────────────────────────────────────────────────
61
+ if csv_path:
62
  st.markdown("---")
63
  st.info("Running analytics pipeline… ⏳")
64
+
65
+ report = analytics_coordinator.run(input=csv_path)
66
 
67
  st.subheader("πŸ“ Analysis & Strategy Report")
68
+ st.text(report)
69
 
70
+ # Show charts generated by tool functions if present
71
+ for plot_name, caption in [
72
+ ("sales_plot.png", "Sales Trend"),
73
+ ("forecast_plot.png", "Forecast Chart"),
74
+ ]:
75
+ plot_path = os.path.join(TEMP_DIR, plot_name)
76
+ if os.path.exists(plot_path):
77
+ st.image(plot_path, caption=caption, use_column_width=True)