This repository contains a comprehensive Medical Insurance Premium Prediction system with both a Streamlit web application and a FastAPI backend. The project leverages multiple Machine Learning algorithms to predict insurance premiums based on user demographics and health factors.
Experience the Medical Insurance Premium Predictor in action!
- Modern UI/UX: Beautiful, responsive interface with dark theme and medical styling
- Real-Time Predictions: Instant premium predictions with API integration
- Interactive Visualizations: Model performance comparisons and insights
- User-Friendly Input: Intuitive form with validation and helpful tooltips
- RESTful API: Production-ready API deployed on Railway
- Model Serving: Loads trained ML model for predictions
- Input Validation: Pydantic models for data validation
- Health Checks: API status monitoring endpoints
- Multiple Algorithms: Random Forest, Gradient Boosting, XGBoost comparison
- Model Performance: Comprehensive evaluation metrics (RΒ² scores, RMSE)
- Trained Model: Pre-trained model included (
Insurance_ML.pkl) - Dataset: Complete insurance dataset (
insurance.csv)
Medical_Insurance_premium_ML/
βββ App.py # Streamlit frontend application
βββ FAPI_MED.py # FastAPI backend server
βββ Insurance_ML.pkl # Trained machine learning model
βββ insurance.csv # Complete dataset (1,340 records)
βββ Medical_Insurance_Premium_Predicting_Model.ipynb # Jupyter notebook with ML pipeline
βββ Medical Insurance Price Prediction using Machine Learning.py # Standalone ML script
βββ requirements.txt # Python dependencies
βββ Procfile # Deployment configuration for Railway
βββ README.md # This file
βββ .gitignore # Git ignore patterns
βββ Model_Performance.png # Model comparison visualization
βββ Preview_1.png # App interface preview
βββ Preview_2.png # Additional app preview
- Streamlit - Interactive web application
- HTML/CSS - Custom styling and theming
- FastAPI - Modern, fast web framework
- Uvicorn - ASGI server
- Pydantic - Data validation
- Scikit-Learn - ML algorithms and preprocessing
- XGBoost - Gradient boosting
- Pandas - Data manipulation
- NumPy - Numerical computing
- Railway - Cloud deployment platform
- Pickle - Model serialization
π Live Application: https://medical-premium-predictor.streamlit.app/
Simply visit the link above to use the application directly in your browser!
-
Clone the Repository:
git clone https://github.com/SwayamAg/Medical-Insurance-Premium-Prediction.git cd Medical-Insurance-Premium-Prediction -
Install Dependencies:
pip install -r requirements.txt
-
Run the Streamlit App:
streamlit run App.py
-
Access the App: Open your browser to
http://localhost:8501
-
Install Dependencies:
pip install -r requirements.txt
-
Run the FastAPI Server:
python FAPI_MED.py
-
Access the API:
- API Documentation:
http://localhost:8080/docs - Health Check:
http://localhost:8080/ - Prediction Endpoint:
http://localhost:8080/predict
- API Documentation:
The FastAPI backend is deployed on Railway and available at:
- API Base URL:
https://medical-insurance-premium-prediction-production.up.railway.app - API Documentation:
https://medical-insurance-premium-prediction-production.up.railway.app/docs
The system accepts the following parameters:
- Age: Customer's age (18-64)
- Sex: Gender (male/female)
- BMI: Body Mass Index (10-50)
- Children: Number of dependents (0-5)
- Smoker: Smoking status (yes/no)
- Region: Geographic region (northwest/northeast/southeast/southwest)
- User inputs demographic and health data
- Data is validated and preprocessed
- Categorical variables are encoded
- Trained ML model makes prediction
- Premium amount is returned to user
The system includes multiple ML models with performance comparisons:
- Random Forest: High accuracy and interpretability
- Gradient Boosting: Excellent predictive power
- XGBoost: State-of-the-art performance
The insurance.csv dataset contains 1,340 records with the following features:
- age: Age of the insured person
- sex: Gender of the insured person
- bmi: Body Mass Index
- children: Number of children/dependents
- smoker: Smoking status
- region: Geographic region
- charges: Insurance premium (target variable)
The Streamlit app can be deployed on:
- Streamlit Cloud
- Heroku
- Railway
- AWS/GCP/Azure
The FastAPI backend is currently deployed on Railway:
- Production URL:
https://medical-insurance-premium-prediction-production.up.railway.app - Auto-scaling: Handles traffic automatically
- SSL Certificate: HTTPS enabled
The project includes a Procfile for deployment on Railway and other platforms:
web: uvicorn FAPI_MED:app --host 0.0.0.0 --port $PORTThis Procfile tells the deployment platform to:
- Run the FastAPI application using
uvicorn - Use
FAPI_MED.pyas the main application file - Bind to all network interfaces (
0.0.0.0) - Use the
$PORTenvironment variable for the port number
-
Create a virtual environment:
python -m venv .MED source .MED/bin/activate # On Windows: .MED\Scripts\activate
-
Install development dependencies:
pip install -r requirements.txt
-
Run tests and development server:
# Frontend streamlit run App.py # Backend python FAPI_MED.py
To retrain the model:
- Open
Medical_Insurance_Premium_Predicting_Model.ipynb - Run all cells to train new models
- Save the best model as
Insurance_ML.pkl
Returns API status
{
"message": "Medical Insurance Premium Predictor API is running."
}Input:
{
"age": 25,
"sex": "male",
"bmi": 28.5,
"children": 0,
"smoker": "no",
"region": "northwest"
}Output:
{
"predicted_premium": 2840.50
}- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Swayam Agrawal
- GitHub: @SwayamAg
- Repository: Medical-Insurance-Premium-Prediction
- Dataset source and contributors
- Streamlit and FastAPI communities
- Machine Learning community for algorithms and techniques
β Star this repository if you find it helpful!


