A production-ready Machine Learning API built with FastAPI, containerized using Docker, and automated using CI/CD via GitHub Actions.
This service predicts health insurance expenses based on user inputs such as age, BMI, smoking status, region, and number of children.
This application exposes a REST API that serves predictions from a trained scikit-learn model.
The project follows production-oriented practices including:
- Modular architecture
- Automated testing with pytest
- Continuous Integration
- Continuous Deployment to Docker Hub
- Lazy model loading for runtime stability
- Dockerized deployment
Health_insurance/
│
├── src/
│ ├── api/
│ │ ├── main.py
│ │ └── schema.py
│ ├── data/
│ ├── features/
│ ├── models/
│ └── utils/
│
├── tests/
│ ├── test_api.py
│ └── test_train.py
│
├── Dockerfile
├── requirements.txt
├── requirements-dev.txt
├── pytest.ini
├── .github/workflows/
│ ├── ci.yml
│ └── cd.yml
{
"age": 30,
"gender": "male",
"bmi": 25.5,
"children": 1,
"discount_eligibility": "no",
"region": "southwest"
}Legacy payloads with sex / smoker are also accepted for backward compatibility.
{
"predicted_expenses": 3456.78
}Interactive API documentation:
http://127.0.0.1:8000/docs
Frontend UI:
http://127.0.0.1:8000/
python -m venv venvActivate:
Windows
venv\Scripts\activateMac/Linux
source venv/bin/activatepip install -r requirements.txt
pip install -r requirements-dev.txtuvicorn src.api.main:app --reloaddocker build -t health-insurance-api .docker run -p 8000:8000 health-insurance-apipytestTests are automatically executed in the CI pipeline on every push to the main branch.
- Installs dependencies
- Runs unit tests
- Builds Docker image
- Builds Docker image
- Pushes image to Docker Hub
Configured GitHub Secrets:
DOCKERHUB_USERNAMEDOCKERHUB_TOKEN
To retrain the model:
python -m src.models.trainThe model is loaded lazily at runtime to prevent import-time failures during CI execution.
If you uploaded a previously trained model and predictions fail, call:
curl http://127.0.0.1:8000/model-infoThis returns the model's expected input columns so you can confirm schema compatibility without retraining.
- Lazy model initialization
- Docker-based isolation
- Automated validation through tests
- Secure secret management
- Ready for cloud deployment
Pull the production image:
docker pull <your-dockerhub-username>/health-insurance-api- Python 3.11
- FastAPI
- scikit-learn
- MLflow
- Docker
- GitHub Actions
- pytest
MIT License