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

Creating and using a parameterized dependency 
with a class

In the previous section, we defined dependencies as regular functions, which work well in most cases. Still, you may need to set some parameters on a dependency to finely tune its behavior. Since the arguments of the function are set by the dependency injection system, we can’t add an argument to the function.

In the pagination example, we added some logic to cap the limit value at 100. If we wanted to set this maximum limit dynamically, how would we do that?

The solution is to create a class that will be used as a dependency. This way, we can set class properties – with the __init__ method, for example – and use them in the logic of the dependency itself. This logic will be defined in the __call__ method of the class. If you remember what we learned in the Callable object section of Chapter 2, Python Programming Specificities, you know that it makes the object callable, meaning it can be...