Skip to content

Commit a217792

Browse files
committed
Add CI pipeline with Docker build
1 parent 803e724 commit a217792

3 files changed

Lines changed: 178 additions & 112 deletions

File tree

.github/workflows/ci.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ "main", "master" ]
6+
pull_request:
7+
branches: [ "main", "master" ]
8+
9+
jobs:
10+
build-and-test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@v4
16+
17+
- name: Set up Python
18+
uses: actions/setup-python@v5
19+
with:
20+
python-version: "3.10"
21+
22+
- name: Install dependencies
23+
run: |
24+
python -m pip install --upgrade pip
25+
pip install -r requirements.txt
26+
27+
- name: Import check (fail fast)
28+
run: |
29+
python - << 'EOF'
30+
import app.main
31+
import app.predict
32+
import app.explain
33+
print("Imports successful")
34+
EOF
35+
36+
- name: Build Docker image
37+
run: docker build -t fraud-detection-api .

README.md

Lines changed: 140 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,215 +1,243 @@
11
# Fraud Detection API
22

3-
## Tags
4-
5-
`machine-learning` `fraud-detection` `lightgbm` `fastapi` `shap` `docker` `api` `mlops`
6-
7-
---
3+
## Overview
84

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.
106

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:
128

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
1412

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**.
1714

1815
---
1916

20-
## Tech Stack
17+
## Key Features
2118

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
2926

3027
---
3128

3229
## Project Structure
3330

34-
```
31+
```text
3532
fraud-detection-api/
3633
├── 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
4242
├── models/
4343
│ ├── lgbm_tuned.pkl
4444
│ ├── scaler.pkl
4545
│ └── lgbm_metadata.json
46-
├── docker-compose.yml
4746
├── Dockerfile
47+
├── docker-compose.yml
4848
├── requirements.txt
49-
└── README.md
49+
├── README.md
50+
└── .gitignore
5051
```
5152

5253
---
5354

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+
5469
## API Endpoints
5570

56-
### POST /predict
71+
### `POST /predict`
72+
73+
Predicts fraud probability for a transaction.
5774

58-
Predict whether a transaction is fraudulent.
75+
An example of an input data:
5976

60-
**Request**
77+
**Request Body**
6178

6279
```json
6380
{
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+
}
6988
}
7089
```
7190

7291
**Response**
7392

7493
```json
7594
{
76-
"fraud_probability": 0.0342,
95+
"fraud_probability": 0.0123,
7796
"prediction": 0
7897
}
7998
```
8099

81100
---
82101

83-
### POST /explain
102+
### `POST /explain`
84103

85-
Return SHAP values explaining the fraud prediction.
104+
Returns SHAP‑based explanations for a transaction.
86105

87-
**Response**
106+
**Request Body**
88107

89108
```json
90109
{
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+
}
94117
}
95118
```
96119

97-
---
98-
99-
## Running Locally (Without Docker)
120+
**Response**
100121

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+
}
106134
```
107135

108-
Visit: [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs)
109-
110136
---
111137

112-
## Docker Setup
138+
### `GET /health`
139+
140+
Health check endpoint.
113141

114-
### Dockerfile
142+
**Response**
115143

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+
```
117149

118150
---
119151

120-
### Docker Compose
152+
## Running Locally
121153

122-
Create a `docker-compose.yml` file:
154+
### 1. Clone Repository
123155

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+
```
126160

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
134167
```
135168

136-
### Run with Docker Compose
169+
### 3. Run API
137170

138171
```bash
139-
docker-compose up --build
172+
uvicorn app.main:app --reload
140173
```
141174

175+
Open: [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs)
176+
142177
---
143178

144-
## GitHub Actions (CI)
179+
## Running with Docker
145180

146-
Create `.github/workflows/ci.yml`:
181+
### Build Image
147182

148-
```yaml
149-
name: CI Pipeline
183+
```bash
184+
docker build -t fraud-detection-api .
185+
```
150186

151-
on:
152-
push:
153-
branches: [ "main" ]
154-
pull_request:
155-
branches: [ "main" ]
187+
### Run Container
156188

157-
jobs:
158-
build-and-test:
159-
runs-on: ubuntu-latest
189+
```bash
190+
docker run -p 8000:8000 fraud-detection-api
191+
```
160192

161-
steps:
162-
- name: Checkout repository
163-
uses: actions/checkout@v4
193+
---
164194

165-
- name: Set up Python
166-
uses: actions/setup-python@v5
167-
with:
168-
python-version: "3.10"
195+
## Running with Docker Compose
169196

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+
```
174200

175-
- name: Lint check
176-
run: |
177-
pip install flake8
178-
flake8 app --max-line-length=100
201+
Stop services:
179202

180-
- name: Docker build
181-
run: docker build -t fraud-detection-api .
203+
```bash
204+
docker compose down
182205
```
183206

184207
---
185208

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+
---
187217

188-
This API is ready to be deployed on:
218+
## Tech Stack
189219

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
194226

195227
---
196228

197-
## License
229+
## Tags
198230

199-
MIT License
231+
`fastapi` `machine-learning` `fraud-detection` `lightgbm` `shap` `ml-api` `docker`
200232

201233
---
202234

203-
## Author
235+
## License
204236

205-
Saint Yves
237+
MIT License
206238

207239
---
208240

209-
## Status
241+
## Disclaimer⚠️
210242

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.

app/logging_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'''Structured logging'''
12
import logging
23

34
logging.basicConfig(

0 commit comments

Comments
 (0)