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.

Objectives

By the end of this post you should be able to:

  1. Develop a RESTful API with Python and FastAPI
  2. Build a basic machine learning model to predict stock prices
  3. Deploy a FastAPI app to Heroku

FastAPI

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:

  1. Heavily inspired by Flask, it has a lightweight microframework feel with support for Flask-like route decorators.
  2. It takes advantage of Python type hints for parameter declaration which enables data validation (via Pydantic) and OpenAPI/Swagger documentation.
  3. Built on top of Starlette, it supports the development of asynchronous APIs.
  4. It’s fast. Since async is much more efficient than the traditional synchronous threading model, it can compete with Node and Go with regards to performance.

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.

Project Setup

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:

  1. --reload enables auto-reload so the server will restart after changes are made to the code base.
  2. --workers 1 provides a single worker process.
  3. --host 0.0.0.0 defines the address to host the server on.
  4. --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

Deploying and Hosting a Machine Learning Model with FastAPI and Heroku
10.10 GEEK