Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

13 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ›ก๏ธ Anti-Fraud Detection API

Hybrid real-time fraud detection microservice Redis Velocity Check + LightGBM + A/B Testing Router โ€” production-ready architecture


๐Ÿš€ Live Demo

Endpoint Link
๐Ÿ  API Root fraud-detection-api-1-hf78.onrender.com
๐Ÿ“– Swagger UI fraud-detection-api-1-hf78.onrender.com/docs
โค๏ธ Health Check fraud-detection-api-1-hf78.onrender.com/health
๐Ÿ’ณ Test Cards fraud-detection-api-1-hf78.onrender.com/cards

๐Ÿ“Š Architecture โ€” Hybrid Fraud Guardian

Incoming Transaction (card_id + amount only)
        โ”‚
        โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Layer 1: Redis Velocity Check         โ”‚
โ”‚   Checks transaction frequency          โ”‚
โ”‚   Protection against brute force        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Redis Feature Store                   โ”‚
โ”‚   Client sends only card_id + amount    โ”‚
โ”‚   API fetches full historical profile   โ”‚
โ”‚   (V1-V28 features) from Redis          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   A/B Testing Router                    โ”‚
โ”‚   80% โ†’ Model A (Champion)              โ”‚
โ”‚   20% โ†’ Model B (Challenger)            โ”‚
โ”‚   Safe testing of new hypotheses        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   LightGBM Inference                    โ”‚
โ”‚   Class Imbalance handling              โ”‚
โ”‚   Fraud probability โ†’ APPROVE / BLOCK   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ›  Tech Stack

Component Technology
Machine Learning LightGBM (Class Imbalance handling)
Feature Store Redis (Upstash โ€” in-memory profiles)
Backend FastAPI, Pydantic
A/B Testing Custom 80/20 Champion/Challenger Router
Containerization Docker, Docker Compose
Deployment Render
Dataset MLG-ULB Credit Card Fraud (Kaggle)

๐Ÿ”‘ Key Features

1. Redis Feature Store

Client sends only card_id and amount โ€” the API fetches the full historical profile (V1-V28 features) from Redis automatically. This mirrors production anti-fraud systems at Visa and Mastercard:

# Client sends minimal data
{"card_id": "card_0001", "amount": 500.00}

# API enriches with Redis profile internally
features = cache.get(f"profile:{card_id}")  # V1-V28 from Redis
features["Amount"] = amount                  # Add current transaction

2. A/B Testing Router (Champion/Challenger)

Built-in traffic splitter tests new model versions safely in production:

if random.random() < 0.20:
    active_model = ml_models["model_b"]  # Challenger โ€” 20%
else:
    active_model = ml_models["model_a"]  # Champion โ€” 80%

No downtime, no risk โ€” new models proven before full rollout.

3. Class Imbalance Handling

Credit card fraud datasets are heavily imbalanced (0.17% fraud). LightGBM trained with scale_pos_weight to properly detect rare fraud events.


โšก API Reference

GET /

{
  "service": "Anti-Fraud Detection API",
  "architecture": "Hybrid: Redis Feature Store + LightGBM",
  "features": {
    "ab_testing": "80/20 Champion/Challenger split",
    "feature_store": "Redis in-memory profiles (Upstash)",
    "model": "LightGBM with Class Imbalance handling"
  }
}

GET /health

{
  "status": "healthy",
  "redis": "connected",
  "models_loaded": ["model_a", "model_b"],
  "ab_split": "80% Model A / 20% Model B"
}

GET /cards

{
  "available_cards": ["card_0001", "card_0002", "..."],
  "total": 101,
  "test_cards": {
    "normal": "card_0001",
    "fraud": "fraud_card_001"
  }
}

POST /scan

Normal transaction:

// Request
{
  "card_id": "card_0001",
  "amount": 500.00
}

// Response
{
  "card_id": "card_0001",
  "amount": 500.0,
  "decision": "APPROVE",
  "risk_level": "LOW",
  "risk_probability": 0.0821,
  "model_used": "Model A (Champion)",
  "feature_store": "Redis (Upstash)"
}

Fraud transaction:

// Request
{
  "card_id": "fraud_card_001",
  "amount": 150000.00
}

// Response
{
  "card_id": "fraud_card_001",
  "amount": 150000.0,
  "decision": "BLOCK",
  "risk_level": "HIGH",
  "risk_probability": 0.8934,
  "model_used": "Model A (Champion)",
  "feature_store": "Redis (Upstash)"
}

๐Ÿš€ Quick Start (Local)

1. Clone the repository

git clone https://github.com/RaNurbekov/fraud-detection-api.git
cd fraud-detection-api

2. Start with Docker Compose

docker-compose up --build

This starts both Redis and FastAPI containers automatically.

3. Test the API

# Normal transaction
curl -X POST "http://localhost:8000/scan" \
  -H "Content-Type: application/json" \
  -d '{"card_id": "card_0001", "amount": 500}'

# Fraud transaction
curl -X POST "http://localhost:8000/scan" \
  -H "Content-Type: application/json" \
  -d '{"card_id": "fraud_card_001", "amount": 150000}'

๐Ÿ“ Project Structure

fraud-detection-api/
โ”œโ”€โ”€ api.py                  # FastAPI: /scan, /health, /cards
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ database.py         # SQLite audit logging
โ”œโ”€โ”€ models/
โ”‚   โ”œโ”€โ”€ lgbm_fraud.pkl      # Champion model (Model A)
โ”‚   โ””โ”€โ”€ lgbm_fraud_b.pkl    # Challenger model (Model B)
โ”œโ”€โ”€ notebooks/              # Training & EDA
โ”œโ”€โ”€ docker-compose.yml      # Redis + FastAPI orchestration
โ”œโ”€โ”€ Dockerfile
โ”œโ”€โ”€ requirements.txt
โ””โ”€โ”€ README.md

๐Ÿ”ฎ Roadmap

Feature Description
Streamlit Dashboard Visual A/B testing stats + fraud monitoring
MLflow Integration Model versioning and experiment tracking
Evidently AI Data drift monitoring in production
PostgreSQL Replace SQLite with production-grade DB
Kafka Integration Connect with kafka-fraud-streaming pipeline

๐Ÿ”— Related Projects

Part of a Fintech ML ecosystem:

๐Ÿ’ก Production vision: Kafka streams transactions โ†’ this API scores them in real-time โ†’ GNN catches collaborative fraud rings โ†’ credit-risk-api assesses customer risk. That's the complete fintech ML stack.


๐Ÿ“ซ Author

Rashid Nurbekov โ€” ML Engineer | Fintech & Generative AI | Almaty, Kazakhstan ๐Ÿ‡ฐ๐Ÿ‡ฟ

Telegram Email GitHub

About

Hybrid fraud detection API: Redis Feature Store + LightGBM + A/B Testing Router. Live on Render.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages