A machine learning web application that predicts medical insurance costs based on demographic and health factors. The application features a modern, interactive frontend with a Flask API backend serving a trained neural network model.
- Interactive Web Interface: Modern, responsive UI with smooth animations
- Real-time Predictions: Get instant insurance cost estimates
- RESTful API: Flask backend with JSON endpoints
- Dockerized Deployment: Easy setup and deployment with Docker
- Machine Learning Model: Neural network trained on insurance data with log transformation
Enter your details and get an instant insurance cost prediction
- Algorithm: Neural Network
- Features: Age, Sex, BMI, Children, Smoker Status, Region
- Target: Medical Insurance Charges (log10 transformed)
- Framework: scikit-learn
- Age: 18-100 years
- Sex: Male/Female
- BMI: Body Mass Index (15-50)
- Children: Number of dependents (0-10)
- Smoker: Yes/No
- Region: Northeast, Northwest, Southeast, Southwest (US)
βββββββββββββββββββ HTTP/JSON βββββββββββββββββββ
β Frontend β βββββββββββββββ β Backend β
β (HTML/JS) β β (Flask API) β
β Port: 3000 β βββββββββββββββ β Port: 5010 β
βββββββββββββββββββ βββββββββββββββββββ
β
βΌ
βββββββββββββββββββ
β ML Model β
β (.pkl file) β
βββββββββββββββββββ
- Docker and Docker Compose installed
- Git
git clone https://github.com/omarsinno54/insurance-cost-predictor.git
cd insurance-cost-predictordocker-compose up --build- Frontend: http://localhost:3000
- API: http://localhost:5010
- Health Check: http://localhost:5010/health
Health Check:
curl http://localhost:5010/healthExpected response:
{
"model_loaded": true,
"status": "healthy"
}Make a Prediction:
curl -X POST http://localhost:5010/predict \
-H "Content-Type: application/json" \
-d '{
"age": 30,
"sex": "male",
"bmi": 25.5,
"children": 2,
"smoker": "no",
"region": "northeast"
}'Expected response:
{
"prediction": 5444.925698040062,
"status": "success"
}- Go to http://localhost:3000
- Fill out the insurance form with your details
- Click "Predict Insurance Cost"
- See your estimated annual insurance cost
insurance-cost-predictor/
βββ README.md
βββ docker-compose.yml # Multi-container orchestration
βββ .gitignore
βββ Makefile # Makefile including MLflow
β
βββ backend/ # Flask API backend
β βββ Dockerfile
β βββ requirements.txt
β βββ app.py # Flask API server
β βββ model.pkl # Trained ML model
β βββ Makefile # Makefile used in Docker container
β
βββ frontend/ # Web interface
βββ Dockerfile
βββ nginx.conf # Nginx configuration
βββ index.html # Interactive web interface
Check API health status.
Response:
{
"status": "healthy",
"model_loaded": true
}Predict insurance cost for given parameters.
Request Body:
{
"age": 30,
"sex": "male",
"bmi": 25.5,
"children": 2,
"smoker": "no",
"region": "northeast"
}Response:
{
"prediction": 5444.925698040062,
"status": "success"
}Parameters:
age: Integer (18-100)sex: String ("male" or "female")bmi: Float (15.0-50.0)children: Integer (0-10)smoker: String ("yes" or "no")region: String ("northeast", "northwest", "southeast", "southwest")
1. "Docker command not found"
# Check Docker installation
docker --version
docker-compose --version
# On macOS, if Docker Desktop is installed but command not found:
echo 'export PATH="/Applications/Docker.app/Contents/Resources/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc2. "Port already in use"
If you see port conflict errors, change the ports in docker-compose.yml:
services:
backend:
ports:
- "5011:5010" # Use different external port
frontend:
ports:
- "3001:80" # Use different external portThen access via http://localhost:3001 and http://localhost:5011
3. "Model file not found"
Ensure your .pkl model file is in the backend/ directory with the correct name in app.py.
4. Changes not reflecting Always rebuild after making changes:
docker-compose down && docker-compose up --build5. "Failed to load resource: net::ERR_NAME_NOT_RESOLVED" Check that the frontend JavaScript is using the correct URL format:
// Correct (with colon after http):
fetch('http://localhost:5010/predict', ...)
// Wrong (missing colon):
fetch('http//localhost:5010/predict', ...)This project showcases:
- Machine Learning Model Deployment: Serving ML models via REST API
- Containerization: Docker and Docker Compose for consistent environments
- Full-Stack Development: Frontend interface + Backend API
- Data Processing: Log transformation for skewed target variables
- Production Considerations: Health checks, proper error handling, CORS
The neural network model was trained with the following preprocessing:
- Target transformation:
log10(charges)to handle right-skewed distribution - Feature encoding: Categorical variables encoded appropriately
To retrain the model, feel free to explore the attached jupyter notebook notebook.ipynb, or write your own script!
| Metric | Value |
|---|---|
| RΒ² Score | 0.85 |
| MAE | 0.09 |
- Dataset Size: 1338 rows
- Features: 6 input features
- Target: Insurance charges (USD)
- Smoker Status - Highest impact on premiums
- Age - Strong positive correlation
- BMI - Significant for high BMI values
- Region - Regional cost variations
- Children - Moderate impact
- Sex - Minor statistical difference
To have cleaner structure, CLI commands were set in a Makefile
# Run application
make app
# Format code
make format
# Install dependencies
make install
# Run mlflow server
make mlflowThe project includes MLflow to log model hyperparameters, metrics and artifacts. The MLflow UI provides an impressive dashboard to compare model performance.
Omar Sinno
- GitHub: @omarsinno54
- LinkedIn: @omarhsinno
- Email: omarsinno54@gmail.com
- Dataset: Kaggle @"mosapabdelghany/medical-insurance-cost-dataset"
@misc{mosap_abdelghany_2025,
title={Medical Insurance Cost Dataset},
url={https://www.kaggle.com/dsv/12853160},
DOI={10.34740/KAGGLE/DSV/12853160},
publisher={Kaggle},
author={mosap abdelghany},
year={2025}
}
