Skip to content

prince3235/Transaction-Fraud-Intelligence

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

79 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Typing SVG

End-to-end fraud detection platform β€” ML scoring, rule engine, explainability, and live simulation in one system.


Python FastAPI Streamlit Scikit-Learn SQLite License


πŸš€ Live Demo Β· πŸ“Š Features Β· πŸ—οΈ Architecture Β· πŸ“Έ Screenshots Β· πŸ“– API Docs


🎯 What Problem Does This Solve?

Banks process millions of transactions daily. A human can't review each one. Traditional fraud systems are black boxes β€” they flag transactions but can't explain why. Compliance teams need audit trails. Business teams need explainability.

This system solves all three:

Challenge This System's Solution
πŸ”΄ Real-time detection ML scoring on every transaction in < 100ms
⚫ Black-box decisions SHAP explainability + policy reason tracking
πŸ“‹ Audit compliance Full SQLite log of every prediction + override
πŸ’Έ False positives Hybrid model β€” ML + business rules in layers
πŸ“Š No visibility Live SaaS dashboard with trend analytics

πŸ“Έ Screenshots

πŸ–₯️ Main Dashboard β€” KPIs + Risk Distribution

Dashboard Hero

⚑ Live Transaction Simulator

Simulator

πŸ“‹ Transaction Feed + Risk Badges

Live Feed

πŸ“Š Policy Override Analysis

Override Analysis

UI Design: Custom CSS design system β€” glassmorphism cards, spline charts, gradient fills. Zero default Streamlit components. Built to look like a real fintech SaaS product.


✨ Key Features

🧠 Machine Learning Engine

  • Random Forest classifier β€” trained on 9,600+ financial transactions
  • 20+ engineered features β€” balance errors, velocity ratios, behavioral signals, time patterns
  • Imbalanced data handling β€” undersampling + class weighting
  • Evaluation metrics β€” ROC-AUC, PR-AUC, Precision/Recall at multiple thresholds

πŸ”’ Policy Override Engine (5 Rules)

Rule 1: Amount > sender balance + account emptied  β†’  CRITICAL override
Rule 2: Multiple suspicious signals (β‰₯3)           β†’  HIGH override  
Rule 3: Very high signal count (β‰₯5)                β†’  CRITICAL override
Rule 4: Large transaction + zero-balance dest.     β†’  HIGH override
Rule 5: Velocity flag + new device                 β†’  HIGH override

🎨 Premium Streamlit Dashboard

  • Live Transaction Simulator β€” inject single or batch transactions, charts update instantly
  • Session State architecture β€” no page reload required
  • 5 KPI cards β€” total transactions, critical alerts, high risk, override rate, avg score
  • Risk distribution donut β€” always shows full dataset, unaffected by filters
  • Alert trend line chart β€” spline curves, gradient fills, simulated points highlighted

⚑ Live Simulation Engine

  • Build any transaction manually β€” merchant, country, amount, hour, signals
  • Real-time risk preview β€” score updates as you move sliders
  • Batch simulate up to 100 transactions with custom fraud mix %
  • Session stats β€” injected count, high risk, critical breakdown

🌐 FastAPI Backend

  • POST /predict β€” fraud prediction with full breakdown
  • GET /logs β€” paginated audit trail
  • GET /stats β€” aggregate metrics
  • GET /debug/{id} β€” feature-level explanation
  • POST /admin/seed-logs β€” test data generation

πŸ“– SHAP Explainability

  • Global feature importance visualization
  • Per-transaction explanation β€” "why was this flagged?"
  • Policy trigger reasons tracked and displayed

πŸ—οΈ System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        DATA FLOW                                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

  Transaction Input
        β”‚
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Feature Engine   β”‚  ← 20+ derived features
β”‚  (engineering.py) β”‚     balance_error, velocity,
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     amount_ratio, time_flags
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   ML Scorer       β”‚        β”‚  Policy Engine    β”‚
β”‚  Random Forest    │──────▢ β”‚  5 Business Rules β”‚
β”‚  β†’ probability    β”‚        β”‚  β†’ override flag  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                      β”‚
                                      β–Ό
                             β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                             β”‚  Decision Layer   β”‚
                             β”‚  Final Risk Level β”‚
                             β”‚  LOW/MED/HIGH/    β”‚
                             β”‚  CRITICAL         β”‚
                             β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                      β”‚
               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
               β–Ό                      β–Ό                      β–Ό
      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
      β”‚  SQLite Log    β”‚    β”‚  FastAPI JSON   β”‚    β”‚  Streamlit       β”‚
      β”‚  Audit Trail   β”‚    β”‚  Response       β”‚    β”‚  Dashboard       β”‚
      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Project Structure

Transaction-Fraud-Intelligence/
β”‚
β”œβ”€β”€ πŸ“‚ app/                          # Streamlit Dashboard
β”‚   β”œβ”€β”€ Home.py                      # Main dashboard entry point
β”‚   β”œβ”€β”€ premium_design.py            # CSS design system
β”‚   β”œβ”€β”€ simulator.py                 # Live ML simulation engine
β”‚   └── utils_dashboard.py           # DB query helpers
β”‚
β”œβ”€β”€ πŸ“‚ pages/                        # Multi-page Streamlit
β”‚   β”œβ”€β”€ 1_Alerts_Monitoring.py
β”‚   β”œβ”€β”€ 2_Model_Performance.py
β”‚   └── 3_Transaction_Simulator.py
β”‚
β”œβ”€β”€ πŸ“‚ api/                          # FastAPI Backend
β”‚   └── main.py                      # All API endpoints
β”‚
β”œβ”€β”€ πŸ“‚ data/
β”‚   β”œβ”€β”€ processed/                   # Cleaned, feature-engineered data
β”‚   └── raw/                         # Original source data (gitignored)
β”‚
β”œβ”€β”€ πŸ“‚ models/                       # Saved ML models (.pkl)
β”‚
β”œβ”€β”€ πŸ“‚ notebooks/                    # Jupyter analysis (8 notebooks)
β”‚   β”œβ”€β”€ 01_data_understanding.ipynb
β”‚   β”œβ”€β”€ 02_eda.ipynb
β”‚   β”œβ”€β”€ 03_data_preprocessing.ipynb
β”‚   β”œβ”€β”€ 04_feature_engineering.ipynb
β”‚   β”œβ”€β”€ 05_model_building.ipynb
β”‚   β”œβ”€β”€ 06_model_evaluation.ipynb
β”‚   └── 07_explainability_shap.ipynb
β”‚
β”œβ”€β”€ πŸ“‚ reports/                      # Generated charts and PDFs
β”œβ”€β”€ πŸ“‚ scripts/                      # One-time utility scripts
β”‚
β”œβ”€β”€ .gitignore
β”œβ”€β”€ requirements.txt
└── README.md

πŸš€ Quick Start

Prerequisites

Python 3.12+
pip

Installation

# 1. Clone the repository
git clone https://github.com/prince3235/Transaction-Fraud-Intelligence.git
cd Transaction-Fraud-Intelligence

# 2. Install dependencies
pip install -r requirements.txt

# 3. Seed the database with sample data
python scripts/seed_data.py

# 4. Launch the dashboard
streamlit run app/Home.py

Run the API

# In a separate terminal
uvicorn api.main:app --reload

# API docs available at:
# http://localhost:8000/docs

Dashboard runs on http://localhost:8501 API runs on http://localhost:8000


πŸ“– API Reference

Method Endpoint Description
POST /predict Score a transaction β€” returns risk level + reasons
GET /logs Paginated audit trail of all predictions
GET /stats Aggregate metrics β€” counts, rates, distributions
GET /debug/{id} Feature-level breakdown for a specific transaction
POST /admin/seed-logs Generate sample transaction data

Example Request

curl -X POST "http://localhost:8000/predict" \
  -H "Content-Type: application/json" \
  -d '{
    "amount": 85000,
    "oldbalanceOrg": 90000,
    "newbalanceOrig": 0,
    "type": "TRANSFER",
    "step": 2
  }'

Example Response

{
  "transaction_id": 9628,
  "ml_probability": 0.8934,
  "ml_risk_level": "CRITICAL",
  "final_risk_level": "CRITICAL",
  "final_risk_score": 94,
  "policy_override_applied": true,
  "policy_reasons": [
    "Policy: Amount > sender balance + sender account emptied",
    "Policy: Multiple suspicious signals detected (>=3)"
  ],
  "recommended_action": "HOLD",
  "explanation": "High transfer amount with complete account drainage detected."
}

🧠 ML Model Details

Metric Score
ROC-AUC 0.97+
PR-AUC 0.89+
Precision (CRITICAL) 0.91
Recall (CRITICAL) 0.88
F1 Score 0.89

Top Features by SHAP Importance:

  1. balance_error β€” amount vs balance discrepancy
  2. amount_to_orig_ratio β€” transaction size relative to account
  3. is_high_amount β€” absolute amount threshold flag
  4. transaction_hour β€” time-of-day risk signal
  5. velocity_flag β€” unusual transaction frequency

πŸ› οΈ Tech Stack

Layer Technology
ML Model Scikit-learn (Random Forest)
Explainability SHAP
Backend API FastAPI + Uvicorn
Dashboard Streamlit
Charts Plotly (spline, donut, gradient fills)
Database SQLite
Data Pandas, NumPy
UI Design Custom CSS β€” DM Sans, glassmorphism, CSS variables

πŸ—ΊοΈ Roadmap

  • ML model training + evaluation
  • Policy override engine (5 rules)
  • FastAPI backend with audit logging
  • Premium Streamlit dashboard
  • Live transaction simulator
  • SHAP explainability
  • Email/Slack alerts on CRITICAL detection
  • Docker containerization
  • Real-time streaming (Kafka integration)
  • Model retraining pipeline

🀝 Contributing

Pull requests are welcome! For major changes, please open an issue first.

git checkout -b feature/your-feature
git commit -m "feat: add your feature"
git push origin feature/your-feature

πŸ“„ License

This project is licensed under the MIT License β€” see LICENSE for details.


Built with obsession over 3 weeks πŸ”₯

If this helped you β€” give it a ⭐ on GitHub

GitHub stars

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors