|
1 | 1 | # Fraud Detection API |
2 | 2 |
|
3 | | -## Tags |
4 | | - |
5 | | -`machine-learning` `fraud-detection` `lightgbm` `fastapi` `shap` `docker` `api` `mlops` |
6 | | - |
7 | | ---- |
| 3 | +## Overview |
8 | 4 |
|
9 | | -## Description |
| 5 | +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](https://github.com/SaintJeane/credit_card_fraud_detection_system_for_credit_card_transactions), where the best trained model, and the model's metadata are retrieved from for deployment. |
10 | 6 |
|
11 | | -A production-ready **Fraud Detection REST API** built with **FastAPI** and **LightGBM**, supporting real-time predictions and **SHAP-based explainability**. The API is designed for deployment using **Docker**, follows ML serving best practices, and includes automated CI via **GitHub Actions**. |
| 7 | +The system exposes endpoints for: |
12 | 8 |
|
13 | | -The model was trained on engineered PCA-based transaction features and exposes two main endpoints: |
| 9 | +* Fraud probability prediction |
| 10 | +* Local, per‑transaction explainability (top contributing features) |
| 11 | +* Health and readiness checks |
14 | 12 |
|
15 | | -* `/predict` – fraud probability and classification |
16 | | -* `/explain` – per-feature SHAP explanations |
| 13 | +The application is fully **Dockerized**, uses **strict request/response schemas**, and follows best practices for **ML inference APIs**. |
17 | 14 |
|
18 | 15 | --- |
19 | 16 |
|
20 | | -## Tech Stack |
| 17 | +## Key Features |
21 | 18 |
|
22 | | -* **Python 3.10** |
23 | | -* **FastAPI** – API framework |
24 | | -* **LightGBM** – fraud classification model |
25 | | -* **SHAP** – model explainability |
26 | | -* **scikit-learn** – preprocessing |
27 | | -* **Docker & Docker Compose** – containerization |
28 | | -* **GitHub Actions** – CI pipeline |
| 19 | +* ⚡ FastAPI for high‑performance inference |
| 20 | +* 🌲 LightGBM binary classification model |
| 21 | +* 🔍 SHAP‑based explainability (TreeExplainer) |
| 22 | +* 📦 Docker & Docker Compose support |
| 23 | +* 🧪 Input validation with Pydantic |
| 24 | +* 🩺 Health and startup checks |
| 25 | +* 🧾 Structured logging |
29 | 26 |
|
30 | 27 | --- |
31 | 28 |
|
32 | 29 | ## Project Structure |
33 | 30 |
|
34 | | -``` |
| 31 | +```text |
35 | 32 | fraud-detection-api/ |
36 | 33 | ├── app/ |
37 | | -│ ├── main.py |
38 | | -│ ├── predict.py |
39 | | -│ ├── explain.py |
40 | | -│ ├── model_loader.py |
41 | | -│ └── schemas.py |
| 34 | +│ ├── main.py # FastAPI app & routes |
| 35 | +│ ├── predict.py # Prediction logic |
| 36 | +│ ├── explain.py # SHAP explainability logic |
| 37 | +│ ├── schemas.py # Pydantic schemas |
| 38 | +│ ├── model_loader.py # Model, scaler, metadata loading |
| 39 | +│ ├── feature_descriptions.py # Helper for feature descriptions mapping |
| 40 | +| ├── logging_config.py |
| 41 | +│ └── __init__.py |
42 | 42 | ├── models/ |
43 | 43 | │ ├── lgbm_tuned.pkl |
44 | 44 | │ ├── scaler.pkl |
45 | 45 | │ └── lgbm_metadata.json |
46 | | -├── docker-compose.yml |
47 | 46 | ├── Dockerfile |
| 47 | +├── docker-compose.yml |
48 | 48 | ├── requirements.txt |
49 | | -└── README.md |
| 49 | +├── README.md |
| 50 | +└── .gitignore |
50 | 51 | ``` |
51 | 52 |
|
52 | 53 | --- |
53 | 54 |
|
| 55 | +## Model Details |
| 56 | + |
| 57 | +* **Algorithm**: LightGBM (binary classifier) |
| 58 | +* **Output**: Fraud probability + threshold‑based prediction |
| 59 | +* **Threshold**: Loaded from model metadata |
| 60 | +* **Features**: |
| 61 | + |
| 62 | + * PCA components: `V1` … `V28` |
| 63 | + * Engineered features: `Amount_scaled`, `Time_scaled` |
| 64 | + |
| 65 | +All inference inputs are internally aligned to the exact feature set used during training. |
| 66 | + |
| 67 | +--- |
| 68 | + |
54 | 69 | ## API Endpoints |
55 | 70 |
|
56 | | -### POST /predict |
| 71 | +### `POST /predict` |
| 72 | + |
| 73 | +Predicts fraud probability for a transaction. |
57 | 74 |
|
58 | | -Predict whether a transaction is fraudulent. |
| 75 | +An example of an input data: |
59 | 76 |
|
60 | | -**Request** |
| 77 | +**Request Body** |
61 | 78 |
|
62 | 79 | ```json |
63 | 80 | { |
64 | | - "V1": -1.23, |
65 | | - "V2": 0.45, |
66 | | - "V3": -0.67, |
67 | | - "Amount": 120.5, |
68 | | - "Time": 35000 |
| 81 | + "data": { |
| 82 | + "V1": -1.23, |
| 83 | + "V2": 0.45, |
| 84 | + "V3": -0.67, |
| 85 | + "Amount": 120.5, |
| 86 | + "Time": 35000 |
| 87 | + } |
69 | 88 | } |
70 | 89 | ``` |
71 | 90 |
|
72 | 91 | **Response** |
73 | 92 |
|
74 | 93 | ```json |
75 | 94 | { |
76 | | - "fraud_probability": 0.0342, |
| 95 | + "fraud_probability": 0.0123, |
77 | 96 | "prediction": 0 |
78 | 97 | } |
79 | 98 | ``` |
80 | 99 |
|
81 | 100 | --- |
82 | 101 |
|
83 | | -### POST /explain |
| 102 | +### `POST /explain` |
84 | 103 |
|
85 | | -Return SHAP values explaining the fraud prediction. |
| 104 | +Returns SHAP‑based explanations for a transaction. |
86 | 105 |
|
87 | | -**Response** |
| 106 | +**Request Body** |
88 | 107 |
|
89 | 108 | ```json |
90 | 109 | { |
91 | | - "V1": -0.021, |
92 | | - "V2": 0.013, |
93 | | - "Amount_scaled": 0.004 |
| 110 | + "data": { |
| 111 | + "V1": -1.23, |
| 112 | + "V2": 0.45, |
| 113 | + "V3": -0.67, |
| 114 | + "Amount": 120.5, |
| 115 | + "Time": 35000 |
| 116 | + } |
94 | 117 | } |
95 | 118 | ``` |
96 | 119 |
|
97 | | ---- |
98 | | - |
99 | | -## Running Locally (Without Docker) |
| 120 | +**Response** |
100 | 121 |
|
101 | | -```bash |
102 | | -python -m venv venv |
103 | | -source venv/bin/activate # Windows: venv\\Scripts\\activate |
104 | | -pip install -r requirements.txt |
105 | | -uvicorn app.main:app --reload |
| 122 | +```json |
| 123 | +{ |
| 124 | + "fraud_probability": 0.0123, |
| 125 | + "top_features": [ |
| 126 | + { |
| 127 | + "feature": "V14", |
| 128 | + "description": "Transaction risk signal", |
| 129 | + "shap_value": 0.345678, |
| 130 | + "impact": "increases fraud risk" |
| 131 | + } |
| 132 | + ] |
| 133 | +} |
106 | 134 | ``` |
107 | 135 |
|
108 | | -Visit: [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) |
109 | | - |
110 | 136 | --- |
111 | 137 |
|
112 | | -## Docker Setup |
| 138 | +### `GET /health` |
| 139 | + |
| 140 | +Health check endpoint. |
113 | 141 |
|
114 | | -### Dockerfile |
| 142 | +**Response** |
115 | 143 |
|
116 | | -The API is containerized using Python 3.10 slim and includes system dependencies required by LightGBM. |
| 144 | +```json |
| 145 | +{ |
| 146 | + "status": "ok" |
| 147 | +} |
| 148 | +``` |
117 | 149 |
|
118 | 150 | --- |
119 | 151 |
|
120 | | -### Docker Compose |
| 152 | +## Running Locally |
121 | 153 |
|
122 | | -Create a `docker-compose.yml` file: |
| 154 | +### 1. Clone Repository |
123 | 155 |
|
124 | | -```yaml |
125 | | -version: "3.9" |
| 156 | +```bash |
| 157 | +git clone https://github.com/SaintJeane/fraud-detection-api.git |
| 158 | +cd fraud-detection-api |
| 159 | +``` |
126 | 160 |
|
127 | | -services: |
128 | | - fraud-api: |
129 | | - build: . |
130 | | - container_name: fraud-detection-api |
131 | | - ports: |
132 | | - - "8000:8000" |
133 | | - restart: unless-stopped |
| 161 | +### 2. Create Virtual Environment |
| 162 | + |
| 163 | +```bash |
| 164 | +python -m venv venv |
| 165 | +source venv/bin/activate # Windows: venv\Scripts\activate |
| 166 | +pip install -r requirements.txt |
134 | 167 | ``` |
135 | 168 |
|
136 | | -### Run with Docker Compose |
| 169 | +### 3. Run API |
137 | 170 |
|
138 | 171 | ```bash |
139 | | -docker-compose up --build |
| 172 | +uvicorn app.main:app --reload |
140 | 173 | ``` |
141 | 174 |
|
| 175 | +Open: [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs) |
| 176 | + |
142 | 177 | --- |
143 | 178 |
|
144 | | -## GitHub Actions (CI) |
| 179 | +## Running with Docker |
145 | 180 |
|
146 | | -Create `.github/workflows/ci.yml`: |
| 181 | +### Build Image |
147 | 182 |
|
148 | | -```yaml |
149 | | -name: CI Pipeline |
| 183 | +```bash |
| 184 | +docker build -t fraud-detection-api . |
| 185 | +``` |
150 | 186 |
|
151 | | -on: |
152 | | - push: |
153 | | - branches: [ "main" ] |
154 | | - pull_request: |
155 | | - branches: [ "main" ] |
| 187 | +### Run Container |
156 | 188 |
|
157 | | -jobs: |
158 | | - build-and-test: |
159 | | - runs-on: ubuntu-latest |
| 189 | +```bash |
| 190 | +docker run -p 8000:8000 fraud-detection-api |
| 191 | +``` |
160 | 192 |
|
161 | | - steps: |
162 | | - - name: Checkout repository |
163 | | - uses: actions/checkout@v4 |
| 193 | +--- |
164 | 194 |
|
165 | | - - name: Set up Python |
166 | | - uses: actions/setup-python@v5 |
167 | | - with: |
168 | | - python-version: "3.10" |
| 195 | +## Running with Docker Compose |
169 | 196 |
|
170 | | - - name: Install dependencies |
171 | | - run: | |
172 | | - python -m pip install --upgrade pip |
173 | | - pip install -r requirements.txt |
| 197 | +```bash |
| 198 | +docker compose up --build |
| 199 | +``` |
174 | 200 |
|
175 | | - - name: Lint check |
176 | | - run: | |
177 | | - pip install flake8 |
178 | | - flake8 app --max-line-length=100 |
| 201 | +Stop services: |
179 | 202 |
|
180 | | - - name: Docker build |
181 | | - run: docker build -t fraud-detection-api . |
| 203 | +```bash |
| 204 | +docker compose down |
182 | 205 | ``` |
183 | 206 |
|
184 | 207 | --- |
185 | 208 |
|
186 | | -## Deployment Ready |
| 209 | +## Explainability Notes |
| 210 | + |
| 211 | +* SHAP values are computed using `TreeExplainer` |
| 212 | +* Binary classification output is normalized to handle SHAP API changes |
| 213 | +* Only top‑K most impactful features are returned |
| 214 | +* All SHAP values are JSON‑safe floats |
| 215 | + |
| 216 | +--- |
187 | 217 |
|
188 | | -This API is ready to be deployed on: |
| 218 | +## Tech Stack |
189 | 219 |
|
190 | | -* AWS EC2 / ECS |
191 | | -* Google Cloud Run |
192 | | -* Azure Container Apps |
193 | | -* Any Docker-compatible platform |
| 220 | +* Python 3.10 |
| 221 | +* FastAPI |
| 222 | +* LightGBM |
| 223 | +* SHAP |
| 224 | +* Pandas / NumPy |
| 225 | +* Docker & Docker Compose |
194 | 226 |
|
195 | 227 | --- |
196 | 228 |
|
197 | | -## License |
| 229 | +## Tags |
198 | 230 |
|
199 | | -MIT License |
| 231 | +`fastapi` `machine-learning` `fraud-detection` `lightgbm` `shap` `ml-api` `docker` |
200 | 232 |
|
201 | 233 | --- |
202 | 234 |
|
203 | | -## Author |
| 235 | +## License |
204 | 236 |
|
205 | | -Saint Yves |
| 237 | +MIT License |
206 | 238 |
|
207 | 239 | --- |
208 | 240 |
|
209 | | -## Status |
| 241 | +## Disclaimer⚠️ |
210 | 242 |
|
211 | | -✔ Model loaded at startup |
212 | | -✔ Deterministic feature alignment |
213 | | -✔ Explainability enabled |
214 | | -✔ Dockerized |
215 | | -✔ CI enabled |
| 243 | +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. |
0 commit comments