Book Image

Building Data Science Applications with FastAPI - Second Edition

By : François Voron
3 (1)
Book Image

Building Data Science Applications with FastAPI - Second Edition

3 (1)
By: François Voron

Overview of this book

Building Data Science Applications with FastAPI is the go-to resource for creating efficient and dependable data science API backends. This second edition incorporates the latest Python and FastAPI advancements, along with two new AI projects – a real-time object detection system and a text-to-image generation platform using Stable Diffusion. The book starts with the basics of FastAPI and modern Python programming. You'll grasp FastAPI's robust dependency injection system, which facilitates seamless database communication, authentication implementation, and ML model integration. As you progress, you'll learn testing and deployment best practices, guaranteeing high-quality, resilient applications. Throughout the book, you'll build data science applications using FastAPI with the help of projects covering common AI use cases, such as object detection and text-to-image generation. These hands-on experiences will deepen your understanding of using FastAPI in real-world scenarios. By the end of this book, you'll be well equipped to maintain, design, and monitor applications to meet the highest programming standards using FastAPI, empowering you to create fast and reliable data science API backends with ease while keeping up with the latest advancements.
Table of Contents (21 chapters)
1
Part 1: Introduction to Python and FastAPI
7
Part 2: Building and Deploying a Complete Web Backend with FastAPI
13
Part 3: Building Resilient and Distributed Data Science Systems with FastAPI

Caching results with Joblib

If your model takes time to make predictions, it may be interesting to cache the results: if the prediction for a particular input has already been done, it makes sense to return the same result we saved on disk rather than running the computations again. In this section, we’ll learn how to do this with the help of Joblib.

Joblib provides us with a very convenient and easy-to-use tool to do this, so the implementation is quite straightforward. The main concern will be about whether we should choose standard or async functions to implement the endpoints and dependencies. This will allow us to explain some of the technical details of FastAPI in more detail.

We’ll build upon the example we provided in the previous section. The first thing we must do is initialize a Joblib Memory class, which is the helper for caching function results. Then, we can add a decorator to the functions we want to cache. You can see this in the following example...