Skip to content

int-arsh/insurwise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ₯ Insurance Premium Category Predictor

A full-stack machine learning application that predicts insurance premium categories based on user demographics and lifestyle factors. Built with FastAPI for the backend API and Streamlit for the interactive frontend.

πŸ“‹ Table of Contents

✨ Features

  • RESTful API built with FastAPI for fast, asynchronous predictions
  • Interactive Web Interface using Streamlit for easy user interaction
  • Machine Learning Model that classifies insurance premiums into categories (Low, Medium, High)
  • Intelligent Feature Engineering including:
    • Automatic BMI calculation from height and weight
    • Lifestyle risk assessment based on smoking status and BMI
    • City tier classification (Tier 1, Tier 2, Others)
    • Age group categorization
  • Comprehensive Predictions with confidence scores and class probabilities
  • Docker Support for containerized deployment
  • Input Validation using Pydantic models
  • Health Check Endpoint for monitoring

πŸ›  Tech Stack

Backend:

  • FastAPI - Modern, fast web framework for building APIs
  • Pydantic - Data validation using Python type annotations
  • Scikit-learn - Machine learning model training and prediction
  • Pandas & NumPy - Data manipulation and processing
  • Uvicorn - ASGI server for FastAPI

Frontend:

  • Streamlit - Interactive web application framework

Development Tools:

  • Black, YAPF, Autopep8 - Code formatting
  • isort - Import sorting
  • Flake8 - Code linting

πŸ“ Project Structure

fastapi-model/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py           # FastAPI application and endpoints
β”‚   β”œβ”€β”€ models.py         # Pydantic models for request validation
β”‚   β”œβ”€β”€ schemas.py        # Response schemas
β”‚   └── utils.py          # Utility functions (city tier classification)
β”œβ”€β”€ model/
β”‚   β”œβ”€β”€ ml.py             # ML model loading and prediction logic
β”‚   └── model.pkl         # Trained scikit-learn model (pickle file)
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ app.py            # Streamlit web interface
β”‚   └── .streamlit/
β”‚       └── config.toml   # Streamlit configuration
β”œβ”€β”€ data/
β”‚   └── insurance.csv     # Training dataset
β”œβ”€β”€ noteboooks/
β”‚   └── fastapi_ml_model.ipynb  # Model training notebook
β”œβ”€β”€ tests/                # Test directory
β”œβ”€β”€ Dockerfile            # Docker configuration
β”œβ”€β”€ requirements.txt      # Python dependencies
β”œβ”€β”€ pyproject.toml        # Project configuration
└── README.md             # This file

πŸš€ Installation

Prerequisites

  • Python 3.12 or higher
  • pip package manager

Setup

  1. Clone the repository:

    git clone <repository-url>
    cd fastapi-model
  2. Create a virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install dependencies:

    pip install -r requirements.txt

πŸ’» Usage

Running the Backend API

Start the FastAPI server:

uvicorn app.main:app --reload

The API will be available at http://127.0.0.1:8000

  • Interactive API docs: http://127.0.0.1:8000/docs
  • Alternative docs: http://127.0.0.1:8000/redoc

Running the Frontend

In a separate terminal, start the Streamlit app:

streamlit run frontend/app.py

The web interface will open automatically at http://localhost:8501

πŸ“š API Documentation

Endpoints

GET /

Welcome endpoint

  • Response: {"message": "Premium prediction model"}

GET /health

Health check endpoint

  • Response: {"status": "ok"}

POST /predict

Predict insurance premium category

Request Body:

{
  "age": 30,
  "weight": 70.0,
  "height": 1.75,
  "income_lpa": 12.0,
  "smoker": false,
  "city": "Mumbai",
  "occupation": "private_job"
}

Response:

{
  "predicted_category": "High",
  "confidence": 0.8758,
  "class_probabilities": {
    "Low": 0.01,
    "Medium": 0.15,
    "High": 0.84
  }
}

Fields:

Field Type Constraints Description
age integer 0 < age < 120 User's age
weight float > 0 Weight in kg
height float 0 < height < 2.5 Height in meters
income_lpa float > 0 Annual income in lakhs per annum
smoker boolean - Smoking status
city string - City of residence
occupation string enum One of: retired, freelancer, student, government_job, business_owner, unemployed, private_job

Computed Features:

  • BMI: Calculated as weight / (heightΒ²)
  • Lifestyle Risk: Categorized as high/medium/low based on smoking and BMI
  • City Tier: Classified as Tier 1, Tier 2, or Others
  • Age Group: Categorized into appropriate age ranges

🐳 Docker Deployment

Building the Docker Image

docker build -t insurance-premium-api .

Running the Container

docker run -d -p 8000:8000 --name insurance-api insurance-premium-api

The API will be accessible at http://localhost:8000

Docker Compose (Optional)

Create a docker-compose.yml for easier orchestration:

version: '3.8'

services:
  api:
    build: .
    ports:
      - "8000:8000"
    environment:
      - ENVIRONMENT=production
    restart: unless-stopped

Run with:

docker-compose up -d

πŸ”§ Development

Code Formatting

Format code with Black:

black app/ model/ frontend/

Import Sorting

Sort imports with isort:

isort app/ model/ frontend/

Linting

Check code quality:

flake8 app/ model/ frontend/

Running Tests

pytest tests/

🎯 Model Information

The machine learning model uses the following features for prediction:

  1. BMI - Body Mass Index calculated from height and weight
  2. Age Group - Categorized age ranges
  3. Lifestyle Risk - Computed from smoking status and BMI
  4. City Tier - Metropolitan classification
  5. Income (LPA) - Annual income in lakhs
  6. Occupation - Professional category

The model outputs:

  • Predicted Category - Low, Medium, or High premium
  • Confidence Score - Model's certainty (0-1)
  • Class Probabilities - Probability distribution across all categories

πŸ™ Acknowledgments

  • FastAPI for the excellent web framework
  • Streamlit for the simple yet powerful UI framework
  • Scikit-learn for machine learning capabilities