Spaces:
Sleeping
Sleeping
Upload 6 files
#146
by pauli1234 - opened
- 1_Churn_Data_Creation_and_Processing.ipynb +0 -0
- 2_Churn_Data_Analysis_and_Insights.ipynb +0 -0
- app.py +165 -750
- feature_cols.pkl +3 -0
- requirements.txt +5 -17
- rf_model.pkl +3 -0
1_Churn_Data_Creation_and_Processing.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
2_Churn_Data_Analysis_and_Insights.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
CHANGED
|
@@ -1,758 +1,173 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import re
|
| 3 |
-
import json
|
| 4 |
-
import time
|
| 5 |
-
import traceback
|
| 6 |
-
from pathlib import Path
|
| 7 |
-
from typing import Dict, Any, List, Tuple
|
| 8 |
-
|
| 9 |
-
import pandas as pd
|
| 10 |
import gradio as gr
|
| 11 |
-
import
|
| 12 |
-
import
|
| 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 |
-
def stamp():
|
| 59 |
-
return time.strftime("%Y%m%d-%H%M%S")
|
| 60 |
-
|
| 61 |
-
def tail(text: str, n: int = MAX_LOG_CHARS) -> str:
|
| 62 |
-
return (text or "")[-n:]
|
| 63 |
-
|
| 64 |
-
def _ls(dir_path: Path, exts: Tuple[str, ...]) -> List[str]:
|
| 65 |
-
if not dir_path.is_dir():
|
| 66 |
-
return []
|
| 67 |
-
return sorted(p.name for p in dir_path.iterdir() if p.is_file() and p.suffix.lower() in exts)
|
| 68 |
-
|
| 69 |
-
def _read_csv(path: Path) -> pd.DataFrame:
|
| 70 |
-
return pd.read_csv(path, nrows=MAX_PREVIEW_ROWS)
|
| 71 |
-
|
| 72 |
-
def _read_json(path: Path):
|
| 73 |
-
with path.open(encoding="utf-8") as f:
|
| 74 |
-
return json.load(f)
|
| 75 |
-
|
| 76 |
-
def artifacts_index() -> Dict[str, Any]:
|
| 77 |
-
return {
|
| 78 |
-
"python": {
|
| 79 |
-
"figures": _ls(PY_FIG_DIR, (".png", ".jpg", ".jpeg")),
|
| 80 |
-
"tables": _ls(PY_TAB_DIR, (".csv", ".json")),
|
| 81 |
-
},
|
| 82 |
-
}
|
| 83 |
-
|
| 84 |
-
# =========================================================
|
| 85 |
-
# PIPELINE RUNNERS
|
| 86 |
-
# =========================================================
|
| 87 |
-
|
| 88 |
-
def run_notebook(nb_name: str) -> str:
|
| 89 |
-
ensure_dirs()
|
| 90 |
-
nb_in = BASE_DIR / nb_name
|
| 91 |
-
if not nb_in.exists():
|
| 92 |
-
return f"ERROR: {nb_name} not found."
|
| 93 |
-
nb_out = RUNS_DIR / f"run_{stamp()}_{nb_name}"
|
| 94 |
-
pm.execute_notebook(
|
| 95 |
-
input_path=str(nb_in),
|
| 96 |
-
output_path=str(nb_out),
|
| 97 |
-
cwd=str(BASE_DIR),
|
| 98 |
-
log_output=True,
|
| 99 |
-
progress_bar=False,
|
| 100 |
-
request_save_on_cell_execute=True,
|
| 101 |
-
execution_timeout=PAPERMILL_TIMEOUT,
|
| 102 |
-
)
|
| 103 |
-
return f"Executed {nb_name}"
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
def run_datacreation() -> str:
|
| 107 |
-
try:
|
| 108 |
-
log = run_notebook(NB1)
|
| 109 |
-
csvs = [f.name for f in BASE_DIR.glob("*.csv")]
|
| 110 |
-
return f"OK {log}\n\nCSVs now in /app:\n" + "\n".join(f" - {c}" for c in sorted(csvs))
|
| 111 |
-
except Exception as e:
|
| 112 |
-
return f"FAILED {e}\n\n{traceback.format_exc()[-2000:]}"
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
def run_pythonanalysis() -> str:
|
| 116 |
-
try:
|
| 117 |
-
log = run_notebook(NB2)
|
| 118 |
-
idx = artifacts_index()
|
| 119 |
-
figs = idx["python"]["figures"]
|
| 120 |
-
tabs = idx["python"]["tables"]
|
| 121 |
-
return (
|
| 122 |
-
f"OK {log}\n\n"
|
| 123 |
-
f"Figures: {', '.join(figs) or '(none)'}\n"
|
| 124 |
-
f"Tables: {', '.join(tabs) or '(none)'}"
|
| 125 |
-
)
|
| 126 |
-
except Exception as e:
|
| 127 |
-
return f"FAILED {e}\n\n{traceback.format_exc()[-2000:]}"
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
def run_full_pipeline() -> str:
|
| 131 |
-
logs = []
|
| 132 |
-
logs.append("=" * 50)
|
| 133 |
-
logs.append("STEP 1/2: Data Creation (web scraping + synthetic data)")
|
| 134 |
-
logs.append("=" * 50)
|
| 135 |
-
logs.append(run_datacreation())
|
| 136 |
-
logs.append("")
|
| 137 |
-
logs.append("=" * 50)
|
| 138 |
-
logs.append("STEP 2/2: Python Analysis (sentiment, ARIMA, dashboard)")
|
| 139 |
-
logs.append("=" * 50)
|
| 140 |
-
logs.append(run_pythonanalysis())
|
| 141 |
-
return "\n".join(logs)
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
# =========================================================
|
| 145 |
-
# GALLERY LOADERS
|
| 146 |
-
# =========================================================
|
| 147 |
-
|
| 148 |
-
def _load_all_figures() -> List[Tuple[str, str]]:
|
| 149 |
-
"""Return list of (filepath, caption) for Gallery."""
|
| 150 |
-
items = []
|
| 151 |
-
for p in sorted(PY_FIG_DIR.glob("*.png")):
|
| 152 |
-
items.append((str(p), p.stem.replace('_', ' ').title()))
|
| 153 |
-
return items
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
def _load_table_safe(path: Path) -> pd.DataFrame:
|
| 157 |
-
try:
|
| 158 |
-
if path.suffix == ".json":
|
| 159 |
-
obj = _read_json(path)
|
| 160 |
-
if isinstance(obj, dict):
|
| 161 |
-
return pd.DataFrame([obj])
|
| 162 |
-
return pd.DataFrame(obj)
|
| 163 |
-
return _read_csv(path)
|
| 164 |
-
except Exception as e:
|
| 165 |
-
return pd.DataFrame([{"error": str(e)}])
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
def refresh_gallery():
|
| 169 |
-
"""Called when user clicks Refresh on Gallery tab."""
|
| 170 |
-
figures = _load_all_figures()
|
| 171 |
-
idx = artifacts_index()
|
| 172 |
-
|
| 173 |
-
table_choices = list(idx["python"]["tables"])
|
| 174 |
-
|
| 175 |
-
default_df = pd.DataFrame()
|
| 176 |
-
if table_choices:
|
| 177 |
-
default_df = _load_table_safe(PY_TAB_DIR / table_choices[0])
|
| 178 |
-
|
| 179 |
-
return (
|
| 180 |
-
figures if figures else [],
|
| 181 |
-
gr.update(choices=table_choices, value=table_choices[0] if table_choices else None),
|
| 182 |
-
default_df,
|
| 183 |
-
)
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
def on_table_select(choice: str):
|
| 187 |
-
if not choice:
|
| 188 |
-
return pd.DataFrame([{"hint": "Select a table above."}])
|
| 189 |
-
path = PY_TAB_DIR / choice
|
| 190 |
-
if not path.exists():
|
| 191 |
-
return pd.DataFrame([{"error": f"File not found: {choice}"}])
|
| 192 |
-
return _load_table_safe(path)
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
# =========================================================
|
| 196 |
-
# KPI LOADER
|
| 197 |
-
# =========================================================
|
| 198 |
-
|
| 199 |
-
def load_kpis() -> Dict[str, Any]:
|
| 200 |
-
for candidate in [PY_TAB_DIR / "kpis.json", PY_FIG_DIR / "kpis.json"]:
|
| 201 |
-
if candidate.exists():
|
| 202 |
-
try:
|
| 203 |
-
return _read_json(candidate)
|
| 204 |
-
except Exception:
|
| 205 |
-
pass
|
| 206 |
-
return {}
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
# =========================================================
|
| 210 |
-
# AI DASHBOARD -- LLM picks what to display
|
| 211 |
-
# =========================================================
|
| 212 |
-
|
| 213 |
-
DASHBOARD_SYSTEM = """You are an AI dashboard assistant for a book-sales analytics app.
|
| 214 |
-
The user asks questions or requests about their data. You have access to pre-computed
|
| 215 |
-
artifacts from a Python analysis pipeline.
|
| 216 |
-
|
| 217 |
-
AVAILABLE ARTIFACTS (only reference ones that exist):
|
| 218 |
-
{artifacts_json}
|
| 219 |
-
|
| 220 |
-
KPI SUMMARY: {kpis_json}
|
| 221 |
-
|
| 222 |
-
YOUR JOB:
|
| 223 |
-
1. Answer the user's question conversationally using the KPIs and your knowledge of the artifacts.
|
| 224 |
-
2. At the END of your response, output a JSON block (fenced with ```json ... ```) that tells
|
| 225 |
-
the dashboard which artifact to display. The JSON must have this shape:
|
| 226 |
-
{{"show": "figure"|"table"|"none", "scope": "python", "filename": "..."}}
|
| 227 |
-
|
| 228 |
-
- Use "show": "figure" to display a chart image.
|
| 229 |
-
- Use "show": "table" to display a CSV/JSON table.
|
| 230 |
-
- Use "show": "none" if no artifact is relevant.
|
| 231 |
-
|
| 232 |
-
RULES:
|
| 233 |
-
- If the user asks about sales trends or forecasting by title, show sales_trends or arima figures.
|
| 234 |
-
- If the user asks about sentiment, show sentiment figure or sentiment_counts table.
|
| 235 |
-
- If the user asks about forecast accuracy or ARIMA, show arima figures.
|
| 236 |
-
- If the user asks about top sellers, show top_titles_by_units_sold.csv.
|
| 237 |
-
- If the user asks a general data question, pick the most relevant artifact.
|
| 238 |
-
- Keep your answer concise (2-4 sentences), then the JSON block.
|
| 239 |
-
"""
|
| 240 |
-
|
| 241 |
-
JSON_BLOCK_RE = re.compile(r"```json\s*(\{.*?\})\s*```", re.DOTALL)
|
| 242 |
-
FALLBACK_JSON_RE = re.compile(r"\{[^{}]*\"show\"[^{}]*\}", re.DOTALL)
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
def _parse_display_directive(text: str) -> Dict[str, str]:
|
| 246 |
-
m = JSON_BLOCK_RE.search(text)
|
| 247 |
-
if m:
|
| 248 |
-
try:
|
| 249 |
-
return json.loads(m.group(1))
|
| 250 |
-
except json.JSONDecodeError:
|
| 251 |
-
pass
|
| 252 |
-
m = FALLBACK_JSON_RE.search(text)
|
| 253 |
-
if m:
|
| 254 |
-
try:
|
| 255 |
-
return json.loads(m.group(0))
|
| 256 |
-
except json.JSONDecodeError:
|
| 257 |
-
pass
|
| 258 |
-
return {"show": "none"}
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
def _clean_response(text: str) -> str:
|
| 262 |
-
"""Strip the JSON directive block from the displayed response."""
|
| 263 |
-
return JSON_BLOCK_RE.sub("", text).strip()
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
def _n8n_call(msg: str) -> Tuple[str, Dict]:
|
| 267 |
-
"""Call the student's n8n webhook and return (reply, directive)."""
|
| 268 |
-
import requests as req
|
| 269 |
try:
|
| 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 |
-
msgs = [{"role": "system", "content": system}]
|
| 303 |
-
for entry in (history or [])[-6:]:
|
| 304 |
-
msgs.append(entry)
|
| 305 |
-
msgs.append({"role": "user", "content": user_msg})
|
| 306 |
-
|
| 307 |
-
try:
|
| 308 |
-
r = llm_client.chat_completion(
|
| 309 |
-
model=MODEL_NAME,
|
| 310 |
-
messages=msgs,
|
| 311 |
-
temperature=0.3,
|
| 312 |
-
max_tokens=600,
|
| 313 |
-
stream=False,
|
| 314 |
-
)
|
| 315 |
-
raw = (
|
| 316 |
-
r["choices"][0]["message"]["content"]
|
| 317 |
-
if isinstance(r, dict)
|
| 318 |
-
else r.choices[0].message.content
|
| 319 |
-
)
|
| 320 |
-
directive = _parse_display_directive(raw)
|
| 321 |
-
reply = _clean_response(raw)
|
| 322 |
-
except Exception as e:
|
| 323 |
-
reply = f"LLM error: {e}. Falling back to keyword matching."
|
| 324 |
-
reply_fb, directive = _keyword_fallback(user_msg, idx, kpis)
|
| 325 |
-
reply += "\n\n" + reply_fb
|
| 326 |
-
|
| 327 |
-
# Resolve artifacts — build interactive Plotly charts when possible
|
| 328 |
-
chart_out = None
|
| 329 |
-
tab_out = None
|
| 330 |
-
show = directive.get("show", "none")
|
| 331 |
-
fname = directive.get("filename", "")
|
| 332 |
-
chart_name = directive.get("chart", "")
|
| 333 |
-
|
| 334 |
-
# Interactive chart builders keyed by name
|
| 335 |
-
chart_builders = {
|
| 336 |
-
"sales": build_sales_chart,
|
| 337 |
-
"sentiment": build_sentiment_chart,
|
| 338 |
-
"top_sellers": build_top_sellers_chart,
|
| 339 |
-
}
|
| 340 |
-
|
| 341 |
-
if chart_name and chart_name in chart_builders:
|
| 342 |
-
chart_out = chart_builders[chart_name]()
|
| 343 |
-
elif show == "figure" and fname:
|
| 344 |
-
# Fallback: try to match filename to a chart builder
|
| 345 |
-
if "sales_trend" in fname:
|
| 346 |
-
chart_out = build_sales_chart()
|
| 347 |
-
elif "sentiment" in fname:
|
| 348 |
-
chart_out = build_sentiment_chart()
|
| 349 |
-
elif "arima" in fname or "forecast" in fname:
|
| 350 |
-
chart_out = build_sales_chart() # closest interactive equivalent
|
| 351 |
else:
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
else:
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
new_history = (history or []) + [
|
| 362 |
-
{"role": "user", "content": user_msg},
|
| 363 |
-
{"role": "assistant", "content": reply},
|
| 364 |
-
]
|
| 365 |
-
|
| 366 |
-
return new_history, "", chart_out, tab_out
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
def _keyword_fallback(msg: str, idx: Dict, kpis: Dict) -> Tuple[str, Dict]:
|
| 370 |
-
"""Simple keyword matcher when LLM is unavailable."""
|
| 371 |
-
msg_lower = msg.lower()
|
| 372 |
-
|
| 373 |
-
if not idx["python"]["figures"] and not idx["python"]["tables"]:
|
| 374 |
-
return (
|
| 375 |
-
"No artifacts found yet. Please run the pipeline first (Tab 1), "
|
| 376 |
-
"then come back here to explore the results.",
|
| 377 |
-
{"show": "none"},
|
| 378 |
-
)
|
| 379 |
-
|
| 380 |
-
kpi_text = ""
|
| 381 |
-
if kpis:
|
| 382 |
-
total = kpis.get("total_units_sold", 0)
|
| 383 |
-
kpi_text = (
|
| 384 |
-
f"Quick summary: **{kpis.get('n_titles', '?')}** book titles across "
|
| 385 |
-
f"**{kpis.get('n_months', '?')}** months, with **{total:,.0f}** total units sold."
|
| 386 |
-
)
|
| 387 |
-
|
| 388 |
-
if any(w in msg_lower for w in ["trend", "sales trend", "monthly sale"]):
|
| 389 |
-
return (
|
| 390 |
-
f"Here are the sales trends. {kpi_text}",
|
| 391 |
-
{"show": "figure", "chart": "sales"},
|
| 392 |
-
)
|
| 393 |
-
|
| 394 |
-
if any(w in msg_lower for w in ["sentiment", "review", "positive", "negative"]):
|
| 395 |
-
return (
|
| 396 |
-
f"Here is the sentiment distribution across sampled book titles. {kpi_text}",
|
| 397 |
-
{"show": "figure", "chart": "sentiment"},
|
| 398 |
-
)
|
| 399 |
-
|
| 400 |
-
if any(w in msg_lower for w in ["arima", "forecast", "predict"]):
|
| 401 |
-
return (
|
| 402 |
-
f"Here are the sales trends and forecasts. {kpi_text}",
|
| 403 |
-
{"show": "figure", "chart": "sales"},
|
| 404 |
-
)
|
| 405 |
-
|
| 406 |
-
if any(w in msg_lower for w in ["top", "best sell", "popular", "rank"]):
|
| 407 |
-
return (
|
| 408 |
-
f"Here are the top-selling titles by units sold. {kpi_text}",
|
| 409 |
-
{"show": "table", "scope": "python", "filename": "top_titles_by_units_sold.csv"},
|
| 410 |
-
)
|
| 411 |
|
| 412 |
-
|
| 413 |
-
return (
|
| 414 |
-
f"Here are the pricing decisions. {kpi_text}",
|
| 415 |
-
{"show": "table", "scope": "python", "filename": "pricing_decisions.csv"},
|
| 416 |
-
)
|
| 417 |
|
| 418 |
-
|
| 419 |
-
return (
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
'
|
| 447 |
-
'
|
| 448 |
-
'
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
val = f"{val:,.0f}"
|
| 484 |
-
html += card(icon, label, str(val), colour)
|
| 485 |
-
# Extra KPIs not in config
|
| 486 |
-
known = {k for k, *_ in kpi_config}
|
| 487 |
-
for key, val in kpis.items():
|
| 488 |
-
if key not in known:
|
| 489 |
-
label = key.replace("_", " ").title()
|
| 490 |
-
if isinstance(val, (int, float)) and val > 100:
|
| 491 |
-
val = f"{val:,.0f}"
|
| 492 |
-
html += card("📈", label, str(val), "#8fa8f8")
|
| 493 |
-
html += "</div>"
|
| 494 |
-
return html
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
# =========================================================
|
| 498 |
-
# INTERACTIVE PLOTLY CHARTS (BubbleBusters style)
|
| 499 |
-
# =========================================================
|
| 500 |
-
|
| 501 |
-
CHART_PALETTE = ["#7c5cbf", "#2ec4a0", "#e8537a", "#e8a230", "#5e8fef",
|
| 502 |
-
"#c45ea8", "#3dbacc", "#a0522d", "#6aaa3a", "#d46060"]
|
| 503 |
-
|
| 504 |
-
def _styled_layout(**kwargs) -> dict:
|
| 505 |
-
defaults = dict(
|
| 506 |
-
template="plotly_white",
|
| 507 |
-
paper_bgcolor="rgba(255,255,255,0.95)",
|
| 508 |
-
plot_bgcolor="rgba(255,255,255,0.98)",
|
| 509 |
-
font=dict(family="system-ui, sans-serif", color="#2d1f4e", size=12),
|
| 510 |
-
margin=dict(l=60, r=20, t=70, b=70),
|
| 511 |
-
legend=dict(
|
| 512 |
-
orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1,
|
| 513 |
-
bgcolor="rgba(255,255,255,0.92)",
|
| 514 |
-
bordercolor="rgba(124,92,191,0.35)", borderwidth=1,
|
| 515 |
-
),
|
| 516 |
-
title=dict(font=dict(size=15, color="#4b2d8a")),
|
| 517 |
-
)
|
| 518 |
-
defaults.update(kwargs)
|
| 519 |
-
return defaults
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
def _empty_chart(title: str) -> go.Figure:
|
| 523 |
-
fig = go.Figure()
|
| 524 |
-
fig.update_layout(
|
| 525 |
-
title=title, height=420, template="plotly_white",
|
| 526 |
-
paper_bgcolor="rgba(255,255,255,0.95)",
|
| 527 |
-
annotations=[dict(text="Run the pipeline to generate data",
|
| 528 |
-
x=0.5, y=0.5, xref="paper", yref="paper", showarrow=False,
|
| 529 |
-
font=dict(size=14, color="rgba(124,92,191,0.5)"))],
|
| 530 |
-
)
|
| 531 |
-
return fig
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
def build_sales_chart() -> go.Figure:
|
| 535 |
-
path = PY_TAB_DIR / "df_dashboard.csv"
|
| 536 |
-
if not path.exists():
|
| 537 |
-
return _empty_chart("Sales Trends — run the pipeline first")
|
| 538 |
-
df = pd.read_csv(path)
|
| 539 |
-
date_col = next((c for c in df.columns if "month" in c.lower() or "date" in c.lower()), None)
|
| 540 |
-
val_cols = [c for c in df.columns if c != date_col and df[c].dtype in ("float64", "int64")]
|
| 541 |
-
if not date_col or not val_cols:
|
| 542 |
-
return _empty_chart("Could not auto-detect columns in df_dashboard.csv")
|
| 543 |
-
df[date_col] = pd.to_datetime(df[date_col], errors="coerce")
|
| 544 |
-
fig = go.Figure()
|
| 545 |
-
for i, col in enumerate(val_cols):
|
| 546 |
-
fig.add_trace(go.Scatter(
|
| 547 |
-
x=df[date_col], y=df[col], name=col.replace("_", " ").title(),
|
| 548 |
-
mode="lines+markers", line=dict(color=CHART_PALETTE[i % len(CHART_PALETTE)], width=2),
|
| 549 |
-
marker=dict(size=4),
|
| 550 |
-
hovertemplate=f"<b>{col.replace('_',' ').title()}</b><br>%{{x|%b %Y}}: %{{y:,.0f}}<extra></extra>",
|
| 551 |
-
))
|
| 552 |
-
fig.update_layout(**_styled_layout(height=450, hovermode="x unified",
|
| 553 |
-
title=dict(text="Monthly Overview")))
|
| 554 |
-
fig.update_xaxes(gridcolor="rgba(124,92,191,0.15)", showgrid=True)
|
| 555 |
-
fig.update_yaxes(gridcolor="rgba(124,92,191,0.15)", showgrid=True)
|
| 556 |
-
return fig
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
def build_sentiment_chart() -> go.Figure:
|
| 560 |
-
path = PY_TAB_DIR / "sentiment_counts_sampled.csv"
|
| 561 |
-
if not path.exists():
|
| 562 |
-
return _empty_chart("Sentiment Distribution — run the pipeline first")
|
| 563 |
-
df = pd.read_csv(path)
|
| 564 |
-
title_col = df.columns[0]
|
| 565 |
-
sent_cols = [c for c in ["negative", "neutral", "positive"] if c in df.columns]
|
| 566 |
-
if not sent_cols:
|
| 567 |
-
return _empty_chart("No sentiment columns found in CSV")
|
| 568 |
-
colors = {"negative": "#e8537a", "neutral": "#5e8fef", "positive": "#2ec4a0"}
|
| 569 |
-
fig = go.Figure()
|
| 570 |
-
for col in sent_cols:
|
| 571 |
-
fig.add_trace(go.Bar(
|
| 572 |
-
name=col.title(), y=df[title_col], x=df[col],
|
| 573 |
-
orientation="h", marker_color=colors.get(col, "#888"),
|
| 574 |
-
hovertemplate=f"<b>{col.title()}</b>: %{{x}}<extra></extra>",
|
| 575 |
-
))
|
| 576 |
-
fig.update_layout(**_styled_layout(
|
| 577 |
-
height=max(400, len(df) * 28), barmode="stack",
|
| 578 |
-
title=dict(text="Sentiment Distribution by Book"),
|
| 579 |
-
))
|
| 580 |
-
fig.update_xaxes(title="Number of Reviews")
|
| 581 |
-
fig.update_yaxes(autorange="reversed")
|
| 582 |
-
return fig
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
def build_top_sellers_chart() -> go.Figure:
|
| 586 |
-
path = PY_TAB_DIR / "top_titles_by_units_sold.csv"
|
| 587 |
-
if not path.exists():
|
| 588 |
-
return _empty_chart("Top Sellers — run the pipeline first")
|
| 589 |
-
df = pd.read_csv(path).head(15)
|
| 590 |
-
title_col = next((c for c in df.columns if "title" in c.lower()), df.columns[0])
|
| 591 |
-
val_col = next((c for c in df.columns if "unit" in c.lower() or "sold" in c.lower()), df.columns[-1])
|
| 592 |
-
fig = go.Figure(go.Bar(
|
| 593 |
-
y=df[title_col], x=df[val_col], orientation="h",
|
| 594 |
-
marker=dict(color=df[val_col], colorscale=[[0, "#c5b4f0"], [1, "#7c5cbf"]]),
|
| 595 |
-
hovertemplate="<b>%{y}</b><br>Units: %{x:,.0f}<extra></extra>",
|
| 596 |
-
))
|
| 597 |
-
fig.update_layout(**_styled_layout(
|
| 598 |
-
height=max(400, len(df) * 30),
|
| 599 |
-
title=dict(text="Top Selling Titles"), showlegend=False,
|
| 600 |
-
))
|
| 601 |
-
fig.update_yaxes(autorange="reversed")
|
| 602 |
-
fig.update_xaxes(title="Total Units Sold")
|
| 603 |
-
return fig
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
def refresh_dashboard():
|
| 607 |
-
return render_kpi_cards(), build_sales_chart(), build_sentiment_chart(), build_top_sellers_chart()
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
# =========================================================
|
| 611 |
-
# UI
|
| 612 |
-
# =========================================================
|
| 613 |
-
|
| 614 |
-
ensure_dirs()
|
| 615 |
-
|
| 616 |
-
def load_css() -> str:
|
| 617 |
-
css_path = BASE_DIR / "style.css"
|
| 618 |
-
return css_path.read_text(encoding="utf-8") if css_path.exists() else ""
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
with gr.Blocks(title="AIBDM 2026 Workshop App") as demo:
|
| 622 |
-
|
| 623 |
-
gr.Markdown(
|
| 624 |
-
"# SE21 App Template\n"
|
| 625 |
-
"*This is an app template for SE21 students*",
|
| 626 |
-
elem_id="escp_title",
|
| 627 |
)
|
| 628 |
|
| 629 |
-
|
| 630 |
-
# TAB 1 -- Pipeline Runner
|
| 631 |
-
# ===========================================================
|
| 632 |
-
with gr.Tab("Pipeline Runner"):
|
| 633 |
-
gr.Markdown()
|
| 634 |
-
|
| 635 |
-
with gr.Row():
|
| 636 |
-
with gr.Column(scale=1):
|
| 637 |
-
btn_nb1 = gr.Button("Step 1: Data Creation", variant="secondary")
|
| 638 |
-
with gr.Column(scale=1):
|
| 639 |
-
btn_nb2 = gr.Button("Step 2: Python Analysis", variant="secondary")
|
| 640 |
-
|
| 641 |
-
with gr.Row():
|
| 642 |
-
btn_all = gr.Button("Run Full Pipeline (Both Steps)", variant="primary")
|
| 643 |
-
|
| 644 |
-
run_log = gr.Textbox(
|
| 645 |
-
label="Execution Log",
|
| 646 |
-
lines=18,
|
| 647 |
-
max_lines=30,
|
| 648 |
-
interactive=False,
|
| 649 |
-
)
|
| 650 |
-
|
| 651 |
-
btn_nb1.click(run_datacreation, outputs=[run_log])
|
| 652 |
-
btn_nb2.click(run_pythonanalysis, outputs=[run_log])
|
| 653 |
-
btn_all.click(run_full_pipeline, outputs=[run_log])
|
| 654 |
-
|
| 655 |
-
# ===========================================================
|
| 656 |
-
# TAB 2 -- Dashboard (KPIs + Interactive Charts + Gallery)
|
| 657 |
-
# ===========================================================
|
| 658 |
-
with gr.Tab("Dashboard"):
|
| 659 |
-
kpi_html = gr.HTML(value=render_kpi_cards)
|
| 660 |
-
|
| 661 |
-
refresh_btn = gr.Button("Refresh Dashboard", variant="primary")
|
| 662 |
-
|
| 663 |
-
gr.Markdown("#### Interactive Charts")
|
| 664 |
-
chart_sales = gr.Plot(label="Monthly Overview")
|
| 665 |
-
chart_sentiment = gr.Plot(label="Sentiment Distribution")
|
| 666 |
-
chart_top = gr.Plot(label="Top Sellers")
|
| 667 |
-
|
| 668 |
-
gr.Markdown("#### Static Figures (from notebooks)")
|
| 669 |
-
gallery = gr.Gallery(
|
| 670 |
-
label="Generated Figures",
|
| 671 |
-
columns=2,
|
| 672 |
-
height=480,
|
| 673 |
-
object_fit="contain",
|
| 674 |
-
)
|
| 675 |
-
|
| 676 |
-
gr.Markdown("#### Data Tables")
|
| 677 |
-
table_dropdown = gr.Dropdown(
|
| 678 |
-
label="Select a table to view",
|
| 679 |
-
choices=[],
|
| 680 |
-
interactive=True,
|
| 681 |
-
)
|
| 682 |
-
table_display = gr.Dataframe(
|
| 683 |
-
label="Table Preview",
|
| 684 |
-
interactive=False,
|
| 685 |
-
)
|
| 686 |
-
|
| 687 |
-
def _on_refresh():
|
| 688 |
-
kpi, c1, c2, c3 = refresh_dashboard()
|
| 689 |
-
figs, dd, df = refresh_gallery()
|
| 690 |
-
return kpi, c1, c2, c3, figs, dd, df
|
| 691 |
-
|
| 692 |
-
refresh_btn.click(
|
| 693 |
-
_on_refresh,
|
| 694 |
-
outputs=[kpi_html, chart_sales, chart_sentiment, chart_top,
|
| 695 |
-
gallery, table_dropdown, table_display],
|
| 696 |
-
)
|
| 697 |
-
table_dropdown.change(
|
| 698 |
-
on_table_select,
|
| 699 |
-
inputs=[table_dropdown],
|
| 700 |
-
outputs=[table_display],
|
| 701 |
-
)
|
| 702 |
-
|
| 703 |
-
# ===========================================================
|
| 704 |
-
# TAB 3 -- AI Dashboard
|
| 705 |
-
# ===========================================================
|
| 706 |
-
with gr.Tab('"AI" Dashboard'):
|
| 707 |
-
_ai_status = (
|
| 708 |
-
"Connected to your **n8n workflow**." if N8N_WEBHOOK_URL
|
| 709 |
-
else "**LLM active.**" if LLM_ENABLED
|
| 710 |
-
else "Using **keyword matching**. Upgrade options: "
|
| 711 |
-
"set `N8N_WEBHOOK_URL` to connect your n8n workflow, "
|
| 712 |
-
"or set `HF_API_KEY` for direct LLM access."
|
| 713 |
-
)
|
| 714 |
-
gr.Markdown(
|
| 715 |
-
"### Ask questions, get interactive visualisations\n\n"
|
| 716 |
-
f"Type a question and the system will pick the right interactive chart or table. {_ai_status}"
|
| 717 |
-
)
|
| 718 |
-
|
| 719 |
-
with gr.Row(equal_height=True):
|
| 720 |
-
with gr.Column(scale=1):
|
| 721 |
-
chatbot = gr.Chatbot(
|
| 722 |
-
label="Conversation",
|
| 723 |
-
height=380,
|
| 724 |
-
)
|
| 725 |
-
user_input = gr.Textbox(
|
| 726 |
-
label="Ask about your data",
|
| 727 |
-
placeholder="e.g. Show me sales trends / What are the top sellers? / Sentiment analysis",
|
| 728 |
-
lines=1,
|
| 729 |
-
)
|
| 730 |
-
gr.Examples(
|
| 731 |
-
examples=[
|
| 732 |
-
"Show me the sales trends",
|
| 733 |
-
"What does the sentiment look like?",
|
| 734 |
-
"Which titles sell the most?",
|
| 735 |
-
"Show the ARIMA forecasts",
|
| 736 |
-
"What are the pricing decisions?",
|
| 737 |
-
"Give me a dashboard overview",
|
| 738 |
-
],
|
| 739 |
-
inputs=user_input,
|
| 740 |
-
)
|
| 741 |
-
|
| 742 |
-
with gr.Column(scale=1):
|
| 743 |
-
ai_figure = gr.Plot(
|
| 744 |
-
label="Interactive Chart",
|
| 745 |
-
)
|
| 746 |
-
ai_table = gr.Dataframe(
|
| 747 |
-
label="Data Table",
|
| 748 |
-
interactive=False,
|
| 749 |
-
)
|
| 750 |
-
|
| 751 |
-
user_input.submit(
|
| 752 |
-
ai_chat,
|
| 753 |
-
inputs=[user_input, chatbot],
|
| 754 |
-
outputs=[chatbot, user_input, ai_figure, ai_table],
|
| 755 |
-
)
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
demo.launch(css=load_css(), allowed_paths=[str(BASE_DIR)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
|
| 5 |
+
rf_model = joblib.load("rf_model.pkl")
|
| 6 |
+
feature_cols = joblib.load("feature_cols.pkl")
|
| 7 |
+
|
| 8 |
+
gender_map = {'Female': 0, 'Male': 1}
|
| 9 |
+
partner_map = {'No': 0, 'Yes': 1}
|
| 10 |
+
dependents_map = {'No': 0, 'Yes': 1}
|
| 11 |
+
contract_map = {'Month-to-month': 0, 'One year': 1, 'Two year': 2}
|
| 12 |
+
payment_map = {
|
| 13 |
+
'Bank transfer (automatic)': 0,
|
| 14 |
+
'Credit card (automatic)': 1,
|
| 15 |
+
'Electronic check': 2,
|
| 16 |
+
'Mailed check': 3
|
| 17 |
+
}
|
| 18 |
+
internet_map = {'DSL': 0, 'Fiber optic': 1, 'No': 2}
|
| 19 |
+
tech_map = {'No': 0, 'No internet service': 1, 'Yes': 2}
|
| 20 |
+
complaint_map = {
|
| 21 |
+
'Billing Issue': 0,
|
| 22 |
+
'Contract Dispute': 1,
|
| 23 |
+
'Overcharge': 2,
|
| 24 |
+
'Service Outage': 3,
|
| 25 |
+
'Speed/Performance': 4,
|
| 26 |
+
'Technical Failure': 5
|
| 27 |
+
}
|
| 28 |
+
risk_map = {'High': 0, 'Low': 1, 'Medium': 2}
|
| 29 |
+
|
| 30 |
+
def predict_churn(
|
| 31 |
+
tenure,
|
| 32 |
+
monthly_charges,
|
| 33 |
+
total_charges,
|
| 34 |
+
senior,
|
| 35 |
+
gender,
|
| 36 |
+
partner,
|
| 37 |
+
dependents,
|
| 38 |
+
contract,
|
| 39 |
+
payment,
|
| 40 |
+
internet,
|
| 41 |
+
tech,
|
| 42 |
+
support_calls,
|
| 43 |
+
avg_call_duration,
|
| 44 |
+
days_since_last_contact,
|
| 45 |
+
sentiment,
|
| 46 |
+
complaint,
|
| 47 |
+
risk
|
| 48 |
+
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
try:
|
| 50 |
+
input_df = pd.DataFrame([{
|
| 51 |
+
'SeniorCitizen': senior,
|
| 52 |
+
'tenure': tenure,
|
| 53 |
+
'MonthlyCharges': monthly_charges,
|
| 54 |
+
'TotalCharges': total_charges,
|
| 55 |
+
'gender_enc': gender_map[gender],
|
| 56 |
+
'Partner_enc': partner_map[partner],
|
| 57 |
+
'Dependents_enc': dependents_map[dependents],
|
| 58 |
+
'Contract_enc': contract_map[contract],
|
| 59 |
+
'PaymentMethod_enc': payment_map[payment],
|
| 60 |
+
'InternetService_enc': internet_map[internet],
|
| 61 |
+
'TechSupport_enc': tech_map[tech],
|
| 62 |
+
'support_calls': support_calls,
|
| 63 |
+
'avg_call_duration': avg_call_duration,
|
| 64 |
+
'days_since_last_contact': days_since_last_contact,
|
| 65 |
+
'sentiment_score': sentiment,
|
| 66 |
+
'complaint_type_enc': complaint_map[complaint],
|
| 67 |
+
'support_churn_risk_enc': risk_map[risk]
|
| 68 |
+
}])
|
| 69 |
+
|
| 70 |
+
input_df = input_df[feature_cols]
|
| 71 |
+
|
| 72 |
+
pred = rf_model.predict(input_df)[0]
|
| 73 |
+
prob = rf_model.predict_proba(input_df)[0][1]
|
| 74 |
+
|
| 75 |
+
if prob >= 0.75:
|
| 76 |
+
risk_level = "High"
|
| 77 |
+
recommendation = "Immediate retention outreach and service recovery."
|
| 78 |
+
elif prob >= 0.40:
|
| 79 |
+
risk_level = "Medium"
|
| 80 |
+
recommendation = "Proactive follow-up and customer care check-in."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
else:
|
| 82 |
+
risk_level = "Low"
|
| 83 |
+
recommendation = "Maintain normal engagement and service quality."
|
| 84 |
+
|
| 85 |
+
churn_label = "Yes" if pred == 1 else "No"
|
| 86 |
+
|
| 87 |
+
drivers = []
|
| 88 |
+
if contract == "Month-to-month":
|
| 89 |
+
drivers.append("month-to-month contract")
|
| 90 |
+
if support_calls >= 5:
|
| 91 |
+
drivers.append("frequent support calls")
|
| 92 |
+
if sentiment < 0:
|
| 93 |
+
drivers.append("negative sentiment")
|
| 94 |
+
if tech == "No":
|
| 95 |
+
drivers.append("lack of tech support")
|
| 96 |
+
if monthly_charges > 80:
|
| 97 |
+
drivers.append("higher monthly charges")
|
| 98 |
+
|
| 99 |
+
if drivers:
|
| 100 |
+
explanation = "Main risk drivers: " + ", ".join(drivers) + "."
|
| 101 |
else:
|
| 102 |
+
explanation = "Risk is based on the combined customer, billing, and support profile."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
return churn_label, f"{prob:.2%}", risk_level, explanation, recommendation
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
except Exception as e:
|
| 107 |
+
return "Error", "Error", "Error", f"Something went wrong: {str(e)}", "Please review the inputs."
|
| 108 |
+
|
| 109 |
+
with gr.Blocks(title="ChurnGuard – AI Customer Retention Advisor") as demo:
|
| 110 |
+
gr.Markdown("""
|
| 111 |
+
# ChurnGuard – AI Customer Retention Advisor
|
| 112 |
+
This app turns our churn model into a decision-support tool for telecom retention strategy.
|
| 113 |
+
It combines billing, contract, demographic, and synthetic support-interaction features to estimate churn risk.
|
| 114 |
+
""")
|
| 115 |
+
|
| 116 |
+
with gr.Row():
|
| 117 |
+
with gr.Column(scale=2):
|
| 118 |
+
gr.Markdown("## Customer Profile")
|
| 119 |
+
senior = gr.Dropdown([0, 1], value=0, label="Senior Citizen (0 = No, 1 = Yes)")
|
| 120 |
+
gender = gr.Dropdown(['Female', 'Male'], value='Female', label="Gender")
|
| 121 |
+
partner = gr.Dropdown(['No', 'Yes'], value='No', label="Partner")
|
| 122 |
+
dependents = gr.Dropdown(['No', 'Yes'], value='No', label="Dependents")
|
| 123 |
+
tenure = gr.Slider(0, 72, value=12, step=1, label="Tenure (months)")
|
| 124 |
+
|
| 125 |
+
gr.Markdown("## Service & Contract")
|
| 126 |
+
monthly_charges = gr.Slider(0, 150, value=70, step=0.1, label="Monthly Charges")
|
| 127 |
+
total_charges = gr.Slider(0, 10000, value=1000, step=1, label="Total Charges")
|
| 128 |
+
contract = gr.Dropdown(['Month-to-month', 'One year', 'Two year'], value='Month-to-month', label="Contract")
|
| 129 |
+
payment = gr.Dropdown([
|
| 130 |
+
'Bank transfer (automatic)',
|
| 131 |
+
'Credit card (automatic)',
|
| 132 |
+
'Electronic check',
|
| 133 |
+
'Mailed check'
|
| 134 |
+
], value='Bank transfer (automatic)', label="Payment Method")
|
| 135 |
+
internet = gr.Dropdown(['DSL', 'Fiber optic', 'No'], value='DSL', label="Internet Service")
|
| 136 |
+
tech = gr.Dropdown(['No', 'No internet service', 'Yes'], value='No', label="Tech Support")
|
| 137 |
+
|
| 138 |
+
gr.Markdown("## Support Interaction Signals")
|
| 139 |
+
support_calls = gr.Slider(0, 15, value=3, step=1, label="Support Calls")
|
| 140 |
+
avg_call_duration = gr.Slider(0, 30, value=8, step=0.1, label="Average Call Duration (minutes)")
|
| 141 |
+
days_since_last_contact = gr.Slider(0, 120, value=20, step=1, label="Days Since Last Contact")
|
| 142 |
+
sentiment = gr.Slider(-1, 1, value=0, step=0.01, label="Sentiment Score")
|
| 143 |
+
complaint = gr.Dropdown([
|
| 144 |
+
'Billing Issue',
|
| 145 |
+
'Contract Dispute',
|
| 146 |
+
'Overcharge',
|
| 147 |
+
'Service Outage',
|
| 148 |
+
'Speed/Performance',
|
| 149 |
+
'Technical Failure'
|
| 150 |
+
], value='Billing Issue', label="Complaint Type")
|
| 151 |
+
risk = gr.Dropdown(['High', 'Low', 'Medium'], value='Low', label="Support Churn Risk")
|
| 152 |
+
|
| 153 |
+
submit_btn = gr.Button("Predict Churn Risk", variant="primary")
|
| 154 |
+
|
| 155 |
+
with gr.Column(scale=1):
|
| 156 |
+
gr.Markdown("## Prediction Results")
|
| 157 |
+
pred_out = gr.Textbox(label="Predicted Churn")
|
| 158 |
+
prob_out = gr.Textbox(label="Churn Probability")
|
| 159 |
+
risk_out = gr.Textbox(label="Risk Level")
|
| 160 |
+
explanation_out = gr.Textbox(label="Explanation")
|
| 161 |
+
recommendation_out = gr.Textbox(label="Recommended Action")
|
| 162 |
+
|
| 163 |
+
submit_btn.click(
|
| 164 |
+
fn=predict_churn,
|
| 165 |
+
inputs=[
|
| 166 |
+
tenure, monthly_charges, total_charges, senior, gender, partner, dependents,
|
| 167 |
+
contract, payment, internet, tech, support_calls, avg_call_duration,
|
| 168 |
+
days_since_last_contact, sentiment, complaint, risk
|
| 169 |
+
],
|
| 170 |
+
outputs=[pred_out, prob_out, risk_out, explanation_out, recommendation_out]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
)
|
| 172 |
|
| 173 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
feature_cols.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ce6990e6c7ce2832d09be35dcca074a8ec6768472398575156f3cd9a33103450
|
| 3 |
+
size 318
|
requirements.txt
CHANGED
|
@@ -1,17 +1,5 @@
|
|
| 1 |
-
gradio
|
| 2 |
-
pandas
|
| 3 |
-
numpy
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
statsmodels>=0.14.0
|
| 7 |
-
scikit-learn>=1.3.0
|
| 8 |
-
papermill>=2.5.0
|
| 9 |
-
nbformat>=5.9.0
|
| 10 |
-
pillow>=10.0.0
|
| 11 |
-
requests>=2.31.0
|
| 12 |
-
beautifulsoup4>=4.12.0
|
| 13 |
-
vaderSentiment>=3.3.2
|
| 14 |
-
huggingface_hub>=0.20.0
|
| 15 |
-
textblob>=0.18.0
|
| 16 |
-
faker>=20.0.0
|
| 17 |
-
plotly>=5.18.0
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
scikit-learn
|
| 5 |
+
joblib
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rf_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:700ca7431f7aeaf069688dee6493d0687fca9d058c3d516539427503d2b13ce7
|
| 3 |
+
size 1256233
|