Eng Fastapi v2
Eng Fastapi v2
All-in-One Course
(TA Session)
Dinh-Thang Duong – TA
Nguyen-Thuan Duong – STA
Year 2024
AI VIETNAM
All-in-One Course
(TA Session) Getting Started
❖ Objectives
Our objectives:
- Discuss about the definition of API.
- Learn the basis of FastAPI.
- Learn how to deploy a Deep Learning model
as an API service with FastAPI.
2
AI VIETNAM
All-in-One Course
(TA Session)
Outline
➢ Introduction
➢ API
➢ FastAPI
➢ Model Deployment
➢ Question
3
AI VIETNAM
All-in-One Course
(TA Session)
Introduction
4
AI VIETNAM
All-in-One Course
(TA Session) Introduction
❖ Getting Started
5
AI VIETNAM
All-in-One Course
(TA Session) Introduction
❖ Getting Started
The ChatGPT website get the response from a something called API
6
https://semaphoreci.com/blog/function-calling
AI VIETNAM
All-in-One Course
(TA Session)
API
7
AI VIETNAM
All-in-One Course
(TA Session) API
❖ Getting Started
• API (Application Programming Interface) is a facilitator that enables apps, databases, softwares and IoT
devices to communicate with each other.
8
AI VIETNAM
All-in-One Course
(TA Session) API
❖ Getting Started
9
AI VIETNAM
All-in-One Course
(TA Session) API
❖ Getting Started
• The application sending the request is called the client, and the application sending the response is called
the server.
Client Server
10
AI VIETNAM
All-in-One Course
(TA Session) API
❖ How do APIs work ?
• A client sends a request to API server. The request is made using a specific protocol (such as HTTP) and
includes information about the operation the client wants to perform, e.g., retrieving data or updating a
resource.
Request
Response
• The API server receives the request and processes it. The API server sends a response back to the client,
which may include data, an error message, or a status code indicating the result of the operation.
11
AI VIETNAM
All-in-One Course
(TA Session) API
❖ Types of APIs
Database
Private API
API
12
AI VIETNAM
All-in-One Course
(TA Session) API
❖ Private / Public / Partner API
Apps are more targeted Apps could be targeted at end App are usually targeted at
towards end consumers consumers or business users employees of the enterprise
13
AI VIETNAM
All-in-One Course
(TA Session) API
❖ Database API
• A database API allows applications to interact with a database to access and manipulate data.
Client Database
14
AI VIETNAM
All-in-One Course
(TA Session) API
❖ Remote API
• Using a remote API, two distant applications communicate across a communications network, primarily the
internet.
Internet
15
AI VIETNAM
All-in-One Course
(TA Session) API
❖ Web API
• Web API (or Web Service API) is an application inferface between a web browser and a web server.
Request Request
Response Response
• All web services are APIs but not all APIs are web services.
• Web services require an internet connection to work, but APIs do not.
16
AI VIETNAM
All-in-One Course
(TA Session) API
❖ API protocol types
17
AI VIETNAM
All-in-One Course
(TA Session) API
❖ Timeline
18
AI VIETNAM
All-in-One Course
(TA Session) API
❖ Conclusion
REST GraphQL SOAP RPC
Structure Follows six Schema and type Message structure Local procedural
architectural calls
constraints
Format Json, XLM, HTML, Json XML Json, XML,
plain text Flatbuffers, etc
Advantages Flexible terms of Sovles over-fetching Highly secure and Lightweight
data format and and under-fetching extensible payloads make it
structure high performing
Use cases Resources based Mobile APIs Payment gateways Command-focused
apps systems
19
AI VIETNAM
All-in-One Course
(TA Session)
FastAPI
20
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Introduction
21
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Python Web Framework
22
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Django vs Flask vs FastAPI
23
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Which companies use FastAPI ?
24
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ The key features of FastAPI
Fast to code Increase the speed to develop features by about 200% to 300%
25
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice
26
https://www.apisec.ai/blog/rest-api-and-its-significance-to-web-service-providers
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Initialize first API
27
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Initialize first API
28
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Initialize first API
29
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Initialize first API
30
https://swagger.io/tools/swagger-ui/
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Initialize first API
31
https://swagger.io/tools/swagger-ui/
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Path Operations
Request Response
“Hello world”
Client
32
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Path Operations GET Method
Defining a route
33
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Path Operations GET Method
34
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Path Operations
35
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Path Operations
https://api_domain.com
POST https://api_domain.com/catdog_classification/predict
POST https://api_domain.com/speech_classification/predict
GET https://api_domain.com/features_database/{features_id}
GET https://api_domain.com/speech_classification/results/{task_id}
API
36
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Path Operations
37
https://api.arcadier.com/introduction-to-arcadier-api
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Path Operations
Database
38
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Path Operations GET Method
39
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Path Operations (Path Parameters)
40
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Path Operations POST Method
41
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Path Operations PUT Method
42
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Path Operations DELETE Method
43
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Path Operations
44
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Pydantic Model
Item: {
”name”: string,
”price”: number,
“description”: string,
“tax”: number
}
Item: {
”name”: string,
”price”: number, To ensure API to receive the exact
“description”: string, structure, we can use Pydantic
“tax”: number Model.
}
46
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Pydantic Model
47
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Pydantic Model
- ID: string
- Name: string
- Age: number
- Gender: string
48
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Pydantic Model
49
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Pydantic Model
50
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Pydantic Model
51
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Pydantic Model
52
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Parameters Annotation
53
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Parameters Annotation
54
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Response Model
55
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Status Code
56
https://www.infidigit.com/blog/http-status-codes/
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Status Code
57
https://www.infidigit.com/blog/http-status-codes/
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Status Code
58
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Upload File
Beside Pydantic model, we can also have request body as a uploaded file 59
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Upload File
60
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Middleware
61
https://semaphoreci.com/blog/custom-middleware-fastapi
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Middleware
62
https://semaphoreci.com/blog/custom-middleware-fastapi
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Middleware
63
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: Middleware
64
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: CORS Middleware
65
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: CORS Middleware
66
https://www.haproxy.com/blog/enabling-cors-in-haproxy
AI VIETNAM
All-in-One Course
(TA Session) FastAPI
❖ Practice: CORS Middleware
67
AI VIETNAM
All-in-One Course
(TA Session)
Quiz
68
AI VIETNAM
All-in-One Course
(TA Session)
Model Deployment
69
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Introduction
Deep
Learning
Model
But how … ?
Applications
71
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Practice
Problem statement: Deploy a Cat Dog Image Classification model as an API service using FastAPI.
Request
Response
{
"prediction_id": 0,
User "prediction_cls": "cat" API
}
72
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Practice
Problem statement: Deploy a Cat Dog Image Classification model as an API service using FastAPI.
73
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build model
74
https://www.analyticsvidhya.com/blog/2021/06/beginner-friendly-project-cat-and-dog-classification-using-cnn/
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build model: Dataset
76
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build model step 2: Load dataset and train val test split
77
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build model step 3: Create DataLoader
78
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build model step 4: Build model
79
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build model step 5: Training and saving model weights
80
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build API
Problem statement: Deploy a Cat Dog Image Classification model as an API service using FastAPI.
81
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build API Step 1: Organize source code
82
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build API Step 1: Organize source code
- config/: Folder containing configuration for some modules.
- logs/: Folder containing logging information when running API.
- middleware/: Folder containing code for middleware.
- models/: Folder containing Deep Learning weights.
- routes/: Folder containing API Endpoints declaration.
- schemas/: Folder containing Pydantic model declaration.
- utils/: Folder containing codes for general purpose (varying
between projects).
- app.py: Python file containing codes for FastAPI app initialization.
- requirements.txt: File containing packages version information to
run the soruce code.
- server.py: Python file containing codes to host the API service.
83
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build API Step 2: List packages requirements
84
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build API Step 3: Define configuration
85
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build API Step 3: Define configuration
86
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build API Step 4: Build logging function
87
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build API Step 4: Build logging function
88
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build API Step 5: Define models folder
catdog_model.py
89
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build API Step 5: Define models folder
catdog_predictor.py
90
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build API Step 6: Define middleware folder
cors.py
91
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build API Step 6: Define middleware folder
http.py
92
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build API Step 7: Define Schemas
93
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build API Step 8: Define routes
94
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Build API Step 9: Define app.py and server.py
95
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment
❖ Result
1
96
AI VIETNAM
All-in-One Course
(TA Session)
97
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment (MLOps)
❖ Getting Started
Applications
98
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment (MLOps)
❖ Data Drift Problem
User input
User input
Applications
Data Collection
99
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment (MLOps)
❖ Data Drift Solution
Applications
100
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment (MLOps)
❖ Machine Learning Life Cycle
Data drift
101
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment (MLOps)
❖ Machine Learning Operations (MLOps) pipeline
Experiments
Model Registry
Monitoring
Caching
102
AI VIETNAM
All-in-One Course
(TA Session) Model Deployment (MLOps)
❖ MLOps simple pipeline
Experiments
Model Registry
Source Control
103
AI VIETNAM
All-in-One Course
(TA Session) Summarization
❖ What we have learned so far
Summarization:
- Discuss about the definition of API.
- Learn the basis of FastAPI:
- Path Operations.
- Pydantic Model.
- Response Code.
- Middleware.
- Learn how to deploy a Deep Learning model
as an API service with FastAPI.
- Deploy a Cat Dog Classification model.
- Simple MLOps pipeline.
104
AI VIETNAM
All-in-One Course
(TA Session) Question
?
105
106