Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,439 +1,454 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 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 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Here's the code without line numbers:
|
| 2 |
+
|
| 3 |
+
```python
|
| 4 |
import streamlit as st
|
| 5 |
+
import plotly.express as px
|
| 6 |
+
import plotly.graph_objects as go
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import numpy as np
|
| 9 |
+
from datetime import datetime, timedelta
|
| 10 |
+
from typing import Dict, List, Any
|
| 11 |
+
import streamlit as st
|
| 12 |
+
import streamlit.components.v1 as components
|
| 13 |
+
|
| 14 |
+
# --- Data Processing Class ---
|
| 15 |
+
class DataProcessor:
|
| 16 |
+
def __init__(self):
|
| 17 |
+
self.data = None
|
| 18 |
+
self.numeric_columns = []
|
| 19 |
+
self.categorical_columns = []
|
| 20 |
+
|
| 21 |
+
self.date_columns = []
|
| 22 |
+
|
| 23 |
+
def load_data(self, file) -> bool:
|
| 24 |
+
try:
|
| 25 |
+
self.data = pd.read_csv(file)
|
| 26 |
+
self._classify_columns()
|
| 27 |
+
return True
|
| 28 |
+
except Exception as e:
|
| 29 |
+
st.error(f"Error loading data: {str(e)}")
|
| 30 |
+
return False
|
| 31 |
+
|
| 32 |
+
def _classify_columns(self):
|
| 33 |
+
for col in self.data.columns:
|
| 34 |
+
if pd.api.types.is_numeric_dtype(self.data[col]):
|
| 35 |
+
self.numeric_columns.append(col)
|
| 36 |
+
elif pd.api.types.is_datetime64_any_dtype(self.data[col]):
|
| 37 |
+
self.date_columns.append(col)
|
| 38 |
+
else:
|
| 39 |
+
try:
|
| 40 |
+
pd.to_datetime(self.data[col])
|
| 41 |
+
self.date_columns.append(col)
|
| 42 |
+
except:
|
| 43 |
+
self.categorical_columns.append(col)
|
| 44 |
+
|
| 45 |
+
def get_basic_stats(self) -> Dict[str, Any]:
|
| 46 |
+
if self.data is None:
|
| 47 |
+
return {}
|
| 48 |
+
|
| 49 |
+
stats = {
|
| 50 |
+
'summary': self.data[self.numeric_columns].describe(),
|
| 51 |
+
'missing_values': self.data.isnull().sum(),
|
| 52 |
+
'row_count': len(self.data),
|
| 53 |
+
'column_count': len(self.data.columns)
|
| 54 |
+
}
|
| 55 |
+
return stats
|
| 56 |
+
|
| 57 |
+
def create_visualization(self, chart_type: str, x_col: str, y_col: str, color_col: str = None) -> go.Figure:
|
| 58 |
+
if chart_type == "Line Plot":
|
| 59 |
+
fig = px.line(self.data, x=x_col, y=y_col, color=color_col)
|
| 60 |
+
elif chart_type == "Bar Plot":
|
| 61 |
+
fig = px.bar(self.data, x=x_col, y=y_col, color=color_col)
|
| 62 |
+
elif chart_type == "Scatter Plot":
|
| 63 |
+
fig = px.scatter(self.data, x=x_col, y=y_col, color=color_col)
|
| 64 |
+
elif chart_type == "Box Plot":
|
| 65 |
+
fig = px.box(self.data, x=x_col, y=y_col, color=color_col)
|
| 66 |
+
else:
|
| 67 |
+
fig = px.histogram(self.data, x=x_col, color=color_col)
|
| 68 |
+
|
| 69 |
+
return fig
|
| 70 |
+
|
| 71 |
+
class BrainstormManager:
|
| 72 |
+
def __init__(self):
|
| 73 |
+
if 'products' not in st.session_state:
|
| 74 |
+
st.session_state.products = {}
|
| 75 |
+
|
| 76 |
+
def generate_product_form(self) -> Dict:
|
| 77 |
+
with st.form("product_form"):
|
| 78 |
+
basic_info = {
|
| 79 |
+
"name": st.text_input("Product Name"),
|
| 80 |
+
"category": st.selectbox("Category", ["Digital", "Physical", "Service"]),
|
| 81 |
+
"description": st.text_area("Description"),
|
| 82 |
+
"target_audience": st.multiselect("Target Audience",
|
| 83 |
+
["Students", "Professionals", "Businesses", "Seniors", "Youth"]),
|
| 84 |
+
"price_range": st.slider("Price Range ($)", 0, 1000, (50, 200)),
|
| 85 |
+
"launch_date": st.date_input("Expected Launch Date")
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
st.subheader("Market Analysis")
|
| 89 |
+
market_analysis = {
|
| 90 |
+
"competitors": st.text_area("Main Competitors (one per line)"),
|
| 91 |
+
"unique_features": st.text_area("Unique Selling Points"),
|
| 92 |
+
"market_size": st.selectbox("Market Size",
|
| 93 |
+
["Small", "Medium", "Large", "Enterprise"]),
|
| 94 |
+
"growth_potential": st.slider("Growth Potential", 1, 10)
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
submitted = st.form_submit_button("Save Product")
|
| 98 |
+
return basic_info, market_analysis, submitted
|
| 99 |
+
|
| 100 |
+
def analyze_product(self, product_data: Dict) -> Dict:
|
| 101 |
+
insights = {
|
| 102 |
+
"market_opportunity": self._calculate_opportunity_score(product_data),
|
| 103 |
+
"suggested_price": self._suggest_price(product_data),
|
| 104 |
+
"risk_factors": self._identify_risks(product_data),
|
| 105 |
+
"next_steps": self._generate_next_steps(product_data)
|
| 106 |
+
}
|
| 107 |
+
return insights
|
| 108 |
+
|
| 109 |
+
def _calculate_opportunity_score(self, data: Dict) -> int:
|
| 110 |
+
score = 0
|
| 111 |
+
if data.get("market_size") == "Large":
|
| 112 |
+
score += 3
|
| 113 |
+
if len(data.get("target_audience", [])) >= 2:
|
| 114 |
+
score += 2
|
| 115 |
+
if data.get("growth_potential", 0) > 7:
|
| 116 |
+
score += 2
|
| 117 |
+
return min(score, 10)
|
| 118 |
+
|
| 119 |
+
def _suggest_price(self, data: Dict) -> float:
|
| 120 |
+
base_price = sum(data.get("price_range", (0, 0))) / 2
|
| 121 |
+
if data.get("market_size") == "Enterprise":
|
| 122 |
+
base_price *= 1.5
|
| 123 |
+
return round(base_price, 2)
|
| 124 |
+
|
| 125 |
+
def _identify_risks(self, data: Dict) -> List[str]:
|
| 126 |
+
risks = []
|
| 127 |
+
if data.get("competitors"):
|
| 128 |
+
risks.append("Competitive market - differentiation crucial")
|
| 129 |
+
if len(data.get("target_audience", [])) < 2:
|
| 130 |
+
risks.append("Narrow target audience - consider expansion")
|
| 131 |
+
return risks
|
| 132 |
+
|
| 133 |
+
def _generate_next_steps(self, data: Dict) -> List[str]:
|
| 134 |
+
steps = [
|
| 135 |
+
"Create detailed product specification",
|
| 136 |
+
"Develop MVP timeline",
|
| 137 |
+
"Plan marketing strategy"
|
| 138 |
+
]
|
| 139 |
+
if data.get("market_size") == "Enterprise":
|
| 140 |
+
steps.append("Prepare enterprise sales strategy")
|
| 141 |
+
return steps
|
| 142 |
+
|
| 143 |
+
# --- Sample Data Generation ---
|
| 144 |
+
def generate_sample_data():
|
| 145 |
+
dates = pd.date_range(start='2024-01-01', end='2024-01-31', freq='D')
|
| 146 |
+
return pd.DataFrame({
|
| 147 |
+
'Date': dates,
|
| 148 |
+
'Revenue': np.random.normal(1000, 100, len(dates)),
|
| 149 |
+
'Users': np.random.randint(100, 200, len(dates)),
|
| 150 |
+
'Engagement': np.random.uniform(0.5, 0.9, len(dates)),
|
| 151 |
+
'Category': np.random.choice(['A', 'B', 'C'], len(dates))
|
| 152 |
+
})
|
| 153 |
+
|
| 154 |
+
# --- Page Rendering Functions ---
|
| 155 |
+
def render_dashboard():
|
| 156 |
+
st.header("π Comprehensive Business Performance Dashboard")
|
| 157 |
+
|
| 158 |
+
# Generate sample data with more complex structure
|
| 159 |
+
data = generate_sample_data()
|
| 160 |
+
data['Profit_Margin'] = data['Revenue'] * np.random.uniform(0.1, 0.3, len(data))
|
| 161 |
+
|
| 162 |
+
# Top-level KPI Section
|
| 163 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 164 |
+
with col1:
|
| 165 |
+
st.metric("Total Revenue",
|
| 166 |
+
f"${data['Revenue'].sum():,.2f}",
|
| 167 |
+
delta=f"{data['Revenue'].pct_change().mean()*100:.2f}%")
|
| 168 |
+
with col2:
|
| 169 |
+
st.metric("Total Users",
|
| 170 |
+
f"{data['Users'].sum():,}",
|
| 171 |
+
delta=f"{data['Users'].pct_change().mean()*100:.2f}%")
|
| 172 |
+
with col3:
|
| 173 |
+
st.metric("Avg Engagement",
|
| 174 |
+
f"{data['Engagement'].mean():.2%}",
|
| 175 |
+
delta=f"{data['Engagement'].pct_change().mean()*100:.2f}%")
|
| 176 |
+
with col4:
|
| 177 |
+
st.metric("Profit Margin",
|
| 178 |
+
f"{data['Profit_Margin'].mean():.2%}",
|
| 179 |
+
delta=f"{data['Profit_Margin'].pct_change().mean()*100:.2f}%")
|
| 180 |
+
|
| 181 |
+
# Visualization Grid
|
| 182 |
+
col1, col2 = st.columns(2)
|
| 183 |
+
|
| 184 |
+
with col1:
|
| 185 |
+
st.subheader("Revenue & Profit Trends")
|
| 186 |
+
fig_revenue = go.Figure()
|
| 187 |
+
fig_revenue.add_trace(go.Scatter(
|
| 188 |
+
x=data['Date'],
|
| 189 |
+
y=data['Revenue'],
|
| 190 |
+
mode='lines',
|
| 191 |
+
name='Revenue',
|
| 192 |
+
line=dict(color='blue')
|
| 193 |
+
))
|
| 194 |
+
fig_revenue.add_trace(go.Scatter(
|
| 195 |
+
x=data['Date'],
|
| 196 |
+
y=data['Profit_Margin'],
|
| 197 |
+
mode='lines',
|
| 198 |
+
name='Profit Margin',
|
| 199 |
+
line=dict(color='green')
|
| 200 |
+
))
|
| 201 |
+
fig_revenue.update_layout(height=350)
|
| 202 |
+
st.plotly_chart(fig_revenue, use_container_width=True)
|
| 203 |
+
|
| 204 |
+
with col2:
|
| 205 |
+
st.subheader("User Engagement Analysis")
|
| 206 |
+
fig_engagement = px.scatter(
|
| 207 |
+
data,
|
| 208 |
+
x='Users',
|
| 209 |
+
y='Engagement',
|
| 210 |
+
color='Category',
|
| 211 |
+
size='Revenue',
|
| 212 |
+
hover_data=['Date'],
|
| 213 |
+
title='User Engagement Dynamics'
|
| 214 |
+
)
|
| 215 |
+
fig_engagement.update_layout(height=350)
|
| 216 |
+
st.plotly_chart(fig_engagement, use_container_width=True)
|
| 217 |
+
|
| 218 |
+
# Category Performance
|
| 219 |
+
st.subheader("Category Performance Breakdown")
|
| 220 |
+
category_performance = data.groupby('Category').agg({
|
| 221 |
+
'Revenue': 'sum',
|
| 222 |
+
'Users': 'sum',
|
| 223 |
+
'Engagement': 'mean'
|
| 224 |
+
}).reset_index()
|
| 225 |
+
|
| 226 |
+
fig_category = px.bar(
|
| 227 |
+
category_performance,
|
| 228 |
+
x='Category',
|
| 229 |
+
y='Revenue',
|
| 230 |
+
color='Engagement',
|
| 231 |
+
title='Revenue by Category with Engagement Overlay'
|
| 232 |
+
)
|
| 233 |
+
st.plotly_chart(fig_category, use_container_width=True)
|
| 234 |
+
|
| 235 |
+
# Bottom Summary
|
| 236 |
+
st.subheader("Quick Insights")
|
| 237 |
+
insights_col1, insights_col2 = st.columns(2)
|
| 238 |
+
|
| 239 |
+
with insights_col1:
|
| 240 |
+
st.metric("Top Performing Category",
|
| 241 |
+
category_performance.loc[category_performance['Revenue'].idxmax(), 'Category'])
|
| 242 |
+
|
| 243 |
+
with insights_col2:
|
| 244 |
+
st.metric("Highest Engagement Category",
|
| 245 |
+
category_performance.loc[category_performance['Engagement'].idxmax(), 'Category'])
|
| 246 |
+
|
| 247 |
+
def render_analytics():
|
| 248 |
+
st.header("π Data Analytics")
|
| 249 |
+
|
| 250 |
+
processor = DataProcessor()
|
| 251 |
+
uploaded_file = st.file_uploader("Upload your CSV data", type=['csv'])
|
| 252 |
+
|
| 253 |
+
if uploaded_file is not None:
|
| 254 |
+
if processor.load_data(uploaded_file):
|
| 255 |
+
st.success("Data loaded successfully!")
|
| 256 |
+
|
| 257 |
+
tabs = st.tabs(["Data Preview", "Statistics", "Visualization", "Metrics"])
|
| 258 |
+
|
| 259 |
+
with tabs[0]:
|
| 260 |
+
st.subheader("Data Preview")
|
| 261 |
+
st.dataframe(processor.data.head())
|
| 262 |
+
st.info(f"Total rows: {len(processor.data)}, Total columns: {len(processor.data.columns)}")
|
| 263 |
+
|
| 264 |
+
with tabs[1]:
|
| 265 |
+
st.subheader("Basic Statistics")
|
| 266 |
+
stats = processor.get_basic_stats()
|
| 267 |
+
st.write(stats['summary'])
|
| 268 |
+
|
| 269 |
+
st.subheader("Missing Values")
|
| 270 |
+
st.write(stats['missing_values'])
|
| 271 |
+
|
| 272 |
+
with tabs[2]:
|
| 273 |
+
st.subheader("Create Visualization")
|
| 274 |
+
col1, col2, col3 = st.columns(3)
|
| 275 |
+
|
| 276 |
+
with col1:
|
| 277 |
+
chart_type = st.selectbox(
|
| 278 |
+
"Select Chart Type",
|
| 279 |
+
["Line Plot", "Bar Plot", "Scatter Plot", "Box Plot", "Histogram"]
|
| 280 |
+
)
|
| 281 |
+
|
| 282 |
+
with col2:
|
| 283 |
+
x_col = st.selectbox("Select X-axis", processor.data.columns)
|
| 284 |
+
|
| 285 |
+
with col3:
|
| 286 |
+
y_col = st.selectbox("Select Y-axis", processor.numeric_columns) if chart_type != "Histogram" else None
|
| 287 |
+
|
| 288 |
+
color_col = st.selectbox("Select Color Variable (optional)",
|
| 289 |
+
['None'] + processor.categorical_columns)
|
| 290 |
+
color_col = None if color_col == 'None' else color_col
|
| 291 |
+
|
| 292 |
+
fig = processor.create_visualization(
|
| 293 |
+
chart_type,
|
| 294 |
+
x_col,
|
| 295 |
+
y_col if y_col else x_col,
|
| 296 |
+
color_col
|
| 297 |
+
)
|
| 298 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 299 |
+
|
| 300 |
+
with tabs[3]:
|
| 301 |
+
st.subheader("Column Metrics")
|
| 302 |
+
selected_col = st.selectbox("Select column", processor.numeric_columns)
|
| 303 |
+
|
| 304 |
+
metrics = {
|
| 305 |
+
'Mean': processor.data[selected_col].mean(),
|
| 306 |
+
'Median': processor.data[selected_col].median(),
|
| 307 |
+
'Std Dev': processor.data[selected_col].std(),
|
| 308 |
+
'Min': processor.data[selected_col].min(),
|
| 309 |
+
'Max': processor.data[selected_col].max()
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
cols = st.columns(len(metrics))
|
| 313 |
+
for col, (metric, value) in zip(cols, metrics.items()):
|
| 314 |
+
col.metric(metric, f"{value:.2f}")
|
| 315 |
+
|
| 316 |
+
def render_brainstorm_page():
|
| 317 |
+
st.title("Product Brainstorm Hub")
|
| 318 |
+
manager = BrainstormManager()
|
| 319 |
+
|
| 320 |
+
action = st.sidebar.radio("Action", ["View Products", "Create New Product"])
|
| 321 |
+
|
| 322 |
+
if action == "Create New Product":
|
| 323 |
+
basic_info, market_analysis, submitted = manager.generate_product_form()
|
| 324 |
+
|
| 325 |
+
if submitted:
|
| 326 |
+
product_data = {**basic_info, **market_analysis}
|
| 327 |
+
insights = manager.analyze_product(product_data)
|
| 328 |
+
|
| 329 |
+
product_id = f"prod_{len(st.session_state.products)}"
|
| 330 |
+
st.session_state.products[product_id] = {
|
| 331 |
+
"data": product_data,
|
| 332 |
+
"insights": insights,
|
| 333 |
+
"created_at": str(datetime.now())
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
+
st.success("Product added! View insights in the Products tab.")
|
| 337 |
+
|
| 338 |
+
else:
|
| 339 |
+
if st.session_state.products:
|
| 340 |
+
for prod_id, product in st.session_state.products.items():
|
| 341 |
+
with st.expander(f"π― {product['data']['name']}"):
|
| 342 |
+
col1, col2 = st.columns(2)
|
| 343 |
+
|
| 344 |
+
with col1:
|
| 345 |
+
st.subheader("Product Details")
|
| 346 |
+
st.write(f"Category: {product['data']['category']}")
|
| 347 |
+
st.write(f"Target: {', '.join(product['data']['target_audience'])}")
|
| 348 |
+
st.write(f"Description: {product['data']['description']}")
|
| 349 |
+
|
| 350 |
+
with col2:
|
| 351 |
+
st.subheader("Insights")
|
| 352 |
+
st.metric("Opportunity Score", f"{product['insights']['market_opportunity']}/10")
|
| 353 |
+
st.metric("Suggested Price", f"${product['insights']['suggested_price']}")
|
| 354 |
+
|
| 355 |
+
st.write("**Risk Factors:**")
|
| 356 |
+
for risk in product['insights']['risk_factors']:
|
| 357 |
+
st.write(f"- {risk}")
|
| 358 |
+
|
| 359 |
+
st.write("**Next Steps:**")
|
| 360 |
+
for step in product['insights']['next_steps']:
|
| 361 |
+
st.write(f"- {step}")
|
| 362 |
+
else:
|
| 363 |
+
st.info("No products yet. Create one to get started!")
|
| 364 |
+
|
| 365 |
+
def generate_response(self, prompt: str, context: list = None) -> str:
|
| 366 |
+
if not self.model or not self.tokenizer:
|
| 367 |
+
return "LLM not initialized. Please check model configuration."
|
| 368 |
+
|
| 369 |
+
# Prepare conversation context
|
| 370 |
+
if context is None:
|
| 371 |
+
context = []
|
| 372 |
+
|
| 373 |
+
# Create full prompt with conversation history
|
| 374 |
+
full_prompt = "".join([f"{msg['role']}: {msg['content']}\n" for msg in context])
|
| 375 |
+
full_prompt += f"user: {prompt}\nassistant: "
|
| 376 |
+
|
| 377 |
+
# Tokenize input
|
| 378 |
+
input_ids = self.tokenizer(full_prompt, return_tensors="pt").input_ids.to(self.model.device)
|
| 379 |
+
|
| 380 |
+
# Generate response
|
| 381 |
+
try:
|
| 382 |
+
output = self.model.generate(
|
| 383 |
+
input_ids,
|
| 384 |
+
max_length=500,
|
| 385 |
+
num_return_sequences=1,
|
| 386 |
+
no_repeat_ngram_size=2,
|
| 387 |
+
temperature=0.7,
|
| 388 |
+
top_p=0.9
|
| 389 |
+
)
|
| 390 |
+
|
| 391 |
+
# Decode response
|
| 392 |
+
response = self.tokenizer.decode(output[0], skip_special_tokens=True)
|
| 393 |
+
|
| 394 |
+
# Extract only the new part of the response
|
| 395 |
+
response = response[len(full_prompt):].strip()
|
| 396 |
+
|
| 397 |
+
return response
|
| 398 |
+
except Exception as e:
|
| 399 |
+
return f"Response generation error: {e}"
|
| 400 |
+
|
| 401 |
+
def render_chat():
|
| 402 |
+
st.header("π¬AI Business Mentor")
|
| 403 |
+
st.title("π€ Prospira AI Business Mentor")
|
| 404 |
+
|
| 405 |
+
iframe_code = """
|
| 406 |
+
<iframe
|
| 407 |
+
src="https://demoorganisation34-vinay.hf.space"
|
| 408 |
+
frameborder="0"
|
| 409 |
+
width="850"
|
| 410 |
+
height="450"
|
| 411 |
+
></iframe>
|
| 412 |
+
|
| 413 |
+
|
| 414 |
+
"""
|
| 415 |
+
components.html(iframe_code, height=600)
|
| 416 |
+
|
| 417 |
+
def render_home():
|
| 418 |
+
st.title("π Welcome to Prospira")
|
| 419 |
+
st.subheader("π Data-Driven Solutions for Businesses and Creators")
|
| 420 |
+
st.markdown("""
|
| 421 |
+
**Prospira** empowers businesses and creators to enhance their content, products, and marketing strategies using AI-driven insights.
|
| 422 |
+
|
| 423 |
+
### **β¨ Key Features**
|
| 424 |
+
- **π Performance Analytics:** Real-time insights into business metrics.
|
| 425 |
+
- **π Competitive Analysis:** Benchmark your business against competitors.
|
| 426 |
+
- **π‘ Smart Product Ideas:** AI-generated recommendations for future products and content.
|
| 427 |
+
- **π§ AI Business Mentor:** Personalized AI guidance for strategy and growth.
|
| 428 |
+
Explore how **Prospira** can help optimize your decision-making and drive success! π‘π
|
| 429 |
+
""")
|
| 430 |
+
|
| 431 |
+
def main():
|
| 432 |
+
st.set_page_config(
|
| 433 |
+
page_title="Prospira",
|
| 434 |
+
page_icon="π",
|
| 435 |
+
layout="wide"
|
| 436 |
+
)
|
| 437 |
+
|
| 438 |
+
st.sidebar.title("Navigation")
|
| 439 |
+
page = st.sidebar.radio("Go to", ["Home", "Dashboard", "Analytics", "Brainstorm", "Chat"])
|
| 440 |
+
|
| 441 |
+
if page == "Home":
|
| 442 |
+
render_home()
|
| 443 |
+
elif page == "Dashboard":
|
| 444 |
+
render_dashboard()
|
| 445 |
+
elif page == "Analytics":
|
| 446 |
+
render_analytics()
|
| 447 |
+
elif page == "Brainstorm":
|
| 448 |
+
render_brainstorm_page()
|
| 449 |
+
elif page == "Chat":
|
| 450 |
+
render_chat()
|
| 451 |
+
|
| 452 |
+
if __name__ == "__main__":
|
| 453 |
+
main()
|
| 454 |
+
```
|