This project aims to build, deploy, and monitor a machine learning solution for fraud detection in financial transactions. It demonstrates a full MLOps workflow, including data engineering, model training, API serving, cloud deployment, and infrastructure-as-code.
- Detect fraudulent transactions using machine learning
- Provide a robust, production-ready API for real-time scoring
- Enable automated deployment to Azure ML and Kubernetes
- Ensure reproducibility and maintainability with MLOps best practices
- Data Pipeline: Raw transaction data is processed and feature-engineered in
src/features.pyandsrc/generate_data.py. - Model Training: The model is trained using scikit-learn in
src/train.py, with outputs saved asmodel.joblib. - API Service: The FastAPI app in
src/serve.pyloads the trained model and exposes a/predictendpoint for scoring. - Deployment: The solution can be deployed locally (Docker, Uvicorn), to Azure ML managed endpoints (
deployment.yml), or Kubernetes (k8s/deployment.yaml). - Infrastructure-as-Code: Azure resources are provisioned using Bicep (
iac/azure_bicep_example.bicep). - Monitoring: Drift and model performance monitoring scripts are in
scripts/drift_monitor.py.
- Install dependencies:
python -m venv venv .\venv\Scripts\Activate.ps1 pip install -r requirements.txt
- Start API locally:
uvicorn src.serve:app --host 127.0.0.1 --port 8080
- Test endpoint:
Invoke-RestMethod -Uri 'http://127.0.0.1:8080/predict' ` -Method POST ` -ContentType 'application/json' ` -Body '{"features": {"amount": 100, "hour_of_day": 14, "avg_monthly_spend": 500, "customer_tenure_days": 365, "num_prev_tx_24h": 5}}'
- Update
environment.ymlanddeployment.ymlas needed. - Deploy with:
az ml online-deployment create ` --file deployment.yml ` --resource-group <resource-group> ` --workspace-name <workspace-name>
- Build Docker image:
docker build -t fraud-api:latest .
- Run container:
docker run -p 8080:8080 fraud-api:latest
- Deploy to Kubernetes using
k8s/deployment.yaml.
src/- Source code (features, training, serving)data/- Raw and processed datamodels/- Saved modelsscripts/- Monitoring and utility scriptsenvironment.yml- Conda environment for Azure MLdeployment.yml- Azure ML deployment specDockerfile- Local/Docker deploymentk8s/- Kubernetes manifestsiac/- Infrastructure-as-code (Azure Bicep)
- Version control for code and data
- Automated environment management
- Reproducible deployments
- Monitoring and alerting for model drift
- Infrastructure-as-code for cloud resources
-
Model Registration & Versioning:
az ml model create --name fraud-detector --version 1 --path models/run_local/model.joblib
-
Managed Online Endpoint Deployment:
az ml online-endpoint create --file azureml/endpoint.yml az ml online-deployment create --file deployment.yml --resource-group mlops-rg --workspace-name scotia-mlops-ws
-
Application Insights Integration:
- Enable monitoring in Azure ML workspace
- Configure custom metrics for prediction accuracy
- Set up alerts for latency > 2s or error rate > 5%
-
Data & Model Drift Detection:
python scripts/drift_monitor.py --baseline-data data/raw/txs.csv --new-data <incoming_data>
-
Performance Monitoring Dashboard:
- Track prediction latency, throughput, and accuracy
- Monitor resource utilization (CPU, memory)
- Alert on model performance degradation
-
Auto-scaling Configuration:
# In deployment.yml scale_settings: type: target_utilization min_instances: 2 max_instances: 10 target_utilization_percentage: 70
-
Multi-region Deployment:
- Deploy endpoints in multiple Azure regions
- Use Azure Traffic Manager for load balancing
- Configure disaster recovery procedures
-
API Gateway Integration:
- Deploy behind Azure API Management
- Enable authentication, rate limiting, and caching
- Implement request/response transformation
- Automated Training Pipeline (see
.azure-pipelines/training-pipeline.yml) - Continuous Deployment (see
.github/workflows/deploy.yml) - Model Validation & Testing:
- Automated model performance testing
- A/B testing for model versions
- Rollback capabilities
-
Identity & Access Management:
- Use managed identities for Azure resources
- Implement RBAC for model access
- Enable audit logging
-
Data Protection:
- Encrypt data at rest and in transit
- Implement data lineage tracking
- GDPR compliance for model explanations
- Resource Management:
- Use spot instances for training workloads
- Implement auto-shutdown for dev environments
- Monitor and optimize compute costs
# Start local API
uvicorn src.serve:app --host 127.0.0.1 --port 8080
# Test endpoint
Invoke-RestMethod -Uri 'http://127.0.0.1:8080/predict' -Method POST -ContentType 'application/json' -Body '{"features": {"amount": 100, "hour_of_day": 14, "avg_monthly_spend": 500, "customer_tenure_days": 365, "num_prev_tx_24h": 5}}'
# Docker deployment
docker build -t fraud-api:latest .
docker run -p 8080:8080 fraud-api:latest# Deploy to Azure ML
az ml online-deployment update --name fraud-deploy --endpoint-name fraud-endpoint --file deployment.yml --resource-group mlops-rg --workspace-name scotia-mlops-ws
# Monitor deployment
az ml online-endpoint show --name fraud-endpoint --resource-group mlops-rg --workspace-name scotia-mlops-ws
# Test Azure endpoint
az ml online-endpoint invoke --name fraud-endpoint --request-file test_data.jsonThis project includes a scheduled GitHub Actions workflow (.github/workflows/retrain-and-deploy.yml) that retrains the fraud detection model, registers it in Azure ML, and redeploys it to the managed endpoint.
How it works:
- Runs every Monday at 05:00 UTC (or manually via GitHub Actions > Run workflow)
- Retrains the model using
src/train.pyand the latest features data - Registers the new model in Azure ML workspace
- Updates the endpoint deployment with the new model
Customizing:
- Change the schedule by editing the
cronvalue in the workflow YAML - Update training data path or script arguments as needed
- Monitor workflow runs and logs in the GitHub Actions tab
Manual trigger:
- Go to GitHub Actions > Retrain and Deploy Model > Run workflow
Requirements:
- Azure credentials must be set in repository secrets as
AZURE_CREDENTIALS - Training script must output model to
models/run_local/model.joblib - Deployment configuration is read from
deployment.yml
For questions or contributions, please contact the project maintainer.
Quick setup (Windows PowerShell):
# activate venv
.\venv\Scripts\Activate.ps1
# install dev deps and enable hooks
pip install -r requirements-dev.txt
pre-commit install
pre-commit run --all-filesQuick setup (Linux/macOS):
python -m venv venv
source venv/bin/activate
pip install -r requirements-dev.txt
pre-commit install
pre-commit run --all-filesYou can also use the Makefile target on systems that have make:
make precommit-installThis repository uses pre-commit to run formatters, linters and tests before commits. On Windows, run the following from a PowerShell with your virtualenv activated:
# create and activate venv (if needed)
python -m venv venv; .\venv\Scripts\Activate.ps1
# install dependencies and set up git hooks
.\scripts\install_precommit.ps1After that pre-commit will run black, isort, flake8 and pytest automatically on each commit. If a hook fails, fix the issues locally and re-run the failing checks or re-run the installer script.