Book Image

Building Data Science Applications with FastAPI

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

Building Data Science Applications with FastAPI

5 (1)
By: François Voron

Overview of this book

FastAPI is a web framework for building APIs with Python 3.6 and its later versions based on standard Python-type hints. With this book, you’ll be able to create fast and reliable data science API backends using practical examples. This book starts with the basics of the FastAPI framework and associated modern Python programming language concepts. You'll be taken through all the aspects of the framework, including its powerful dependency injection system and how you can use it to communicate with databases, implement authentication and integrate machine learning models. Later, you’ll cover best practices relating to testing and deployment to run a high-quality and robust application. You’ll also be introduced to the extensive ecosystem of Python data science packages. As you progress, you’ll learn how to build data science applications in Python using FastAPI. The book also demonstrates how to develop fast and efficient machine learning prediction backends and test them to achieve the best performance. Finally, you’ll see how to implement a real-time face detection system using WebSockets and a web browser as a client. By the end of this FastAPI book, you’ll have not only learned how to implement Python in data science projects but also how to maintain and design them to meet high programming standards with the help of FastAPI.
Table of Contents (19 chapters)
1
Section 1: Introduction to Python and FastAPI
7
Section 2: Build and Deploy a Complete Web Backend with FastAPI
13
Section 3: Build a Data Science API with Python and FastAPI

What this book covers

Chapter 1, Python Development Environment Setup, is aimed at setting up the development environment so that you can start working with Python and FastAPI. We'll introduce the various tools that are commonly used in the Python community to ease development.

Chapter 2, Python Programming Specificities, introduces you to the specificities of programming in Python, specifically, block indentation, control flow statements, exceptions handling, and the object-oriented paradigm. We'll also cover features such as list comprehensions and generators. Finally, we'll see how type hinting and asynchronous I/O work. 

Chapter 3, Developing a RESTful API with FastAPI, covers the basics of the creation of a RESTful API with FastAPI: routing, parameters, request body validation, and response. We'll also show how to properly structure a FastAPI project with dedicated modules and separate routers.

Chapter 4, Managing pydantic Data Models in FastAPI, covers in more detail the definition of data models with Pydantic, the underlying data validation library used by FastAPI. We'll explain how to implement variations of the same model without repeating ourselves thanks to class inheritance. Finally, we'll show how to implement custom data validation logic on those models.

Chapter 5, Dependency Injections in FastAPI, explains how dependency injection works and how we can define our own dependencies to reuse logic across different routers and endpoints.

Chapter 6, Databases and Asynchronous ORMs, demonstrates how we can set up a connection with a database to read and write data. We'll cover how to use two libraries to work asynchronously with SQL databases and how they interact with the Pydantic model. Finally, we'll also show you how to work with MongoDB, a NoSQL database.

Chapter 7, Managing Authentication and Security in FastAPI, shows us how to implement a basic authentication system to protect our API endpoints and return the relevant data for the authenticated user. We'll also talk about the best practices around CORS and how to be safe from CSRF attacks.

Chapter 8, Defining WebSockets for Two-Way Interactive Communication in FastAPI, is aimed at understanding WebSockets and how to create them and handle the messages received with FastAPI.

Chapter 9, Testing an API Asynchronously with pytest and HTTPX, shows us how to write tests for our REST API endpoints.

Chapter 10, Deploying a FastAPI Project, covers the common configuration for running FastAPI applications smoothly in production. We'll also explore several deployment options: DigitalOcean App Platform, Docker, and the traditional server setup.

Chapter 11, Introduction to NumPy and pandas, introduces two core libraries for data science in Python: NumPy and pandas. We'll see how to create and manipulate arrays with NumPy and how we can do efficient operations on them. We'll then show how to manage large datasets with pandas.

Chapter 12, Training Machine Learning Models with scikit-learn, gives a quick introduction to machine learning before moving on to the scikit-learn library, a set of ready-to-use tools to perform machine learning tasks in Python. We'll review some of the most common algorithms and train prediction models.

Chapter 13, Creating an Efficient Prediction API Endpoint with FastAPI, shows us how we can efficiently store a trained machine learning model using Joblib. Then, we'll integrate it in a FastAPI backend, considering some technical details of FastAPI internals to achieve maximum performance. Finally, we'll show a way to cache results using Joblib.

Chapter 14, Implementing a Real-Time Face Detection System Using WebSockets with FastAPI and OpenCV, implements a simple application to perform face detection in the browser, backed by a FastAPI WebSocket and OpenCV, a popular library for computer vision.