Skip to content

Akkii88/Secure180

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›‘οΈ Secure180

Real-Time Credit Card Fraud Detection System with AutoML

An end-to-end AI-powered fraud detection pipeline that generates synthetic transactions, trains models using AutoML (PyCaret), detects fraud in real-time via WebSockets, and visualizes everything on a live cyberpunk dashboard.


🎯 Overview

Secure180 is a complete real-time fraud detection system that simulates a production environment for credit card transaction monitoring. It combines AutoML model training, real-time transaction simulation, instant fraud scoring, and a live cyberpunk-themed dashboard β€” all in a single, self-contained application.

Key Highlights

Feature Description
πŸ€– AutoML Training PyCaret compares 8+ ML algorithms and selects the best one automatically
πŸ“‘ Real-Time Streaming WebSocket-based live data feed β€” transactions every 2 seconds
🚨 Instant Alerts Color-coded terminal alerts + dashboard toast notifications
🌐 Interactive Globe 3D globe visualization showing fraud origin locations
πŸ“Š 6 Live Charts Doughnut, line, radar, bar, hourly, and geospatial visualizations
⚑ Sub-100ms Detection Millisecond-level fraud scoring per transaction
πŸ—„οΈ Persistent Storage SQLite database for transaction history and model metrics

πŸ—οΈ System Architecture

graph TB
    subgraph "Data Layer"
        A[("πŸ—ƒοΈ Synthetic Data<br/>Generator")] -->|50K transactions| B[("πŸ“ creditcard_sample.csv")]
        B --> C["βš–οΈ SMOTE<br/>Balancing"]
    end

    subgraph "ML Layer"
        C --> D["πŸ”§ PyCaret Setup<br/>normalize + fix_imbalance"]
        D --> E["πŸ† compare_models()<br/>8 algorithms"]
        E --> F["🎯 tune_model()<br/>Hyperparameter Tuning"]
        F --> G[("πŸ’Ύ best_fraud_model.pkl")]
    end

    subgraph "Application Layer"
        H["🎲 Transaction<br/>Simulator"] -->|every 2s| I["πŸ›‘οΈ Fraud<br/>Detector"]
        G --> I
        I --> J["πŸ—„οΈ SQLite<br/>Database"]
        I --> K["🚨 Alert<br/>System"]
        I --> L["πŸ“‘ WebSocket<br/>Broadcast"]
    end

    subgraph "API Layer (FastAPI)"
        L --> M["🌐 REST Endpoints"]
        L --> N["πŸ”Œ WebSocket /ws"]
        M --> O["πŸ“Š Dashboard"]
        N --> O
    end

    style A fill:#1a1a2e,stroke:#00d4ff,color:#fff
    style G fill:#1a1a2e,stroke:#00ff9d,color:#fff
    style I fill:#1a1a2e,stroke:#ff2d55,color:#fff
    style O fill:#1a1a2e,stroke:#a855f7,color:#fff
Loading

πŸ”„ Data Pipeline

flowchart LR
    A["make_classification()<br/>10K samples, 29 features"] --> B["Feature Engineering<br/>V1-V28 + Amount + Time"]
    B --> C["Class Imbalance<br/>99% legit, 1% fraud"]
    C --> D["SMOTE Oversampling<br/>Balance to ~50/50"]
    D --> E["Export CSV<br/>creditcard_sample.csv"]

    style A fill:#0d1b2a,stroke:#00d4ff,color:#e0e0e0
    style B fill:#0d1b2a,stroke:#00d4ff,color:#e0e0e0
    style C fill:#0d1b2a,stroke:#ffb800,color:#e0e0e0
    style D fill:#0d1b2a,stroke:#00ff9d,color:#e0e0e0
    style E fill:#0d1b2a,stroke:#a855f7,color:#e0e0e0
Loading

🧠 ML Pipeline

flowchart TD
    A["πŸ“₯ Load Dataset"] --> B["βš™οΈ PyCaret Setup<br/>normalize=True<br/>fix_imbalance=True<br/>session_id=42"]
    B --> C["🏁 compare_models()"]
    
    C --> D1["Random Forest"]
    C --> D2["Extra Trees"]
    C --> D3["Gradient Boosting"]
    C --> D4["Logistic Regression"]
    C --> D5["Decision Tree"]
    C --> D6["KNN"]
    C --> D7["Naive Bayes"]
    C --> D8["AdaBoost"]
    
    D1 & D2 & D3 & D4 & D5 & D6 & D7 & D8 --> E["πŸ† Best Model<br/>Selected by F1 Score"]
    E --> F["πŸ”§ tune_model()<br/>Hyperparameter Optimization"]
    F --> G["πŸ’Ύ Save Model<br/>best_fraud_model.pkl"]
    F --> H["πŸ“Š Save Metrics<br/>model_comparison.json"]

    style E fill:#1a1a2e,stroke:#00ff9d,color:#fff
    style F fill:#1a1a2e,stroke:#ffb800,color:#fff
    style G fill:#1a1a2e,stroke:#00d4ff,color:#fff
Loading

Model Comparison Metrics

Metric Description
F1 Score Primary selection criterion β€” harmonic mean of precision and recall
AUC Area Under ROC Curve β€” overall discriminative ability
Precision Ratio of true positives to predicted positives
Recall Ratio of true positives to actual positives
Accuracy Overall correct predictions

✨ Features

πŸ”¬ Detection Engine

  • AutoML model selection across 8 classification algorithms
  • Hyperparameter tuning with F1 optimization
  • Three-tier risk classification: LOW / MEDIUM / HIGH
  • Configurable thresholds (fraud: 0.5, high-risk: 0.7)

πŸ“‘ Real-Time Processing

  • WebSocket streaming β€” live transaction feed to all connected clients
  • 2-second intervals β€” simulated transaction generation
  • Broadcast architecture β€” multiple dashboard instances supported
  • Automatic stats refresh β€” KPIs update every 5 transactions

πŸ“Š Dashboard Visualizations

  • Doughnut Chart β€” Legit vs. fraud transaction ratio
  • Live Line Chart β€” Streaming transaction amounts (last 60)
  • Radar Chart β€” Fraud distribution by merchant category
  • Bar Chart β€” Fraud rate per store category
  • 3D Globe β€” Geographic fraud origin visualization
  • Hourly Chart β€” Fraud activity by hour of day

🚨 Alert System

  • Terminal alerts with color-coded severity (🚨 RED / 🟑 YELLOW / βœ… GREEN)
  • Dashboard toast notifications for fraud events
  • Persistent fraud log file for audit trail
  • Risk-level badges on all transaction records

πŸ› οΈ Tech Stack

Component Technology Purpose
Backend FastAPI + Uvicorn REST API + WebSocket server
ML Engine PyCaret 3.3 AutoML training and prediction
Database SQLite + SQLAlchemy Transaction & model storage
Frontend Vanilla HTML/CSS/JS Single-file cyberpunk dashboard
Charts Chart.js 4.4 Interactive data visualizations
Globe Globe.gl 3D geospatial fraud mapping
Data Gen scikit-learn + imbalanced-learn Synthetic dataset + SMOTE
Streaming WebSockets Real-time bi-directional communication

πŸ“ Project Structure

Secure360/
└── fraud_detection_system/
    β”œβ”€β”€ run_system.py              # πŸš€ Main entry point β€” orchestrates everything
    β”œβ”€β”€ config.py                  # βš™οΈ Global configuration (thresholds, paths)
    β”œβ”€β”€ requirements.txt           # πŸ“¦ Python dependencies
    β”‚
    β”œβ”€β”€ api/
    β”‚   β”œβ”€β”€ __init__.py
    β”‚   └── main_api.py            # 🌐 FastAPI app β€” REST + WebSocket endpoints
    β”‚
    β”œβ”€β”€ models/
    β”‚   β”œβ”€β”€ __init__.py
    β”‚   β”œβ”€β”€ automl_trainer.py      # πŸ€– PyCaret AutoML training pipeline
    β”‚   └── saved_model/
    β”‚       └── best_fraud_model.pkl  # πŸ’Ύ Trained model (generated)
    β”‚
    β”œβ”€β”€ realtime/
    β”‚   β”œβ”€β”€ __init__.py
    β”‚   β”œβ”€β”€ fraud_detector.py      # πŸ›‘οΈ Real-time prediction engine
    β”‚   └── transaction_simulator.py # 🎲 Synthetic transaction generator
    β”‚
    β”œβ”€β”€ database/
    β”‚   β”œβ”€β”€ __init__.py
    β”‚   β”œβ”€β”€ db_handler.py          # πŸ—„οΈ SQLite operations & queries
    β”‚   └── fraud_detection.db     # πŸ“€ Database file (generated)
    β”‚
    β”œβ”€β”€ alerts/
    β”‚   β”œβ”€β”€ __init__.py
    β”‚   β”œβ”€β”€ alert_system.py        # 🚨 Terminal alerts & fraud logging
    β”‚   └── fraud_alerts.log       # πŸ“ Fraud audit log (generated)
    β”‚
    β”œβ”€β”€ data/
    β”‚   β”œβ”€β”€ __init__.py
    β”‚   β”œβ”€β”€ generate_dataset.py    # πŸ“Š Synthetic data + SMOTE pipeline
    β”‚   β”œβ”€β”€ creditcard_sample.csv  # πŸ“ Generated dataset (50K rows)
    β”‚   └── model_comparison.json  # πŸ“ˆ AutoML comparison results
    β”‚
    └── dashboard/
        β”œβ”€β”€ __init__.py
        └── index.html             # 🎨 Cyberpunk dashboard (single file)

πŸš€ Quick Start

Prerequisites

  • Python 3.10+ (required for PyCaret 3.x compatibility)
  • pip package manager

Installation

# 1. Clone the repository
git clone https://github.com/<your-username>/Secure360.git
cd Secure360/fraud_detection_system

# 2. Create virtual environment
python3 -m venv .venv
source .venv/bin/activate  # macOS/Linux
# .venv\Scripts\activate   # Windows

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

# 4. Launch the system
python run_system.py

What Happens on Launch

sequenceDiagram
    participant User
    participant System as run_system.py
    participant Data as generate_dataset.py
    participant ML as automl_trainer.py
    participant DB as db_handler.py
    participant API as FastAPI Server

    User->>System: python run_system.py
    System->>Data: Check/Generate dataset
    Data-->>System: βœ… 50K transactions ready
    System->>ML: Check/Train model
    ML-->>System: βœ… Best model saved (.pkl)
    System->>DB: Initialize tables
    DB-->>System: βœ… SQLite ready
    System->>API: Start Uvicorn server
    API-->>User: 🌐 http://localhost:8000
    
    loop Every 2 seconds
        API->>API: Generate transaction
        API->>API: Predict fraud
        API->>API: Store + Alert + Broadcast
    end
Loading

Access Points

Service URL
🌐 Dashboard http://localhost:8000
πŸ“‘ WebSocket ws://localhost:8000/ws
πŸ“– API Docs http://localhost:8000/docs

πŸ“‘ API Reference

REST Endpoints

Method Endpoint Description
GET / Serve the dashboard HTML
GET /health Health check β€” model status
GET /stats Fraud statistics + model info
GET /transactions?limit=100 Recent transactions
GET /transactions/fraud?limit=50 Fraud-only transactions
GET /model-comparison AutoML model comparison results
GET /hourly-stats Fraud count by hour
GET /category-stats Fraud rate by merchant category

WebSocket

Endpoint Message Types
ws://localhost:8000/ws transaction β€” new transaction data
stats_update β€” refreshed KPI metrics

Example WebSocket message:

{
  "type": "transaction",
  "data": {
    "transaction_id": "a1b2c3d4-...",
    "timestamp": "2026-02-20T11:30:00",
    "amount": 2450.00,
    "merchant_category": "electronics",
    "location": "Lagos/Nigeria",
    "fraud_probability": 0.9234,
    "prediction": 1,
    "risk_level": "HIGH",
    "processing_time_ms": 45.23,
    "model_used": "AutoML"
  }
}

🎨 Dashboard

The dashboard features a cyberpunk-inspired UI with real-time data streaming:

Dashboard Layout

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  πŸ›‘οΈ FRAUDSHIELD AI            πŸ”΄ LIVE    ● CONNECTED β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ [Total] [Fraud] [Rate%] [Saved$] [Speed] [Model]    β”‚  ← KPI Cards
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 🍩 Donut β”‚  πŸ“ˆ Live Line   β”‚  πŸ“‘ Radar              β”‚  ← Charts Row 1
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ πŸ›’ Categoryβ”‚ 🌍 Globe       β”‚  πŸ• Hourly             β”‚  ← Charts Row 2
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ πŸ“‹ Transaction Feed         β”‚  🚨 Fraud Alerts       β”‚  ← Bottom Row
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Visual Features

  • Dark cyberpunk theme with grid background and neon accents
  • Real-time animations β€” slide-in rows, pulsing indicators, toast alerts
  • 6 KPI cards with color-coded borders (cyan, red, yellow, green, purple)
  • Interactive 3D globe showing fraud origin hotspots
  • Fully responsive β€” works on desktop, tablet, and mobile

πŸ“Έ Add your own screenshots below after running the system!

Place screenshots in a screenshots/ folder and update the paths:

![Dashboard Overview](screenshots/dashboard_overview.png)
![Fraud Alert Toast](screenshots/fraud_alert.png)
![3D Globe](screenshots/globe_view.png)

βš™οΈ How It Works

1. Data Generation (data/generate_dataset.py)

  • Uses sklearn.make_classification() to create 10,000 synthetic transactions with 29 features (V1–V28 + Amount)
  • Applies SMOTE (Synthetic Minority Oversampling) to balance fraud/legit classes
  • Saves balanced dataset to creditcard_sample.csv

2. Model Training (models/automl_trainer.py)

  • PyCaret AutoML compares 8 classification models:
    • Random Forest, Extra Trees, Gradient Boosting, Logistic Regression, Decision Tree, KNN, Naive Bayes, AdaBoost
  • Best model selected by F1 Score
  • Winner is hyperparameter-tuned and saved as .pkl

3. Real-Time Detection (realtime/)

  • Transaction Simulator generates realistic transactions every 2 seconds
    • ~2% fraud rate with high-amount, suspicious-location patterns
  • Fraud Detector loads the trained model and scores each transaction
    • Returns probability, prediction, risk level, and processing time

4. Alert System (alerts/alert_system.py)

  • Color-coded terminal output: 🚨 Fraud / 🟑 Suspicious / βœ… Legitimate
  • Persistent fraud_alerts.log for audit compliance

5. API & Dashboard (api/ + dashboard/)

  • FastAPI serves REST endpoints and WebSocket connections
  • Single HTML dashboard connects via WebSocket for live updates
  • Chart.js renders 6 interactive visualizations
  • Globe.gl provides 3D geographic fraud mapping

Risk Classification

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Fraud Probability    β†’    Risk Level       β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     0.0 - 0.4         β†’    🟒 LOW          β”‚
β”‚     0.4 - 0.7         β†’    🟑 MEDIUM       β”‚
β”‚     0.7 - 1.0         β†’    πŸ”΄ HIGH         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”§ Configuration

All settings are in config.py:

DATABASE_PATH = "database/fraud_detection.db"
MODEL_SAVE_PATH = "models/saved_model/best_fraud_model"
FRAUD_THRESHOLD = 0.5          # Minimum probability to flag as fraud
HIGH_RISK_THRESHOLD = 0.7      # Threshold for HIGH risk level
TRANSACTION_INTERVAL_SECONDS = 2  # Seconds between simulated transactions
HOST = "0.0.0.0"
PORT = 8000

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

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


πŸ‘©β€πŸ’» Author

Gagandeep Kaur (E11625)


Built with ❀️ using Python, FastAPI, PyCaret, and Chart.js

About

End-to-end credit card fraud detection system using AutoML (PyCaret), FastAPI, and WebSocket streaming. Trains and compares 8 ML models, scores transactions in real time, and displays results on a live interactive dashboard with Chart.js and Globe.gl visualizations

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors