A REST API built with FastAPI to serve a classification model trained with scikit-learn.
This project showcases a complete end-to-end model deployment pipeline, including:
- Model training
- Model serialization using
joblib - API exposure via FastAPI
- Containerization with Docker
tp6_api_diegodm/
│
├── app/
│ ├── main.py # FastAPI app and endpoints
│ ├── model.py # Model loading and prediction logic
│ └── schemas.py # Data validation using Pydantic
│
├── models/
│ ├── final_meta_classifier.joblib
│ └── ...
│
├── Dockerfile # Deployment image
├── requirements.txt # Python dependencies
└── README.md
- Python 3.10+
- pip
- Docker (optional, for deployment)
pip install -r requirements.txtuvicorn app.main:app --reload --port 8000The API will be available at:
http://127.0.0.1:8000
Interactive documentation (Swagger UI):
http://127.0.0.1:8000/docs
FastAPI automatically generates a Swagger UI interface to test endpoints in real time.
GET /health
Checks the service status and whether the model is loaded.
Example response:
{
"status": "healthy",
"model_loaded": true,
"model_version": "1.0"
}POST /predict
Request:
{
"feature_1": 1.0,
"feature_2": 2.0,
"feature_3": 3.0
}Response:
{
"label": 1,
"probability": 0.999,
"model_version": "1.0"
}docker build -t <username>/tp6-api:1.0 .docker run --rm -p 8000:8000 <username>/tp6-api:1.0curl http://127.0.0.1:8000/healthLogin:
docker loginPush the image:
docker push <username>/tp6-api:1.0- The model is loaded once at application startup to minimize latency.
- Input validation is handled using Pydantic (
app/schemas.py). - If the model features change, you must update:
app/schemas.py
app/main.py
- FastAPI
- Scikit-Learn
- Pydantic
- Uvicorn
- Docker
- Joblib