Inference API using FastAPI with Python 3.12 Data explorations on the IRIS dataset done in Jupyter notebooks helped data understanding and informed model evaluation.
Development follows an interative cycle between:
- Devops
- Software engineering/full-stack developement
- Data science
- Data engineering
Each step allowed to have minimal project basis, which could foster collaboration in a team setting. It allowed for quick wins and identifying future improvements quickly. Devops setup informed project structure and tooling for the tech-stack and tooling in Github. Designing and implementing a first version of the API with mocked endpoints allowed simple integration of the model to come. Data exploration/science informed model selection, which in turn informed model training scripts. These scripts then were available to be included in the devops tooling, restarting the cycle. Second cycle the addresses improvements and informs the discussion to be had on the project.
Setting up the Python virtual env for dev requires running the following commands.
> python -m venv .venv
> source .venv/bin/activate
> pip install -r requirements.txt# Start the FastAPI server
python srcThe API will be available at http://localhost:8888
# Build and start all services
docker compose up -d --build
# Stop all services
docker compose downThe API will be available as a single uvicorn worker at http://localhost:8888 and behind a NGINX proxy in replicas at http://localhost/api.
To train the KNN model:
# Make sure you're in the project root
python models/train_knn.pyThe trained model will be saved to models/knn_model.pkl.
- Type: K-Nearest Neighbors (KNN) Classifier
- Hyperparameters:
- n_neighbors: 9
- Default settings for other parameters (uniform weights, euclidean distance)
- Features used:
- Sepal length (cm)
- Sepal width (cm)
- Petal length (cm)
- Petal width (cm)
- Training Accuracy: 98.2%
- Mean Absolute Error: 0.013422818791946308
The model was selected after comparing multiple algorithms including SVC, LogisticRegression, DecisionTree, and RandomForest classifiers. KNN demonstrated the best performance on this dataset before and after removing duplicates.
For detailed model evaluation and selection process, refer to notebooks/model-selection.ipynb.
- Open the repository in VS Code
- Select the
.venvPython interpreter - Navigate to
notebooks/directory - Open and run
.ipynbfiles
# Activate virtual environment
source .venv/bin/activate
# Start Jupyter
jupyter notebook notebooks/Available notebooks:
eda.ipynb: Exploratory data analysismodel-selection.ipynb: Model evaluation and selection
Once the API is running, you can access:
- Interactive API documentation (Swagger UI) at http://localhost:8888/docs
This project uses Ruff for code linting and formatting.
To check your code:
> ruff check .To automatically fix linting issues:
> ruff check --fix .To format your code:
> ruff format .This project uses pytest for testing. To run the tests:
> pytestTo run tests with coverage report:
> pytest --cov=src --cov-report=term-missingTo generate an HTML coverage report:
> pytest --cov=src --cov-report=htmlThe HTML coverage report will be generated in the htmlcov directory and can be viewed in your browser:
> open htmlcov/index.htmlVS Code users can run tests directly in the editor using the Testing sidebar or by clicking the "Run Test" links that appear above each test.
