Skip to content

NotArnav03/SafePay-AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SafePay AI - Fraud Detection System

Python 3.9+ Flask scikit-learn

AI-powered fraud detection system that analyzes SMS messages and transaction patterns to prevent digital payment scams before payment approval.

🎯 Features

  • SMS Scam Detection - NLP-based analysis using TF-IDF and Logistic Regression
  • Transaction Risk Analysis - ML-based scoring using Random Forest classifier
  • Explainable AI - Clear explanations of fraud indicators and risk factors
  • Real-time Analysis - Combined risk assessment with actionable recommendations
  • Modern UI - Premium dark mode interface with glassmorphism and animations
  • Demo Ready - Pre-loaded examples for instant testing

πŸ—οΈ Architecture

SafePay AI
β”œβ”€β”€ Backend (Flask + ML)
β”‚   β”œβ”€β”€ SMS Scam Detector (Logistic Regression + TF-IDF)
β”‚   β”œβ”€β”€ Transaction Analyzer (Random Forest)
β”‚   └── REST API (/analyze endpoint)
└── Frontend (Vanilla JS)
    β”œβ”€β”€ Interactive fraud detection form
    β”œβ”€β”€ Real-time risk visualization
    └── Detailed explanations

πŸš€ Quick Start

Prerequisites

  • Python 3.9 or higher
  • pip (Python package manager)

Installation

  1. Clone or navigate to the project directory

    cd safepay-ai
  2. Create a virtual environment

    # Windows
    python -m venv venv
    venv\Scripts\activate
    
    # Linux/Mac
    python3 -m venv venv
    source venv/bin/activate
  3. Install dependencies

    cd backend
    pip install -r requirements.txt

Training the Models

Before running the application, you need to train the ML models:

# From backend directory
cd training

# Train SMS scam detection model
python train_sms_model.py

# Train transaction risk analysis model
python train_transaction_model.py

Expected output:

  • Model accuracy metrics
  • Feature importance scores
  • Saved model files in data/trained_models/

Running the Application

  1. Start the Flask backend

    # From backend directory
    python app.py

    Server will start at: http://localhost:5000

  2. Open the application

    • Navigate to http://localhost:5000 in your web browser
    • The frontend will automatically connect to the backend API

πŸ“Š Demo Scenarios

Click the Quick Examples buttons for instant demos:

1. Scam SMS (High Risk)

  • Amount: β‚Ή50,000
  • Receiver: New
  • Message: Urgent account block warning with suspicious link
  • Expected: High risk, BLOCK recommendation

2. Legitimate SMS (Low Risk)

  • Amount: β‚Ή599
  • Receiver: Known
  • Message: OTP verification code
  • Expected: Low risk, APPROVE recommendation

3. High Risk Transaction

  • Amount: β‚Ή85,000
  • Receiver: New
  • Message: Prize/lottery scam
  • Expected: High risk, BLOCK recommendation

πŸ” API Documentation

POST /analyze

Analyze a payment for fraud risk.

Request:

{
  "amount": 50000,
  "is_new_receiver": 1,
  "transactions_today": 1,
  "message": "URGENT: Your account will be blocked..."
}

Response:

{
  "risk_level": "High",
  "risk_score": 85,
  "recommendation": "BLOCK - Manual review required",
  "explanation": "Detailed analysis...",
  "sms_analysis": {
    "is_scam": true,
    "confidence": 92.5,
    "risk_score": 93,
    "indicators": ["Urgency language detected", "Suspicious URL detected"]
  },
  "transaction_analysis": {
    "risk_level": "High Risk",
    "risk_score": 100,
    "confidence": 88.3,
    "risk_factors": ["Very high transaction amount", "New/unknown receiver"]
  }
}

GET /health

Health check endpoint.

Response:

{
  "status": "healthy",
  "message": "SafePay AI is running"
}

🧠 ML Models

SMS Scam Detector

  • Algorithm: Logistic Regression
  • Features: TF-IDF vectorization (1000 features, bigrams)
  • Training Data: 35 labeled SMS samples
  • Accuracy: ~85-90%
  • Explainability: Keyword detection, URL analysis, urgency patterns

Transaction Analyzer

  • Algorithm: Random Forest (100 trees)
  • Features: Amount, receiver history, transaction frequency
  • Training Data: 50 labeled transactions
  • Accuracy: ~85-90%
  • Output: 3-class classification (Low/Medium/High risk)

πŸ“ Project Structure

safepay-ai/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ app.py                      # Flask application
β”‚   β”œβ”€β”€ config.py                   # Configuration
β”‚   β”œβ”€β”€ requirements.txt            # Python dependencies
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ sms_detector.py         # SMS scam detection
β”‚   β”‚   β”œβ”€β”€ transaction_analyzer.py # Transaction risk analysis
β”‚   β”‚   └── explainer.py            # Model explainability
β”‚   β”œβ”€β”€ data/
β”‚   β”‚   β”œβ”€β”€ raw/
β”‚   β”‚   β”‚   β”œβ”€β”€ sms_dataset.csv     # SMS training data
β”‚   β”‚   β”‚   └── transaction_dataset.csv
β”‚   β”‚   └── trained_models/         # Saved .pkl files
β”‚   └── training/
β”‚       β”œβ”€β”€ train_sms_model.py      # SMS model trainer
β”‚       └── train_transaction_model.py
└── frontend/
    β”œβ”€β”€ index.html                  # Main UI
    β”œβ”€β”€ css/
    β”‚   └── styles.css              # Premium styling
    └── js/
        └── app.js                  # Frontend logic

⚠️ Important Notes

  • Demo Only: This is a hackathon project with simulated data
  • No Real Integration: No actual UPI/bank APIs are used
  • Synthetic Data: All training data is artificially generated
  • Not Production Ready: Requires additional security, validation, and testing for real-world use

🎨 Design Features

  • Dark mode with vibrant gradients
  • Glassmorphism effects
  • Smooth animations and transitions
  • Responsive design for all devices
  • Real-time risk meter visualization
  • Interactive demo examples

πŸ› οΈ Technologies Used

  • Backend: Python, Flask, Flask-CORS
  • ML/NLP: scikit-learn, pandas, numpy, nltk
  • Frontend: HTML5, CSS3, Vanilla JavaScript
  • Design: Custom CSS with modern aesthetics

πŸ“ License

This is a hackathon demo project for educational purposes.

πŸ‘¨β€πŸ’» Development

Built with ❀️ for hackathon demonstration


Need Help?

  • Ensure Python 3.9+ is installed
  • Activate virtual environment before running
  • Train models before starting the application
  • Check that port 5000 is available

About

Real-time AI-powered digital payment fraud prevention system (Hackathon Project)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors