Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
24 |
-
if
|
25 |
-
csv_path = os.path.join(TEMP_DIR,
|
26 |
with open(csv_path, "wb") as f:
|
27 |
-
f.write(
|
28 |
-
st.success("CSV saved
|
29 |
|
30 |
elif input_source == "Connect to SQL Database":
|
31 |
-
engine = st.selectbox("
|
32 |
-
|
33 |
-
if
|
34 |
-
tables = list_tables(
|
35 |
if tables:
|
36 |
-
|
37 |
-
if
|
38 |
-
csv_path = fetch_data_from_db(
|
39 |
-
st.success(f"Fetched **{
|
40 |
|
41 |
-
#
|
42 |
-
# 2. OPTIONAL IMAGE UPLOAD & PREVIEW
|
43 |
-
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
44 |
st.markdown("---")
|
45 |
-
st.subheader("π· Optional
|
46 |
|
47 |
-
|
48 |
-
|
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(
|
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 |
-
#
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
st.subheader("π Analysis & Strategy Report")
|
69 |
st.text(report)
|
70 |
|
71 |
-
#
|
72 |
-
for
|
73 |
-
|
74 |
-
(
|
75 |
-
|
76 |
-
|
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)
|
|
|
|