Deploy a Scikit-Learn NLP Model with Docker, GCP Cloud Run and Flask

A brief guide to building an app to serve a natural language processing model, containerizing it and deploying it.

Building the App

The application to serve this model is simple. We just need to import our model into the app, receive a POST request and return the model’s response to that POST.

<script src=”https://gist.github.com/DougAF/1f27f7bf79603c996518c6e5eeacbf69.js"></script>

Containerizing the App

We need to make some final changes to our project files in preparation for deployment. In our project, we’ve used pipenv and pipenv-to-requirements to handle dependencies and generate a requirements.txt. All you'll need for your Docker container is the requirements.txt file.

Docker and Dockerfiles

Before we get the app running in the cloud, we must first Dockerize it. Check out our README in the GitHub repository for instructions on setting up this app locally with Docker.

Image for post
Photo by Steve Halama on Unsplash

Building and Starting the Docker Container Locally

Name and build the container with this line. We are calling our container spam-detector.

docker build . -t spam-detector
PORT=8000 && docker run -p 5000:${PORT} -e PORT=${PORT} spam-detector

Testing the app with Postman

Postman is a software development tool that enables people to test calls to APIs. Postman users enter data. The data is sent to a web server address. Information is returned as a response or an error, which Postman presents to the user.

  • Click Body and then raw
  • Select JSON from the dropdown to the right
Image for post
Sending a JSON post request with Postman
Image for post
The email is safe ¯\_(ツ)_/¯
Image for post

Testing the app with curl

Curl can be a simple tool for testing that allows us to remain in a CLI. I had to do some tweaking to get the command to work with the app, but adding the flags below resolved the errors.

curl -H "Content-Type: application/json" --request POST -d '{"text": "Spam is my name now give all your money to me"}' http://127.0.0.1:5000/predict
{"result":"ham"}

Docker Images and Google Cloud Registry

GCP Cloud Build allows you to build containers remotely using the instructions contained in Dockerfiles. Remote builds are easy to integrate into CI/CD pipelines. They also save local computational time and energy as Docker uses lots of RAM.

gcloud builds submit --tag gcr.io/PROJECT-ID/container-name

Deploy the container image using the CLI

  1. Deploy using the following command:
gcloud run deploy --image gcr.io/PROJECT-ID/container-name --platform managed

Deploy the container image using the GUI

Now that we have a container image stored in GCR, we are ready to deploy our application. Visit GCP cloud run and click create service, be sure to set up billing as required.

Image for post
Image for post
Selecting a Container image from GCR
Image for post
Services details
Image for post
Woohoo!

Conclusion

We’ve covered setting up an app to serve a model and building docker containers locally. Then we dockerized our app and tested it locally. Next, we stored our docker image in the cloud and used it to build an app on Google Cloud Run.

Comments

Popular posts from this blog

Easy Text-to-Speech with Python

Flutter for Single-Page Scrollable Websites with Navigator 2.0

Better File Storage in Oracle Cloud