Skip to content

onurhanakbulut/churn-model-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง  Churn Model API (XGBoost ยท FastAPI)

๐Ÿ”— Live Demo: https://churn-dashboard-demo.streamlit.app

A lightweight and production-ready REST API that serves an e-commerce churn prediction model trained with XGBoost.
It takes customer behavioral features and returns their churn probability โ€” helping businesses identify at-risk customers.


๐Ÿš€ Features

  • โšก Fast inference powered by FastAPI + Uvicorn
  • โœ… Strict request validation via Pydantic
  • ๐Ÿงฉ Model path configurable with environment variable (MODEL_PATH)
  • ๐Ÿณ Containerized with Docker
  • ๐ŸŒ CORS-enabled โ€” easily integrates with Streamlit dashboards

๐Ÿ“ฆ Tech Stack

Python โ€ข FastAPI โ€ข XGBoost โ€ข scikit-learn โ€ข joblib โ€ข Pydantic โ€ข Docker


๐Ÿ”Œ API Overview

๐Ÿฉบ Health Check

GET /health
โ†’ 200 OK
{
  "status": "ok"
}

๐Ÿ”ฎ Predict (Single Record)
POST /predict
Content-Type: application/json

Body:
{
  "Frequency": 12,
  "Monetary": 358.4,
  "CustomerLifetimeDays": 210
}

Response (200):
{
  "predicted_class": 1,
  "churn_probability": 0.78,
  "is_churn": "Churned"
}


โš™๏ธ Quickstart
1๏ธโƒฃ Clone the repository
git clone https://github.com/onurhanakbulut/churn-model-api.git
cd churn-model-api

2๏ธโƒฃ Install dependencies
python -m venv .venv
source .venv/bin/activate     # On Windows: .venv\Scripts\activate
pip install -r requirements.txt

3๏ธโƒฃ Add your trained model

Place your serialized model (e.g. joblib.dump) inside:

app/churn_xgb_model.pkl


Or specify a custom model path:

# Windows
set MODEL_PATH=app/churn_xgb_model.pkl

# Linux / macOS
export MODEL_PATH=app/churn_xgb_model.pkl

4๏ธโƒฃ Run locally
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload


Then visit ๐Ÿ‘‰ http://localhost:8000/docs


๐Ÿณ Docker Usage
๐Ÿ—๏ธ Build image
docker build -t churn-model-api:latest .

โ–ถ๏ธ Run container
docker run --rm -p 8000:8000 \
  -e MODEL_PATH=/app/app/churn_xgb_model.pkl \
  churn-model-api:latest

โš™๏ธ Environment Variables
Variable	Default	Description
MODEL_PATH	app/churn_xgb_model.pkl	Path to serialized .pkl model file
HOST	0.0.0.0	Bind host for Uvicorn
PORT	8000	Server port
LOG_LEVEL	info	Log level: critical / error / warning / info / debug
๐Ÿงช Example Requests
Using cURL
curl -X POST http://localhost:8000/predict \
  -H "Content-Type: application/json" \
  -d '{"Frequency": 12, "Monetary": 358.4, "CustomerLifetimeDays": 210}'

Using Python
import requests

payload = {"Frequency": 12, "Monetary": 358.4, "CustomerLifetimeDays": 210}
r = requests.post("http://localhost:8000/predict", json=payload)
print(r.json())  # โ†’ {'churn_probability': 0.27}

๐Ÿ‘ฉโ€๐Ÿ’ป Author

Onurhan Akbulut
Data Science & Machine Learning Engineer

About

e-commerce churn model

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors