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.
- Features
- Tech Stack
- Project Structure
- Installation
- Usage
- API Documentation
- Docker Deployment
- Development
- 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
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
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
- Python 3.12 or higher
- pip package manager
-
Clone the repository:
git clone <repository-url> cd fastapi-model
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
Start the FastAPI server:
uvicorn app.main:app --reloadThe 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
In a separate terminal, start the Streamlit app:
streamlit run frontend/app.pyThe web interface will open automatically at http://localhost:8501
Welcome endpoint
- Response:
{"message": "Premium prediction model"}
Health check endpoint
- Response:
{"status": "ok"}
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 build -t insurance-premium-api .docker run -d -p 8000:8000 --name insurance-api insurance-premium-apiThe API will be accessible at http://localhost:8000
Create a docker-compose.yml for easier orchestration:
version: '3.8'
services:
api:
build: .
ports:
- "8000:8000"
environment:
- ENVIRONMENT=production
restart: unless-stoppedRun with:
docker-compose up -dFormat code with Black:
black app/ model/ frontend/Sort imports with isort:
isort app/ model/ frontend/Check code quality:
flake8 app/ model/ frontend/pytest tests/The machine learning model uses the following features for prediction:
- BMI - Body Mass Index calculated from height and weight
- Age Group - Categorized age ranges
- Lifestyle Risk - Computed from smoking status and BMI
- City Tier - Metropolitan classification
- Income (LPA) - Annual income in lakhs
- 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
- FastAPI for the excellent web framework
- Streamlit for the simple yet powerful UI framework
- Scikit-learn for machine learning capabilities