Skip to content

Real-time fraud detection for mobile financial services using a hybrid LSTM-CNN deep learning model, exposed via a FastAPI REST API and containerized for production deployment.

License

Notifications You must be signed in to change notification settings

makozi/FraudProx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FraudProx: AI-Powered Fraud Detection API

Real-time fraud detection for mobile financial services using a hybrid LSTM-CNN deep learning model, exposed via a FastAPI REST API and containerized for production deployment.


Table of Contents


Project Structure

fraud_detection_api/
├── app/
│   ├── main.py           # FastAPI REST API
│   ├── model.py          # Model loader & prediction logic
│   └── preprocessing.py  # Feature scaling for inference
├── model_training/
│   └── train_model.py    # Model training script
├── saved_model/          # Saved model and scaler data
├── requirements.txt      # Python dependencies
└── Dockerfile            # Containerization instructions
  • Why this structure?
    Separation of concerns for training, inference, API, and deployment.

Install Dependencies

pip install -r requirements.txt

Model Training

Train the hybrid LSTM-CNN model and save the scaler:

python model_training/train_model.py

Key Steps

  1. Imports
    Uses numpy, scikit-learn, and tensorflow.keras.

  2. Dataset Simulation

    • 100,000 transactions, 10 features each.
    • ~2% fraud rate.
  3. Feature Scaling & Reshape

    • Standardizes features.
    • Reshapes for LSTM/CNN compatibility.
  4. Train-Test Split

    • 80% train, 20% test, stratified.
  5. Model Creation

    • Hybrid LSTM-CNN architecture.
    • Output: Probability of fraud.
  6. Training & Saving

    • 10 epochs.
    • Saves model and scaler parameters to saved_model/.

Preprocessing

Feature scaling for inference:

class Scaler:
    def __init__(self, mean, scale):
        self.mean = mean
        self.scale = scale

    def transform(self, X):
        return (X - self.mean) / self.scale

def load_scaler():
    mean = np.load("saved_model/scaler.npy")
    scale = np.load("saved_model/scaler_scale.npy")
    return Scaler(mean, scale)
  • Purpose:
    Ensures consistent scaling during inference without refitting.

Model Loader & Prediction

Loads the trained model and scaler, and exposes prediction logic:

from tensorflow.keras.models import load_model
from .preprocessing import load_scaler

model = load_model("saved_model/fraud_model.h5")
scaler = load_scaler()

def predict_fraud(transaction_features):
    features_scaled = scaler.transform(np.array(transaction_features))
    features_reshaped = features_scaled.reshape((1, 1, len(transaction_features)))
    prediction = model.predict(features_reshaped)[0][0]
    return "FRAUD ALERT" if prediction > 0.5 else "LEGITIMATE"

API Usage

Start the FastAPI server:

uvicorn app.main:app --reload --port 8000

Example Endpoint

  • POST /predict
  • Request Body:
    {
      "features": [0.5, 0.2, 0.3, 0.1, 0.7, 0.8, 0.9, 0.4, 0.2, 0.6]
    }
  • Response:
    {
      "result": "LEGITIMATE"
    }

Testing the API

Test with curl:

curl -X POST "http://127.0.0.1:8000/predict" \
-H "Content-Type: application/json" \
-d '{"features":[0.5,0.2,0.3,0.1,0.7,0.8,0.9,0.4,0.2,0.6]}'

Docker Deployment

Containerize and run the API:

Dockerfile:

FROM python:3.10
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY ./app ./app
COPY ./saved_model ./saved_model
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

Build & Run:

docker build -t fraud-api .
docker run -p 8000:8000 fraud-api

Component Summary

  • Training Script: Prepares data, trains hybrid LSTM-CNN, saves model & scaler.
  • Preprocessing: Handles consistent feature scaling at inference.
  • Model Loader: Loads trained model and processes predictions.
  • FastAPI API: Exposes prediction endpoint for mobile banking systems.
  • Docker Deployment: Enables containerized production deployment.

License

This project is licensed under the MIT License.


About

Real-time fraud detection for mobile financial services using a hybrid LSTM-CNN deep learning model, exposed via a FastAPI REST API and containerized for production deployment.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages