This project is an end-to-end Machine Learning application that predicts insurance premiums based on user input (age, BMI, smoking status, region, etc.). It integrates a trained Random Forest Regressor model with a FastAPI backend, a Streamlit frontend, and is fully containerized using Docker for deployment on AWS EC2.
insurance-premium-prediction/
│── main.py # Entry point for starting backend
│── app.py # FastAPI application (model serving API)
│── frontend.py # Streamlit app (frontend UI)
│── model.pkl # Pre-trained Random Forest Regressor model
│── insurance.csv # Dataset used for training
│── patients.json # Example input JSON
│── fastapi_ml_model.ipynb # Notebook for experimentation & training
│── requirements.txt # Python dependencies
│── .gitignore
│── Step_1_Creating_model_pkl.png
│── Step_2a_Creating_endpoint_API_Endpoint.png
│── Step_2b_Input_and_Output.png
│── Improved API.png
- Dataset:
insurance.csv
- Algorithm: Random Forest Regressor (trained in
fastapi_ml_model.ipynb
) - Features: age, sex, BMI, children, smoker, region.
- Target: insurance premium (
charges
). - Model saved as
model.pkl
for inference.
-
app.py
loadsmodel.pkl
and provides/predict
API endpoint. -
Workflow:
- Receive JSON input.
- Preprocess input.
- Call Random Forest Regressor model.
- Return premium prediction.
frontend.py
is a Streamlit app.- It provides an interactive web interface where users can input values (age, BMI, smoking status, etc.).
- The Streamlit app sends data to the FastAPI backend and displays predictions in real-time.
-
Dockerfile builds a containerized version of the app:
- Installs dependencies from
requirements.txt
. - Runs FastAPI and Streamlit inside the container.
- Installs dependencies from
-
Commands:
docker build -t insurance-premium-prediction . docker run -p 8000:8000 insurance-premium-prediction
-
Image uploaded to DockerHub.
-
EC2 Ubuntu instance pulls image and runs container.
-
Accessible at:
http://<EC2-Public-IP>:8000/docs (FastAPI) http://<EC2-Public-IP>:8501 (Streamlit)
- frontend.py (Streamlit) → User inputs data → sends request to FastAPI backend.
- app.py (FastAPI) → Receives request → uses Random Forest Regressor model (
model.pkl
) → returns prediction. - Streamlit UI updates with predicted premium.
Flow: Streamlit → FastAPI → Random Forest Regressor model → Prediction → Streamlit Output
-
Install dependencies:
pip install -r requirements.txt
-
Start FastAPI backend:
uvicorn app:app --reload
Available at
http://127.0.0.1:8000/docs
-
Start Streamlit frontend:
streamlit run frontend.py
Available at
http://localhost:8501
-
Build Docker image:
docker build -t insurance-premium-prediction .
-
Run container (FastAPI + Streamlit):
docker run -p 8000:8000 -p 8501:8501 insurance-premium-prediction
-
Access:
- API:
http://localhost:8000/docs
- UI:
http://localhost:8501
- API:
-
Launch Ubuntu EC2 instance and install Docker:
sudo apt update sudo apt install docker.io -y
-
Pull image:
docker pull <your-dockerhub-username>/insurance-premium-prediction
-
Run container:
docker run -d -p 8000:8000 -p 8501:8501 <your-dockerhub-username>/insurance-premium-prediction
-
Access:
http://<EC2-Public-IP>:8000/docs
http://<EC2-Public-IP>:8501
This project demonstrates a complete ML pipeline:
- Random Forest Regressor model for premium prediction.
- FastAPI backend for model serving.
- Streamlit frontend for interactive UI.
- Docker for containerization.
- AWS EC2 for cloud deployment.