Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,57 @@
|
|
1 |
-
import os, tempfile, streamlit as st
|
2 |
-
|
3 |
-
from
|
4 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
st.set_page_config(page_title="BizIntel AI Ultra", layout="wide")
|
7 |
-
st.title("π BizIntel AI UltraΒ β
|
8 |
|
9 |
TEMP_DIR = tempfile.gettempdir()
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
if
|
16 |
-
|
17 |
-
if up:
|
18 |
-
csv_path = os.path.join(TEMP_DIR, up.name)
|
19 |
-
with open(csv_path, "wb") as f:
|
20 |
-
f.write(up.read())
|
21 |
-
st.success("CSV saved β
")
|
22 |
|
|
|
|
|
23 |
else:
|
24 |
-
|
25 |
-
conn = st.text_input("SQLAlchemy connection string")
|
26 |
-
if conn:
|
27 |
-
tbls = list_tables(conn)
|
28 |
-
tbl = st.selectbox("Table", tbls) if tbls else None
|
29 |
-
if tbl:
|
30 |
-
csv_path = fetch_data_from_db(conn, tbl)
|
31 |
-
st.success(f"Fetched **{tbl}** as CSV β
")
|
32 |
-
|
33 |
-
# ββ Optional image preview ββββββββββββββββββββββββββββββββββββ
|
34 |
-
st.markdown("---")
|
35 |
-
img = st.file_uploader("Optional image (PNG/JPG)", ["png", "jpg", "jpeg"])
|
36 |
-
if img:
|
37 |
-
img_path = os.path.join(TEMP_DIR, img.name)
|
38 |
-
with open(img_path, "wb") as f:
|
39 |
-
f.write(img.read())
|
40 |
-
st.image(img_path, use_column_width=True)
|
41 |
-
|
42 |
-
# ββ Run pipeline via ADK runner βββββββββββββββββββββββββββββββ
|
43 |
-
if csv_path:
|
44 |
-
st.markdown("---")
|
45 |
-
st.info("Running analytics pipelineβ¦")
|
46 |
-
|
47 |
-
try:
|
48 |
-
report = run_agent_sync(agent=analytics_coordinator, input_data=csv_path)
|
49 |
-
except Exception as e:
|
50 |
-
st.error(f"Agent execution failed: {e}")
|
51 |
-
st.stop()
|
52 |
-
|
53 |
-
st.subheader("π Analysis & Strategy Report")
|
54 |
-
st.text(report)
|
55 |
-
|
56 |
-
# Show charts if generated
|
57 |
-
for name, cap in [("sales_plot.png", "Sales Trend"),
|
58 |
-
("forecast_plot.png", "Forecast Chart")]:
|
59 |
-
p = os.path.join(TEMP_DIR, name)
|
60 |
-
if os.path.exists(p):
|
61 |
-
st.image(p, caption=cap, use_column_width=True)
|
|
|
1 |
+
import os, tempfile, streamlit as st, pandas as pd
|
2 |
+
import google.generativeai as genai
|
3 |
+
from tools.csv_parser import parse_csv_tool
|
4 |
+
from tools.plot_generator import plot_sales_tool
|
5 |
+
from tools.forecaster import forecast_tool
|
6 |
+
|
7 |
+
# Configure Gemini
|
8 |
+
import os
|
9 |
+
genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
|
10 |
+
model = genai.GenerativeModel("gemini-pro")
|
11 |
|
12 |
st.set_page_config(page_title="BizIntel AI Ultra", layout="wide")
|
13 |
+
st.title("π BizIntel AI UltraΒ β Emergency Pipeline")
|
14 |
|
15 |
TEMP_DIR = tempfile.gettempdir()
|
16 |
|
17 |
+
# 1. CSV upload
|
18 |
+
csv = st.file_uploader("Upload CSV", ["csv"])
|
19 |
+
if csv:
|
20 |
+
csv_path = os.path.join(TEMP_DIR, csv.name)
|
21 |
+
with open(csv_path, "wb") as f:
|
22 |
+
f.write(csv.read())
|
23 |
+
st.success("CSV saved β
")
|
24 |
+
|
25 |
+
# 2. Run local tools
|
26 |
+
summary = parse_csv_tool(csv_path)
|
27 |
+
plot_msg = plot_sales_tool(csv_path)
|
28 |
+
forecast_msg = forecast_tool(csv_path)
|
29 |
+
|
30 |
+
# 3. Strategy via Gemini
|
31 |
+
prompt = f"""
|
32 |
+
You are a business strategist AI.
|
33 |
+
CSV SUMMARY:
|
34 |
+
{summary}
|
35 |
+
|
36 |
+
FORECAST:
|
37 |
+
{forecast_msg}
|
38 |
+
|
39 |
+
Provide key insights and 3 actionable strategies.
|
40 |
+
"""
|
41 |
+
|
42 |
+
strat = model.generate_content(prompt).text
|
43 |
+
|
44 |
+
# 4. Display results
|
45 |
+
st.subheader("π Data Summary")
|
46 |
+
st.text(summary)
|
47 |
+
|
48 |
+
if "Generated sales_plot.png" in plot_msg and os.path.exists("sales_plot.png"):
|
49 |
+
st.image("sales_plot.png", caption="Sales Trend", use_column_width=True)
|
50 |
|
51 |
+
if "Generated forecast_plot.png" in forecast_msg and os.path.exists("forecast_plot.png"):
|
52 |
+
st.image("forecast_plot.png", caption="Forecast Chart", use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
st.subheader("π Strategy Recommendations")
|
55 |
+
st.markdown(strat)
|
56 |
else:
|
57 |
+
st.info("Upload a CSV to begin.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|