Assume that you’re a data scientist. Following a typical machine learning workflow, you’ll define the problem statement along with objectives based on business needs. You’ll then start finding and cleaning data followed by analyzing the collected data and building and training your model. Once trained, you’ll evaluate the results. This process of finding and cleansing data, training the model, and evaluating the results will continue until you’re satisfied with the results. You’ll then refactor the code and package it up in a module, along with its dependencies, in preparation for testing and deployment.
What happens next? Do you hand the model off to another team to test and deploy the model? Or do you have to handle this yourself? Either way, it’s important to understand what happens when a model gets deployed. You may have to deploy the model yourself one day. Or maybe you have a side project that you’d just like to stand up in production and make available to end users.
In this tutorial, we’ll look at how to deploy a machine learning model, for predicting stock prices, into production on Heroku as a RESTful API using FastAPI.
By the end of this post you should be able to:
FastAPI is a modern, high-performance, batteries-included Python web framework that’s perfect for building RESTful APIs. It can handle both synchronous and asynchronous requests and has built-in support for data validation, JSON serialization, authentication and authorization, and OpenAPI.
Highlights:
Review the Features guide from the official docs for more info. It’s also encouraged to review Alternatives, Inspiration, and Comparisons, which details how FastAPI compares to other web frameworks and technologies, for context.
Create a project folder called “fastapi-ml”:
$ mkdir fastapi-ml
$ cd fastapi-ml
Then, create and activate a new virtual environment:
$ python3.8 -m venv env
$ source env/bin/activate
(venv)$
Add a two new files: requirements.txt and main.py.
Unlike Django or Flask, FastAPI does not have a built-in development server. So, we’ll use Uvicorn, an ASGI server, to serve up FastAPI.
New to ASGI? Read through the excellent Introduction to ASGI: Emergence of an Async Python Web Ecosystem blog post.
Add FastAPI and Uvicorn to the requirements file:
fastapi==0.58.1
uvicorn==0.11.5
Install the dependencies:
(venv)$ pip install -r requirements.txt
Then, within main.py, create a new instance of FastAPI and set up a quick test route:
from fastapi import FastAPI
app = FastAPI()
@app.get("/ping")
def pong():
return {"ping": "pong!"}
Start the app:
(venv)$ uvicorn main:app --reload --workers 1 --host 0.0.0.0 --port 8008
So, we defined the following settings for Uvicorn:
--reload
enables auto-reload so the server will restart after changes are made to the code base.--workers 1
provides a single worker process.--host 0.0.0.0
defines the address to host the server on.--port 8008
defines the port to host the server on.main:app
tells Uvicorn where it can find the FastAPI ASGI application – e.g., "within the the ‘main.py’ file, you’ll find the ASGI app, app = FastAPI()
.
Navigate to http://localhost:8008/ping. You should see:
{
"ping": "pong!"
}
#machine-learning