This project is a production‑ready Fraud Detection REST API built with FastAPI and a LightGBM binary classifier, designed to score financial transactions for fraud risk and provide model explainability using SHAP. A continuation of my previous project credit_card_fraud_detection_system_for_credit_card_transactions, where the best trained model, and the model's metadata are retrieved from for deployment.
The system exposes endpoints for:
- Fraud probability prediction
- Local, per‑transaction explainability (top contributing features)
- Health and readiness checks
The application is fully Dockerized, uses strict request/response schemas, and follows best practices for ML inference APIs.
- ⚡ FastAPI for high‑performance inference
- 🌲 LightGBM binary classification model
- 🔍 SHAP‑based explainability (TreeExplainer)
- 📦 Docker & Docker Compose support
- 🧪 Input validation with Pydantic
- 🩺 Health and startup checks
- 🧾 Structured logging
fraud-detection-api/
├── app/
│ ├── main.py # FastAPI app & routes
│ ├── predict.py # Prediction logic
│ ├── explain.py # SHAP explainability logic
│ ├── schemas.py # Pydantic schemas
│ ├── model_loader.py # Model, scaler, metadata loading
│ ├── feature_descriptions.py # Helper for feature descriptions mapping
| ├── logging_config.py
│ └── __init__.py
├── models/
│ ├── lgbm_tuned.pkl
│ ├── scaler.pkl
│ └── lgbm_metadata.json
├── Dockerfile
├── docker-compose.yml
├── requirements.txt
├── README.md
└── .gitignore
-
Algorithm: LightGBM (binary classifier)
-
Output: Fraud probability + threshold‑based prediction
-
Threshold: Loaded from model metadata
-
Features:
- PCA components:
V1…V28 - Engineered features:
Amount_scaled,Time_scaled
- PCA components:
All inference inputs are internally aligned to the exact feature set used during training.
Predicts fraud probability for a transaction.
An example of an input data:
Request Body
{
"data": {
"V1": -1.23,
"V2": 0.45,
"V3": -0.67,
"Amount": 120.5,
"Time": 35000
}
}Response
{
"fraud_probability": 0.0123,
"prediction": 0
}Returns SHAP‑based explanations for a transaction.
Request Body
{
"data": {
"V1": -1.23,
"V2": 0.45,
"V3": -0.67,
"Amount": 120.5,
"Time": 35000
}
}Response
{
"fraud_probability": 0.0123,
"top_features": [
{
"feature": "V14",
"description": "Transaction risk signal",
"shap_value": 0.345678,
"impact": "increases fraud risk"
}
]
}Health check endpoint.
Response
{
"status": "ok"
}git clone https://github.com/SaintJeane/fraud-detection-api.git
cd fraud-detection-apipython -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txtuvicorn app.main:app --reloadOpen: http://127.0.0.1:8000/docs
docker build -t fraud-detection-api .docker run -p 8000:8000 fraud-detection-apidocker compose up --buildStop services:
docker compose downdocker run -p 8000:8000 ghcr.io/saintjeane/fraud-detection-api:latest- SHAP values are computed using
TreeExplainer - Binary classification output is normalized to handle SHAP API changes
- Only top‑K most impactful features are returned
- All SHAP values are JSON‑safe floats
- Python 3.10
- FastAPI
- LightGBM
- SHAP
- Pandas / NumPy
- Docker & Docker Compose
fastapi machine-learning fraud-detection lightgbm shap ml-api docker
MIT License
This project is for educational and demonstrative purposes. It should not be used as‑is for real‑world financial decision‑making without additional validation, monitoring, and compliance controls.