Spaces:
Sleeping
Sleeping
# app.py โ BizIntelย AIย Ultra (Geminiโฏ1.5ย Pro, interactive Plotly visuals) | |
import os | |
import tempfile | |
import pandas as pd | |
import streamlit as st | |
import google.generativeai as genai | |
import plotly.graph_objects as go | |
from tools.csv_parser import parse_csv_tool | |
from tools.plot_generator import plot_sales_tool # accepts date_col, returns Figure or str | |
from tools.forecaster import forecast_tool # accepts date_col | |
from tools.visuals import ( | |
histogram_tool, | |
scatter_matrix_tool, | |
corr_heatmap_tool, | |
) | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
# 1. GEMINI CONFIG (1.5โPro, temperature 0.7) | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
genai.configure(api_key=os.getenv("GEMINI_APIKEY")) | |
gemini = genai.GenerativeModel( | |
"gemini-1.5-pro-latest", | |
generation_config={ | |
"temperature": 0.7, | |
"top_p": 0.9, | |
"response_mime_type": "text/plain", # must be allowed type | |
}, | |
) | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
# 2. STREAMLIT PAGE SETUP | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
st.set_page_config(page_title="BizIntel AI Ultra โ Geminiย 1.5ย Pro", layout="wide") | |
st.title("๐ BizIntelย AIย Ultraย โ Advanced Analytics") | |
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 โ ") | |
# Preview & date column selection | |
df_preview = pd.read_csv(csv_path, nrows=5) | |
st.dataframe(df_preview) | |
date_col = st.selectbox("Select date/time column for forecasting", df_preview.columns) | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
# 4. LOCAL TOOL EXECUTION | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
with st.spinner("๐ Parsing CSVโฆ"): | |
summary_text = parse_csv_tool(csv_path) | |
with st.spinner("๐ Generating sales trend chartโฆ"): | |
sales_fig = plot_sales_tool(csv_path, date_col=date_col) | |
# Show chart or warn | |
if isinstance(sales_fig, go.Figure): | |
st.plotly_chart(sales_fig, use_container_width=True) | |
else: # returned error message | |
st.warning(sales_fig) | |
with st.spinner("๐ฎ Forecasting future metricsโฆ"): | |
forecast_text = forecast_tool(csv_path, date_col=date_col) | |
# Display forecast PNG if created | |
if os.path.exists("forecast_plot.png"): | |
st.image("forecast_plot.png", caption="Sales Forecast", use_column_width=True) | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
# 5. GEMINI 1.5โPRO STRATEGY | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
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" | |
"Return **Markdown** with:\n" | |
"1. Five key insights (bullets)\n" | |
"2. Three actionable strategies (with expected impact)\n" | |
"3. Risk factors or anomalies\n" | |
"4. Suggested additional visuals\n" | |
) | |
st.subheader("๐ Strategy Recommendations (Geminiย 1.5ย Pro)") | |
with st.spinner("Geminiย 1.5ย Pro is generating insightsโฆ"): | |
strategy_md = gemini.generate_content(prompt).text | |
st.markdown(strategy_md) | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
# 6. OPTIONAL EXPLORATORY VISUALS | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
st.markdown("---") | |
st.subheader("๐ Optional Exploratory Visuals") | |
num_cols = df_preview.select_dtypes("number").columns | |
# Histogram | |
if st.checkbox("Show histogram"): | |
hist_col = st.selectbox("Histogram variable", num_cols, key="hist") | |
fig_hist = histogram_tool(csv_path, hist_col) | |
st.plotly_chart(fig_hist, use_container_width=True) | |
# Scatterโmatrix | |
if st.checkbox("Show scatterโmatrix"): | |
mult_cols = st.multiselect( | |
"Choose up to 5 columns", num_cols, default=num_cols[:3], key="scatter" | |
) | |
if mult_cols: | |
fig_scatter = scatter_matrix_tool(csv_path, mult_cols) | |
st.plotly_chart(fig_scatter, use_container_width=True) | |
# Correlation heatโmap | |
if st.checkbox("Show correlation heatโmap"): | |
fig_corr = corr_heatmap_tool(csv_path) | |
st.plotly_chart(fig_corr, use_container_width=True) | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
# 7. CSV SUMMARY TEXT | |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
st.markdown("---") | |
st.subheader("๐ CSV Summary (full stats)") | |
st.text(summary_text) | |