Skip to content

Latest commit

Β 

History

21 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Vehicle Insurance Predictor

Python FastAPI Scikit-Learn MongoDB Docker AWS GitHub Actions License


An end-to-end MLOps pipeline that predicts whether a customer is likely to respond positively to a vehicle insurance cross-sell offer β€” from raw data in MongoDB to a fully automated, containerized deployment on AWS.


πŸš€ Overview Β· πŸ—οΈ Architecture Β· βš™οΈ Installation Β· ☁️ AWS Setup Β· πŸ” CI/CD Β· πŸ“‘ App Routes


πŸš€ Project Overview

This project goes beyond a one-off notebook model and simulates a production-grade MLOps workflow end to end:

  • πŸ“¦ Modular, installable Python package (src) via setup.py / pyproject.toml
  • πŸƒ MongoDB Atlas as the source data store
  • πŸͺ΅ Custom logging and exception-handling modules
  • πŸ”„ A fully modular training pipeline β€” Data Ingestion β†’ Data Validation β†’ Data Transformation β†’ Model Trainer β†’ Model Evaluation β†’ Model Pusher
  • πŸͺ£ AWS S3 used as a model registry (push/pull trained models)
  • ⚑ A FastAPI web app to trigger training and serve real-time predictions
  • πŸ” Fully automated CI/CD with GitHub Actions, Docker, Amazon ECR, and a self-hosted EC2 runner

✨ Features

Feature Description
πŸƒ Cloud Data Source Raw data ingested directly from MongoDB Atlas
πŸ§ͺ Schema-Based Validation Automated data validation against config/schema.yaml
πŸ› οΈ Feature Engineering Pipeline Reusable preprocessing objects persisted as .pkl artifacts
πŸ€– Model Training & Evaluation Scikit-learn based training with threshold-based model acceptance
πŸͺ£ S3 Model Registry Trained models versioned and pushed/pulled from AWS S3
⚑ FastAPI Backend REST endpoints to trigger training and serve predictions
🐳 Containerized Deployment Dockerized app shipped via Amazon ECR
πŸ” Full CI/CD Automation GitHub Actions + self-hosted EC2 runner auto-deploys on every push
πŸ—‚οΈ Timestamped Artifacts Every pipeline run stores versioned artifacts for full traceability

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     MongoDB Atlas (Raw Data)                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     TRAINING PIPELINE (src/)                 β”‚
β”‚                                                               β”‚
β”‚   Data Ingestion β†’ Data Validation β†’ Data Transformation     β”‚
β”‚                         β”‚                                    β”‚
β”‚                         β–Ό                                    β”‚
β”‚                    Model Trainer                             β”‚
β”‚                         β”‚                                    β”‚
β”‚                         β–Ό                                    β”‚
β”‚                  Model Evaluation  ───────► AWS S3            β”‚
β”‚                         β”‚                (Model Registry)     β”‚
β”‚                         β–Ό                                    β”‚
β”‚                    Model Pusher                               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 FASTAPI APPLICATION (main.py)                β”‚
β”‚                                                               β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚   β”‚  pages.py  β”‚   β”‚ prediction.py  β”‚   β”‚  training.py    β”‚  β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         Docker Image ─► Amazon ECR ─► EC2 (self-hosted        β”‚
β”‚                runner) ─► Live App (port 5000)                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Training Pipeline Flow

MongoDB Collection
       β”‚
       β–Ό
Data Ingestion  ──►  feature_store/data.csv  ──►  ingested/train.csv, test.csv
       β”‚
       β–Ό
Data Validation  ──►  report.yaml  (schema drift & completeness checks)
       β”‚
       β–Ό
Data Transformation  ──►  transformed/train.npy, test.npy
       β”‚                  transformed_object/preprocessing.pkl
       β–Ό
Model Trainer  ──►  trained_model/model.pkl
       β”‚
       β–Ό
Model Evaluation  ──►  compares against current production model in S3
       β”‚
       β–Ό
Model Pusher  ──►  pushes accepted model to S3 (my-model-mlopsproj)

πŸ—‚οΈ Project Structure

vehicle-insurance-mlops/
β”‚
β”œβ”€β”€ .github/workflows/
β”‚   └── aws.yaml                     # CI/CD pipeline (build β†’ push β†’ deploy)
β”‚
β”œβ”€β”€ app/                              # FastAPI application layer
β”‚   β”œβ”€β”€ config.py                     # App settings (host, port, CORS, log level)
β”‚   β”œβ”€β”€ schemas.py                    # Pydantic request/response models
β”‚   └── routers/
β”‚       β”œβ”€β”€ pages.py                  # Home / UI routes
β”‚       β”œβ”€β”€ prediction.py             # /predict route
β”‚       β”œβ”€β”€ training.py               # /training route
β”‚       └── __init__.py
β”‚
β”œβ”€β”€ artifact/                         # Auto-generated, timestamped pipeline runs
β”‚   └── <run_timestamp>/
β”‚       β”œβ”€β”€ data_ingestion/
β”‚       β”‚   β”œβ”€β”€ feature_store/data.csv
β”‚       β”‚   └── ingested/{train,test}.csv
β”‚       β”œβ”€β”€ data_validation/report.yaml
β”‚       β”œβ”€β”€ data_transformation/
β”‚       β”‚   β”œβ”€β”€ transformed/{train,test}.npy
β”‚       β”‚   └── transformed_object/preprocessing.pkl
β”‚       └── model_trainer/trained_model/model.pkl
β”‚
β”œβ”€β”€ config/
β”‚   β”œβ”€β”€ model.yaml                    # Model hyperparameters / selection config
β”‚   └── schema.yaml                   # Expected dataset schema for validation
β”‚
β”œβ”€β”€ logs/                              # Auto-generated log files
β”‚
β”œβ”€β”€ notebook/
β”‚   β”œβ”€β”€ data.csv
β”‚   β”œβ”€β”€ exp-notebook.ipynb            # EDA & feature engineering
β”‚   └── mongo_DB_demo.ipynb           # Push raw dataset to MongoDB
β”‚
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ cloud_storage/                # AWS S3 read/write utilities
β”‚   β”œβ”€β”€ components/                   # Ingestion, validation, transformation, trainer, evaluation, pusher
β”‚   β”œβ”€β”€ configuration/                # MongoDB & AWS connection setup
β”‚   β”œβ”€β”€ constants/                    # Project-wide constants
β”‚   β”œβ”€β”€ data_access/                  # MongoDB β†’ DataFrame access layer
β”‚   β”œβ”€β”€ entity/                       # Config / artifact / estimator entities
β”‚   β”œβ”€β”€ exception/                    # Custom exception handling
β”‚   β”œβ”€β”€ logger/                       # Custom logging setup
β”‚   β”œβ”€β”€ pipeline/                     # Training & prediction pipelines
β”‚   └── utils/                        # Shared utility functions
β”‚
β”œβ”€β”€ static/
β”‚   β”œβ”€β”€ css/style.css
β”‚   └── js/script.js
β”‚
β”œβ”€β”€ templates/
β”‚   └── index.html                    # Jinja2 UI template
β”‚
β”œβ”€β”€ demo.py                            # Quick script to test logger/exception/pipeline
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ main.py                            # FastAPI app entry point
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ setup.py
β”œβ”€β”€ pyproject.toml
└── template.py                        # Scaffolds the initial project structure

βš™οΈ Installation

Prerequisites

1. Clone the repository

git clone https://github.com/sharif-abusad/vehicle-insurance-mlops.git
cd vehicle-insurance-mlops

2. Scaffold & set up the environment

# (Optional) Regenerate the project skeleton
python template.py

# Create and activate a virtual environment
conda create -n vehicle python=3.10 -y
conda activate vehicle

# Install dependencies (installs local "src" package via -e .)
pip install -r requirements.txt

# Verify local package installation
pip list

3. MongoDB Atlas Setup

  1. Sign up on MongoDB Atlas and create a new project.
  2. Create a cluster β†’ select the M0 (free tier) β†’ keep defaults β†’ Create Deployment.
  3. Create a database user with a username and password.
  4. Under Network Access, add 0.0.0.0/0 to allow access from anywhere.
  5. Go to Get Connection String β†’ Drivers β†’ select Python, version 3.6+ β†’ copy the connection string and replace <password>.
  6. Add your dataset to notebook/ and push it to MongoDB via mongo_DB_demo.ipynb.
  7. Verify the upload under Atlas β†’ Database β†’ Browse Collections.

4. Configure environment variables

cp .env.example .env

Bash

export MONGODB_URL="mongodb+srv://<username>:<password>@<cluster-url>/?appName=Cluster0"
export AWS_ACCESS_KEY_ID="<your-aws-access-key-id>"
export AWS_SECRET_ACCESS_KEY="<your-aws-secret-access-key>"
export AWS_DEFAULT_REGION="us-east-1"

PowerShell

$env:MONGODB_URL = "mongodb+srv://<username>:<password>@<cluster-url>/?appName=Cluster0"
$env:AWS_ACCESS_KEY_ID = "<your-aws-access-key-id>"
$env:AWS_SECRET_ACCESS_KEY = "<your-aws-secret-access-key>"
$env:AWS_DEFAULT_REGION = "us-east-1"

⚠️ Security note: Never commit .env files or real credentials. Ensure .env, _env, and artifact/ are listed in .gitignore, and rotate any keys that may have been exposed.


πŸ–₯️ Running Locally

Sanity-check logger, exception handling & pipeline

python demo.py

Run the training pipeline

python demo.py

Each run creates a timestamped folder under artifact/ containing intermediate outputs (ingested data, transformed arrays, preprocessing object, trained model, validation report).

Start the web app

python main.py
Resource URL
App http://localhost:5000
Trigger Training http://localhost:5000/training
Prediction UI http://localhost:5000/predict

☁️ AWS Setup (S3 Model Registry)

  1. Log in to the AWS Console, keep the region set to us-east-1.
  2. Go to IAM β†’ Create User (e.g., firstproj) β†’ attach the AdministratorAccess policy.
  3. Under the user's Security Credentials β†’ Access Keys, create a new CLI access key and download the CSV.
  4. Export the access key/secret as environment variables (see above).
  5. Add the following to src/constants/__init__.py:
    MODEL_EVALUATION_CHANGED_THRESHOLD_SCORE: float = 0.02
    MODEL_BUCKET_NAME = "my-model-mlopsproj"
    MODEL_PUSHER_S3_KEY = "model-registry"
  6. Create an S3 bucket (my-model-mlopsproj) in us-east-1, unchecking "Block all public access" (acknowledge the warning).
  7. src/configuration/aws_connection.py and src/cloud_storage/aws_storage.py handle the AWS session and S3 push/pull logic, while src/entity/s3_estimator.py wraps the pull/push operations used by Model Evaluation and Model Pusher.

πŸ” CI/CD Deployment (GitHub Actions + EC2 + ECR)

The pipeline is defined in .github/workflows/aws.yaml and runs in two stages:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Continuous Integration     β”‚        β”‚   Continuous Deployment           β”‚
β”‚  (runs-on: ubuntu-latest)   β”‚  ───►  β”‚   (runs-on: self-hosted / EC2)    β”‚
β”‚                              β”‚        β”‚                                    β”‚
β”‚  β€’ Checkout code            β”‚        β”‚  β€’ Pull latest image from ECR     β”‚
β”‚  β€’ Configure AWS creds      β”‚        β”‚  β€’ Stop & remove old container    β”‚
β”‚  β€’ Login to Amazon ECR      β”‚        β”‚  β€’ Run new container with env     β”‚
β”‚  β€’ Build & push Docker imageβ”‚        β”‚    vars injected as secrets       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Setup Steps

  1. ECR Repository β€” AWS Console β†’ ECR β†’ create repository vehicleproj in us-east-1.

  2. EC2 Instance β€” Launch an Ubuntu Server 24.04 (free-tier eligible) instance (e.g., vehicledata-machine), allow HTTP/HTTPS traffic, attach a new key pair, and connect via EC2 Instance Connect.

  3. Install Docker on EC2:

    sudo apt-get update -y
    sudo apt-get upgrade
    curl -fsSL https://get.docker.com -o get-docker.sh
    sudo sh get-docker.sh
    sudo usermod -aG docker ubuntu
    newgrp docker
  4. Self-hosted GitHub Runner β€” In your GitHub repo, go to Settings β†’ Actions β†’ Runners β†’ New self-hosted runner, select Linux, and run the provided download/configure commands on the EC2 instance. Start it with ./run.sh.

  5. GitHub Secrets β€” Under Settings β†’ Secrets and variables β†’ Actions, add:

    Secret Description
    AWS_ACCESS_KEY_ID IAM user access key
    AWS_SECRET_ACCESS_KEY IAM user secret key
    AWS_DEFAULT_REGION e.g. us-east-1
    ECR_REPO ECR repository name (vehicleproj)
    MONGODB_URL MongoDB Atlas connection string
  6. Open the App Port β€” In the EC2 instance's Security Group, add an inbound rule for the app port (e.g., 5000) with source 0.0.0.0/0.

  7. Push to main β€” the workflow triggers automatically: builds, pushes to ECR, and deploys the container on EC2.

  8. Access the app at http://<EC2-public-ip>:<port>.


πŸ“‘ Application Routes

Method Route Description
GET / Home page (served via Jinja2 templates)
POST/GET /training Triggers the model training pipeline
POST /predict Serves predictions from the trained model

🧩 Pipeline Components

Component Responsibility
Data Ingestion Pulls data from MongoDB, converts to DataFrame, splits into train/test
Data Validation Validates schema/data drift against config/schema.yaml
Data Transformation Feature engineering, preprocessing pipeline, saves transformed arrays
Model Trainer Trains the ML model and saves the trained estimator
Model Evaluation Compares the new model against the production model in S3
Model Pusher Pushes the accepted model to the S3 model registry

Each component is driven by strongly-typed config and artifact entities defined in src/entity/config_entity.py and src/entity/artifact_entity.py, keeping the pipeline modular and easy to extend.


πŸ› οΈ Tech Stack

Data & ML

Backend & Serving

Data & Cloud

  • PyMongo β€” MongoDB Atlas connectivity
  • boto3 β€” AWS S3 integration
  • AWS S3 / IAM / ECR / EC2 β€” model registry & deployment infrastructure

DevOps


πŸ—ΊοΈ Roadmap

  • Add automated data drift monitoring
  • Add unit and integration tests (pytest)
  • Add model explainability (SHAP)
  • Add experiment tracking (MLflow / DVC)
  • Add batch prediction endpoint
  • Add structured logging and monitoring (Sentry / CloudWatch)
  • Blue-green or canary deployment strategy on EC2

🀝 Contributing

Contributions are welcome and appreciated.

# 1. Fork the repository
# 2. Create a feature branch
git checkout -b feature/your-feature-name

# 3. Commit your changes
git commit -m "feat: add your feature"

# 4. Push and open a Pull Request
git push origin feature/your-feature-name

Please follow Conventional Commits for commit messages and open an issue before starting large changes.


πŸ“„ License

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


πŸ‘€ Author

Sharif Abusad πŸ“§ abusharif866@gmail.com

GitHub LinkedIn

If you found this project useful, consider giving it a ⭐ on GitHub β€” it helps a lot!


Built with ❀️ using MongoDB, FastAPI, Scikit-learn, Docker & AWS

About

πŸš— End-to-end MLOps project predicting vehicle insurance cross-sell response β€” MongoDB, FastAPI, Docker, AWS (S3/ECR/EC2), and automated CI/CD with GitHub Actions.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages