A small end-to-end machine learning service that trains a scikit-learn classifier and serves predictions through a validated FastAPI endpoint. Built as a portfolio project to demonstrate a straightforward path from model training to a tested, containerized API.
Demo: Live API · API docs locally
Send four Iris flower measurements in centimeters to POST /predict. The API returns the predicted species, class ID, and model confidence. FastAPI validates the request and rejects missing or out-of-range values with HTTP 422.
| Component | Choice |
|---|---|
| Dataset | Fisher's Iris dataset (150 samples, 4 measurements, 3 species) |
| Classifier | StandardScaler + LogisticRegression |
| API | FastAPI + Pydantic validation |
| Tests and CI | pytest + GitHub Actions |
| Deployment | Docker + Render Blueprint |
The training script uses a stratified 80/20 train/test split with random_state=42. On the 30 held-out examples, the model achieved 93.3% accuracy and 0.933 macro F1. The saved API model is then refit on all 150 examples. This compact, classic dataset is useful for demonstrating the service workflow; these results are not a claim of production performance.
The dataset is provided by scikit-learn's load_iris and originates from the UCI Machine Learning Repository.
Requires Python 3.12 or later.
git clone https://github.com/codereen/iris-classifier-api.git
cd iris-classifier-api
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txt
python train.py
uvicorn app:app --reloadOpen http://127.0.0.1:8000/docs for the interactive Swagger UI. The health check is at http://127.0.0.1:8000/health.
curl -X POST http://127.0.0.1:8000/predict \
-H 'Content-Type: application/json' \
-d '{"sepal_length":5.1,"sepal_width":3.5,"petal_length":1.4,"petal_width":0.2}'Example response:
{
"species": "setosa",
"class_id": 0,
"confidence": 0.98
}Valid input fields are sepal_length, sepal_width, petal_length, and petal_width. Each value must be greater than 0 and no more than 10 centimeters.
pytest -qGitHub Actions installs dependencies, retrains the model, and runs the test suite on every push and pull request.
docker build -t iris-classifier-api .
docker run --rm -p 8000:8000 iris-classifier-apiThen visit http://localhost:8000/docs. The image installs dependencies, trains the model, and starts the API. It honors the PORT environment variable on hosting platforms.
- Create a GitHub repository and push this project.
- In Render, select New → Blueprint, connect the repository, and apply the settings from
render.yaml. - Wait for the Docker build and deployment. Copy the public service URL Render provides.
- Test
<service-url>/healthand<service-url>/docs.
Render may take a short time to wake a free service after inactivity.
| File | Purpose |
|---|---|
app.py |
FastAPI app, input schema, health check, and prediction endpoint |
train.py |
Reproducible evaluation and model training; saves the model bundle |
models/iris_classifier.joblib |
Serialized model loaded by the API; regenerated by python train.py |
tests/test_api.py |
Tests for health, valid predictions, and invalid inputs |
requirements.txt |
Runtime and test dependencies |
Dockerfile |
Container build and API startup |
render.yaml |
Render Blueprint configuration |
.github/workflows/tests.yml |
Automated test workflow for pushes and pull requests |
pytest.ini |
Pytest test discovery and import configuration |
.gitignore |
Ignores local environments and Python cache files |