Skip to content

About

A small, end-to-end Iris classification API built with scikit-learn and FastAPI. Includes validated predictions, pytest tests, Docker, GitHub Actions CI and a Render deployment blueprint.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Iris Classifier API

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.

Tests Python FastAPI scikit--learn

Demo: Live API · API docs locally

What it does

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

Model results

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.

Run locally

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 --reload

Open http://127.0.0.1:8000/docs for the interactive Swagger UI. The health check is at http://127.0.0.1:8000/health.

Example prediction

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.

Run tests

pytest -q

GitHub Actions installs dependencies, retrains the model, and runs the test suite on every push and pull request.

Run with Docker

docker build -t iris-classifier-api .
docker run --rm -p 8000:8000 iris-classifier-api

Then 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.

Deploy to Render

  1. Create a GitHub repository and push this project.
  2. In Render, select New → Blueprint, connect the repository, and apply the settings from render.yaml.
  3. Wait for the Docker build and deployment. Copy the public service URL Render provides.
  4. Test <service-url>/health and <service-url>/docs.

Render may take a short time to wake a free service after inactivity.

Project structure

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

About

A small, end-to-end Iris classification API built with scikit-learn and FastAPI. Includes validated predictions, pytest tests, Docker, GitHub Actions CI and a Render deployment blueprint.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages