Spaces:
Sleeping
Sleeping
Download app.py from mgbam/BizIntel_AI: direct link, hf CLI and curl.
- Browser
- Download file 4.34 kB
-
https://huggingface.co/spaces/mgbam/BizIntel_AI/resolve/3acbc9cd301e8f155a24ff255d51130fdc5e72ba/app.py
- Command line
-
hf download hf://spaces/mgbam/BizIntel_AI@3acbc9cd301e8f155a24ff255d51130fdc5e72ba/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mgbam/BizIntel_AI/resolve/3acbc9cd301e8f155a24ff255d51130fdc5e72ba/app.py
4.34 kB
| 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 | |
| from tools.forecaster import forecast_tool | |
| from tools.visuals import ( | |
| histogram_tool, | |
| scatter_matrix_tool, | |
| corr_heatmap_tool, | |
| ) | |
| # ββ Gemini 1.5βPro configuration βββββββββββββββββββββββββββββ | |
| 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", | |
| }, | |
| ) | |
| # ββ 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() | |
| # ββ 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) | |
| # ββ Local tools: summary, sales trend, forecast ββββββββββββββ | |
| 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) | |
| st.plotly_chart(sales_fig, use_container_width=True) | |
| with st.spinner("Forecasting future metricsβ¦"): | |
| forecast_text = forecast_tool(csv_path, date_col=date_col) | |
| if os.path.exists("forecast_plot.png"): | |
| forecast_img = "forecast_plot.png" | |
| else: | |
| forecast_img = None | |
| # ββ Gemini strategy insights βββββββββββββββββββββββββββββββββ | |
| 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** (bullet list)\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) | |
| # Display forecast image if exists | |
| if forecast_img: | |
| st.image(forecast_img, caption="Sales Forecast", use_column_width=True) | |
| # ββ 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"): | |
| multi_cols = st.multiselect("Choose up to 5 columns", num_cols, default=num_cols[:3]) | |
| if multi_cols: | |
| fig_scatter = scatter_matrix_tool(csv_path, multi_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) | |
| # ββ CSV summary text at bottom βββββββββββββββββββββββββββββββ | |
| st.markdown("---") | |
| st.subheader("π CSV Summary (full Stats)") | |
| st.text(summary_text) | |