Skip to content

Repository files navigation

⚑ Real-Time Fraud Detection Engine

A production-grade, stream-processing fraud detection system β€” containerised end-to-end and observable in real time.

Built with Kafka (Redpanda), Redis, XGBoost + SHAP, FastAPI, Streamlit, Prometheus, and Grafana.


πŸ–ΌοΈ Live System Snapshots

Grafana β€” Real-Time Metrics Dashboard

Grafana Dashboard β€” Transactions Per Second & Fraud Detection Rate

Transactions flowing at ~5.2 TPS with a live fraud detection rate tracked per second.


Docker β€” All Services Running

Docker β€” All 8 Services Healthy

All 8 containers running in sync. The detector and ML service communicate on every transaction.


πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     Kafka Topic        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  generator.py   β”‚ ──(raw-transactions)──▢ β”‚    detector.py       β”‚
β”‚                 β”‚                         β”‚                      β”‚
β”‚ Simulates 50    β”‚                         β”‚  β€’ Velocity check    β”‚
β”‚ user profiles   β”‚                         β”‚  β€’ Amount anomaly    β”‚
β”‚ w/ fraud        β”‚                         β”‚  β€’ Location mismatch β”‚
β”‚ injection       β”‚                         β”‚  β€’ ML inference call β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                     β”‚ HTTP POST /predict
                                          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”‚    ml_service.py     β”‚
        β”‚       Redis          │◀────────▢│                      β”‚
        β”‚  User profiles &     β”‚          β”‚  XGBoost classifier  β”‚
        β”‚  TX velocity state   β”‚          β”‚  + SHAP explainabilityβ”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
                                                 β”‚        β”‚ HTTP POST /batch-analyze
                                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”  β”Œβ”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                    β”‚  Prometheus  β”‚  β”‚    app.py        β”‚
                                    β”‚  + Grafana   β”‚  β”‚  Streamlit UI    β”‚
                                    β”‚  (:8001 src) β”‚  β”‚  FraudOps Portal β”‚
                                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Services at a Glance

Service File / Image Port Role
Generator generator.py β€” Produces synthetic transactions to Kafka
Detector detector.py 8001 (metrics) Consumes, applies rules + ML, emits verdicts
ML Service ml_service.py 8000 XGBoost inference + SHAP explanations
Frontend app.py 8501 Streamlit FraudOps Portal (live view + batch)
Redpanda redpandadata/redpanda 19092 Kafka-compatible message broker
Redis redis:alpine 6379 User profiles + velocity state store
Prometheus prom/prometheus 9090 Metrics scraper
Grafana grafana/grafana 3000 Dashboards

🧠 How Fraud Is Detected

The detector applies a layered defence strategy per transaction:

  1. Rule-Based Checks (fast, synchronous)

    • πŸ“ˆ Amount Anomaly β€” flags if amount > 5Γ— the user's historical average
    • πŸš€ Velocity Anomaly β€” flags 5+ transactions within 60 seconds
    • 🌍 Location Anomaly β€” flags transactions outside the user's home region
  2. ML Inference (XGBoost via HTTP, 500 ms timeout)

    • Features: amount, user_avg_amount, amount_ratio, location_mismatch, is_international
    • Threshold: fraud probability > 80%
    • SHAP values generate human-readable explanations for every positive prediction
  3. Cold-Start Handling β€” new/unknown users fall back to safe defaults so the system never crashes on missing profiles.


πŸš€ Quickstart

Prerequisites

  • Docker & Docker Compose
  • Python 3.9+ (only for local runs / model retraining)

1 β€” Clone & spin up infrastructure

git clone <your-repo-url>
cd realtime-fraud-engine
docker compose up -d

This starts Redpanda, Redis, Prometheus, Grafana, the ML service, detector, generator, and the Streamlit frontend automatically.

2 β€” Seed user profiles into Redis

⚠️ Important: If you have a local Redis instance running on port 6379, the setup_redis.py script will hit it instead of the Docker Redis container. Use the command below to seed directly into the correct container.

# Recommended β€” seeds directly into the Docker Redis container (works regardless of local Redis)
bash -c '
LOCATIONS=("NY" "CA" "TX" "FL" "IL")
{ echo "FLUSHALL"
  for i in $(seq -w 0 49); do
    LOC="${LOCATIONS[$((RANDOM % 5))]}"
    AVG=$(awk "BEGIN{printf \"%.2f\", 10 + rand() * 140}")
    STD=$(awk "BEGIN{printf \"%.2f\", 2 + rand() * 13}")
    echo "SET user_profile:usr_${i} {\"user_id\":\"usr_${i}\",\"base_location\":\"${LOC}\",\"avg_transaction_amount\":${AVG},\"std_dev_amount\":${STD}}"
  done
} | docker exec -i redis redis-cli --pipe
'

Creates 50 synthetic user profiles, each with a home location, average spend, and spend volatility.

3 β€” Start the application stack

docker compose up --build

Or run each service individually for development:

# Terminal 1 β€” ML inference API
uvicorn ml_service:app --reload --port 8000

# Terminal 2 β€” Fraud detector
python detector.py

# Terminal 3 β€” Transaction generator
python generator.py

# Terminal 4 β€” Streamlit FraudOps Portal
streamlit run app.py --server.port 8501

4 β€” Train the ML model (optional)

python train_model.py

Generates ~10,000 labelled transactions, trains an XGBoost classifier, fits a SHAP TreeExplainer, and saves both as pickle artifacts (fraud_model.pkl, shap_explainer.pkl).

Pre-trained artifacts are already committed β€” skip this step if you just want to run the engine.


πŸ—‚οΈ Project Structure

realtime-fraud-engine/
β”œβ”€β”€ generator.py        # Synthetic transaction producer (Kafka)
β”œβ”€β”€ detector.py         # Core stream processor & fraud logic
β”œβ”€β”€ ml_service.py       # FastAPI ML inference endpoint (/predict & /batch-analyze)
β”œβ”€β”€ app.py              # Streamlit FraudOps Portal (live monitoring + batch analysis)
β”œβ”€β”€ train_model.py      # XGBoost + SHAP model training script
β”œβ”€β”€ setup_redis.py      # Seeds user profiles into Redis
β”œβ”€β”€ consumer.py         # Lightweight debug consumer (print-only)
β”œβ”€β”€ test_batch.csv      # Sample CSV for batch analysis testing
β”œβ”€β”€ fraud_model.pkl     # Pre-trained XGBoost model artifact
β”œβ”€β”€ shap_explainer.pkl  # Pre-trained SHAP TreeExplainer artifact
β”œβ”€β”€ requirements.txt    # Python dependencies
β”œβ”€β”€ Dockerfile          # Single image for all Python services
β”œβ”€β”€ docker-compose.yml  # Full stack orchestration (8 services)
β”œβ”€β”€ prometheus.yml      # Prometheus scrape config
└── screenshots/        # Live system snapshots
    β”œβ”€β”€ grafana_dashboard.png
    └── docker_services.png

βš™οΈ Configuration

All services read configuration from environment variables with sensible defaults:

Variable Default Description
KAFKA_BROKER localhost:19092 Kafka / Redpanda bootstrap servers
KAFKA_TOPIC raw-transactions Input topic name
OUT_TOPIC processed-transactions Output topic for verdicts
REDIS_HOST localhost Redis hostname
REDIS_PORT 6379 Redis port
ML_SERVICE_URL http://localhost:8000/predict ML inference endpoint (streaming)
ML_SERVICE_URL_BATCH http://localhost:8000/batch-analyze ML batch analysis endpoint
PROMETHEUS_URL http://localhost:9090 Prometheus base URL for Streamlit UI
FRAUD_PROBABILITY 0.05 Fraction of injected fraud events
TRANSACTIONS_PER_SECOND 5.0 Generator throughput rate

When running with Docker Compose these are automatically wired to the correct service hostnames.


πŸ“Š Observability

The detector exposes a Prometheus metrics endpoint on port 8001:

Metric Type Description
transactions_processed_total Counter Total transactions consumed
fraud_caught_total Counter Total transactions flagged as fraud
transaction_processing_seconds Histogram Per-transaction processing latency

Access Dashboards & UIs

Tool URL
FraudOps Portal http://localhost:8501 (Streamlit live + batch)
Prometheus http://localhost:9090
Grafana http://localhost:3000 (default: admin/admin)
ML API Docs http://localhost:8000/docs (Swagger UI)
Raw Metrics http://localhost:8001/metrics

To visualise fraud metrics in Grafana, add Prometheus as a data source (http://prometheus:9090) and create panels using the metrics above.


πŸ”Œ ML Service API

POST /predict β€” Real-Time Single Transaction

// Request
{
  "amount": 850.00,
  "user_avg_amount": 45.00,
  "location_mismatch": 1,
  "is_international": 0
}

// Response β€” Fraud detected
{
  "fraud_probability": 0.9995,
  "is_fraud": true,
  "explanation": [
    "Transaction location does not match user's typical region.",
    "Transaction amount is unusually high compared to user history."
  ]
}

// Response β€” Normal transaction
{
  "fraud_probability": 0.0315,
  "is_fraud": false,
  "explanation": []
}

POST /batch-analyze β€” CSV Batch Analysis

Accepts a multipart CSV upload with columns: transaction_id, user_id, amount, location. Looks up each user's profile from Redis, engineers features, runs XGBoost inference + SHAP, and returns annotated results.

// Response
{
  "analyzed_transactions": [
    {
      "transaction_id": "txn_001",
      "user_id": "usr_12",
      "amount": 1200.0,
      "location": "TX",
      "is_fraud": true,
      "fraud_probability": 0.9812,
      "reasons": "Amount unusually high vs history., Location mismatch."
    }
  ]
}

Use the FraudOps Portal at http://localhost:8501 for an interactive drag-and-drop batch analysis UI with a downloadable annotated report.

Interactive API docs available at /docs (Swagger UI).


πŸ› οΈ Tech Stack

Layer Technology
Message Broker Redpanda (Kafka-compatible)
Stream Processor Python + confluent-kafka
State Store Redis
ML Model XGBoost 1.7.6
Explainability SHAP (TreeExplainer)
Inference API FastAPI + Uvicorn
Frontend Streamlit
Observability Prometheus + Grafana
Containerisation Docker / Docker Compose

πŸ“„ License

MIT β€” feel free to use, modify, and distribute.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages