Atharva-khetale/churn-mlops
GitHub: Atharva-khetale/churn-mlops
一个完整的客户流失预测 MLOps 流水线项目,演示了从数据处理、多模型训练、MLflow 跟踪、FastAPI 部署到漂移检测与自动重训的生产级 ML 工程实践。
Stars: 1 | Forks: 0
# 🔁 流失预测 MLOps 流水线
[](https://github.com/YOUR_USERNAME/churn-mlops/actions)
[](https://codecov.io/gh/YOUR_USERNAME/churn-mlops)
[](https://python.org)
[](https://mlflow.org)
[](https://fastapi.tiangolo.com)
[](https://docker.com)
[](https://dvc.org)
[](https://YOUR_API_URL.onrender.com/docs)
## 🏗️ 架构
```
Git Push
│
▼
GitHub Actions CI/CD
├── ruff + black (code quality)
├── pytest (45 tests, 82% coverage)
├── python pipeline.py (train all models)
└── Docker build + smoke test
│
▼
StreamLit Deploy
│
▼
┌─────────────────────────────────────────────┐
│ MLOps Pipeline │
│ │
│ CSV ──► Preprocessing ──► Training │
│ ├── XGBoost │
│ ├── RF │
│ └── GBM │
│ │ │
│ MLflow Track │
│ + Registry │
│ │ │
│ best_model.pkl │
│ │ │
│ FastAPI Serve │
│ /predict /metrics │
│ │ │
│ Drift Monitor (PSI) │
│ Weekly Retrain Cron │
└─────────────────────────────────────────────┘
```
## 🛠️ 技术栈
| 层级 | 技术 |
|-------|-----------|
| 语言 | Python 3.11 |
| ML 模型 | XGBoost, Random Forest, Gradient Boosting |
| 实验跟踪 | MLflow (参数, 指标, artifacts, model registry) |
| API 服务 | FastAPI + Uvicorn |
| 容器化 | Docker (多阶段构建) |
| CI/CD | GitHub Actions |
| 代码质量 | ruff + black |
| 测试 | pytest + pytest-cov (82% 覆盖率) |
| 数据版本控制 | DVC |
| Drift 检测 | PSI + Evidently AI |
| 部署 | Render / Railway / Fly.io |
| 数据集 | IBM Telco Customer Churn (7,043 行, 20 个特征) |
## 📁 项目结构
```
churn-mlops/
├── src/
│ ├── data_ingestion.py # Download IBM dataset, schema validation
│ ├── preprocessing.py # LabelEncoder, StandardScaler, train/test split
│ └── train.py # MLflow multi-model training + business metrics
│
├── api/
│ └── main.py # FastAPI: /predict /batch /metrics /health /logs
│
├── monitoring/
│ └── drift_monitor.py # PSI + Evidently + prediction log analysis
│
├── tests/
│ ├── conftest.py # Shared fixtures
│ └── test_pipeline.py # 45 tests: data, preprocessing, model, API, drift
│
├── .github/workflows/
│ ├── mlops_ci.yml # CI/CD pipeline
│ └── retrain.yml # Weekly automated retraining
│
├── pipeline.py # End-to-end orchestrator
├── Dockerfile # Multi-stage production container
├── render.yaml # Render deployment config
├── dvc.yaml # DVC pipeline stages
├── pyproject.toml # Tool configuration
├── HUMAN_CHANGES_AND_SETUP.md # Full setup guide ← read this first
└── requirements.txt
```
## 🚀 快速开始
```
git clone https://github.com/YOUR_USERNAME/churn-mlops.git
cd churn-mlops
pip install -r requirements.txt
# 完整 pipeline (ingest → preprocess → train → monitor)
python pipeline.py
# MLflow UI
mlflow ui --port 5000
# 启动 API
uvicorn api.main:app --reload --port 8000
# 测试 + coverage
pytest tests/ -v --cov=src --cov=api --cov=monitoring
# 简单方式 (将运行 frontend+Backend)
python streamlit_app.py
```
## 📊 MLflow 实验
每次 3 个模型运行都会跟踪:
**ML 指标**
- `accuracy`, `f1_score`, `roc_auc`, `precision`, `recall`
**业务指标** ← _这是它的突出之处_
- `business/revenue_saved_usd` — 捕获的预测流失客户 × 留存 ROI
- `business/wasted_spend_usd` — 假阳性 × 留存成本
- `business/net_impact_usd` — 挽回的收入减去浪费的支出
- `business/missed_revenue_usd` — 假阴性 × 生命周期价值
**Artifacts**
- 混淆矩阵、ROC 曲线、特征重要性图表
## 🌐 API Endpoints
| 方法 | Endpoint | 描述 |
|--------|----------|-------------|
| `GET` | `/` | 欢迎 + 文档链接 |
| `GET` | `/health` | 模型存活检查 |
| `POST` | `/predict` | 单个客户预测 |
| `POST` | `/predict/batch` | 批量预测(最多 500 个) |
| `GET` | `/metrics` | 聚合预测统计信息 |
| `GET` | `/logs?last=50` | 最近的预测日志 |
| `POST` | `/reload` | 热重载模型 artifacts |
### 请求示例
```
curl -X POST https://YOUR_API_URL.onrender.com/predict \
-H "Content-Type: application/json" \
-d '{
"gender": "Female", "SeniorCitizen": 0, "Partner": "Yes",
"Dependents": "No", "tenure": 6, "PhoneService": "Yes",
"MultipleLines": "No", "InternetService": "Fiber optic",
"OnlineSecurity": "No", "OnlineBackup": "No",
"DeviceProtection": "No", "TechSupport": "No",
"StreamingTV": "No", "StreamingMovies": "No",
"Contract": "Month-to-month", "PaperlessBilling": "Yes",
"PaymentMethod": "Electronic check",
"MonthlyCharges": 70.70, "TotalCharges": 151.65
}'
```
### 响应示例
```
{
"churn_probability": 0.8234,
"churn_prediction": true,
"risk_level": "HIGH",
"expected_revenue_loss": 645.42,
"timestamp": "2024-06-01T10:30:00Z",
"model_version": "2.0.0",
"latency_ms": 4.2
}
```
## 🧪 测试
```
pytest tests/ -v
45 passed in 26s
Coverage:
src/train.py 91%
api/main.py 91%
src/preprocessing.py 80%
monitoring/... 64%
TOTAL 82%
```
测试涵盖:
- 数据接入 + schema 验证
- 预处理(清洗、编码、缩放、拆分)
- 模型训练(概率、AUC、业务指标)
- 图表生成(混淆矩阵、ROC、特征重要性)
- 完整的 MLflow `train_and_track` 集成
- Drift 监控(PSI、检测阈值)
- 所有 10 个 FastAPI endpoints,包括 503 错误处理
## 🔄 CI/CD 流程
```
git push main
│
├── ruff check (linting)
├── black --check (formatting)
│
├── pytest (45 tests, 82% coverage)
│
├── python pipeline.py (train + MLflow)
│
├── docker build + smoke test
│ ├── GET /health → 200 ✅
│ └── POST /predict → probability ✅
│
└── Render auto-deploy → live URL
```
**每周(每周日 UTC 02:00):**
```
GitHub Actions cron
└── python pipeline.py → retrain on latest data
```
## 📈 模型性能
| 模型 | ROC-AUC | F1 | Precision | Recall |
|-------|---------|-----|-----------|--------|
| XGBoost | ~0.84 | ~0.62 | ~0.68 | ~0.57 |
| Random Forest | ~0.82 | ~0.60 | ~0.65 | ~0.55 |
| Gradient Boosting | ~0.83 | ~0.61 | ~0.66 | ~0.56 |
_由于采样中的随机性,每次运行的指标会略有不同._
## 🗃️ 数据集
**IBM Telco Customer Churn** (开源)
- 记录:7,043 名客户
- 特征:20 个(人口统计、服务、账单)
- 目标:Churn(是/否)— 26.5% 流失率
- 来源:[IBM GitHub](https://github.com/IBM/telco-customer-churn-on-icp4d)
## 📖 设置指南
请参阅 **[HUMAN_CHANGES_AND_SETUP.md](HUMAN_CHANGES_AND_SETUP.md)** 了解:
- 完整的本地设置说明
- 如何进行公开部署
- GitHub secrets 配置
- DVC 数据版本控制设置
- 可以在简历上写些什么
## 👤 作者
作品集 MLOps 项目 — 展示生产级 ML 工程技能。
⭐ 如果这个仓库对您有帮助,请点个 Star!
标签:Apex, AV绕过, DVC, FastAPI, Kubernetes, MLOps, 安全规则引擎, 开源框架, 持续集成, 机器学习, 模型部署, 请求拦截, 逆向工具