Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,78 +1,72 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
import google.generativeai as genai
|
| 5 |
|
| 6 |
from tools.csv_parser import parse_csv_tool
|
| 7 |
from tools.plot_generator import plot_sales_tool
|
| 8 |
-
from tools.forecaster import forecast_tool
|
| 9 |
|
| 10 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 11 |
-
# 0.
|
| 12 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 13 |
genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
|
| 14 |
-
gemini = genai.GenerativeModel(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 17 |
-
#
|
| 18 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 19 |
-
st.set_page_config(page_title="BizIntel AI Ultra β
|
| 20 |
-
st.title("π BizIntel
|
| 21 |
|
| 22 |
TEMP_DIR = tempfile.gettempdir()
|
| 23 |
|
| 24 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 25 |
-
#
|
| 26 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 27 |
-
|
| 28 |
-
if not
|
| 29 |
-
st.info("Upload a CSV to begin.")
|
| 30 |
st.stop()
|
| 31 |
|
| 32 |
-
csv_path = os.path.join(TEMP_DIR,
|
| 33 |
with open(csv_path, "wb") as f:
|
| 34 |
-
f.write(
|
| 35 |
st.success("CSV saved to temporary storage β
")
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 38 |
-
#
|
| 39 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 40 |
-
|
| 41 |
-
plot_msg = plot_sales_tool(csv_path) # creates sales_plot.png
|
| 42 |
-
forecast_text = forecast_tool(csv_path) # creates forecast_plot.png
|
| 43 |
|
| 44 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 45 |
-
#
|
| 46 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
CSV SUMMARY:
|
| 51 |
-
{summary_text}
|
| 52 |
-
|
| 53 |
-
FORECAST OUTPUT:
|
| 54 |
-
{forecast_text}
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
2. Three actionable strategies to improve business performance.
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
strategy = gemini.generate_content(prompt).text
|
| 64 |
|
| 65 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 66 |
-
#
|
| 67 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
if os.path.exists("sales_plot.png"):
|
| 72 |
-
st.image("sales_plot.png", caption="Sales Trend", use_column_width=True)
|
| 73 |
-
|
| 74 |
-
if os.path.exists("forecast_plot.png"):
|
| 75 |
-
st.image("forecast_plot.png", caption="Forecast Chart", use_column_width=True)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
st.markdown(strategy)
|
|
|
|
| 1 |
+
# app.py β BizIntel AI Ultra (GeminiΒ 1.5Β Pro, selectable date column)
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
import tempfile
|
| 5 |
+
import pandas as pd
|
| 6 |
import streamlit as st
|
| 7 |
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 # 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(
|
| 18 |
+
model="gemini-1.5-pro-latest",
|
| 19 |
+
temperature=0.7,
|
| 20 |
+
top_p=0.9,
|
| 21 |
+
response_mime_type="text/markdown",
|
| 22 |
+
)
|
| 23 |
|
| 24 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 25 |
+
# 2. STREAMLIT PAGE SETUP
|
| 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")
|
| 29 |
|
| 30 |
TEMP_DIR = tempfile.gettempdir()
|
| 31 |
|
| 32 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 33 |
+
# 3. CSV UPLOAD
|
| 34 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 35 |
+
csv_file = st.file_uploader("Upload a CSV file (β€β―200β―MB)", type=["csv"])
|
| 36 |
+
if not csv_file:
|
| 37 |
+
st.info("β¬οΈ Upload a CSV to begin.")
|
| 38 |
st.stop()
|
| 39 |
|
| 40 |
+
csv_path = os.path.join(TEMP_DIR, csv_file.name)
|
| 41 |
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 for column selection
|
| 46 |
+
df_preview = pd.read_csv(csv_path, nrows=5)
|
| 47 |
+
st.dataframe(df_preview)
|
| 48 |
+
|
| 49 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 50 |
+
# 4. DATE COLUMN CHOICE
|
| 51 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 52 |
+
date_col = st.selectbox("Choose the date/time column for forecasting", df_preview.columns)
|
|
|
|
|
|
|
| 53 |
|
| 54 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 55 |
+
# 5. RUN LOCAL TOOLS
|
| 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 |
+
trend_msg = 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) # β passes chosen column
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 67 |
+
# 6. GEMINI 1.5βPRO STRATEGY (stream)
|
| 68 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 69 |
+
prompt = f"""
|
| 70 |
+
You are BizIntel Strategist AI.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
**CSV SUMMARY**
|
|
|