Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,34 +6,7 @@ import numpy as np
|
|
6 |
from datetime import datetime, timedelta
|
7 |
from typing import Dict, List, Any
|
8 |
|
9 |
-
#
|
10 |
-
import torch
|
11 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
12 |
-
|
13 |
-
def load_huggingface_model(model_name="google/flan-t5-base"):
|
14 |
-
try:
|
15 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
16 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
17 |
-
return model, tokenizer
|
18 |
-
except Exception as e:
|
19 |
-
st.error(f"Error loading model: {e}")
|
20 |
-
return None, None
|
21 |
-
|
22 |
-
def generate_text(model, tokenizer, prompt, max_length=200):
|
23 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
24 |
-
|
25 |
-
with torch.no_grad():
|
26 |
-
outputs = model.generate(
|
27 |
-
**inputs,
|
28 |
-
max_length=max_length,
|
29 |
-
num_return_sequences=1,
|
30 |
-
do_sample=True,
|
31 |
-
temperature=0.7
|
32 |
-
)
|
33 |
-
|
34 |
-
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
35 |
-
|
36 |
-
# Data Processing Class
|
37 |
class DataProcessor:
|
38 |
def __init__(self):
|
39 |
self.data = None
|
@@ -161,124 +134,110 @@ class BrainstormManager:
|
|
161 |
steps.append("Prepare enterprise sales strategy")
|
162 |
return steps
|
163 |
|
164 |
-
# Sample Data Generation
|
165 |
def generate_sample_data():
|
166 |
dates = pd.date_range(start='2024-01-01', end='2024-01-31', freq='D')
|
167 |
-
|
168 |
'Date': dates,
|
169 |
'Revenue': np.random.normal(1000, 100, len(dates)),
|
170 |
'Users': np.random.randint(100, 200, len(dates)),
|
171 |
'Engagement': np.random.uniform(0.5, 0.9, len(dates)),
|
172 |
-
'Category': np.random.choice(['
|
173 |
})
|
174 |
-
|
175 |
-
# Add predictive elements
|
176 |
-
base_data['Predicted_Revenue'] = base_data['Revenue'] * np.linspace(1, 1.2, len(dates))
|
177 |
-
base_data['Revenue_Trend'] = np.where(base_data['Predicted_Revenue'] > base_data['Revenue'], 'Positive', 'Negative')
|
178 |
-
|
179 |
-
return base_data
|
180 |
|
181 |
-
#
|
182 |
def render_dashboard():
|
183 |
-
st.header("π
|
184 |
|
|
|
185 |
data = generate_sample_data()
|
|
|
186 |
|
187 |
-
#
|
188 |
-
st.sidebar.header("Dashboard Filters")
|
189 |
-
selected_categories = st.sidebar.multiselect(
|
190 |
-
"Select Categories",
|
191 |
-
options=data['Category'].unique(),
|
192 |
-
default=data['Category'].unique()
|
193 |
-
)
|
194 |
-
|
195 |
-
date_range = st.sidebar.date_input(
|
196 |
-
"Select Date Range",
|
197 |
-
filtered_data = data[
|
198 |
-
(data['Category'].isin(selected_categories)) &
|
199 |
-
(data['Date'].dt.date.between(date_range[0], date_range[1]))
|
200 |
-
]
|
201 |
-
)
|
202 |
-
|
203 |
-
# Filter Data
|
204 |
-
filtered_data = data[
|
205 |
-
(data['Category'].isin(selected_categories)) &
|
206 |
-
(data['Date'].between(date_range[0], date_range[1]))
|
207 |
-
]
|
208 |
-
|
209 |
-
# KPI Metrics
|
210 |
col1, col2, col3, col4 = st.columns(4)
|
211 |
with col1:
|
212 |
st.metric("Total Revenue",
|
213 |
-
f"${
|
214 |
-
delta=f"{
|
215 |
with col2:
|
216 |
st.metric("Total Users",
|
217 |
-
f"{
|
218 |
-
delta=f"{
|
219 |
with col3:
|
220 |
st.metric("Avg Engagement",
|
221 |
-
f"{
|
|
|
222 |
with col4:
|
223 |
-
st.metric("
|
224 |
-
|
|
|
225 |
|
226 |
-
#
|
227 |
col1, col2 = st.columns(2)
|
228 |
|
229 |
with col1:
|
230 |
-
st.subheader("Revenue
|
231 |
-
|
232 |
-
|
233 |
-
x=
|
234 |
-
y=
|
235 |
mode='lines',
|
236 |
-
name='
|
237 |
line=dict(color='blue')
|
238 |
))
|
239 |
-
|
240 |
-
x=
|
241 |
-
y=
|
242 |
mode='lines',
|
243 |
-
name='
|
244 |
-
line=dict(color='
|
245 |
))
|
246 |
-
|
|
|
247 |
|
248 |
with col2:
|
249 |
-
st.subheader("
|
250 |
-
|
251 |
-
|
252 |
-
'Users'
|
253 |
-
'Engagement'
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
category_performance,
|
259 |
-
x='Category',
|
260 |
-
y='Total_Revenue',
|
261 |
-
color='Avg_Engagement',
|
262 |
-
hover_data=['Total_Users', 'Avg_Revenue']
|
263 |
)
|
264 |
-
|
|
|
265 |
|
266 |
-
#
|
267 |
-
st.subheader("
|
268 |
-
|
|
|
|
|
|
|
|
|
269 |
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
|
|
|
|
|
|
275 |
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
|
281 |
-
# Analytics Rendering (from previous implementation)
|
282 |
def render_analytics():
|
283 |
st.header("π Data Analytics")
|
284 |
|
@@ -348,7 +307,6 @@ def render_analytics():
|
|
348 |
for col, (metric, value) in zip(cols, metrics.items()):
|
349 |
col.metric(metric, f"{value:.2f}")
|
350 |
|
351 |
-
# Brainstorm Rendering
|
352 |
def render_brainstorm_page():
|
353 |
st.title("Product Brainstorm Hub")
|
354 |
manager = BrainstormManager()
|
@@ -398,42 +356,6 @@ def render_brainstorm_page():
|
|
398 |
else:
|
399 |
st.info("No products yet. Create one to get started!")
|
400 |
|
401 |
-
# AI Assistant Rendering
|
402 |
-
def render_ai_assistant():
|
403 |
-
st.title("π€ Business AI Assistant")
|
404 |
-
|
405 |
-
# Model Selection
|
406 |
-
model_options = {
|
407 |
-
"Google Flan-T5": "google/flan-t5-base",
|
408 |
-
"DialoGPT": "microsoft/DialoGPT-medium",
|
409 |
-
"GPT-2 Small": "gpt2"
|
410 |
-
}
|
411 |
-
|
412 |
-
selected_model = st.selectbox(
|
413 |
-
"Choose AI Model",
|
414 |
-
list(model_options.keys())
|
415 |
-
)
|
416 |
-
|
417 |
-
# Load Selected Model
|
418 |
-
model_name = model_options[selected_model]
|
419 |
-
model, tokenizer = load_huggingface_model(model_name)
|
420 |
-
|
421 |
-
if model and tokenizer:
|
422 |
-
# Prompt Input
|
423 |
-
user_prompt = st.text_area(
|
424 |
-
"Enter your business query",
|
425 |
-
placeholder="Ask about business strategy, product analysis, etc."
|
426 |
-
)
|
427 |
-
|
428 |
-
if st.button("Generate Response"):
|
429 |
-
with st.spinner("Generating response..."):
|
430 |
-
response = generate_text(model, tokenizer, user_prompt)
|
431 |
-
st.success("AI Response:")
|
432 |
-
st.write(response)
|
433 |
-
else:
|
434 |
-
st.error("Failed to load model")
|
435 |
-
|
436 |
-
# Chat Rendering (simplified)
|
437 |
def render_chat():
|
438 |
st.header("π¬ Business Assistant")
|
439 |
|
@@ -455,7 +377,6 @@ def render_chat():
|
|
455 |
st.markdown(response)
|
456 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
457 |
|
458 |
-
# Main Application Function
|
459 |
def main():
|
460 |
st.set_page_config(
|
461 |
page_title="Prospira",
|
@@ -470,7 +391,7 @@ def main():
|
|
470 |
|
471 |
page = st.radio(
|
472 |
"Navigation",
|
473 |
-
["Dashboard", "Analytics", "Brainstorm", "
|
474 |
)
|
475 |
|
476 |
if page == "Dashboard":
|
@@ -479,8 +400,6 @@ def main():
|
|
479 |
render_analytics()
|
480 |
elif page == "Brainstorm":
|
481 |
render_brainstorm_page()
|
482 |
-
elif page == "AI Assistant":
|
483 |
-
render_ai_assistant()
|
484 |
elif page == "Chat":
|
485 |
render_chat()
|
486 |
|
|
|
6 |
from datetime import datetime, timedelta
|
7 |
from typing import Dict, List, Any
|
8 |
|
9 |
+
# --- Data Processing Class ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
class DataProcessor:
|
11 |
def __init__(self):
|
12 |
self.data = None
|
|
|
134 |
steps.append("Prepare enterprise sales strategy")
|
135 |
return steps
|
136 |
|
137 |
+
# --- Sample Data Generation ---
|
138 |
def generate_sample_data():
|
139 |
dates = pd.date_range(start='2024-01-01', end='2024-01-31', freq='D')
|
140 |
+
return pd.DataFrame({
|
141 |
'Date': dates,
|
142 |
'Revenue': np.random.normal(1000, 100, len(dates)),
|
143 |
'Users': np.random.randint(100, 200, len(dates)),
|
144 |
'Engagement': np.random.uniform(0.5, 0.9, len(dates)),
|
145 |
+
'Category': np.random.choice(['A', 'B', 'C'], len(dates))
|
146 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
|
148 |
+
# --- Page Rendering Functions ---
|
149 |
def render_dashboard():
|
150 |
+
st.header("π Comprehensive Business Performance Dashboard")
|
151 |
|
152 |
+
# Generate sample data with more complex structure
|
153 |
data = generate_sample_data()
|
154 |
+
data['Profit_Margin'] = data['Revenue'] * np.random.uniform(0.1, 0.3, len(data))
|
155 |
|
156 |
+
# Top-level KPI Section
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
col1, col2, col3, col4 = st.columns(4)
|
158 |
with col1:
|
159 |
st.metric("Total Revenue",
|
160 |
+
f"${data['Revenue'].sum():,.2f}",
|
161 |
+
delta=f"{data['Revenue'].pct_change().mean()*100:.2f}%")
|
162 |
with col2:
|
163 |
st.metric("Total Users",
|
164 |
+
f"{data['Users'].sum():,}",
|
165 |
+
delta=f"{data['Users'].pct_change().mean()*100:.2f}%")
|
166 |
with col3:
|
167 |
st.metric("Avg Engagement",
|
168 |
+
f"{data['Engagement'].mean():.2%}",
|
169 |
+
delta=f"{data['Engagement'].pct_change().mean()*100:.2f}%")
|
170 |
with col4:
|
171 |
+
st.metric("Profit Margin",
|
172 |
+
f"{data['Profit_Margin'].mean():.2%}",
|
173 |
+
delta=f"{data['Profit_Margin'].pct_change().mean()*100:.2f}%")
|
174 |
|
175 |
+
# Visualization Grid
|
176 |
col1, col2 = st.columns(2)
|
177 |
|
178 |
with col1:
|
179 |
+
st.subheader("Revenue & Profit Trends")
|
180 |
+
fig_revenue = go.Figure()
|
181 |
+
fig_revenue.add_trace(go.Scatter(
|
182 |
+
x=data['Date'],
|
183 |
+
y=data['Revenue'],
|
184 |
mode='lines',
|
185 |
+
name='Revenue',
|
186 |
line=dict(color='blue')
|
187 |
))
|
188 |
+
fig_revenue.add_trace(go.Scatter(
|
189 |
+
x=data['Date'],
|
190 |
+
y=data['Profit_Margin'],
|
191 |
mode='lines',
|
192 |
+
name='Profit Margin',
|
193 |
+
line=dict(color='green')
|
194 |
))
|
195 |
+
fig_revenue.update_layout(height=350)
|
196 |
+
st.plotly_chart(fig_revenue, use_container_width=True)
|
197 |
|
198 |
with col2:
|
199 |
+
st.subheader("User Engagement Analysis")
|
200 |
+
fig_engagement = px.scatter(
|
201 |
+
data,
|
202 |
+
x='Users',
|
203 |
+
y='Engagement',
|
204 |
+
color='Category',
|
205 |
+
size='Revenue',
|
206 |
+
hover_data=['Date'],
|
207 |
+
title='User Engagement Dynamics'
|
|
|
|
|
|
|
|
|
|
|
208 |
)
|
209 |
+
fig_engagement.update_layout(height=350)
|
210 |
+
st.plotly_chart(fig_engagement, use_container_width=True)
|
211 |
|
212 |
+
# Category Performance
|
213 |
+
st.subheader("Category Performance Breakdown")
|
214 |
+
category_performance = data.groupby('Category').agg({
|
215 |
+
'Revenue': 'sum',
|
216 |
+
'Users': 'sum',
|
217 |
+
'Engagement': 'mean'
|
218 |
+
}).reset_index()
|
219 |
|
220 |
+
fig_category = px.bar(
|
221 |
+
category_performance,
|
222 |
+
x='Category',
|
223 |
+
y='Revenue',
|
224 |
+
color='Engagement',
|
225 |
+
title='Revenue by Category with Engagement Overlay'
|
226 |
+
)
|
227 |
+
st.plotly_chart(fig_category, use_container_width=True)
|
228 |
|
229 |
+
# Bottom Summary
|
230 |
+
st.subheader("Quick Insights")
|
231 |
+
insights_col1, insights_col2 = st.columns(2)
|
232 |
+
|
233 |
+
with insights_col1:
|
234 |
+
st.metric("Top Performing Category",
|
235 |
+
category_performance.loc[category_performance['Revenue'].idxmax(), 'Category'])
|
236 |
+
|
237 |
+
with insights_col2:
|
238 |
+
st.metric("Highest Engagement Category",
|
239 |
+
category_performance.loc[category_performance['Engagement'].idxmax(), 'Category'])
|
240 |
|
|
|
241 |
def render_analytics():
|
242 |
st.header("π Data Analytics")
|
243 |
|
|
|
307 |
for col, (metric, value) in zip(cols, metrics.items()):
|
308 |
col.metric(metric, f"{value:.2f}")
|
309 |
|
|
|
310 |
def render_brainstorm_page():
|
311 |
st.title("Product Brainstorm Hub")
|
312 |
manager = BrainstormManager()
|
|
|
356 |
else:
|
357 |
st.info("No products yet. Create one to get started!")
|
358 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
359 |
def render_chat():
|
360 |
st.header("π¬ Business Assistant")
|
361 |
|
|
|
377 |
st.markdown(response)
|
378 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
379 |
|
|
|
380 |
def main():
|
381 |
st.set_page_config(
|
382 |
page_title="Prospira",
|
|
|
391 |
|
392 |
page = st.radio(
|
393 |
"Navigation",
|
394 |
+
["Dashboard", "Analytics", "Brainstorm", "Chat"]
|
395 |
)
|
396 |
|
397 |
if page == "Dashboard":
|
|
|
400 |
render_analytics()
|
401 |
elif page == "Brainstorm":
|
402 |
render_brainstorm_page()
|
|
|
|
|
403 |
elif page == "Chat":
|
404 |
render_chat()
|
405 |
|