Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# app.py
|
2 |
|
3 |
import os
|
4 |
import tempfile
|
@@ -8,10 +8,10 @@ import google.generativeai as genai
|
|
8 |
|
9 |
from tools.csv_parser import parse_csv_tool
|
10 |
from tools.plot_generator import plot_sales_tool
|
11 |
-
from tools.forecaster import forecast_tool
|
12 |
|
13 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
14 |
-
# 1. GEMINI CONFIG (1.5โ
|
15 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
16 |
genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
|
17 |
gemini = genai.GenerativeModel(
|
@@ -22,7 +22,7 @@ gemini = genai.GenerativeModel(
|
|
22 |
)
|
23 |
|
24 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
25 |
-
# 2. STREAMLIT PAGE
|
26 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
27 |
st.set_page_config(page_title="BizIntel AI Ultra โ Geminiย 1.5ย Pro", layout="wide")
|
28 |
st.title("๐ BizIntelย AIย Ultraย โ Advanced Analytics Pipeline")
|
@@ -32,8 +32,8 @@ TEMP_DIR = tempfile.gettempdir()
|
|
32 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
33 |
# 3. CSV UPLOAD
|
34 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
35 |
-
csv_file = st.file_uploader("Upload
|
36 |
-
if
|
37 |
st.info("โฌ๏ธ Upload a CSV to begin.")
|
38 |
st.stop()
|
39 |
|
@@ -42,31 +42,63 @@ with open(csv_path, "wb") as f:
|
|
42 |
f.write(csv_file.read())
|
43 |
st.success("CSV saved to temporary storage โ
")
|
44 |
|
45 |
-
# Preview first rows
|
46 |
df_preview = pd.read_csv(csv_path, nrows=5)
|
47 |
st.dataframe(df_preview)
|
48 |
|
49 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
50 |
-
# 4. DATE COLUMN
|
51 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
52 |
date_col = st.selectbox("Choose the date/time column for forecasting", df_preview.columns)
|
53 |
|
54 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
55 |
-
# 5.
|
56 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
57 |
-
with st.spinner("Parsing CSVโฆ"):
|
58 |
summary_text = parse_csv_tool(csv_path)
|
59 |
|
60 |
-
with st.spinner("Generating sales trend chartโฆ"):
|
61 |
-
|
62 |
|
63 |
-
with st.spinner("Forecasting future metricsโฆ"):
|
64 |
-
forecast_text = forecast_tool(csv_path, date_col=date_col) #
|
65 |
|
66 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
67 |
# 6. GEMINI 1.5โPRO STRATEGY (stream)
|
68 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
69 |
-
prompt =
|
70 |
-
You are BizIntel Strategist AI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
-
|
|
|
|
1 |
+
# app.py โ BizIntelย AIย Ultra (Geminiย 1.5โฏPro, advanced pipeline)
|
2 |
|
3 |
import os
|
4 |
import tempfile
|
|
|
8 |
|
9 |
from tools.csv_parser import parse_csv_tool
|
10 |
from tools.plot_generator import plot_sales_tool
|
11 |
+
from tools.forecaster import forecast_tool # must accept date_col param
|
12 |
|
13 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
14 |
+
# 1. GEMINI CONFIG (1.5โPro, temperature 0.7)
|
15 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
16 |
genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
|
17 |
gemini = genai.GenerativeModel(
|
|
|
22 |
)
|
23 |
|
24 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
25 |
+
# 2. STREAMLIT PAGE CONFIG
|
26 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
27 |
st.set_page_config(page_title="BizIntel AI Ultra โ Geminiย 1.5ย Pro", layout="wide")
|
28 |
st.title("๐ BizIntelย AIย Ultraย โ Advanced Analytics Pipeline")
|
|
|
32 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
33 |
# 3. CSV UPLOAD
|
34 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
35 |
+
csv_file = st.file_uploader("Upload CSV (โคโฏ200โฏMB)", type=["csv"])
|
36 |
+
if csv_file is None:
|
37 |
st.info("โฌ๏ธ Upload a CSV to begin.")
|
38 |
st.stop()
|
39 |
|
|
|
42 |
f.write(csv_file.read())
|
43 |
st.success("CSV saved to temporary storage โ
")
|
44 |
|
45 |
+
# Preview first rows
|
46 |
df_preview = pd.read_csv(csv_path, nrows=5)
|
47 |
st.dataframe(df_preview)
|
48 |
|
49 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
50 |
+
# 4. DATE COLUMN SELECTION
|
51 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
52 |
date_col = st.selectbox("Choose the date/time column for forecasting", df_preview.columns)
|
53 |
|
54 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
55 |
+
# 5. LOCAL TOOL EXECUTION
|
56 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
57 |
+
with st.spinner("๐ Parsing CSVโฆ"):
|
58 |
summary_text = parse_csv_tool(csv_path)
|
59 |
|
60 |
+
with st.spinner("๐ Generating sales trend chartโฆ"):
|
61 |
+
_ = plot_sales_tool(csv_path) # creates sales_plot.png
|
62 |
|
63 |
+
with st.spinner("๐ฎ Forecasting future metricsโฆ"):
|
64 |
+
forecast_text = forecast_tool(csv_path, date_col=date_col) # pass chosen column
|
65 |
|
66 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
67 |
# 6. GEMINI 1.5โPRO STRATEGY (stream)
|
68 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
69 |
+
prompt = (
|
70 |
+
f"You are **BizIntel Strategist AI**.\n\n"
|
71 |
+
f"## CSV Summary\n```\n{summary_text}\n```\n\n"
|
72 |
+
f"## Forecast Output\n```\n{forecast_text}\n```\n\n"
|
73 |
+
"### Task\n"
|
74 |
+
"Return Markdown with:\n"
|
75 |
+
"1. **Five key insights** (bullet list)\n"
|
76 |
+
"2. **Three actionable strategies** (with expected impact)\n"
|
77 |
+
"3. **Risk factors or anomalies** you see\n"
|
78 |
+
"4. **Suggested additional visuals** to aid stakeholder understanding\n"
|
79 |
+
)
|
80 |
+
|
81 |
+
st.subheader("๐ Strategy Recommendations (Geminiย 1.5ย Pro)")
|
82 |
+
placeholder = st.empty()
|
83 |
+
strategy_md = ""
|
84 |
+
|
85 |
+
for chunk in gemini.generate_content_stream(prompt):
|
86 |
+
if chunk.candidate and chunk.candidate.content.parts:
|
87 |
+
strategy_md += chunk.candidate.content.parts[0].text
|
88 |
+
placeholder.markdown(strategy_md)
|
89 |
+
|
90 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
91 |
+
# 7. DISPLAY SUMMARY & CHARTS
|
92 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
93 |
+
st.markdown("---")
|
94 |
+
st.subheader("๐ CSV Summary")
|
95 |
+
st.text(summary_text)
|
96 |
+
|
97 |
+
sales_plot_path = os.path.join("sales_plot.png")
|
98 |
+
forecast_plot_path = os.path.join("forecast_plot.png")
|
99 |
+
|
100 |
+
if os.path.exists(sales_plot_path):
|
101 |
+
st.image(sales_plot_path, caption="Sales Trend", use_column_width=True)
|
102 |
|
103 |
+
if os.path.exists(forecast_plot_path):
|
104 |
+
st.image(forecast_plot_path, caption="Forecast Chart", use_column_width=True)
|