FastAPI: The Right Replacement For Flask?

Kaustubh Gupta Last Updated : 12 Dec, 2023
7 min read

This article was published as a part of the Data Science Blogathon.

Introduction

After you are done with model building and proper hyperparameter tunning, the next step in Data Science projects is to showcase the final results to the general public. It is essential to do this so because not everybody is interested to view the code and they are more interested in the final result. It also helps Data Science aspirants to build an end-to-end project which gives them an edge over others and give them a taste for other technologies.

Deployment of machine learning models can take different routes depending upon the platform where you want to serve the model. The web interface is the most common way to serve a model but not limited to android and IOS apps or an IOT device like Raspberry Pi. If you research this in detail, then one framework that tops the search query is the flask framework which is a minimalistic application to quickly set up web servers but it has some issues which are now solved in a newly released framework call FastAPI which is gaining a lot of popularity these days.

In this article, we will see how the FastAPI framework has an edge over Flask with an example code to understand things in a better way. Before that, if you are interested in android app deployment then you can read my article Deploying ML in the Android App.

FastAPI vs Flask

Flask

It is a Python-based framework that allows you to hook up websites with less amount of code. You can create a small-scale website with this as it allows customization at every step. Being a minimalistic package, only core components are bundled with this and all other extensions require explicit setup. Flask is used by many developers to host their APIs. API (Application Program Interface) is an interface that allows communication between multiple intermediaries meaning that one can access any type of data using any technology. For instance, you can access an API using Javascript which could be built using Python. A simple program in flask looks like this:

from flask import Flask, jsonify app = Flask(__name__) @app.route("/<name>", methods=['GET']) def home(name): return jsonify({"message": f"Hello! {name}"}) if __name__ == "__main__": app.run(debug=True)

On hitting the URL localhost/AnyNameHere, a JSON result would be displayed something similar to this: (I use chrome extension called JSON viewer. You may be prompted with plain text instead of this formatted output)

Flask

Problems with Flask

The problem with this approach is that there is no data validation, meaning, that we can pass any type of data being it string, tuple, numbers, or any character. This can break the program often and you can imagine if an ML model getting wrong data types, the program will crash. You can create a data checker before passing the values further but it would add up additional work.

The error pages in Flask as simple HTML pages that can raise decoder errors when the API is being called in other applications. There are other issues with Flask such as slow nature, no async, and web sockets support that can speed up the processes, and finally no automated docs generation system. You need to manually design the user interface for the usage and examples of the API. All these issues are resolved in the new framework.

FastAPI

It is a modern framework that allows you to build APIs seamlessly without much effort. It has the ability to separate the server code from the business logic increasing code maintainability. As the name itself has fast in it, it is much faster as compared to the flask because it’s built over ASGI (Asynchronous Server Gateway Interface) instead of WSGI (Web Server Gateway Interface). It has a data validation system that can detect any invalid data type at the runtime and returns the reason for bad inputs to the user in the JSON format only which frees developers from managing this exception explicitly.

It generates the documentation on the go when you are developing the API which is the most requested thing from all the developers. Documentation is a great way for other developers to collaborate on a project as it presents them with everything that can be done with the necessary instructions. It also generates a nice GUI which solves everything that was missing in the flask.

It does all these things OpenAI specifications and Swagger for implementing these specifications. Being a developer, you are only focusing on the logic building part and the rest of the things are managed by the FastAPI. Let’s look at the same example which was created using Flask now implemented in FastAPI:import uvicorn from fastapi

import uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def home(name: str):
    return {"message": f"Hello! {name}"}
if __name__ == "__main__":
    uvicorn.run(app, host='127.0.0.1', port=8000, debug=True)

On hitting the URL localhost/?name=AnyNameHere, you will be prompted with output such as:

FastAPI

You can see that the code is very similar to flask but here we are using uvicorn server which is an ASGI implementation. Also, here we are not routing any endpoints and creating them directly using decorators which makes more sense. The function here simply takes the arguments required further which eliminates the need for the request object to be called.

Now comes the interesting part. To access the automated generated docs, hit the endpoint /docs and you will be presented with Swagger UI which allows you to test the API endpoints as well as you can define as an example for users to test out the endpoints:

API endpoint,flask fast API

There is another documentation generator that is bundled with FastAPI, i.e., ReDoc that also generated beautiful documentation with all the endpoints listed. It can be accessed by hitting the endpoint /redoc:

ReDoc,Flask Fast API

To set up the data validation, we can simply create the datatype class inherited from the base-model of Pydantic. It is a library which offers data validation using Python type annotations. We can add the description of the entities and provide the custom example to be displayed in the docs.

ML FastAPI Example

I would like to share one example where an ML DecisionTree classifier model has been deployed using FastAPI. The problem statement for this is a music genre classifier where based on the technical aspects of music such as tempo, valence, the music is either rock or hip-hop. I made a music class to validate the data to be passed to the model which looks like this:from pydantic import BaseModel class Music(BaseModel): acousticness: float danceability: float energy: float instrumentalness: float liveness: float speechiness: float tempo: float valence: float

Here is the resultant API:

FastApi example

If you want to look at the whole code then head over to this GitHub repository.

Performance Comparison: FastAPI vs. Flask 

1. Speed:

  • FastAPI is generally faster than Flask. This is because it uses asynchronous code, which allows it to handle requests concurrently. Flask, on the other hand, is a traditional synchronous framework.
  • The difference in speed can be significant, especially for applications with many concurrent requests. However, Flask can be faster sometimes, especially when using Greenlet-powered WSGI servers.

2. Type annotations:

  • FastAPI leverages type annotations to improve performance and provide better type safety. This means that the framework can automatically infer the data types being used, which can optimize code execution. Flask does not use type annotations by default.

3. Asynchronous vs. synchronous:

  • FastAPI is built on an asynchronous foundation, which can handle multiple requests concurrently. This makes it ideal for building APIs and microservices. Flask is primarily a synchronous framework, which means it can only take one request at a time. However, Flask can be made asynchronous by using extensions like asyncio.

4. Benchmarks:

  • Benchmarks show that FastAPI can be significantly faster than Flask in many scenarios. However, the specific performance difference will depend on the type of application, the web server used, and other factors.

Conclusion

After all this discussion the question is still unanswered, who wins? Based on all the factors, I would suggest adopting FastAPI over Flask. It is very easy to set up, migrating an old flask project into this won’t take much time, async, web sockets, and automatic docs generation feature is the cherry on top.

One can choose the flask framework to set up the whole web interface (Front-end and back-end) but concerning ML where the main goal is to check if the model is working in the production environment or not, creating an API makes more sense because the rest of the things can be managed by other teams of developers and to clearly explain them the usage of the program you developed, FastAPI auto docs is a good solution.

Frequently Asked Questions

Q1. Is Flask or FastAPI better for ML?

It depends on your needs. Flask is well-established, while FastAPI excels with better performance and automatic data validation, making it great for ML applications.

Q2. Is Flask faster than Django?

Yes, Flask is generally faster than Django. Flask’s micro-framework nature and flexibility make it a preferred choice for speed, especially in smaller projects.

Q3.Should I learn Flask or Django?

It depends on your goals. Learn Flask for simplicity and flexibility, which is suitable for smaller projects. Opt for Django for a feature-rich framework, ideal for larger projects or when you prefer a more structured approach.

Connect with Author

You can connect with me on Linkedin to discuss anything regarding Python development and Data Science, GitHub to view my projects or you can read my articles over medium.

Kaustubh – Medium

Kaustubh Gupta is a skilled engineer with a B.Tech in Information Technology from Maharaja Agrasen Institute of Technology. With experience as a CS Analyst and Analyst Intern at Prodigal Technologies, Kaustubh excels in Python, SQL, Libraries, and various engineering tools. He has developed core components of product intent engines, created gold tables in Databricks, and built internal tools and dashboards using Streamlit and Tableau. Recognized as India’s Top 5 Community Contributor 2023 by Analytics Vidhya, Kaustubh is also a prolific writer and mentor, contributing significantly to the tech community through speaking sessions and workshops.

Responses From Readers

Clear

Rajveer Narang
Rajveer Narang

Quiet well written and informative.

Sebastian
Sebastian

No professional Python developer would use pure Flask today to build a new API. In context of Flask, one would at least also apply Flask-RestX plugin, which provides functionality like typed APIs and generation of Swagger documentation. So in this article, you basically compared apple and oranges.

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details