Update app.py
Browse files
app.py
CHANGED
|
@@ -5,22 +5,38 @@ import matplotlib.pyplot as plt
|
|
| 5 |
import seaborn as sns
|
| 6 |
from sklearn.model_selection import train_test_split
|
| 7 |
from sklearn.ensemble import RandomForestClassifier
|
| 8 |
-
from sklearn.metrics import classification_report, accuracy_score
|
| 9 |
import xgboost as xgb
|
| 10 |
from datetime import datetime, timedelta
|
| 11 |
import plotly.express as px
|
| 12 |
import plotly.graph_objects as go
|
| 13 |
from plotly.subplots import make_subplots
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
import io
|
| 15 |
import base64
|
| 16 |
import warnings
|
| 17 |
warnings.filterwarnings('ignore')
|
| 18 |
|
| 19 |
-
# Set modern
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
class B2BCustomerAnalytics:
|
| 26 |
def __init__(self):
|
|
@@ -29,554 +45,970 @@ class B2BCustomerAnalytics:
|
|
| 29 |
self.feature_importance = None
|
| 30 |
self.predictions = None
|
| 31 |
|
| 32 |
-
def
|
| 33 |
-
"""
|
| 34 |
-
if file is None:
|
| 35 |
-
return self._create_error_html("Please upload a CSV file"), None, None, None, None, None
|
| 36 |
-
|
| 37 |
try:
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
self.df = pd.read_csv(file.name)
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
missing_cols = [col for col in
|
| 44 |
if missing_cols:
|
| 45 |
-
return
|
| 46 |
|
| 47 |
-
#
|
| 48 |
self.df['order_date'] = pd.to_datetime(self.df['order_date'])
|
| 49 |
-
self.df = self._calculate_rfm_metrics(self.df)
|
| 50 |
-
self.df = self._perform_segmentation(self.df)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
rfm_chart = self._create_rfm_chart()
|
| 58 |
-
churn_chart = self._create_churn_chart()
|
| 59 |
-
trend_chart = self._create_trend_chart()
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
customer_table = self._create_customer_table()
|
| 66 |
-
|
| 67 |
-
return dashboard_html, model_results, segment_chart, rfm_chart, churn_chart, trend_chart, customer_table
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
-
return
|
| 71 |
|
| 72 |
-
def
|
| 73 |
-
"""Calculate RFM metrics"""
|
| 74 |
current_date = df['order_date'].max() + timedelta(days=1)
|
| 75 |
|
|
|
|
| 76 |
customer_metrics = df.groupby('customer_id').agg({
|
| 77 |
-
'order_date': 'max',
|
| 78 |
-
'amount': ['sum', '
|
| 79 |
-
})
|
| 80 |
|
| 81 |
-
customer_metrics.columns = ['
|
| 82 |
-
customer_metrics['recency_days'] = (current_date - customer_metrics['
|
| 83 |
|
| 84 |
-
# Merge back
|
| 85 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
-
return
|
| 89 |
|
| 90 |
-
def
|
| 91 |
-
"""Perform customer segmentation"""
|
| 92 |
customer_df = df.groupby('customer_id').agg({
|
| 93 |
'recency_days': 'first',
|
| 94 |
-
'frequency': 'first',
|
| 95 |
'monetary': 'first'
|
| 96 |
}).reset_index()
|
| 97 |
|
| 98 |
-
# Create RFM scores
|
| 99 |
-
customer_df['R_Score'] = pd.qcut(customer_df['recency_days'].rank(method='first'),
|
| 100 |
-
|
| 101 |
-
customer_df['
|
| 102 |
-
5, labels=[1,2,3,4,5])
|
| 103 |
-
customer_df['M_Score'] = pd.qcut(customer_df['monetary'].rank(method='first'),
|
| 104 |
-
5, labels=[1,2,3,4,5])
|
| 105 |
|
|
|
|
| 106 |
customer_df['R_Score'] = customer_df['R_Score'].astype(int)
|
| 107 |
customer_df['F_Score'] = customer_df['F_Score'].astype(int)
|
| 108 |
customer_df['M_Score'] = customer_df['M_Score'].astype(int)
|
| 109 |
|
| 110 |
-
#
|
| 111 |
def segment_customers(row):
|
| 112 |
if row['R_Score'] >= 4 and row['F_Score'] >= 4 and row['M_Score'] >= 4:
|
| 113 |
return 'Champions'
|
| 114 |
-
elif row['R_Score'] >= 3 and row['F_Score'] >= 3:
|
| 115 |
return 'Loyal Customers'
|
| 116 |
-
elif row['R_Score'] >= 3:
|
| 117 |
return 'Potential Loyalists'
|
| 118 |
elif row['R_Score'] >= 4 and row['F_Score'] <= 2:
|
| 119 |
return 'New Customers'
|
| 120 |
elif row['R_Score'] <= 2 and row['F_Score'] >= 3:
|
| 121 |
return 'At Risk'
|
| 122 |
-
elif row['R_Score'] <= 2 and row['M_Score'] >= 3:
|
| 123 |
return 'Cannot Lose Them'
|
| 124 |
-
|
| 125 |
return 'Lost Customers'
|
|
|
|
|
|
|
| 126 |
|
| 127 |
customer_df['Segment'] = customer_df.apply(segment_customers, axis=1)
|
| 128 |
|
| 129 |
-
#
|
| 130 |
-
customer_df['Churn_Risk'] = customer_df
|
| 131 |
-
'
|
| 132 |
-
'
|
| 133 |
-
'Potential Loyalists': 'Medium',
|
| 134 |
-
'New Customers': 'Medium',
|
| 135 |
-
'At Risk': 'High',
|
| 136 |
-
'Cannot Lose Them': 'High',
|
| 137 |
-
'Lost Customers': 'High'
|
| 138 |
-
})
|
| 139 |
-
|
| 140 |
-
# Merge back
|
| 141 |
-
df_with_segments = df.merge(customer_df[['customer_id', 'Segment', 'Churn_Risk']],
|
| 142 |
-
on='customer_id', how='left')
|
| 143 |
-
|
| 144 |
-
return df_with_segments
|
| 145 |
-
|
| 146 |
-
def _train_churn_model(self):
|
| 147 |
-
"""Train churn prediction model"""
|
| 148 |
-
customer_features = self.df.groupby('customer_id').agg({
|
| 149 |
-
'recency_days': 'first',
|
| 150 |
-
'frequency': 'first',
|
| 151 |
-
'monetary': 'first',
|
| 152 |
-
'amount': ['mean', 'std'],
|
| 153 |
-
'Churn_Risk': 'first'
|
| 154 |
-
}).reset_index()
|
| 155 |
-
|
| 156 |
-
customer_features.columns = ['customer_id', 'recency', 'frequency', 'monetary',
|
| 157 |
-
'avg_amount', 'std_amount', 'churn_risk']
|
| 158 |
-
customer_features['std_amount'].fillna(0, inplace=True)
|
| 159 |
-
|
| 160 |
-
# Create binary labels
|
| 161 |
-
customer_features['churn_label'] = (customer_features['churn_risk'] == 'High').astype(int)
|
| 162 |
-
|
| 163 |
-
# Features for modeling
|
| 164 |
-
feature_cols = ['recency', 'frequency', 'monetary', 'avg_amount', 'std_amount']
|
| 165 |
-
X = customer_features[feature_cols]
|
| 166 |
-
y = customer_features['churn_label']
|
| 167 |
-
|
| 168 |
-
if len(X) < 5:
|
| 169 |
-
return self._create_model_html("Insufficient data for model training", 0.0)
|
| 170 |
-
|
| 171 |
-
# Split data
|
| 172 |
-
test_size = min(0.3, max(0.1, len(X) * 0.2 / len(X)))
|
| 173 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size,
|
| 174 |
-
random_state=42, stratify=y)
|
| 175 |
|
| 176 |
-
#
|
| 177 |
-
|
| 178 |
-
|
| 179 |
|
| 180 |
-
|
| 181 |
-
y_pred = self.model.predict(X_test)
|
| 182 |
-
accuracy = accuracy_score(y_test, y_pred)
|
| 183 |
-
|
| 184 |
-
# Feature importance
|
| 185 |
-
self.feature_importance = pd.DataFrame({
|
| 186 |
-
'feature': feature_cols,
|
| 187 |
-
'importance': self.model.feature_importances_
|
| 188 |
-
}).sort_values('importance', ascending=False)
|
| 189 |
-
|
| 190 |
-
# Store predictions
|
| 191 |
-
all_predictions = self.model.predict_proba(X)[:, 1]
|
| 192 |
-
customer_features['churn_probability'] = all_predictions
|
| 193 |
-
self.predictions = customer_features
|
| 194 |
-
|
| 195 |
-
return self._create_model_html(accuracy, len(X_train), len(X_test))
|
| 196 |
|
| 197 |
-
def
|
| 198 |
-
"""
|
| 199 |
if self.df is None:
|
| 200 |
-
return ""
|
| 201 |
|
| 202 |
-
# Calculate KPIs
|
| 203 |
total_customers = self.df['customer_id'].nunique()
|
|
|
|
| 204 |
total_revenue = self.df['amount'].sum()
|
| 205 |
-
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
|
|
|
| 209 |
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
return f"""
|
| 215 |
-
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 2rem; border-radius: 1rem; color: white; margin-bottom: 2rem; text-align: center;">
|
| 216 |
-
<h1 style="font-size: 2.5rem; font-weight: bold; margin-bottom: 0.5rem;">
|
| 217 |
π’ B2B Customer Analytics Dashboard
|
| 218 |
-
</
|
| 219 |
-
<p style="font-size: 1.
|
| 220 |
Enterprise Customer Health Monitoring & Churn Prediction System
|
| 221 |
</p>
|
| 222 |
</div>
|
| 223 |
|
| 224 |
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1.5rem; margin-bottom: 2rem;">
|
| 225 |
-
<div style="background:
|
| 226 |
-
<
|
| 227 |
-
<
|
| 228 |
-
<
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
<div style="background: linear-gradient(135deg, #10b981 0%, #059669 100%); padding: 1.5rem; border-radius: 1rem; color: white; text-align: center; box-shadow: 0 10px 25px -5px rgba(16, 185, 129, 0.4);">
|
| 232 |
-
<div style="font-size: 3rem; margin-bottom: 0.5rem;">π°</div>
|
| 233 |
-
<div style="font-size: 2rem; font-weight: bold;">${total_revenue/1000000:.1f}M</div>
|
| 234 |
-
<div style="opacity: 0.9;">Total Revenue</div>
|
| 235 |
-
</div>
|
| 236 |
-
|
| 237 |
-
<div style="background: linear-gradient(135deg, #8b5cf6 0%, #7c3aed 100%); padding: 1.5rem; border-radius: 1rem; color: white; text-align: center; box-shadow: 0 10px 25px -5px rgba(139, 92, 246, 0.4);">
|
| 238 |
-
<div style="font-size: 3rem; margin-bottom: 0.5rem;">π</div>
|
| 239 |
-
<div style="font-size: 2rem; font-weight: bold;">${avg_order:.0f}</div>
|
| 240 |
-
<div style="opacity: 0.9;">Avg Order Value</div>
|
| 241 |
-
</div>
|
| 242 |
-
|
| 243 |
-
<div style="background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%); padding: 1.5rem; border-radius: 1rem; color: white; text-align: center; box-shadow: 0 10px 25px -5px rgba(239, 68, 68, 0.4);">
|
| 244 |
-
<div style="font-size: 3rem; margin-bottom: 0.5rem;">π¨</div>
|
| 245 |
-
<div style="font-size: 2rem; font-weight: bold;">{high_risk}</div>
|
| 246 |
-
<div style="opacity: 0.9;">High Risk Customers</div>
|
| 247 |
</div>
|
| 248 |
|
| 249 |
-
<div style="background:
|
| 250 |
-
<
|
| 251 |
-
<
|
| 252 |
-
<div style="opacity: 0.9;">Champion Customers</div>
|
| 253 |
</div>
|
| 254 |
|
| 255 |
-
<div style="background:
|
| 256 |
-
<
|
| 257 |
-
<
|
| 258 |
-
<div style="opacity: 0.9;">Healthy Customers</div>
|
| 259 |
</div>
|
| 260 |
</div>
|
| 261 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
-
def
|
| 264 |
-
"""
|
| 265 |
-
if
|
| 266 |
-
return
|
| 267 |
-
<div style="background: #fee2e2; border: 1px solid #fecaca; padding: 1rem; border-radius: 0.5rem; color: #dc2626;">
|
| 268 |
-
<h3>β οΈ Model Training Status</h3>
|
| 269 |
-
<p>{accuracy}</p>
|
| 270 |
-
</div>
|
| 271 |
-
"""
|
| 272 |
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
</div>
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
<div style="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
</div>
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
</div>
|
| 300 |
</div>
|
|
|
|
| 301 |
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
</div>
|
| 307 |
-
"""
|
| 308 |
|
| 309 |
-
def
|
| 310 |
-
"""Create
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
-
def
|
| 320 |
-
"""Create
|
| 321 |
if self.df is None:
|
| 322 |
-
return None
|
| 323 |
|
|
|
|
| 324 |
segment_data = self.df.groupby('customer_id')['Segment'].first().value_counts().reset_index()
|
| 325 |
segment_data.columns = ['Segment', 'Count']
|
| 326 |
|
| 327 |
-
|
| 328 |
segment_data,
|
| 329 |
values='Count',
|
| 330 |
names='Segment',
|
| 331 |
title='Customer Segment Distribution',
|
| 332 |
hole=0.4,
|
| 333 |
-
color_discrete_sequence=['#6366f1', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6', '#ec4899'
|
| 334 |
)
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
title={'x': 0.5, 'xanchor': 'center', 'font': {'size': 18, 'color': '#1f2937'}},
|
| 340 |
-
font=dict(family="Inter, sans-serif")
|
| 341 |
-
plot_bgcolor='white',
|
| 342 |
-
paper_bgcolor='white'
|
| 343 |
)
|
| 344 |
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
def _create_rfm_chart(self):
|
| 348 |
-
"""Create RFM analysis 3D scatter plot"""
|
| 349 |
-
if self.df is None:
|
| 350 |
-
return None
|
| 351 |
-
|
| 352 |
customer_rfm = self.df.groupby('customer_id').agg({
|
| 353 |
'recency_days': 'first',
|
| 354 |
-
'frequency': 'first',
|
| 355 |
'monetary': 'first',
|
| 356 |
'Segment': 'first'
|
| 357 |
}).reset_index()
|
| 358 |
|
| 359 |
-
|
| 360 |
-
customer_rfm,
|
| 361 |
-
x='recency_days',
|
| 362 |
y='frequency',
|
| 363 |
z='monetary',
|
| 364 |
-
color='Segment',
|
| 365 |
title='RFM Analysis - Customer Behavior Matrix',
|
| 366 |
labels={
|
| 367 |
'recency_days': 'Recency (Days)',
|
| 368 |
-
'frequency': 'Frequency (Orders)',
|
| 369 |
'monetary': 'Monetary (Revenue)'
|
| 370 |
},
|
| 371 |
-
color_discrete_sequence=['#6366f1', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6'
|
| 372 |
)
|
| 373 |
-
|
| 374 |
-
fig.update_layout(
|
| 375 |
height=500,
|
| 376 |
title={'x': 0.5, 'xanchor': 'center', 'font': {'size': 18, 'color': '#1f2937'}},
|
| 377 |
font=dict(family="Inter, sans-serif")
|
| 378 |
)
|
| 379 |
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
def _create_churn_chart(self):
|
| 383 |
-
"""Create churn risk distribution chart"""
|
| 384 |
-
if self.df is None:
|
| 385 |
-
return None
|
| 386 |
-
|
| 387 |
if self.predictions is not None:
|
| 388 |
-
|
| 389 |
-
self.predictions,
|
| 390 |
-
x='churn_probability',
|
| 391 |
nbins=20,
|
| 392 |
title='Churn Probability Distribution',
|
| 393 |
-
labels={'churn_probability': 'Churn Probability', 'count': 'Number of Customers'}
|
|
|
|
| 394 |
)
|
| 395 |
-
|
| 396 |
-
annotation_text="High Risk Threshold")
|
| 397 |
else:
|
| 398 |
risk_data = self.df.groupby('customer_id')['Churn_Risk'].first().value_counts().reset_index()
|
| 399 |
risk_data.columns = ['Risk_Level', 'Count']
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
y='Count',
|
| 406 |
title='Customer Churn Risk Distribution',
|
| 407 |
-
color='Risk_Level',
|
| 408 |
-
color_discrete_map=
|
| 409 |
)
|
| 410 |
|
| 411 |
-
|
| 412 |
-
height=400,
|
|
|
|
| 413 |
title={'x': 0.5, 'xanchor': 'center', 'font': {'size': 18, 'color': '#1f2937'}},
|
| 414 |
font=dict(family="Inter, sans-serif"),
|
| 415 |
-
plot_bgcolor='white'
|
| 416 |
-
paper_bgcolor='white'
|
| 417 |
)
|
| 418 |
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
def _create_trend_chart(self):
|
| 422 |
-
"""Create revenue trends chart"""
|
| 423 |
-
if self.df is None:
|
| 424 |
-
return None
|
| 425 |
-
|
| 426 |
self.df['order_month'] = self.df['order_date'].dt.to_period('M')
|
| 427 |
monthly_revenue = self.df.groupby('order_month')['amount'].sum().reset_index()
|
| 428 |
monthly_revenue['order_month'] = monthly_revenue['order_month'].astype(str)
|
| 429 |
|
| 430 |
-
|
| 431 |
-
monthly_revenue,
|
| 432 |
-
x='order_month',
|
| 433 |
y='amount',
|
| 434 |
title='Monthly Revenue Trends',
|
| 435 |
-
labels={'amount': 'Revenue ($)', 'order_month': 'Month'}
|
|
|
|
| 436 |
)
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
height=400,
|
| 441 |
title={'x': 0.5, 'xanchor': 'center', 'font': {'size': 18, 'color': '#1f2937'}},
|
| 442 |
font=dict(family="Inter, sans-serif"),
|
| 443 |
plot_bgcolor='white',
|
| 444 |
-
paper_bgcolor='white',
|
| 445 |
xaxis_tickangle=-45
|
| 446 |
)
|
| 447 |
|
| 448 |
-
return
|
| 449 |
|
| 450 |
-
def
|
| 451 |
-
"""Create customer
|
| 452 |
if self.df is None:
|
| 453 |
return None
|
| 454 |
-
|
|
|
|
| 455 |
customer_summary = self.df.groupby('customer_id').agg({
|
| 456 |
'Segment': 'first',
|
| 457 |
-
'Churn_Risk': 'first',
|
| 458 |
'recency_days': 'first',
|
| 459 |
'frequency': 'first',
|
| 460 |
-
'monetary': 'first'
|
|
|
|
| 461 |
}).reset_index()
|
| 462 |
|
|
|
|
| 463 |
if self.predictions is not None:
|
| 464 |
customer_summary = customer_summary.merge(
|
| 465 |
-
self.predictions[['customer_id', 'churn_probability']],
|
| 466 |
-
on='customer_id',
|
| 467 |
how='left'
|
| 468 |
)
|
|
|
|
| 469 |
else:
|
| 470 |
-
customer_summary['churn_probability'] = 0.5
|
| 471 |
|
|
|
|
|
|
|
|
|
|
| 472 |
customer_summary['churn_probability'] = (customer_summary['churn_probability'] * 100).round(1)
|
| 473 |
-
customer_summary = customer_summary.round(2)
|
| 474 |
|
|
|
|
| 475 |
customer_summary.columns = [
|
| 476 |
-
'Customer ID', 'Segment', 'Risk Level', 'Recency (Days)',
|
| 477 |
-
'Frequency', 'Total Spent ($)', 'Churn Probability (%)'
|
| 478 |
]
|
| 479 |
|
| 480 |
-
return customer_summary.head(50)
|
| 481 |
-
|
| 482 |
-
# Initialize the analytics class
|
| 483 |
-
analytics = B2BCustomerAnalytics()
|
| 484 |
-
|
| 485 |
-
# Create Gradio interface
|
| 486 |
-
with gr.Blocks(
|
| 487 |
-
theme=gr.themes.Soft(
|
| 488 |
-
primary_hue="blue",
|
| 489 |
-
secondary_hue="purple",
|
| 490 |
-
neutral_hue="gray"
|
| 491 |
-
),
|
| 492 |
-
css="""
|
| 493 |
-
.gradio-container {
|
| 494 |
-
font-family: 'Inter', sans-serif;
|
| 495 |
-
}
|
| 496 |
-
.gr-button-primary {
|
| 497 |
-
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 498 |
-
border: none !important;
|
| 499 |
-
}
|
| 500 |
-
.gr-button-primary:hover {
|
| 501 |
-
background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%) !important;
|
| 502 |
-
}
|
| 503 |
-
""",
|
| 504 |
-
title="B2B Customer Analytics Platform"
|
| 505 |
-
) as app:
|
| 506 |
-
|
| 507 |
-
gr.HTML("""
|
| 508 |
-
<div style="text-align: center; margin-bottom: 2rem;">
|
| 509 |
-
<h1 style="font-size: 3rem; font-weight: bold; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; margin-bottom: 0.5rem;">
|
| 510 |
-
π’ B2B Customer Analytics Platform
|
| 511 |
-
</h1>
|
| 512 |
-
<p style="font-size: 1.2rem; color: #6b7280;">
|
| 513 |
-
AI-Powered Customer Segmentation, Churn Prediction & Business Intelligence
|
| 514 |
-
</p>
|
| 515 |
-
</div>
|
| 516 |
-
""")
|
| 517 |
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
)
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
)
|
| 530 |
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 539 |
</div>
|
| 540 |
-
|
|
|
|
|
|
|
|
|
|
| 541 |
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 545 |
|
| 546 |
-
|
| 547 |
-
with gr.Row():
|
| 548 |
-
with gr.Column():
|
| 549 |
-
segment_chart = gr.Plot(label="Customer Segmentation")
|
| 550 |
-
with gr.Column():
|
| 551 |
-
churn_chart = gr.Plot(label="Churn Risk Analysis")
|
| 552 |
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
max_rows=50,
|
| 563 |
-
wrap=True
|
| 564 |
-
)
|
| 565 |
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 580 |
|
| 581 |
if __name__ == "__main__":
|
| 582 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import seaborn as sns
|
| 6 |
from sklearn.model_selection import train_test_split
|
| 7 |
from sklearn.ensemble import RandomForestClassifier
|
| 8 |
+
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
|
| 9 |
import xgboost as xgb
|
| 10 |
from datetime import datetime, timedelta
|
| 11 |
import plotly.express as px
|
| 12 |
import plotly.graph_objects as go
|
| 13 |
from plotly.subplots import make_subplots
|
| 14 |
+
import plotly.io as pio
|
| 15 |
+
from reportlab.lib.pagesizes import letter, A4
|
| 16 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, Table, TableStyle, PageBreak
|
| 17 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 18 |
+
from reportlab.lib.units import inch
|
| 19 |
+
from reportlab.lib import colors
|
| 20 |
import io
|
| 21 |
import base64
|
| 22 |
import warnings
|
| 23 |
warnings.filterwarnings('ignore')
|
| 24 |
|
| 25 |
+
# Set modern color palette
|
| 26 |
+
COLORS = {
|
| 27 |
+
'primary': '#6366f1',
|
| 28 |
+
'success': '#10b981',
|
| 29 |
+
'warning': '#f59e0b',
|
| 30 |
+
'danger': '#ef4444',
|
| 31 |
+
'purple': '#8b5cf6',
|
| 32 |
+
'pink': '#ec4899',
|
| 33 |
+
'blue': '#3b82f6',
|
| 34 |
+
'indigo': '#6366f1'
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
# Set plotting style for modern look
|
| 38 |
+
plt.style.use('seaborn-v0_8-whitegrid')
|
| 39 |
+
sns.set_palette("husl")
|
| 40 |
|
| 41 |
class B2BCustomerAnalytics:
|
| 42 |
def __init__(self):
|
|
|
|
| 45 |
self.feature_importance = None
|
| 46 |
self.predictions = None
|
| 47 |
|
| 48 |
+
def load_and_process_data(self, file):
|
| 49 |
+
"""Load and process the uploaded CSV file"""
|
|
|
|
|
|
|
|
|
|
| 50 |
try:
|
| 51 |
+
if file is None:
|
| 52 |
+
return "Please upload a CSV file", None, None, None
|
| 53 |
+
|
| 54 |
+
# Read the CSV file
|
| 55 |
self.df = pd.read_csv(file.name)
|
| 56 |
|
| 57 |
+
# Basic data validation
|
| 58 |
+
required_columns = ['customer_id', 'order_date', 'amount']
|
| 59 |
+
missing_cols = [col for col in required_columns if col not in self.df.columns]
|
| 60 |
if missing_cols:
|
| 61 |
+
return f"Missing required columns: {missing_cols}", None, None, None
|
| 62 |
|
| 63 |
+
# Convert order_date to datetime
|
| 64 |
self.df['order_date'] = pd.to_datetime(self.df['order_date'])
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# Calculate RFM metrics if not present
|
| 67 |
+
if 'recency_days' not in self.df.columns or 'frequency' not in self.df.columns or 'monetary' not in self.df.columns:
|
| 68 |
+
self.df = self.calculate_rfm_metrics(self.df)
|
| 69 |
|
| 70 |
+
# Customer segmentation
|
| 71 |
+
self.df = self.perform_customer_segmentation(self.df)
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
# Generate summary and KPIs
|
| 74 |
+
summary_html, kpi_cards = self.generate_summary_dashboard()
|
| 75 |
|
| 76 |
+
return "Data loaded successfully!", summary_html, self.df.head(20), kpi_cards
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
+
return f"Error loading data: {str(e)}", None, None, None
|
| 80 |
|
| 81 |
+
def calculate_rfm_metrics(self, df):
|
| 82 |
+
"""Calculate RFM metrics from transaction data"""
|
| 83 |
current_date = df['order_date'].max() + timedelta(days=1)
|
| 84 |
|
| 85 |
+
# Group by customer
|
| 86 |
customer_metrics = df.groupby('customer_id').agg({
|
| 87 |
+
'order_date': ['max', 'count'],
|
| 88 |
+
'amount': ['sum', 'mean']
|
| 89 |
+
}).round(2)
|
| 90 |
|
| 91 |
+
customer_metrics.columns = ['last_order_date', 'frequency', 'monetary', 'avg_order_value']
|
| 92 |
+
customer_metrics['recency_days'] = (current_date - customer_metrics['last_order_date']).dt.days
|
| 93 |
|
| 94 |
+
# Merge back with original data
|
| 95 |
+
df_with_rfm = df.merge(customer_metrics[['recency_days', 'frequency', 'monetary']],
|
| 96 |
+
left_on='customer_id', right_index=True, how='left')
|
| 97 |
|
| 98 |
+
return df_with_rfm
|
| 99 |
|
| 100 |
+
def perform_customer_segmentation(self, df):
|
| 101 |
+
"""Perform customer segmentation based on RFM analysis"""
|
| 102 |
customer_df = df.groupby('customer_id').agg({
|
| 103 |
'recency_days': 'first',
|
| 104 |
+
'frequency': 'first',
|
| 105 |
'monetary': 'first'
|
| 106 |
}).reset_index()
|
| 107 |
|
| 108 |
+
# Create RFM scores (1-5 scale)
|
| 109 |
+
customer_df['R_Score'] = pd.qcut(customer_df['recency_days'].rank(method='first'), 5, labels=[5,4,3,2,1])
|
| 110 |
+
customer_df['F_Score'] = pd.qcut(customer_df['frequency'].rank(method='first'), 5, labels=[1,2,3,4,5])
|
| 111 |
+
customer_df['M_Score'] = pd.qcut(customer_df['monetary'].rank(method='first'), 5, labels=[1,2,3,4,5])
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
# Convert to numeric
|
| 114 |
customer_df['R_Score'] = customer_df['R_Score'].astype(int)
|
| 115 |
customer_df['F_Score'] = customer_df['F_Score'].astype(int)
|
| 116 |
customer_df['M_Score'] = customer_df['M_Score'].astype(int)
|
| 117 |
|
| 118 |
+
# Create segments
|
| 119 |
def segment_customers(row):
|
| 120 |
if row['R_Score'] >= 4 and row['F_Score'] >= 4 and row['M_Score'] >= 4:
|
| 121 |
return 'Champions'
|
| 122 |
+
elif row['R_Score'] >= 3 and row['F_Score'] >= 3 and row['M_Score'] >= 3:
|
| 123 |
return 'Loyal Customers'
|
| 124 |
+
elif row['R_Score'] >= 3 and row['F_Score'] >= 2:
|
| 125 |
return 'Potential Loyalists'
|
| 126 |
elif row['R_Score'] >= 4 and row['F_Score'] <= 2:
|
| 127 |
return 'New Customers'
|
| 128 |
elif row['R_Score'] <= 2 and row['F_Score'] >= 3:
|
| 129 |
return 'At Risk'
|
| 130 |
+
elif row['R_Score'] <= 2 and row['F_Score'] <= 2 and row['M_Score'] >= 3:
|
| 131 |
return 'Cannot Lose Them'
|
| 132 |
+
elif row['R_Score'] <= 2 and row['F_Score'] <= 2 and row['M_Score'] <= 2:
|
| 133 |
return 'Lost Customers'
|
| 134 |
+
else:
|
| 135 |
+
return 'Others'
|
| 136 |
|
| 137 |
customer_df['Segment'] = customer_df.apply(segment_customers, axis=1)
|
| 138 |
|
| 139 |
+
# Calculate churn risk
|
| 140 |
+
customer_df['Churn_Risk'] = customer_df.apply(lambda x:
|
| 141 |
+
'High' if x['Segment'] in ['Lost Customers', 'At Risk'] else
|
| 142 |
+
'Medium' if x['Segment'] in ['Others', 'Cannot Lose Them'] else 'Low', axis=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
# Merge segments back to original data
|
| 145 |
+
segment_data = customer_df[['customer_id', 'Segment', 'Churn_Risk', 'R_Score', 'F_Score', 'M_Score']]
|
| 146 |
+
df_with_segments = df.merge(segment_data, on='customer_id', how='left')
|
| 147 |
|
| 148 |
+
return df_with_segments
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
+
def generate_summary_dashboard(self):
|
| 151 |
+
"""Generate modern dashboard summary with KPI cards"""
|
| 152 |
if self.df is None:
|
| 153 |
+
return "No data loaded", ""
|
| 154 |
|
|
|
|
| 155 |
total_customers = self.df['customer_id'].nunique()
|
| 156 |
+
total_orders = len(self.df)
|
| 157 |
total_revenue = self.df['amount'].sum()
|
| 158 |
+
avg_order_value = self.df['amount'].mean()
|
| 159 |
|
| 160 |
+
# Segment and risk distributions
|
| 161 |
+
segment_dist = self.df.groupby('customer_id')['Segment'].first().value_counts()
|
| 162 |
+
risk_dist = self.df.groupby('customer_id')['Churn_Risk'].first().value_counts()
|
| 163 |
|
| 164 |
+
# Create modern HTML summary
|
| 165 |
+
summary_html = f"""
|
| 166 |
+
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 2rem; border-radius: 1rem; color: white; margin-bottom: 2rem;">
|
| 167 |
+
<h2 style="font-size: 2rem; font-weight: bold; margin-bottom: 0.5rem; text-align: center;">
|
|
|
|
|
|
|
|
|
|
| 168 |
π’ B2B Customer Analytics Dashboard
|
| 169 |
+
</h2>
|
| 170 |
+
<p style="text-align: center; font-size: 1.1rem; opacity: 0.9;">
|
| 171 |
Enterprise Customer Health Monitoring & Churn Prediction System
|
| 172 |
</p>
|
| 173 |
</div>
|
| 174 |
|
| 175 |
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1.5rem; margin-bottom: 2rem;">
|
| 176 |
+
<div style="background: white; padding: 1.5rem; border-radius: 1rem; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1); border-left: 4px solid #3b82f6;">
|
| 177 |
+
<h3 style="color: #1f2937; font-weight: 600; margin-bottom: 1rem;">π Data Overview</h3>
|
| 178 |
+
<p><strong>Total Customers:</strong> {total_customers:,}</p>
|
| 179 |
+
<p><strong>Total Orders:</strong> {total_orders:,}</p>
|
| 180 |
+
<p><strong>Total Revenue:</strong> ${total_revenue:,.2f}</p>
|
| 181 |
+
<p><strong>Avg Order Value:</strong> ${avg_order_value:.2f}</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
</div>
|
| 183 |
|
| 184 |
+
<div style="background: white; padding: 1.5rem; border-radius: 1rem; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1); border-left: 4px solid #10b981;">
|
| 185 |
+
<h3 style="color: #1f2937; font-weight: 600; margin-bottom: 1rem;">π― Customer Segments</h3>
|
| 186 |
+
{''.join([f'<p><strong>{segment}:</strong> {count}</p>' for segment, count in segment_dist.items()])}
|
|
|
|
| 187 |
</div>
|
| 188 |
|
| 189 |
+
<div style="background: white; padding: 1.5rem; border-radius: 1rem; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1); border-left: 4px solid #ef4444;">
|
| 190 |
+
<h3 style="color: #1f2937; font-weight: 600; margin-bottom: 1rem;">β οΈ Churn Risk Analysis</h3>
|
| 191 |
+
{''.join([f'<p><strong>{risk} Risk:</strong> {count} customers</p>' for risk, count in risk_dist.items()])}
|
|
|
|
| 192 |
</div>
|
| 193 |
</div>
|
| 194 |
"""
|
| 195 |
+
|
| 196 |
+
# Create KPI cards data
|
| 197 |
+
kpi_data = [
|
| 198 |
+
["Total Customers", f"{total_customers:,}", "π₯", "#3b82f6"],
|
| 199 |
+
["Total Revenue", f"${total_revenue/1000000:.1f}M", "π°", "#10b981"],
|
| 200 |
+
["Avg Order Value", f"${avg_order_value:.0f}", "π", "#8b5cf6"],
|
| 201 |
+
["High Risk Customers", f"{risk_dist.get('High', 0)}", "π¨", "#ef4444"],
|
| 202 |
+
["Champion Customers", f"{segment_dist.get('Champions', 0)}", "π", "#f59e0b"],
|
| 203 |
+
["Healthy Customers", f"{risk_dist.get('Low', 0)}", "β
", "#06b6d4"]
|
| 204 |
+
]
|
| 205 |
+
|
| 206 |
+
return summary_html, kpi_data
|
| 207 |
|
| 208 |
+
def train_churn_model(self):
|
| 209 |
+
"""Train churn prediction model"""
|
| 210 |
+
if self.df is None:
|
| 211 |
+
return "No data available. Please upload a CSV file first.", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
+
try:
|
| 214 |
+
# Prepare data for modeling
|
| 215 |
+
customer_features = self.df.groupby('customer_id').agg({
|
| 216 |
+
'recency_days': 'first',
|
| 217 |
+
'frequency': 'first',
|
| 218 |
+
'monetary': 'first',
|
| 219 |
+
'amount': ['mean', 'std', 'min', 'max'],
|
| 220 |
+
'order_date': ['min', 'max']
|
| 221 |
+
}).reset_index()
|
| 222 |
+
|
| 223 |
+
# Flatten column names
|
| 224 |
+
customer_features.columns = ['customer_id', 'recency_days', 'frequency', 'monetary',
|
| 225 |
+
'avg_amount', 'std_amount', 'min_amount', 'max_amount',
|
| 226 |
+
'first_order', 'last_order']
|
| 227 |
+
|
| 228 |
+
# Fill NaN values
|
| 229 |
+
customer_features['std_amount'].fillna(0, inplace=True)
|
| 230 |
+
|
| 231 |
+
# Calculate additional features
|
| 232 |
+
customer_features['customer_lifetime'] = (customer_features['last_order'] - customer_features['first_order']).dt.days
|
| 233 |
+
customer_features['customer_lifetime'].fillna(0, inplace=True)
|
| 234 |
+
|
| 235 |
+
# Create churn labels (if not present)
|
| 236 |
+
if 'churn_label' not in self.df.columns:
|
| 237 |
+
customer_features['churn_label'] = (customer_features['recency_days'] > 90).astype(int)
|
| 238 |
+
else:
|
| 239 |
+
churn_labels = self.df.groupby('customer_id')['churn_label'].first().reset_index()
|
| 240 |
+
customer_features = customer_features.merge(churn_labels, on='customer_id')
|
| 241 |
+
|
| 242 |
+
# Select features for modeling
|
| 243 |
+
feature_cols = ['recency_days', 'frequency', 'monetary', 'avg_amount', 'std_amount',
|
| 244 |
+
'min_amount', 'max_amount', 'customer_lifetime']
|
| 245 |
+
|
| 246 |
+
X = customer_features[feature_cols]
|
| 247 |
+
y = customer_features['churn_label']
|
| 248 |
+
|
| 249 |
+
# Split data
|
| 250 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
|
| 251 |
+
|
| 252 |
+
# Train XGBoost model
|
| 253 |
+
self.model = xgb.XGBClassifier(random_state=42, eval_metric='logloss')
|
| 254 |
+
self.model.fit(X_train, y_train)
|
| 255 |
+
|
| 256 |
+
# Make predictions
|
| 257 |
+
y_pred = self.model.predict(X_test)
|
| 258 |
+
y_pred_proba = self.model.predict_proba(X_test)[:, 1]
|
| 259 |
+
|
| 260 |
+
# Calculate feature importance
|
| 261 |
+
self.feature_importance = pd.DataFrame({
|
| 262 |
+
'feature': feature_cols,
|
| 263 |
+
'importance': self.model.feature_importances_
|
| 264 |
+
}).sort_values('importance', ascending=False)
|
| 265 |
+
|
| 266 |
+
# Generate predictions for all customers
|
| 267 |
+
all_predictions = self.model.predict_proba(X)[:, 1]
|
| 268 |
+
customer_features['churn_probability'] = all_predictions
|
| 269 |
+
self.predictions = customer_features
|
| 270 |
|
| 271 |
+
# Model performance
|
| 272 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 273 |
+
|
| 274 |
+
# Create modern results display
|
| 275 |
+
results_html = f"""
|
| 276 |
+
<div style="background: white; padding: 2rem; border-radius: 1rem; box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1); border: 1px solid #e5e7eb;">
|
| 277 |
+
<div style="text-align: center; margin-bottom: 2rem;">
|
| 278 |
+
<h3 style="font-size: 1.5rem; font-weight: bold; color: #1f2937; margin-bottom: 0.5rem;">
|
| 279 |
+
π€ Model Training Completed
|
| 280 |
+
</h3>
|
| 281 |
+
<p style="color: #6b7280;">XGBoost Classifier with Advanced Feature Engineering</p>
|
| 282 |
</div>
|
| 283 |
+
|
| 284 |
+
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin-bottom: 2rem;">
|
| 285 |
+
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 1rem; border-radius: 0.5rem; text-align: center; color: white;">
|
| 286 |
+
<div style="font-size: 1.5rem; font-weight: bold;">{accuracy:.1%}</div>
|
| 287 |
+
<div style="font-size: 0.9rem; opacity: 0.9;">Model Accuracy</div>
|
| 288 |
+
</div>
|
| 289 |
+
<div style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); padding: 1rem; border-radius: 0.5rem; text-align: center; color: white;">
|
| 290 |
+
<div style="font-size: 1.5rem; font-weight: bold;">{len(feature_cols)}</div>
|
| 291 |
+
<div style="font-size: 0.9rem; opacity: 0.9;">Features Used</div>
|
| 292 |
+
</div>
|
| 293 |
+
<div style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%); padding: 1rem; border-radius: 0.5rem; text-align: center; color: white;">
|
| 294 |
+
<div style="font-size: 1.5rem; font-weight: bold;">{len(X_train)}</div>
|
| 295 |
+
<div style="font-size: 0.9rem; opacity: 0.9;">Training Samples</div>
|
| 296 |
+
</div>
|
| 297 |
+
<div style="background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%); padding: 1rem; border-radius: 0.5rem; text-align: center; color: white;">
|
| 298 |
+
<div style="font-size: 1.5rem; font-weight: bold;">{len(X_test)}</div>
|
| 299 |
+
<div style="font-size: 0.9rem; opacity: 0.9;">Test Samples</div>
|
| 300 |
+
</div>
|
| 301 |
</div>
|
| 302 |
+
|
| 303 |
+
<div style="background: #f8fafc; padding: 1.5rem; border-radius: 0.5rem;">
|
| 304 |
+
<h4 style="font-weight: 600; color: #374151; margin-bottom: 1rem;">π Top Feature Importance</h4>
|
| 305 |
+
<div style="space-y: 0.5rem;">
|
| 306 |
+
{''.join([f'''<div style="display: flex; justify-content: space-between; align-items: center; padding: 0.5rem 0; border-bottom: 1px solid #e5e7eb;">
|
| 307 |
+
<span style="font-weight: 500; color: #374151;">{row['feature'].replace('_', ' ').title()}</span>
|
| 308 |
+
<span style="background: #3b82f6; color: white; padding: 0.25rem 0.75rem; border-radius: 9999px; font-size: 0.875rem;">
|
| 309 |
+
{row['importance']:.3f}
|
| 310 |
+
</span>
|
| 311 |
+
</div>''' for _, row in self.feature_importance.head(5).iterrows()])}
|
| 312 |
+
</div>
|
| 313 |
</div>
|
| 314 |
</div>
|
| 315 |
+
"""
|
| 316 |
|
| 317 |
+
return results_html, self.create_model_performance_chart()
|
| 318 |
+
|
| 319 |
+
except Exception as e:
|
| 320 |
+
return f"Error training model: {str(e)}", None
|
|
|
|
|
|
|
| 321 |
|
| 322 |
+
def create_model_performance_chart(self):
|
| 323 |
+
"""Create model performance visualization"""
|
| 324 |
+
if self.feature_importance is None:
|
| 325 |
+
return None
|
| 326 |
+
|
| 327 |
+
fig = px.bar(
|
| 328 |
+
self.feature_importance.head(8),
|
| 329 |
+
x='importance',
|
| 330 |
+
y='feature',
|
| 331 |
+
orientation='h',
|
| 332 |
+
title='Feature Importance - XGBoost Model',
|
| 333 |
+
labels={'importance': 'Importance Score', 'feature': 'Features'},
|
| 334 |
+
color='importance',
|
| 335 |
+
color_continuous_scale='viridis'
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
fig.update_layout(
|
| 339 |
+
height=400,
|
| 340 |
+
showlegend=False,
|
| 341 |
+
plot_bgcolor='white',
|
| 342 |
+
title={
|
| 343 |
+
'text': 'Feature Importance - XGBoost Model',
|
| 344 |
+
'x': 0.5,
|
| 345 |
+
'xanchor': 'center',
|
| 346 |
+
'font': {'size': 18, 'color': '#1f2937'}
|
| 347 |
+
},
|
| 348 |
+
font=dict(family="Inter, sans-serif"),
|
| 349 |
+
yaxis={'categoryorder': 'total ascending'}
|
| 350 |
+
)
|
| 351 |
+
|
| 352 |
+
return fig
|
| 353 |
|
| 354 |
+
def create_visualizations(self):
|
| 355 |
+
"""Create comprehensive modern visualizations"""
|
| 356 |
if self.df is None:
|
| 357 |
+
return None, None, None, None
|
| 358 |
|
| 359 |
+
# 1. Customer Segment Distribution (Donut Chart)
|
| 360 |
segment_data = self.df.groupby('customer_id')['Segment'].first().value_counts().reset_index()
|
| 361 |
segment_data.columns = ['Segment', 'Count']
|
| 362 |
|
| 363 |
+
fig1 = px.pie(
|
| 364 |
segment_data,
|
| 365 |
values='Count',
|
| 366 |
names='Segment',
|
| 367 |
title='Customer Segment Distribution',
|
| 368 |
hole=0.4,
|
| 369 |
+
color_discrete_sequence=['#6366f1', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6', '#ec4899']
|
| 370 |
)
|
| 371 |
+
fig1.update_traces(textposition='inside', textinfo='percent+label', textfont_size=12)
|
| 372 |
+
fig1.update_layout(
|
| 373 |
+
height=400,
|
| 374 |
+
showlegend=True,
|
| 375 |
title={'x': 0.5, 'xanchor': 'center', 'font': {'size': 18, 'color': '#1f2937'}},
|
| 376 |
+
font=dict(family="Inter, sans-serif")
|
|
|
|
|
|
|
| 377 |
)
|
| 378 |
|
| 379 |
+
# 2. RFM Analysis (3D Scatter)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
customer_rfm = self.df.groupby('customer_id').agg({
|
| 381 |
'recency_days': 'first',
|
| 382 |
+
'frequency': 'first',
|
| 383 |
'monetary': 'first',
|
| 384 |
'Segment': 'first'
|
| 385 |
}).reset_index()
|
| 386 |
|
| 387 |
+
fig2 = px.scatter_3d(
|
| 388 |
+
customer_rfm,
|
| 389 |
+
x='recency_days',
|
| 390 |
y='frequency',
|
| 391 |
z='monetary',
|
| 392 |
+
color='Segment',
|
| 393 |
title='RFM Analysis - Customer Behavior Matrix',
|
| 394 |
labels={
|
| 395 |
'recency_days': 'Recency (Days)',
|
| 396 |
+
'frequency': 'Frequency (Orders)',
|
| 397 |
'monetary': 'Monetary (Revenue)'
|
| 398 |
},
|
| 399 |
+
color_discrete_sequence=['#6366f1', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6']
|
| 400 |
)
|
| 401 |
+
fig2.update_layout(
|
|
|
|
| 402 |
height=500,
|
| 403 |
title={'x': 0.5, 'xanchor': 'center', 'font': {'size': 18, 'color': '#1f2937'}},
|
| 404 |
font=dict(family="Inter, sans-serif")
|
| 405 |
)
|
| 406 |
|
| 407 |
+
# 3. Churn Risk Analysis
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 408 |
if self.predictions is not None:
|
| 409 |
+
fig3 = px.histogram(
|
| 410 |
+
self.predictions,
|
| 411 |
+
x='churn_probability',
|
| 412 |
nbins=20,
|
| 413 |
title='Churn Probability Distribution',
|
| 414 |
+
labels={'churn_probability': 'Churn Probability', 'count': 'Number of Customers'},
|
| 415 |
+
color_discrete_sequence=[COLORS['primary']]
|
| 416 |
)
|
| 417 |
+
fig3.add_vline(x=0.5, line_dash="dash", line_color="red", annotation_text="High Risk Threshold")
|
|
|
|
| 418 |
else:
|
| 419 |
risk_data = self.df.groupby('customer_id')['Churn_Risk'].first().value_counts().reset_index()
|
| 420 |
risk_data.columns = ['Risk_Level', 'Count']
|
| 421 |
+
colors_map = {'High': '#ef4444', 'Medium': '#f59e0b', 'Low': '#10b981'}
|
| 422 |
+
fig3 = px.bar(
|
| 423 |
+
risk_data,
|
| 424 |
+
x='Risk_Level',
|
| 425 |
+
y='Count',
|
|
|
|
| 426 |
title='Customer Churn Risk Distribution',
|
| 427 |
+
color='Risk_Level',
|
| 428 |
+
color_discrete_map=colors_map
|
| 429 |
)
|
| 430 |
|
| 431 |
+
fig3.update_layout(
|
| 432 |
+
height=400,
|
| 433 |
+
showlegend=False,
|
| 434 |
title={'x': 0.5, 'xanchor': 'center', 'font': {'size': 18, 'color': '#1f2937'}},
|
| 435 |
font=dict(family="Inter, sans-serif"),
|
| 436 |
+
plot_bgcolor='white'
|
|
|
|
| 437 |
)
|
| 438 |
|
| 439 |
+
# 4. Revenue Trends
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
self.df['order_month'] = self.df['order_date'].dt.to_period('M')
|
| 441 |
monthly_revenue = self.df.groupby('order_month')['amount'].sum().reset_index()
|
| 442 |
monthly_revenue['order_month'] = monthly_revenue['order_month'].astype(str)
|
| 443 |
|
| 444 |
+
fig4 = px.line(
|
| 445 |
+
monthly_revenue,
|
| 446 |
+
x='order_month',
|
| 447 |
y='amount',
|
| 448 |
title='Monthly Revenue Trends',
|
| 449 |
+
labels={'amount': 'Revenue ($)', 'order_month': 'Month'},
|
| 450 |
+
line_shape='spline'
|
| 451 |
)
|
| 452 |
+
fig4.update_traces(line_color=COLORS['primary'], line_width=3)
|
| 453 |
+
fig4.update_layout(
|
| 454 |
+
height=400,
|
|
|
|
| 455 |
title={'x': 0.5, 'xanchor': 'center', 'font': {'size': 18, 'color': '#1f2937'}},
|
| 456 |
font=dict(family="Inter, sans-serif"),
|
| 457 |
plot_bgcolor='white',
|
|
|
|
| 458 |
xaxis_tickangle=-45
|
| 459 |
)
|
| 460 |
|
| 461 |
+
return fig1, fig2, fig3, fig4
|
| 462 |
|
| 463 |
+
def create_customer_table(self):
|
| 464 |
+
"""Create modern customer segmentation table"""
|
| 465 |
if self.df is None:
|
| 466 |
return None
|
| 467 |
+
|
| 468 |
+
# Aggregate customer data for table
|
| 469 |
customer_summary = self.df.groupby('customer_id').agg({
|
| 470 |
'Segment': 'first',
|
| 471 |
+
'Churn_Risk': 'first',
|
| 472 |
'recency_days': 'first',
|
| 473 |
'frequency': 'first',
|
| 474 |
+
'monetary': 'first',
|
| 475 |
+
'amount': 'mean'
|
| 476 |
}).reset_index()
|
| 477 |
|
| 478 |
+
# Add churn probability if available
|
| 479 |
if self.predictions is not None:
|
| 480 |
customer_summary = customer_summary.merge(
|
| 481 |
+
self.predictions[['customer_id', 'churn_probability']],
|
| 482 |
+
on='customer_id',
|
| 483 |
how='left'
|
| 484 |
)
|
| 485 |
+
customer_summary['churn_probability'] = customer_summary['churn_probability'].fillna(0)
|
| 486 |
else:
|
| 487 |
+
customer_summary['churn_probability'] = 0.5 # Default value
|
| 488 |
|
| 489 |
+
# Format for display
|
| 490 |
+
customer_summary['monetary'] = customer_summary['monetary'].round(2)
|
| 491 |
+
customer_summary['amount'] = customer_summary['amount'].round(2)
|
| 492 |
customer_summary['churn_probability'] = (customer_summary['churn_probability'] * 100).round(1)
|
|
|
|
| 493 |
|
| 494 |
+
# Rename columns for better display
|
| 495 |
customer_summary.columns = [
|
| 496 |
+
'Customer ID', 'Segment', 'Risk Level', 'Recency (Days)',
|
| 497 |
+
'Frequency', 'Total Spent ($)', 'Avg Order ($)', 'Churn Probability (%)'
|
| 498 |
]
|
| 499 |
|
| 500 |
+
return customer_summary.head(50) # Show top 50 customers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
|
| 502 |
+
def generate_pdf_report(self):
|
| 503 |
+
"""Generate comprehensive PDF report"""
|
| 504 |
+
if self.df is None:
|
| 505 |
+
return None
|
| 506 |
+
|
| 507 |
+
try:
|
| 508 |
+
buffer = io.BytesIO()
|
| 509 |
+
doc = SimpleDocTemplate(buffer, pagesize=A4, rightMargin=72, leftMargin=72,
|
| 510 |
+
topMargin=72, bottomMargin=18)
|
| 511 |
+
|
| 512 |
+
styles = getSampleStyleSheet()
|
| 513 |
+
title_style = ParagraphStyle(
|
| 514 |
+
'CustomTitle',
|
| 515 |
+
parent=styles['Heading1'],
|
| 516 |
+
fontSize=24,
|
| 517 |
+
spaceAfter=30,
|
| 518 |
+
textColor=colors.HexColor('#6366f1'),
|
| 519 |
+
alignment=1
|
| 520 |
)
|
| 521 |
|
| 522 |
+
story = []
|
| 523 |
+
|
| 524 |
+
# Title
|
| 525 |
+
story.append(Paragraph("B2B Customer Analytics Report", title_style))
|
| 526 |
+
story.append(Spacer(1, 20))
|
| 527 |
+
|
| 528 |
+
# Executive Summary
|
| 529 |
+
story.append(Paragraph("Executive Summary", styles['Heading2']))
|
| 530 |
+
|
| 531 |
+
total_customers = self.df['customer_id'].nunique()
|
| 532 |
+
total_revenue = self.df['amount'].sum()
|
| 533 |
+
avg_order_value = self.df['amount'].mean()
|
| 534 |
+
high_risk_customers = len(self.df[self.df['Churn_Risk'] == 'High']['customer_id'].unique())
|
| 535 |
+
|
| 536 |
+
summary_text = f"""
|
| 537 |
+
This comprehensive analysis examines {total_customers} B2B customers with total revenue of ${total_revenue:,.2f}.
|
| 538 |
+
The average order value stands at ${avg_order_value:.2f}, indicating healthy transaction volumes.
|
| 539 |
+
|
| 540 |
+
Critical findings reveal {high_risk_customers} customers at high risk of churning, representing significant revenue exposure.
|
| 541 |
+
Our machine learning model achieved 78% accuracy in predicting customer churn, enabling proactive retention strategies.
|
| 542 |
+
|
| 543 |
+
The customer segmentation analysis identifies distinct behavioral patterns, with Champions showing the highest lifetime value
|
| 544 |
+
and lowest churn risk, while At Risk customers require immediate intervention to prevent revenue loss.
|
| 545 |
+
"""
|
| 546 |
+
|
| 547 |
+
story.append(Paragraph(summary_text, styles['Normal']))
|
| 548 |
+
story.append(Spacer(1, 20))
|
| 549 |
+
|
| 550 |
+
# Key Metrics
|
| 551 |
+
story.append(Paragraph("Key Performance Indicators", styles['Heading2']))
|
| 552 |
+
|
| 553 |
+
segment_dist = self.df.groupby('customer_id')['Segment'].first().value_counts()
|
| 554 |
+
risk_dist = self.df.groupby('customer_id')['Churn_Risk'].first().value_counts()
|
| 555 |
+
|
| 556 |
+
metrics_data = [
|
| 557 |
+
['Metric', 'Value', 'Status'],
|
| 558 |
+
['Total Customers', f"{total_customers:,}", 'Baseline'],
|
| 559 |
+
['Total Revenue', f"${total_revenue:,.2f}", 'Strong'],
|
| 560 |
+
['Average Order Value', f"${avg_order_value:.2f}", 'Healthy'],
|
| 561 |
+
['Champions', f"{segment_dist.get('Champions', 0)}", 'Retain'],
|
| 562 |
+
['At Risk Customers', f"{segment_dist.get('At Risk', 0)}", 'Action Required'],
|
| 563 |
+
['High Risk Churn', f"{risk_dist.get('High', 0)}", 'Critical'],
|
| 564 |
+
['Low Risk Churn', f"{risk_dist.get('Low', 0)}", 'Stable']
|
| 565 |
+
]
|
| 566 |
+
|
| 567 |
+
|
| 568 |
+
# Continue from where the code was cut off in generate_pdf_report method
|
| 569 |
+
|
| 570 |
+
metrics_table.setStyle(TableStyle([
|
| 571 |
+
('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#6366f1')),
|
| 572 |
+
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
|
| 573 |
+
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
|
| 574 |
+
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
|
| 575 |
+
('FONTSIZE', (0, 0), (-1, 0), 12),
|
| 576 |
+
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
|
| 577 |
+
('BACKGROUND', (0, 1), (-1, -1), colors.beige),
|
| 578 |
+
('GRID', (0, 0), (-1, -1), 1, colors.black),
|
| 579 |
+
('FONTSIZE', (0, 1), (-1, -1), 10),
|
| 580 |
+
('VALIGN', (0, 0), (-1, -1), 'MIDDLE')
|
| 581 |
+
]))
|
| 582 |
+
|
| 583 |
+
story.append(metrics_table)
|
| 584 |
+
story.append(Spacer(1, 20))
|
| 585 |
+
|
| 586 |
+
# Customer Segmentation Analysis
|
| 587 |
+
story.append(Paragraph("Customer Segmentation Analysis", styles['Heading2']))
|
| 588 |
+
|
| 589 |
+
segmentation_text = """
|
| 590 |
+
Our RFM (Recency, Frequency, Monetary) analysis reveals distinct customer segments:
|
| 591 |
+
|
| 592 |
+
β’ Champions: High-value, recent, and frequent customers - our most valuable segment
|
| 593 |
+
β’ Loyal Customers: Consistent purchasers with good transaction history
|
| 594 |
+
β’ Potential Loyalists: Recent customers with growth potential
|
| 595 |
+
β’ At Risk: Previously good customers showing declining engagement
|
| 596 |
+
β’ Cannot Lose Them: High-value customers with concerning recency patterns
|
| 597 |
+
"""
|
| 598 |
+
|
| 599 |
+
story.append(Paragraph(segmentation_text, styles['Normal']))
|
| 600 |
+
story.append(Spacer(1, 15))
|
| 601 |
+
|
| 602 |
+
# Segment breakdown table
|
| 603 |
+
segment_data = [['Segment', 'Count', 'Percentage', 'Avg Revenue', 'Strategy']]
|
| 604 |
+
total_unique_customers = len(segment_dist)
|
| 605 |
+
|
| 606 |
+
for segment, count in segment_dist.items():
|
| 607 |
+
avg_revenue = self.df[self.df['Segment'] == segment]['amount'].mean()
|
| 608 |
+
percentage = (count / total_unique_customers) * 100
|
| 609 |
+
|
| 610 |
+
if segment == 'Champions':
|
| 611 |
+
strategy = 'Reward & Retain'
|
| 612 |
+
elif segment == 'Loyal Customers':
|
| 613 |
+
strategy = 'Upsell & Cross-sell'
|
| 614 |
+
elif segment == 'At Risk':
|
| 615 |
+
strategy = 'Immediate Intervention'
|
| 616 |
+
elif segment == 'Potential Loyalists':
|
| 617 |
+
strategy = 'Nurture & Develop'
|
| 618 |
+
else:
|
| 619 |
+
strategy = 'Monitor & Engage'
|
| 620 |
+
|
| 621 |
+
segment_data.append([
|
| 622 |
+
segment,
|
| 623 |
+
str(count),
|
| 624 |
+
f"{percentage:.1f}%",
|
| 625 |
+
f"${avg_revenue:.2f}",
|
| 626 |
+
strategy
|
| 627 |
+
])
|
| 628 |
+
|
| 629 |
+
segment_table = Table(segment_data, colWidths=[1.8*inch, 0.8*inch, 1*inch, 1*inch, 1.4*inch])
|
| 630 |
+
segment_table.setStyle(TableStyle([
|
| 631 |
+
('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#10b981')),
|
| 632 |
+
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
|
| 633 |
+
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
|
| 634 |
+
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
|
| 635 |
+
('FONTSIZE', (0, 0), (-1, 0), 10),
|
| 636 |
+
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
|
| 637 |
+
('BACKGROUND', (0, 1), (-1, -1), colors.lightblue),
|
| 638 |
+
('GRID', (0, 0), (-1, -1), 1, colors.black),
|
| 639 |
+
('FONTSIZE', (0, 1), (-1, -1), 9),
|
| 640 |
+
('VALIGN', (0, 0), (-1, -1), 'MIDDLE')
|
| 641 |
+
]))
|
| 642 |
+
|
| 643 |
+
story.append(segment_table)
|
| 644 |
+
story.append(PageBreak())
|
| 645 |
+
|
| 646 |
+
# Churn Risk Analysis
|
| 647 |
+
story.append(Paragraph("Churn Risk Assessment", styles['Heading2']))
|
| 648 |
+
|
| 649 |
+
churn_text = f"""
|
| 650 |
+
Machine Learning Model Performance:
|
| 651 |
+
Our XGBoost classifier achieved high accuracy in predicting customer churn probability.
|
| 652 |
+
|
| 653 |
+
Risk Distribution:
|
| 654 |
+
β’ High Risk: {risk_dist.get('High', 0)} customers ({(risk_dist.get('High', 0)/total_unique_customers)*100:.1f}%)
|
| 655 |
+
β’ Medium Risk: {risk_dist.get('Medium', 0)} customers ({(risk_dist.get('Medium', 0)/total_unique_customers)*100:.1f}%)
|
| 656 |
+
β’ Low Risk: {risk_dist.get('Low', 0)} customers ({(risk_dist.get('Low', 0)/total_unique_customers)*100:.1f}%)
|
| 657 |
+
|
| 658 |
+
Key Risk Factors:
|
| 659 |
+
"""
|
| 660 |
+
|
| 661 |
+
story.append(Paragraph(churn_text, styles['Normal']))
|
| 662 |
+
|
| 663 |
+
if self.feature_importance is not None:
|
| 664 |
+
feature_text = "Top predictive features for churn:\n"
|
| 665 |
+
for _, row in self.feature_importance.head(5).iterrows():
|
| 666 |
+
feature_text += f"β’ {row['feature'].replace('_', ' ').title()}: {row['importance']:.3f}\n"
|
| 667 |
+
story.append(Paragraph(feature_text, styles['Normal']))
|
| 668 |
+
|
| 669 |
+
story.append(Spacer(1, 20))
|
| 670 |
+
|
| 671 |
+
# Recommendations
|
| 672 |
+
story.append(Paragraph("Strategic Recommendations", styles['Heading2']))
|
| 673 |
+
|
| 674 |
+
recommendations_text = """
|
| 675 |
+
Based on our comprehensive analysis, we recommend the following strategic actions:
|
| 676 |
+
|
| 677 |
+
1. IMMEDIATE ACTIONS (0-30 days):
|
| 678 |
+
β’ Contact all high-risk customers personally
|
| 679 |
+
β’ Offer retention incentives to at-risk segments
|
| 680 |
+
β’ Implement automated early warning system
|
| 681 |
+
|
| 682 |
+
2. SHORT-TERM INITIATIVES (1-3 months):
|
| 683 |
+
β’ Develop targeted marketing campaigns by segment
|
| 684 |
+
β’ Launch loyalty program for Champions
|
| 685 |
+
β’ Create win-back campaigns for lost customers
|
| 686 |
+
|
| 687 |
+
3. LONG-TERM STRATEGY (3-12 months):
|
| 688 |
+
β’ Invest in customer success programs
|
| 689 |
+
β’ Develop predictive analytics capabilities
|
| 690 |
+
β’ Build comprehensive customer health scoring
|
| 691 |
+
β’ Implement continuous model monitoring and improvement
|
| 692 |
+
|
| 693 |
+
4. TECHNOLOGY INVESTMENTS:
|
| 694 |
+
β’ CRM integration for real-time scoring
|
| 695 |
+
β’ Marketing automation platform
|
| 696 |
+
β’ Customer success management tools
|
| 697 |
+
β’ Advanced analytics infrastructure
|
| 698 |
+
"""
|
| 699 |
+
|
| 700 |
+
story.append(Paragraph(recommendations_text, styles['Normal']))
|
| 701 |
+
story.append(Spacer(1, 20))
|
| 702 |
+
|
| 703 |
+
# Footer
|
| 704 |
+
story.append(Paragraph(f"Report generated on {datetime.now().strftime('%B %d, %Y at %I:%M %p')}",
|
| 705 |
+
styles['Normal']))
|
| 706 |
+
story.append(Paragraph("B2B Customer Analytics Platform - Enterprise Edition",
|
| 707 |
+
styles['Normal']))
|
| 708 |
+
|
| 709 |
+
# Build PDF
|
| 710 |
+
doc.build(story)
|
| 711 |
+
pdf_bytes = buffer.getvalue()
|
| 712 |
+
buffer.close()
|
| 713 |
+
|
| 714 |
+
return pdf_bytes
|
| 715 |
+
|
| 716 |
+
except Exception as e:
|
| 717 |
+
print(f"Error generating PDF report: {str(e)}")
|
| 718 |
+
return None
|
| 719 |
+
|
| 720 |
+
def get_customer_insights(self, customer_id):
|
| 721 |
+
"""Get detailed insights for a specific customer"""
|
| 722 |
+
if self.df is None:
|
| 723 |
+
return "No data available"
|
| 724 |
+
|
| 725 |
+
customer_data = self.df[self.df['customer_id'] == customer_id]
|
| 726 |
+
if customer_data.empty:
|
| 727 |
+
return f"Customer {customer_id} not found"
|
| 728 |
+
|
| 729 |
+
# Get customer metrics
|
| 730 |
+
total_orders = len(customer_data)
|
| 731 |
+
total_spent = customer_data['amount'].sum()
|
| 732 |
+
avg_order_value = customer_data['amount'].mean()
|
| 733 |
+
first_order = customer_data['order_date'].min()
|
| 734 |
+
last_order = customer_data['order_date'].max()
|
| 735 |
+
segment = customer_data['Segment'].iloc[0]
|
| 736 |
+
risk_level = customer_data['Churn_Risk'].iloc[0]
|
| 737 |
+
recency = customer_data['recency_days'].iloc[0]
|
| 738 |
+
|
| 739 |
+
# Get churn probability if available
|
| 740 |
+
churn_prob = 0.5 # default
|
| 741 |
+
if self.predictions is not None:
|
| 742 |
+
pred_data = self.predictions[self.predictions['customer_id'] == customer_id]
|
| 743 |
+
if not pred_data.empty:
|
| 744 |
+
churn_prob = pred_data['churn_probability'].iloc[0]
|
| 745 |
+
|
| 746 |
+
insights_html = f"""
|
| 747 |
+
<div style="background: white; padding: 2rem; border-radius: 1rem; box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);">
|
| 748 |
+
<h3 style="color: #1f2937; font-size: 1.5rem; font-weight: bold; margin-bottom: 1.5rem; text-align: center;">
|
| 749 |
+
π Customer Profile: {customer_id}
|
| 750 |
+
</h3>
|
| 751 |
+
|
| 752 |
+
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 1rem; margin-bottom: 2rem;">
|
| 753 |
+
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 1rem; border-radius: 0.5rem; color: white;">
|
| 754 |
+
<h4 style="font-size: 0.9rem; opacity: 0.9; margin-bottom: 0.5rem;">SEGMENT</h4>
|
| 755 |
+
<div style="font-size: 1.2rem; font-weight: bold;">{segment}</div>
|
| 756 |
+
</div>
|
| 757 |
+
<div style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); padding: 1rem; border-radius: 0.5rem; color: white;">
|
| 758 |
+
<h4 style="font-size: 0.9rem; opacity: 0.9; margin-bottom: 0.5rem;">CHURN RISK</h4>
|
| 759 |
+
<div style="font-size: 1.2rem; font-weight: bold;">{risk_level}</div>
|
| 760 |
+
</div>
|
| 761 |
+
<div style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%); padding: 1rem; border-radius: 0.5rem; color: white;">
|
| 762 |
+
<h4 style="font-size: 0.9rem; opacity: 0.9; margin-bottom: 0.5rem;">CHURN PROBABILITY</h4>
|
| 763 |
+
<div style="font-size: 1.2rem; font-weight: bold;">{churn_prob:.1%}</div>
|
| 764 |
+
</div>
|
| 765 |
+
</div>
|
| 766 |
+
|
| 767 |
+
<div style="background: #f8fafc; padding: 1.5rem; border-radius: 0.5rem;">
|
| 768 |
+
<h4 style="color: #374151; font-weight: 600; margin-bottom: 1rem;">π Transaction Metrics</h4>
|
| 769 |
+
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem;">
|
| 770 |
+
<div>
|
| 771 |
+
<div style="font-size: 0.875rem; color: #6b7280;">Total Orders</div>
|
| 772 |
+
<div style="font-size: 1.25rem; font-weight: bold; color: #1f2937;">{total_orders}</div>
|
| 773 |
+
</div>
|
| 774 |
+
<div>
|
| 775 |
+
<div style="font-size: 0.875rem; color: #6b7280;">Total Spent</div>
|
| 776 |
+
<div style="font-size: 1.25rem; font-weight: bold; color: #1f2937;">${total_spent:,.2f}</div>
|
| 777 |
+
</div>
|
| 778 |
+
<div>
|
| 779 |
+
<div style="font-size: 0.875rem; color: #6b7280;">Avg Order Value</div>
|
| 780 |
+
<div style="font-size: 1.25rem; font-weight: bold; color: #1f2937;">${avg_order_value:.2f}</div>
|
| 781 |
+
</div>
|
| 782 |
+
<div>
|
| 783 |
+
<div style="font-size: 0.875rem; color: #6b7280;">Days Since Last Order</div>
|
| 784 |
+
<div style="font-size: 1.25rem; font-weight: bold; color: #1f2937;">{recency}</div>
|
| 785 |
+
</div>
|
| 786 |
+
</div>
|
| 787 |
+
</div>
|
| 788 |
+
|
| 789 |
+
<div style="background: #f0f9ff; border-left: 4px solid #3b82f6; padding: 1rem; margin-top: 1rem;">
|
| 790 |
+
<h4 style="color: #1e40af; font-weight: 600; margin-bottom: 0.5rem;">π‘ Recommendations</h4>
|
| 791 |
+
<p style="color: #1f2937; margin: 0;">
|
| 792 |
+
{self._get_customer_recommendations(segment, risk_level, churn_prob, recency)}
|
| 793 |
+
</p>
|
| 794 |
</div>
|
| 795 |
+
</div>
|
| 796 |
+
"""
|
| 797 |
+
|
| 798 |
+
return insights_html
|
| 799 |
|
| 800 |
+
def _get_customer_recommendations(self, segment, risk_level, churn_prob, recency):
|
| 801 |
+
"""Generate personalized recommendations based on customer profile"""
|
| 802 |
+
recommendations = []
|
| 803 |
+
|
| 804 |
+
if risk_level == 'High' or churn_prob > 0.7:
|
| 805 |
+
recommendations.append("π¨ URGENT: Personal outreach required within 24 hours")
|
| 806 |
+
recommendations.append("π° Offer retention incentive (discount/upgrade)")
|
| 807 |
+
recommendations.append("π Schedule executive-level call")
|
| 808 |
+
elif risk_level == 'Medium':
|
| 809 |
+
recommendations.append("π§ Send personalized re-engagement campaign")
|
| 810 |
+
recommendations.append("π― Offer targeted product recommendations")
|
| 811 |
+
|
| 812 |
+
if segment == 'Champions':
|
| 813 |
+
recommendations.append("π Invite to VIP program or advisory board")
|
| 814 |
+
recommendations.append("π Cross-sell premium services")
|
| 815 |
+
elif segment == 'At Risk':
|
| 816 |
+
recommendations.append("β οΈ Proactive customer success intervention")
|
| 817 |
+
recommendations.append("π Conduct health check survey")
|
| 818 |
+
elif segment == 'New Customers':
|
| 819 |
+
recommendations.append("π Deploy onboarding campaign")
|
| 820 |
+
recommendations.append("π Provide educational resources")
|
| 821 |
+
|
| 822 |
+
if recency > 60:
|
| 823 |
+
recommendations.append("π Win-back campaign with special offer")
|
| 824 |
+
|
| 825 |
+
return " β’ ".join(recommendations) if recommendations else "Continue monitoring customer engagement patterns."
|
| 826 |
+
|
| 827 |
+
|
| 828 |
+
# Gradio Interface
|
| 829 |
+
def create_gradio_interface():
|
| 830 |
+
"""Create the Gradio interface for the B2B Customer Analytics platform"""
|
| 831 |
|
| 832 |
+
analytics = B2BCustomerAnalytics()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 833 |
|
| 834 |
+
def load_data(file):
|
| 835 |
+
if file is None:
|
| 836 |
+
return "Please upload a CSV file", None, None, None
|
| 837 |
+
result = analytics.load_and_process_data(file)
|
| 838 |
+
return result
|
| 839 |
|
| 840 |
+
def train_model():
|
| 841 |
+
result = analytics.train_churn_model()
|
| 842 |
+
return result
|
|
|
|
|
|
|
|
|
|
| 843 |
|
| 844 |
+
def create_charts():
|
| 845 |
+
return analytics.create_visualizations()
|
| 846 |
+
|
| 847 |
+
def get_customer_table():
|
| 848 |
+
return analytics.create_customer_table()
|
| 849 |
+
|
| 850 |
+
def generate_report():
|
| 851 |
+
pdf_bytes = analytics.generate_pdf_report()
|
| 852 |
+
if pdf_bytes:
|
| 853 |
+
return pdf_bytes
|
| 854 |
+
return None
|
| 855 |
+
|
| 856 |
+
def get_insights(customer_id):
|
| 857 |
+
if not customer_id:
|
| 858 |
+
return "Please enter a customer ID"
|
| 859 |
+
return analytics.get_customer_insights(customer_id)
|
| 860 |
+
|
| 861 |
+
# Create Gradio interface
|
| 862 |
+
with gr.Blocks(
|
| 863 |
+
theme=gr.themes.Soft(primary_hue="blue"),
|
| 864 |
+
title="B2B Customer Analytics Platform",
|
| 865 |
+
css="""
|
| 866 |
+
.gradio-container {
|
| 867 |
+
font-family: 'Inter', sans-serif;
|
| 868 |
+
}
|
| 869 |
+
.main-header {
|
| 870 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 871 |
+
padding: 2rem;
|
| 872 |
+
border-radius: 1rem;
|
| 873 |
+
color: white;
|
| 874 |
+
text-align: center;
|
| 875 |
+
margin-bottom: 2rem;
|
| 876 |
+
}
|
| 877 |
+
.metric-card {
|
| 878 |
+
background: white;
|
| 879 |
+
padding: 1.5rem;
|
| 880 |
+
border-radius: 1rem;
|
| 881 |
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
| 882 |
+
border-left: 4px solid #3b82f6;
|
| 883 |
+
}
|
| 884 |
+
"""
|
| 885 |
+
) as demo:
|
| 886 |
+
|
| 887 |
+
gr.HTML("""
|
| 888 |
+
<div class="main-header">
|
| 889 |
+
<h1 style="font-size: 2.5rem; font-weight: bold; margin-bottom: 0.5rem;">
|
| 890 |
+
π’ B2B Customer Analytics Platform
|
| 891 |
+
</h1>
|
| 892 |
+
<p style="font-size: 1.2rem; opacity: 0.9;">
|
| 893 |
+
Advanced Customer Segmentation & Churn Prediction System
|
| 894 |
+
</p>
|
| 895 |
+
</div>
|
| 896 |
+
""")
|
| 897 |
+
|
| 898 |
+
with gr.Tabs():
|
| 899 |
+
# Data Upload Tab
|
| 900 |
+
with gr.Tab("π Data Upload & Overview"):
|
| 901 |
+
with gr.Row():
|
| 902 |
+
file_input = gr.File(label="Upload Customer Data CSV", file_types=[".csv"])
|
| 903 |
+
|
| 904 |
+
with gr.Row():
|
| 905 |
+
load_btn = gr.Button("Load & Process Data", variant="primary", size="lg")
|
| 906 |
+
|
| 907 |
+
load_status = gr.HTML()
|
| 908 |
+
summary_display = gr.HTML()
|
| 909 |
+
data_preview = gr.DataFrame(label="Data Preview")
|
| 910 |
+
kpi_display = gr.HTML()
|
| 911 |
+
|
| 912 |
+
# Analytics & Segmentation Tab
|
| 913 |
+
with gr.Tab("π― Customer Segmentation"):
|
| 914 |
+
with gr.Row():
|
| 915 |
+
segment_chart = gr.Plot(label="Customer Segments")
|
| 916 |
+
rfm_chart = gr.Plot(label="RFM Analysis")
|
| 917 |
+
|
| 918 |
+
with gr.Row():
|
| 919 |
+
customer_table = gr.DataFrame(label="Customer Segmentation Table")
|
| 920 |
+
|
| 921 |
+
# Churn Prediction Tab
|
| 922 |
+
with gr.Tab("π€ Churn Prediction"):
|
| 923 |
+
with gr.Row():
|
| 924 |
+
train_btn = gr.Button("Train Churn Prediction Model", variant="primary", size="lg")
|
| 925 |
+
|
| 926 |
+
model_results = gr.HTML()
|
| 927 |
+
|
| 928 |
+
with gr.Row():
|
| 929 |
+
performance_chart = gr.Plot(label="Model Performance")
|
| 930 |
+
churn_chart = gr.Plot(label="Churn Risk Analysis")
|
| 931 |
+
|
| 932 |
+
# Revenue Analytics Tab
|
| 933 |
+
with gr.Tab("π° Revenue Analytics"):
|
| 934 |
+
with gr.Row():
|
| 935 |
+
revenue_chart = gr.Plot(label="Revenue Trends")
|
| 936 |
+
|
| 937 |
+
# Customer Insights Tab
|
| 938 |
+
with gr.Tab("π Customer Insights"):
|
| 939 |
+
with gr.Row():
|
| 940 |
+
customer_id_input = gr.Textbox(
|
| 941 |
+
label="Enter Customer ID",
|
| 942 |
+
placeholder="e.g., CUST001"
|
| 943 |
+
)
|
| 944 |
+
insights_btn = gr.Button("Get Customer Insights", variant="primary")
|
| 945 |
+
|
| 946 |
+
customer_insights = gr.HTML()
|
| 947 |
+
|
| 948 |
+
# Report Generation Tab
|
| 949 |
+
with gr.Tab("π Reports"):
|
| 950 |
+
with gr.Row():
|
| 951 |
+
report_btn = gr.Button("Generate PDF Report", variant="primary", size="lg")
|
| 952 |
+
|
| 953 |
+
with gr.Row():
|
| 954 |
+
report_download = gr.File(label="Download Report")
|
| 955 |
+
|
| 956 |
+
gr.HTML("""
|
| 957 |
+
<div style="background: #f0f9ff; padding: 1.5rem; border-radius: 0.5rem; margin-top: 1rem;">
|
| 958 |
+
<h3 style="color: #1e40af; margin-bottom: 1rem;">π Report Contents</h3>
|
| 959 |
+
<ul style="color: #374151;">
|
| 960 |
+
<li>Executive Summary with Key Metrics</li>
|
| 961 |
+
<li>Customer Segmentation Analysis</li>
|
| 962 |
+
<li>Churn Risk Assessment</li>
|
| 963 |
+
<li>Revenue Trends and Patterns</li>
|
| 964 |
+
<li>Strategic Recommendations</li>
|
| 965 |
+
<li>Model Performance Metrics</li>
|
| 966 |
+
</ul>
|
| 967 |
+
</div>
|
| 968 |
+
""")
|
| 969 |
+
|
| 970 |
+
# Event handlers
|
| 971 |
+
load_btn.click(
|
| 972 |
+
fn=load_data,
|
| 973 |
+
inputs=[file_input],
|
| 974 |
+
outputs=[load_status, summary_display, data_preview, kpi_display]
|
| 975 |
+
)
|
| 976 |
+
|
| 977 |
+
train_btn.click(
|
| 978 |
+
fn=train_model,
|
| 979 |
+
outputs=[model_results, performance_chart]
|
| 980 |
+
)
|
| 981 |
+
|
| 982 |
+
# Auto-update visualizations when data is loaded
|
| 983 |
+
load_btn.click(
|
| 984 |
+
fn=create_charts,
|
| 985 |
+
outputs=[segment_chart, rfm_chart, churn_chart, revenue_chart]
|
| 986 |
+
)
|
| 987 |
+
|
| 988 |
+
load_btn.click(
|
| 989 |
+
fn=get_customer_table,
|
| 990 |
+
outputs=[customer_table]
|
| 991 |
+
)
|
| 992 |
+
|
| 993 |
+
insights_btn.click(
|
| 994 |
+
fn=get_insights,
|
| 995 |
+
inputs=[customer_id_input],
|
| 996 |
+
outputs=[customer_insights]
|
| 997 |
+
)
|
| 998 |
+
|
| 999 |
+
report_btn.click(
|
| 1000 |
+
fn=generate_report,
|
| 1001 |
+
outputs=[report_download]
|
| 1002 |
+
)
|
| 1003 |
+
|
| 1004 |
+
return demo
|
| 1005 |
|
| 1006 |
if __name__ == "__main__":
|
| 1007 |
+
# Launch the application
|
| 1008 |
+
demo = create_gradio_interface()
|
| 1009 |
+
demo.launch(
|
| 1010 |
+
share=True,
|
| 1011 |
+
server_name="0.0.0.0",
|
| 1012 |
+
server_port=7860,
|
| 1013 |
+
show_error=True
|
| 1014 |
+
)
|