This project is an Insurance Premium Prediction API built with FastAPI. The main objective is not only to provide a working insurance premium prediction service, but also to serve as a blueprint for building industry-grade FastAPI applications. The codebase demonstrates best practices in API design, modular code organization, input/output validation, and production readiness.
- FastAPI: High-performance, easy-to-use Python web framework.
- Modular Structure: Clear separation of models, schemas, and API logic.
- Input Validation: Uses Pydantic for robust request/response validation.
- Prediction Endpoint:
/predictendpoint for premium category prediction. - Health Check:
/healthendpoint for service monitoring. - Interactive Docs: Swagger UI available at
/docs. - Error Handling: Graceful error responses for invalid requests or server issues.
- Production Ready: Example
.gitignore, clear project structure, and extensible code.
Below is a demonstration of the working product:
You can pull the pre-built Docker image from Docker Hub:
https://hub.docker.com/r/anujjainbatu/insurance-premium-api
docker pull anujjainbatu/insurance-premium-apiOnce running (locally or via Docker), access the API at:
- Swagger UI: http://localhost:8000/docs
- Health check: http://localhost:8000/health
If you want to use this API in your own application, you can access the deployed instance at:
- Base URL: http://51.20.55.205:8000/
- Swagger UI: http://51.20.55.205:8000/docs
- Health check: http://51.20.55.205:8000/health
Feel free to integrate this endpoint directly into your projects for insurance premium
insurance-premium-prediction/
│
├── app.py # Main FastAPI application
├── model/
│ └── predict.py # Model loading and prediction logic
│ └── model.pkl # Serialized ML model (not included in repo)
├── schema/
│ ├── user_input.py # Pydantic model for input validation
│ └── prediction_response.py # Pydantic model for output schema
├── venv/ # Virtual environment (excluded from git)
├── README.md # Project documentation
├── .gitignore # Git ignore file
└── ...
-
Clone the repository
git clone <repo-url> cd insurance-premium-prediction
-
Create and activate a virtual environment
python3 -m venv venv source venv/bin/activate -
Install dependencies
pip install -r requirements.txt
-
Run the API
uvicorn app:app --reload
-
Access the API
- Swagger UI: http://127.0.0.1:8000/docs
- Health check: http://127.0.0.1:8000/health
POST /predict
{
"bmi": 27.5,
"lifestyle_risk": "medium",
"age_group": "30-40",
"city_tier": "tier_1",
"income_lpa": 12,
"occupation": "salaried"
}Response
{
"premium_category": "Medium",
"confidence": 0.85,
"class_probabilities": {
"Low": 0.1,
"Medium": 0.2,
"High": 0.7
}
}- This project is intended as a reference for building scalable, maintainable FastAPI applications in a production setting.
MIT License
Blueprint for Industry-Grade FastAPI Apps
Use this project as a starting point for your own robust, production-ready APIs!
