A simple python web service to host a machine learning model for prediction as a REST API.
The container trains a simple text classifier and hosts it for prediction as a web service using FastAPI. The data for model training is included in the project.
For more details on customizing this project or using this as a template please follow this article.
- System should have docker engine installed.
Note: I developed and tested this on Ubuntu-16.04.
- Build the docker image
docker build --network=host -t ml-prediction-web-service:v1 .
- Check the image
docker images
- Run the container
docker run -d --net=host --name=ml-prediction-web-service ml-prediction-web-service:v1
- Check whether the container is up
docker ps
When we run the container two scripts are initiated:
train.py
which trains the model to be hosted.app.py
which hosts the model as a web service.
The web services includes the openapi integration. Thus, we can directly use the swagger portal from web browser to use the API. To open the swagger portal go to your browser and enter http://localhost:8080/swagger/
. This will open the swagger portal only if the service is hosted properly.
To check whether service is up:
-
Click on the
GET
bar. -
Click on
Try it out
- Click on
Execute
- If you see the following screen then your service is up.
To predict the label of the text:
-
Click on the
POST
bar. -
Click on
Try it out
- Click on
Execute
- We should see a response similar to the following:
To check the the web service logs we need to get inside the running container. To do so execute the following command:
docker exec -it ml-prediction-web-service bash
Now we are inside the container.
The logs are available in the logs folder in the files ml-prediction-web-service.log
and ml-prediction-web-service.err
.
Run the following command to stop the container:
docker stop ml-prediction-web-service
API Documentation with ReDoc
Author: Pranay Chandekar