FastAPI service that exposes a machine learning model for Iris flower classification using scikit-learn.
The goal of this project is to demonstrate how I:
- train a machine learning model
- serve it through a REST API
- automatically test the API
- integrate the project with continuous integration
| Tool | Purpose |
|---|---|
| Python 3.x | Programming language |
| FastAPI | Web framework for serving the API |
| scikit-learn | Machine learning library |
| joblib | Model serialization |
| pytest | Automated testing framework |
| GitHub Actions | Continuous Integration |
ml-iris-api/
├── app/
│ ├── __init__.py
│ └── main.py # FastAPI app loading the trained model and exposing /predict
│
├── models/
│ └── iris_rf.joblib # trained RandomForest model
│
├── tests/
│ ├── __init__.py
│ └── test_api.py # API tests using TestClient
│
├── train_model.py # script to train and save the model
│
├── requirements.txt
├── pytest.ini
│
└── .github/
└── workflows/
└── tests.yml # CI pipeline
app/ Contains the FastAPI application and prediction endpoint.
models/ Stores the trained machine learning model.
tests/ Automated API tests using pytest and FastAPI TestClient.
train_model.py Script used to train the machine learning model and save it.
GitHub Actions workflow Continuous Integration pipeline configuration.
To train the machine learning model locally:
pip install -r requirements.txt
python train_model.py
This script:
- Downloads the Iris dataset
- Trains a RandomForest classifier
- Saves the trained model to:
models/iris_rf.joblib
Start the FastAPI server:
uvicorn app.main:app --reload
The API will run at:
http://127.0.0.1:8000
GET /health
Returns a simple response confirming that the service is running.
POST /predict
Example request:
{
"sepal_length": 5.1,
"sepal_width": 3.5,
"petal_length": 1.4,
"petal_width": 0.2
}The API returns:
- predicted Iris class
- class probabilities
Possible classes:
- setosa
- versicolor
- virginica
FastAPI automatically generates interactive documentation.
http://127.0.0.1:8000/docs
http://127.0.0.1:8000/openapi.json
These allow testing the API directly from the browser.
The test suite verifies key functionality of the API.
Covered scenarios:
/healthendpoint- valid prediction request
- invalid input validation
- API response correctness
Run all tests:
pytest
Tests use FastAPI TestClient, which allows testing the API without starting a separate server.
The repository includes a GitHub Actions CI pipeline.
Workflow location:
.github/workflows/tests.yml
Triggered on:
- push
- pull_request to the
mainbranch
Pipeline actions:
- Install project dependencies
- Train the ML model using
train_model.py - Run the full pytest test suite
This ensures that:
- the model trains correctly
- the API works
- the tests pass on every commit
You can view pipeline runs in the Actions tab of this repository.
Potential improvements for the project:
- add Docker containerization
- add model versioning
- integrate ML experiment tracking (MLflow)
- add input schema validation
- deploy the API to a cloud environment
Kacper Blok
Machine Learning / Backend / QA Automation Portfolio Project