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.
- Project Structure
- Model Training
- Preprocessing
- Model Loader & Prediction
- API Usage
- Testing the API
- Docker Deployment
- Component Summary
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.
pip install -r requirements.txtTrain the hybrid LSTM-CNN model and save the scaler:
python model_training/train_model.py-
Imports
Usesnumpy,scikit-learn, andtensorflow.keras. -
Dataset Simulation
- 100,000 transactions, 10 features each.
- ~2% fraud rate.
-
Feature Scaling & Reshape
- Standardizes features.
- Reshapes for LSTM/CNN compatibility.
-
Train-Test Split
- 80% train, 20% test, stratified.
-
Model Creation
- Hybrid LSTM-CNN architecture.
- Output: Probability of fraud.
-
Training & Saving
- 10 epochs.
- Saves model and scaler parameters to
saved_model/.
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.
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"Start the FastAPI server:
uvicorn app.main:app --reload --port 8000- 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" }
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]}'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- 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.
This project is licensed under the MIT License.