Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import tempfile
|
| 3 |
import pandas as pd
|
|
@@ -6,32 +8,38 @@ import google.generativeai as genai
|
|
| 6 |
import plotly.graph_objects as go
|
| 7 |
|
| 8 |
from tools.csv_parser import parse_csv_tool
|
| 9 |
-
from tools.plot_generator import plot_sales_tool
|
| 10 |
-
from tools.forecaster import forecast_tool
|
| 11 |
from tools.visuals import (
|
| 12 |
histogram_tool,
|
| 13 |
scatter_matrix_tool,
|
| 14 |
corr_heatmap_tool,
|
| 15 |
)
|
| 16 |
|
| 17 |
-
# โโ
|
|
|
|
|
|
|
| 18 |
genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
|
| 19 |
gemini = genai.GenerativeModel(
|
| 20 |
"gemini-1.5-pro-latest",
|
| 21 |
generation_config={
|
| 22 |
"temperature": 0.7,
|
| 23 |
"top_p": 0.9,
|
| 24 |
-
"response_mime_type": "text/plain",
|
| 25 |
},
|
| 26 |
)
|
| 27 |
|
| 28 |
-
# โโ
|
|
|
|
|
|
|
| 29 |
st.set_page_config(page_title="BizIntel AI Ultra โ Geminiย 1.5ย Pro", layout="wide")
|
| 30 |
st.title("๐ BizIntelย AIย Ultraย โ Advanced Analytics")
|
| 31 |
|
| 32 |
TEMP_DIR = tempfile.gettempdir()
|
| 33 |
|
| 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.")
|
|
@@ -42,49 +50,55 @@ with open(csv_path, "wb") as f:
|
|
| 42 |
f.write(csv_file.read())
|
| 43 |
st.success("CSV saved โ
")
|
| 44 |
|
| 45 |
-
# Preview
|
| 46 |
df_preview = pd.read_csv(csv_path, nrows=5)
|
| 47 |
st.dataframe(df_preview)
|
| 48 |
date_col = st.selectbox("Select date/time column for forecasting", df_preview.columns)
|
| 49 |
|
| 50 |
-
# โโ
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
summary_text = parse_csv_tool(csv_path)
|
| 53 |
|
| 54 |
-
with st.spinner("Generating sales trend chartโฆ"):
|
| 55 |
sales_fig = plot_sales_tool(csv_path, date_col=date_col)
|
|
|
|
|
|
|
|
|
|
| 56 |
st.plotly_chart(sales_fig, use_container_width=True)
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
with st.spinner("Forecasting future metricsโฆ"):
|
| 59 |
forecast_text = forecast_tool(csv_path, date_col=date_col)
|
| 60 |
-
if os.path.exists("forecast_plot.png"):
|
| 61 |
-
forecast_img = "forecast_plot.png"
|
| 62 |
-
else:
|
| 63 |
-
forecast_img = None
|
| 64 |
|
| 65 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
prompt = (
|
| 67 |
f"You are **BizIntel Strategist AI**.\n\n"
|
| 68 |
f"### CSV Summary\n```\n{summary_text}\n```\n\n"
|
| 69 |
f"### Forecast Output\n```\n{forecast_text}\n```\n\n"
|
| 70 |
-
"Return Markdown with:\n"
|
| 71 |
-
"1.
|
| 72 |
-
"2.
|
| 73 |
-
"3.
|
| 74 |
-
"4.
|
| 75 |
)
|
| 76 |
|
| 77 |
st.subheader("๐ Strategy Recommendations (Geminiย 1.5ย Pro)")
|
| 78 |
with st.spinner("Geminiย 1.5ย Pro is generating insightsโฆ"):
|
| 79 |
strategy_md = gemini.generate_content(prompt).text
|
| 80 |
-
|
| 81 |
st.markdown(strategy_md)
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
# โโ Optional exploratory visuals โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 88 |
st.markdown("---")
|
| 89 |
st.subheader("๐ Optional Exploratory Visuals")
|
| 90 |
|
|
@@ -98,9 +112,11 @@ if st.checkbox("Show histogram"):
|
|
| 98 |
|
| 99 |
# Scatterโmatrix
|
| 100 |
if st.checkbox("Show scatterโmatrix"):
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
| 104 |
st.plotly_chart(fig_scatter, use_container_width=True)
|
| 105 |
|
| 106 |
# Correlation heatโmap
|
|
@@ -108,7 +124,9 @@ if st.checkbox("Show correlation heatโmap"):
|
|
| 108 |
fig_corr = corr_heatmap_tool(csv_path)
|
| 109 |
st.plotly_chart(fig_corr, use_container_width=True)
|
| 110 |
|
| 111 |
-
# โโ
|
|
|
|
|
|
|
| 112 |
st.markdown("---")
|
| 113 |
-
st.subheader("๐ CSV Summary (full
|
| 114 |
st.text(summary_text)
|
|
|
|
| 1 |
+
# app.py โ BizIntelย AIย Ultra (Geminiโฏ1.5ย Pro, interactive Plotly visuals)
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
import tempfile
|
| 5 |
import pandas as pd
|
|
|
|
| 8 |
import plotly.graph_objects as go
|
| 9 |
|
| 10 |
from tools.csv_parser import parse_csv_tool
|
| 11 |
+
from tools.plot_generator import plot_sales_tool # accepts date_col, returns Figure or str
|
| 12 |
+
from tools.forecaster import forecast_tool # accepts date_col
|
| 13 |
from tools.visuals import (
|
| 14 |
histogram_tool,
|
| 15 |
scatter_matrix_tool,
|
| 16 |
corr_heatmap_tool,
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 20 |
+
# 1. GEMINI CONFIG (1.5โPro, temperature 0.7)
|
| 21 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 22 |
genai.configure(api_key=os.getenv("GEMINI_APIKEY"))
|
| 23 |
gemini = genai.GenerativeModel(
|
| 24 |
"gemini-1.5-pro-latest",
|
| 25 |
generation_config={
|
| 26 |
"temperature": 0.7,
|
| 27 |
"top_p": 0.9,
|
| 28 |
+
"response_mime_type": "text/plain", # must be allowed type
|
| 29 |
},
|
| 30 |
)
|
| 31 |
|
| 32 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 33 |
+
# 2. STREAMLIT PAGE SETUP
|
| 34 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 35 |
st.set_page_config(page_title="BizIntel AI Ultra โ Geminiย 1.5ย Pro", layout="wide")
|
| 36 |
st.title("๐ BizIntelย AIย Ultraย โ Advanced Analytics")
|
| 37 |
|
| 38 |
TEMP_DIR = tempfile.gettempdir()
|
| 39 |
|
| 40 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 41 |
+
# 3. CSV UPLOAD
|
| 42 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 43 |
csv_file = st.file_uploader("Upload CSV (โคโฏ200โฏMB)", type=["csv"])
|
| 44 |
if csv_file is None:
|
| 45 |
st.info("โฌ๏ธย Upload a CSV to begin.")
|
|
|
|
| 50 |
f.write(csv_file.read())
|
| 51 |
st.success("CSV saved โ
")
|
| 52 |
|
| 53 |
+
# Preview & date column selection
|
| 54 |
df_preview = pd.read_csv(csv_path, nrows=5)
|
| 55 |
st.dataframe(df_preview)
|
| 56 |
date_col = st.selectbox("Select date/time column for forecasting", df_preview.columns)
|
| 57 |
|
| 58 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 59 |
+
# 4. LOCAL TOOL EXECUTION
|
| 60 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 61 |
+
with st.spinner("๐ Parsing CSVโฆ"):
|
| 62 |
summary_text = parse_csv_tool(csv_path)
|
| 63 |
|
| 64 |
+
with st.spinner("๐ Generating sales trend chartโฆ"):
|
| 65 |
sales_fig = plot_sales_tool(csv_path, date_col=date_col)
|
| 66 |
+
|
| 67 |
+
# Show chart or warn
|
| 68 |
+
if isinstance(sales_fig, go.Figure):
|
| 69 |
st.plotly_chart(sales_fig, use_container_width=True)
|
| 70 |
+
else: # returned error message
|
| 71 |
+
st.warning(sales_fig)
|
| 72 |
|
| 73 |
+
with st.spinner("๐ฎ Forecasting future metricsโฆ"):
|
| 74 |
forecast_text = forecast_tool(csv_path, date_col=date_col)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
# Display forecast PNG if created
|
| 77 |
+
if os.path.exists("forecast_plot.png"):
|
| 78 |
+
st.image("forecast_plot.png", caption="Sales Forecast", use_column_width=True)
|
| 79 |
+
|
| 80 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 81 |
+
# 5. GEMINI 1.5โPRO STRATEGY
|
| 82 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 83 |
prompt = (
|
| 84 |
f"You are **BizIntel Strategist AI**.\n\n"
|
| 85 |
f"### CSV Summary\n```\n{summary_text}\n```\n\n"
|
| 86 |
f"### Forecast Output\n```\n{forecast_text}\n```\n\n"
|
| 87 |
+
"Return **Markdown** with:\n"
|
| 88 |
+
"1. Five key insights (bullets)\n"
|
| 89 |
+
"2. Three actionable strategies (with expected impact)\n"
|
| 90 |
+
"3. Risk factors or anomalies\n"
|
| 91 |
+
"4. Suggested additional visuals\n"
|
| 92 |
)
|
| 93 |
|
| 94 |
st.subheader("๐ Strategy Recommendations (Geminiย 1.5ย Pro)")
|
| 95 |
with st.spinner("Geminiย 1.5ย Pro is generating insightsโฆ"):
|
| 96 |
strategy_md = gemini.generate_content(prompt).text
|
|
|
|
| 97 |
st.markdown(strategy_md)
|
| 98 |
|
| 99 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 100 |
+
# 6. OPTIONAL EXPLORATORY VISUALS
|
| 101 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
|
|
|
|
|
| 102 |
st.markdown("---")
|
| 103 |
st.subheader("๐ Optional Exploratory Visuals")
|
| 104 |
|
|
|
|
| 112 |
|
| 113 |
# Scatterโmatrix
|
| 114 |
if st.checkbox("Show scatterโmatrix"):
|
| 115 |
+
mult_cols = st.multiselect(
|
| 116 |
+
"Choose up to 5 columns", num_cols, default=num_cols[:3], key="scatter"
|
| 117 |
+
)
|
| 118 |
+
if mult_cols:
|
| 119 |
+
fig_scatter = scatter_matrix_tool(csv_path, mult_cols)
|
| 120 |
st.plotly_chart(fig_scatter, use_container_width=True)
|
| 121 |
|
| 122 |
# Correlation heatโmap
|
|
|
|
| 124 |
fig_corr = corr_heatmap_tool(csv_path)
|
| 125 |
st.plotly_chart(fig_corr, use_container_width=True)
|
| 126 |
|
| 127 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 128 |
+
# 7. CSV SUMMARY TEXT
|
| 129 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 130 |
st.markdown("---")
|
| 131 |
+
st.subheader("๐ CSV Summary (full stats)")
|
| 132 |
st.text(summary_text)
|