File size: 6,512 Bytes
e490e03
 
309eec4
 
2c362d2
309eec4
10c7dea
3acbc9c
309eec4
10c7dea
e490e03
 
3acbc9c
 
 
 
 
10c7dea
e490e03
 
 
10c7dea
2c362d2
b3a1f0c
 
 
 
e490e03
b3a1f0c
2c362d2
67e3963
e490e03
 
 
2c362d2
3acbc9c
67e3963
72a73ff
 
e490e03
 
 
523228c
 
d826a13
309eec4
 
2c362d2
309eec4
2c362d2
3acbc9c
10c7dea
e490e03
2c362d2
 
3acbc9c
10c7dea
e490e03
 
 
 
2c362d2
10c7dea
e490e03
3acbc9c
e490e03
 
 
3acbc9c
e490e03
 
10c7dea
e490e03
3acbc9c
67e3963
e490e03
 
 
 
 
 
 
523228c
 
3acbc9c
 
e490e03
 
 
 
 
523228c
 
 
3acbc9c
 
d826a13
523228c
e490e03
 
 
523228c
3acbc9c
 
 
523228c
3acbc9c
 
 
 
 
d826a13
3acbc9c
 
e490e03
 
 
 
 
3acbc9c
 
 
 
 
 
 
e490e03
 
 
3acbc9c
e490e03
3acbc9c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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)