Spaces:
Sleeping
Sleeping
# app.py โ BizIntelย AIย Ultra (Geminiย 1.5โฏPro, advanced pipeline) | |
import os | |
import tempfile | |
import pandas as pd | |
import streamlit as st | |
import google.generativeai as genai | |
from tools.csv_parser import parse_csv_tool | |
from tools.plot_generator import plot_sales_tool | |
from tools.forecaster import forecast_tool # must accept date_col param | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
# 1. GEMINI CONFIG (1.5โPro, temperature 0.7) | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
genai.configure(api_key=os.getenv("GEMINI_APIKEY")) | |
gemini = genai.GenerativeModel( | |
model="gemini-1.5-pro-latest", | |
temperature=0.7, | |
top_p=0.9, | |
response_mime_type="text/markdown", | |
) | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
# 2. STREAMLIT PAGE CONFIG | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
st.set_page_config(page_title="BizIntel AI Ultra โ Geminiย 1.5ย Pro", layout="wide") | |
st.title("๐ BizIntelย AIย Ultraย โ Advanced Analytics Pipeline") | |
TEMP_DIR = tempfile.gettempdir() | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
# 3. CSV UPLOAD | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
csv_file = st.file_uploader("Upload CSV (โคโฏ200โฏMB)", type=["csv"]) | |
if csv_file is None: | |
st.info("โฌ๏ธ Upload a CSV to begin.") | |
st.stop() | |
csv_path = os.path.join(TEMP_DIR, csv_file.name) | |
with open(csv_path, "wb") as f: | |
f.write(csv_file.read()) | |
st.success("CSV saved to temporary storage โ ") | |
# Preview first rows | |
df_preview = pd.read_csv(csv_path, nrows=5) | |
st.dataframe(df_preview) | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
# 4. DATE COLUMN SELECTION | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
date_col = st.selectbox("Choose the date/time column for forecasting", df_preview.columns) | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
# 5. LOCAL TOOL EXECUTION | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
with st.spinner("๐ Parsing CSVโฆ"): | |
summary_text = parse_csv_tool(csv_path) | |
with st.spinner("๐ Generating sales trend chartโฆ"): | |
_ = plot_sales_tool(csv_path) # creates sales_plot.png | |
with st.spinner("๐ฎ Forecasting future metricsโฆ"): | |
forecast_text = forecast_tool(csv_path, date_col=date_col) # pass chosen column | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
# 6. GEMINI 1.5โPRO STRATEGY (stream) | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
prompt = ( | |
f"You are **BizIntel Strategist AI**.\n\n" | |
f"## CSV Summary\n```\n{summary_text}\n```\n\n" | |
f"## Forecast Output\n```\n{forecast_text}\n```\n\n" | |
"### Task\n" | |
"Return Markdown with:\n" | |
"1. **Five key insights** (bullet list)\n" | |
"2. **Three actionable strategies** (with expected impact)\n" | |
"3. **Risk factors or anomalies** you see\n" | |
"4. **Suggested additional visuals** to aid stakeholder understanding\n" | |
) | |
st.subheader("๐ Strategy Recommendations (Geminiย 1.5ย Pro)") | |
placeholder = st.empty() | |
strategy_md = "" | |
for chunk in gemini.generate_content_stream(prompt): | |
if chunk.candidate and chunk.candidate.content.parts: | |
strategy_md += chunk.candidate.content.parts[0].text | |
placeholder.markdown(strategy_md) | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
# 7. DISPLAY SUMMARY & CHARTS | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
st.markdown("---") | |
st.subheader("๐ CSV Summary") | |
st.text(summary_text) | |
sales_plot_path = os.path.join("sales_plot.png") | |
forecast_plot_path = os.path.join("forecast_plot.png") | |
if os.path.exists(sales_plot_path): | |
st.image(sales_plot_path, caption="Sales Trend", use_column_width=True) | |
if os.path.exists(forecast_plot_path): | |
st.image(forecast_plot_path, caption="Forecast Chart", use_column_width=True) | |