Advertisement

Active Hackathon

Complete Hands-On Guide To FastAPI With Machine Learning Deployment

In this article, we will discuss the implementation of FastAPI and demonstrate how it can be used for deployment purposes.

Any machine learning model’s end goal is a deployment for production purposes. Building a REST API(Application Programming Interface) is the best possible way to evaluate model performance. In python, Django and more evidently Flask frameworks are used for this purpose. For machine learning, Flask is preferred more than Django. Here comes FastAPI which is faster than Flask, providing higher performance boost, easier to code, comes with automatic documentation, provides data validation on input data provided to the server, and many more such amazing features which I’ll be covering in this article.

FastAPI is one of the fastest python frameworks available. The automatic documentation comes with free of cost. Since it is based on python so it provides nice python type hints for autocompletion and type checks. FastAPI is built upon two major python libraries – Starlette(for web handling) and Pydantic(for data handling). FastAPI is based on some standard integrations — OpenAPI, JSON Schema, OAuth2.

THE BELAMY

Sign up for your weekly dose of what's up in emerging technology.

In this article, we will discuss the implementation of FastAPI and demonstrate how it can be used for deployment purposes.

Getting Started With FastAPI

Software Dependencies that are required for FastAPI:-

  • Python 3.6+
  • FastAPI
  • ASGI server for production such as Uvicorn or Hypercorn.
  • Pydantic

Basic Hello World 

First of all, we will do a very basic implementation to get familiar with FastAPI.

from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def index():
    return {"message": "Hello World"}

Save this program in main.py then from a terminal, execute: 

$uvicorn main:app --reload

The following message should be printed:

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [28720]
INFO:     Started server process [28722]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

After redirecting to the webpage, it should appear as:

{ “message”  :  “Hello World” }

Interactive Documentation

http://127.0.0.1.8000/docs will show the interactive documentation by Swagger UI.

Alternative documentation by redoc

FastAPI for ML models

Let’s take a pre-trained NLP model to extract the entities present in an English sentence. For this I’ll be using the Spacy library’s model ‘en_core_web_sm’  which is a pre-trained statistical model for English and can be installed with the command:

 python -m spacy download en_core_web_sm

Then use it in the following code:

from typing import List
import spacy
from pydantic import BaseModel
from fastapi import FastAPI
app = FastAPI()
nlp = spacy.load("en_core_web_sm")
@app.get("/")
def read_root():
    return {"Hello": "World"}
class Item(BaseModel):
    content: str
    comments: List[str] = []
@app.post("/item/")
def post_item(item: Item):
    doc = nlp(item.content)
    ents = []
    for ent in doc.ents:
        ents.append({"text":ent.text,"label":ent.label_})
    return {"message":item.content,"comments":item.comments, "ents":ents}

This code takes an English statement as input(‘item’) in realtime and extracts entities using the spacy model to show which part of the sentence falls under which class. Here class Item inherits the BaseModel from pydantic for its properties to be used.

Execute the code similarly as done above which will redirect to http://127.0.0.1.8000/docs and the following page will appear.

First, click onto ‘post’ as a post request is being made then click ‘Try it out’ to make changes to content and comments. 

fastapi

Edit string and add text as per your wish. I’ve put in place of content string ‘Apple buys U.K. based startup for $1 billion’. And for comments string – ‘Nice as expected’. 

If the inputs are not as per the requirements then validation error will show which part went wrong.

Click on ‘Execute’ to see the output 

fastapi

Note that both content messages and comments as per user input are printed.

{
  "message": "Apple buys U.K. based startup for $1 billion ",
  "comments": [
    "Nice as expected"
  ],
  "ents": [
    {
      "text": "Apple",
      "label": "ORG"
    },
    {
      "text": "U.K.",
      "label": "GPE"
    },
    {
      "text": "$1 billion",
      "label": "MONEY"
    }
  ]
}

The entities have worked well depicting Apple as an organisation, the U.K as a geopolitical entity and $1 billion as Money.

More experimentation could be done with the code such as adding multiple contents, adding comments and markdowns.

Conclusion

FastAPI can handle 9000 requests at a time. FastAPI can manage database sessions, web sockets, easy GraphQL injection and many more are still being built. FastAPI is being used by tech giants such as Netflix, Facebook, Microsoft, Uber. Making production-ready RestAPIs with few lines of code is apprehensive.

The complete code of this implementation is available on the AIM’s GitHub repository. Please visit this link for that code.

More Great AIM Stories

Jayita Bhattacharyya
Machine learning and data science enthusiast. Eager to learn new technology advances. A self-taught techie who loves to do cool stuff using technology for fun and worthwhile.

Our Upcoming Events

Conference, Virtual
Genpact Analytics Career Day
3rd Sep

Conference, in-person (Bangalore)
Cypher 2022
21-23rd Sep

Conference, in-person (Bangalore)
Machine Learning Developers Summit (MLDS) 2023
19-20th Jan, 2023

Conference, in-person (Bangalore)
Data Engineering Summit (DES) 2023
21st Apr, 2023

Conference, in-person (Bangalore)
MachineCon 2023
23rd Jun, 2023

3 Ways to Join our Community

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Telegram Channel

Discover special offers, top stories, upcoming events, and more.

Subscribe to our newsletter

Get the latest updates from AIM
MOST POPULAR

Data Science Skills Survey 2022 – By AIM and Great Learning

Data science and its applications are becoming more common in a rapidly digitising world. This report presents a comprehensive view to all the stakeholders — students, professionals, recruiters, and others — about the different key data science tools or skillsets required to start or advance a career in the data science industry.

How to Kill Google Play Monopoly

The only way to break Google’s monopoly is to have localised app stores with an interface as robust as Google’s – and this isn’t an easy ask. What are the options?

[class^="wpforms-"]
[class^="wpforms-"]