Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
11 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
12 |
input_source = st.radio("Select data source", ["Upload CSV", "Connect to SQL Database"])
|
13 |
-
|
14 |
|
15 |
if input_source == "Upload CSV":
|
16 |
uploaded_file = st.file_uploader("Upload CSV", type="csv")
|
17 |
if uploaded_file:
|
18 |
-
|
19 |
-
with open(
|
20 |
f.write(uploaded_file.read())
|
21 |
-
st.success("CSV file
|
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 |
-
|
32 |
st.success(f"Fetched **{table_name}** as CSV β
")
|
33 |
|
34 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
35 |
-
#
|
36 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
37 |
st.markdown("---")
|
38 |
st.subheader("π· Optional: Upload an Image for Preview / Future Analysis")
|
39 |
|
40 |
-
|
41 |
-
"Upload an image (PNG
|
42 |
)
|
43 |
|
44 |
-
if
|
45 |
-
|
46 |
-
with open(
|
47 |
-
f.write(
|
48 |
-
st.image(
|
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 |
-
#
|
55 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
56 |
-
if
|
57 |
st.markdown("---")
|
58 |
st.info("Running analytics pipelineβ¦ β³")
|
59 |
-
|
|
|
60 |
|
61 |
st.subheader("π Analysis & Strategy Report")
|
62 |
-
st.text(
|
63 |
|
64 |
-
#
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
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)
|