A RESTful API to return predictions from a trained ML model, built with Python 3 and FastAPI
10K+
A RESTful API to return predictions from a trained ML model, built with Python 3 and FastAPI
First, open a command line interface and clone the GitHub repo in your workspace
PS > cd $WORKSPACE_PATH$
PS > git clone https://github.com/jgbustos/ml_rest_fastapi
PS > cd ml_rest_fastapi
Create and activate a Python virtual environment, then install the required Python packages using pip
PS > virtualenv venv
PS > venv\scripts\activate.ps1
(venv) PS > pip install -r requirements.txt
Once dependencies are installed, set up the project for development
(venv) PS > pip install -e .
Finally, run the project:
(venv) PS > uvicorn ml_rest_fastapi.app:app --host 0.0.0.0 --port 8888 --reload
Open the URL http://localhost:8888/docs/ with your browser and see the sample OpenAPI documentation
OpenAPI JSON available from URL http://localhost:8888/openapi.json
These two methods are meant to be used as the liveness and readiness probes in a Kubernetes deployment:
Configuration parameters are contained in the file ml_rest_fastapi/settings.py, but they can also be overriden by setting env vars:
settings: Dict = {
# Trained ML/AI model settings
"TRAINED_MODEL_MODULE_NAME": "sample_model",
# Module settings
"DEBUG": True,
"MULTITHREADED_INIT": True,
}
| Parameter | Values | Details |
|---|---|---|
| TRAINED_MODEL_MODULE_NAME | e.g.: sample_model | Name of the Python module that initialises the ML model and returns predictions (see section below) |
| DEBUG | False/True | When Debug = True, Uvicorn will run in reload mode, and FastAPI will return a colourful traceback instead of a sober 500/"Internal Server Error" |
| MULTITHREADED_INIT | False/True | Multi-threaded initialisation of the trained model |
The trained ML model is meant to be initialised and invoked to make predictions in the context of a Python unit saved inside the directory ml_rest_fastapi/trained_model. The structure of this Python module is explained in this document
Content type
Image
Digest
sha256:87f40b24d…
Size
158.3 MB
Last updated
8 months ago
docker pull jgbustos/ml-rest-fastapi