<p><b>Get well-versed with FastAPI features and best practices for testing, monitoring, and deployment to run high-quality and robust data science applications</b></p><h4>Key Features</h4><ul><li>Cover the concepts of the FastAPI framework, including aspects relating to asynchronous programming, typ
Building Data Science Applications with FastAPI: Develop, manage, and deploy efficient machine learning applications with Python
β Scribed by FranΓ§ois Voron
- Publisher
- Packt Publishing
- Year
- 2021
- Tongue
- English
- Leaves
- 426
- Category
- Library
No coin nor oath required. For personal study only.
β¦ Synopsis
Get well-versed with FastAPI features and best practices for testing, monitoring, and deployment to run high-quality and robust data science applications
Key Features
- Cover the concepts of the FastAPI framework, including aspects relating to asynchronous programming, type hinting, and dependency injection
- Develop efficient RESTful APIs for data science with modern Python
- Build, test, and deploy high performing data science and machine learning systems with FastAPI
Book Description
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.
What you will learn
- Explore the basics of modern Python and async I/O programming
- Get to grips with basic and advanced concepts of the FastAPI framework
- Implement a FastAPI dependency to efficiently run a machine learning model
- Integrate a simple face detection algorithm in a FastAPI backend
- Integrate common Python data science libraries in a web backend
- Deploy a performant and reliable web backend for a data science application
Who this book is for
This Python data science book is for data scientists and software developers interested in gaining knowledge of FastAPI and its ecosystem to build data science applications. Basic knowledge of data science and machine learning concepts and how to apply them in Python is recommended.
Table of Contents
- Python Development Environment Setup
- Python Programming Specificities
- Developing RESTful API with FastAPI
- Managing pydantic Data Models in FastAPI
- Dependency Injections in FastAPI
- Databases and Asynchronous ORMs
- Managing Authentication and Security in FastAPI
- Defining WebSockets for Two-Way Interactive Communication in FastAPI
- Testing an API Asynchronously with pytest and HTTPX
- Deploying a FastAPI Project
- Introduction to NumPy and Pandas
- Training Machine Learning Models with scikit-learn
- Creating an Efficient Prediction API Endpoint with FastAPI
- Implement a Real-Time Face Detection System Using WebSockets with FastAPI and OpenCV
β¦ Table of Contents
Cover
Title Page
Copyright and Credits
Contributors
Table of Contents
Preface
Section 1: Introduction to Python and FastAPI
Chapter 1: Python Development Environment Setup
Technical requirements
Installing a Python distribution using pyenv
Creating a Python virtual environment
Installing Python packages with pip
Installing the HTTPie command-line utility
Summary
Chapter 2: Python Programming Specificities
Technical requirements
Basics of Python programming
Running Python scripts
Indentation matters
Working with built-in types
Working with data structures β lists, tuples, dictionaries, and sets
Performing Boolean logic and checking for existence
Controlling the flow of a program
Defining functions
Writing and using packages and modules
Operating over sequences β list comprehensions and generators
List comprehensions
Generators
Writing object-oriented programs
Defining a class
Implementing magic methods
Reusing logic and avoiding repetition with inheritance
Type hinting and type checking with mypy
Getting started
The typing module
Type function signatures with Callable
Any and cast
Asynchronous I/O
Summary
Chapter 3: Developing a RESTful API with FastAPI
Technical requirements
Creating the first endpoint and running it locally
Handling request parameters
Path parameters
Query parameters
The request body
Form data and file uploads
Headers and cookies
The request object
Customizing the response
Path operation parameters
The response parameter
Raising HTTP errors
Building a custom response
Structuring a bigger project with multiple routers
Summary
Chapter 4: Managing Pydantic Data Models in FastAPI
Technical requirements
Defining models and their field types with Pydantic
Standard field types
Optional fields and default values
Field validation
Validating email addresses and URLs with Pydantic types
Creating model variations with class inheritance
Adding custom data validation with Pydantic
Applying validation at a field level
Applying validation at an object level
Applying validation before Pydantic parsing
Working with Pydantic objects
Converting an object into a dictionary
Creating an instance from a sub-class object
Updating an instance with a partial one
Summary
Chapter 5: Dependency Injections in FastAPI
Technical requirements
What is dependency injection?
Creating and using a function dependency
Get an object or raise a 404 error
Creating and using a parameterized dependency with a class
Use class methods as dependencies
Using dependencies at a path, router, and global level
Use a dependency on a path decorator
Use a dependency on a whole router
Use a dependency on a whole application
Summary
Section 2: Build and Deploy a Complete Web Backend with FastAPI
Chapter 6: Databases and Asynchronous ORMs
Technical requirements
An overview of relational and NoSQL databases
Relational databases
NoSQL databases
Which one should you choose?
Communicating with a SQL database with SQLAlchemy
Creating the table schema
Connecting to a database
Making insert queries
Making select queries
Making update and delete queries
Adding relationships
Setting up a database migration system with Alembic
Communicating with a SQL database with Tortoise ORM
Creating database models
Setting up the Tortoise engine
Creating objects
Updating and deleting objects
Adding relationships
Setting up a database migration system with Aerich
Communicating with a MongoDB database using Motor
Creating models compatible with MongoDB ID
Connecting to a database
Inserting documents
Getting documents
Updating and deleting documents
Nesting documents
Summary
Chapter 7: Managing Authentication and Security in FastAPI
Technical requirements
Security dependencies in FastAPI
Storing a user and their password securely in a database
Creating models and tables
Hashing passwords
Implementing registration routes
Retrieving a user and generating an access token
Implementing a database access token
Implementing a login endpoint
Securing endpoints with access tokens
Configuring CORS and protecting against CSRF attacks
Understanding CORS and configuring it in FastAPI
Implementing double-submit cookies to prevent CSRF attacks
Summary
Chapter 8: Defining WebSockets forTwo-Way Interactive Communication in FastAPI
Technical requirements
Understanding the principles of two-way communication with WebSockets
Creating a WebSocket with FastAPI
Handling concurrency
Using dependencies
Handling multiple WebSocket connections and broadcasting messages
Summary
Chapter 9: Testing an API Asynchronously with pytest and HTTPX
Technical requirements
Introduction to unit testing with pytest
Generating tests with parametrize
Reusing test logic by creating fixtures
Setting up testing tools for FastAPI with HTTPX
Writing tests for REST API endpoints
Writing tests for POST endpoints
Testing with a database
Writing tests for WebSocket endpoints
Summary
Chapter 10: Deploying a FastAPI Project
Technical requirements
Setting and using environment variables
Using a .env file
Managing Python dependencies
Adding Gunicorn as a server process for deployment
Deploying a FastAPI application on a serverless platform
Adding database servers
Deploying a FastAPI application with Docker
Writing a Dockerfile
Building a Docker image
Running a Docker image locally
Deploying a Docker image
Deploying a FastAPI application on a traditional server
Summary
Section 3: Build a Data Science API with Python and FastAPI
Chapter 11: Introduction to NumPy and pandas
Technical requirements
Getting started with NumPy
Creating arrays
Accessing elements and sub-arrays
Manipulating arrays with NumPy β computation, aggregations, comparisons
Adding and multiplicating arrays
Aggregating arrays β sum, min, max, meanβ¦
Comparing arrays
Getting started with pandas
Using pandas Series for one-dimensional data
Using pandas DataFrames for multi-dimensional data
Importing and exporting CSV data
Summary
Chapter 12: Training Machine Learning Models with scikit-learn
Technical requirements
What is machine learning?
Supervised versus unsupervised learning
Model validation
Basics of scikit-learn
Training models and predicting
Chaining pre-processors and estimators with pipelines
Validating the model with cross-validation
Classifying data with Naive Bayes models
Intuition
Classifying data with Gaussian Naive Bayes
Classifying data with Multinomial Naive Bayes
Classifying data with support vector machines
Intuition
Using SVM in scikit-learn
Finding the best parameters
Summary
Chapter 13: Creating an Efficient Prediction API Endpoint with FastAPI
Technical requirements
Persisting a trained model with Joblib
Dumping a trained model
Loading a dumped model
Implementing an efficient prediction endpoint
Caching results with Joblib
Choosing between standard or async functions
Summary
Chapter 14: Implement a Real-Time Face Detection System Using WebSockets with FastAPI and OpenCV
Technical requirements
Getting started with OpenCV
Implementing an HTTP endpoint to perform face detection on a single image
Implementing a WebSocket to perform face detection on a stream of images
Sending a stream of images from the browser in a WebSocket
Showing the face detection results in the browser
Summary
Other Books You May Enjoy
Index
π SIMILAR VOLUMES
<p><span>Get well-versed with FastAPI features and best practices for testing, monitoring, and deployment to run high-quality and robust data science applications</span></p><h4><span>Key Features</span></h4><ul><li><span><span>Cover the concepts of the FastAPI framework, including aspects relating t
<p><span>Learn all the features and best practices of FastAPI to build, deploy, and monitor powerful data science and AI apps, like object detection or image generation.</span></p><p><span>Purchase of the print or Kindle book includes a free PDF eBook</span></p><h4><span>Key Features</span></h4><ul>
Learn all the features and best practices of FastAPI to build, deploy, and monitor powerful data science and AI apps, like object detection or image generation. Purchase of the print or Kindle book includes a free PDF eBook Key Features Uncover the secrets of FastAPI, including async I/O, typ
Aspiring data science professionals can learn the Scikit-Learn library along with the fundamentals of machine learning with this book. The book combines the Anaconda Python distribution with the popular Scikit-Learn library to demonstrate a wide range of supervised and unsupervised machine learning
1. Introduction to Scikit-Learn -- 2. Classification from Simple Training Sets -- 3. Classification from Complex Training Sets -- 4. Predictive Modeling through Regression -- 5. Scikit-Learn Classifier Tuning from Simple Training Sets -- 6. Scikit-Learn Classifier Tuning from Complex Training Sets -