Skip to content

Commit 3461315

Browse files
committed
feat: add lesson three and flask-api
1 parent e44af9d commit 3461315

33 files changed

+4911
-2598
lines changed

README.md

+70-68
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22

33
One essential and last phase of the CRISP-DM data framework is deployment. The key focus in this phase is the usability of the developed model by intended users or customers. Depending on the type of solution and use case, this can involve deploying and integrating the model on any medium like the web, a mobile application, a hardware-embedded system, etc. While this might "sound easy,” many beginner ML engineers find it daunting to deploy their projects on the web for their intended users to test and for their solutions to solve their users' problems.
44

5-
In this workshop, Bolaji will introduce you to ML model deployment by showing you the steps and processes involved with turning a machine learning model into an API with Python and Flask, testing the API, building a web application, and deploying it to the cloud for global usage. We would also discuss several other alternative JavaScript frameworks and tools you can consider.
5+
In this workshop, Bolaji will introduce you to machine learning model deployment by showing you the steps and processes involved with turning a machine learning model into an API with Python and Flask, testing the API with Postman, building a web application, and deploying it to the cloud for global usage. We would also discuss several other alternative Python/JavaScript frameworks and tools to consider.
66

77
---
88

9-
This repository contains the workshop materials, lesson notes, machine learning model notebook, serilized model, web application code, and links to further resources. You can follow the workshop lessons and build the project yourself or use the materials to learn at your own pace.
9+
This repository contains the workshop materials, lesson notes, machine learning model notebooks, serilized models, backend API/web application code, and links to further resources. You can follow the workshop lessons and build the project yourself or use the materials to learn at your own pace.
1010

1111
In this workshop, we will build the following:
1212

13-
- A simple machine learning model that predicts the personality type of an individual based off their messages in conversations using Python, Scikit-learn, and other libraries.
14-
- A web application and API that consumes the model using HTML, CSS, JavaScript, and Flask.
15-
- A web application and API that consumes the model using HTML, CSS, JavaScript, Nextjs, and Flask.
16-
- A web application and API that consumes the model using HTML, CSS, JavaScript, and FastAPI.
17-
- A web application and API that consumes the model using HTML, CSS, JavaScript, Nextjs, and FastAPI.
13+
- A simple machine learning model that predicts the personality type of an individual using Python, Scikit-learn, and other libraries.
14+
- A backend and API for the model using Python and Flask.
15+
- A web application that consumes the model/API using HTML, CSS, JavaScript, and Flask.
16+
- A web application and API that consumes the model/API using HTML, CSS, JavaScript, Nextjs, and Flask.
1817
- An AI web application using Nextjs and OpenAPI.
1918

2019
[Demo GIF]
@@ -35,19 +34,19 @@ In this workshop, we will build the following:
3534

3635
## 🎧 Overview
3736

38-
Workshop duration: three (3) hours.
37+
Workshop duration: ~three (3) hours.
3938

4039
At the end of this workshop; you should be able to:
4140

4241
- Explain what machine learning and a machine learning model are to your friends and family.
4342
- Understand how the CRISP-DM data framework works.
4443
- Understand the fundamentals of building a machine learning model using the Python programming language.
45-
- Understand how to export and serialize a machine learning model.
44+
- Understand how to serialize (export/import) a machine learning model.
4645
- Understand how to turn a machine learning model into an API using the Flask Python framework.
47-
- Understand how to build a web application using the FastAPI Python framework.
48-
-
46+
- Understand how to build a web application using the Flask Python framework.
4947
- Understand how to build a web application using the Nextjs JavaScript framework.
50-
- Understand how to deploy a web application to the cloud using Fly.
48+
- Understand how to deploy a Python/Flask application to the cloud using Fly.
49+
- Understand how to deploy a Nextjs/Flask application to the cloud using Vercel.
5150

5251
---
5352

@@ -73,7 +72,7 @@ To make the workshop a smooth process, we will use [Google Colab](https://colab.
7372

7473
But if you prefer local (I strongly advise against this; at least for this workshop), you should install [Anancoda](https://anaconda.com/download?utm_source=ba-deploy-ml-web-workshop) to use Jupyter Notebook on your local computer. You will need to install and import all the required packages yourself too and resolve any conflicts that might arise.
7574

76-
> **Note**
75+
> [!NOTE]
7776
>
7877
> [Jupyter Notebooks](https://jupyter.org?utm_source=ba-deploy-ml-web-workshop) allow you to combine executable code and rich text in a single document, along with images, HTML, LaTeX, and more.
7978
@@ -86,9 +85,10 @@ You should have the following software and packages installed on your computer.
8685
<summary>Git and GitHub</summary>
8786
<br />
8887

89-
> [Git](https://github.com/pallets/flask?utm_source=ba-deploy-ml-web-workshop) is a free and open-sourced distributed version control system.
90-
>
91-
> [GitHub](https://github.com?utm_source=ba-deploy-ml-web-workshop) is a code hosting platform for version control and collaboration.
88+
We will install/use the following packages:
89+
90+
- [Git](https://github.com/pallets/flask?utm_source=ba-deploy-ml-web-workshop): a free and open-sourced distributed version control system.
91+
- [GitHub](https://github.com?utm_source=ba-deploy-ml-web-workshop): a code hosting platform for version control and collaboration.
9292

9393
Download and install Git from [this website](https://git-scm.com/downloads?utm_source=ba-deploy-ml-web-workshop) for all operating systems or follow this [installation guide](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git?utm_source=ba-deploy-ml-web-workshop).
9494

@@ -102,80 +102,80 @@ git --version
102102

103103
Next, create a [free GitHub account](https://github.com/signup?utm_source=ba-deploy-ml-web-workshop) if you don't have one already.
104104

105-
> **Note**
105+
> [!NOTE]
106106
>
107107
> Bonus: If you're a student, you should check out the [GitHub Student Developer Pack](https://education.github.com/pack?utm_source=ba-deploy-ml-web-workshop), which gives students free access to the best developer tools (paid tools/services/courses for free) in one place so they can learn by doing.
108108
109109
</details>
110110

111111
<details>
112112

113-
<summary>Python 3 and PIP</summary>
113+
<summary>Postman Desktop App or VSCode Extension</summary>
114114
<br />
115115

116-
> [Python](https://python.org?utm_source=ba-deploy-ml-web-workshop) is a general-purpose programming language that lets you work quickly and integrate systems more effectively.
117-
>
118-
> [PIP](https://pypi.org/project/pip?utm_source=ba-deploy-ml-web-workshop) is a package manager for installing Python packages or modules.
119-
120-
Download and install Python 3 from [this website](https://python.org/downloads?utm_source=ba-deploy-ml-web-workshop) for all operating systems or follow this [installation guide](https://realpython.com/installing-python?utm_source=ba-deploy-ml-web-workshop).
121-
122-
Once installed successfully, confirm the version using the command:
123-
124-
```bash
125-
python --version
126-
127-
or
128-
129-
python3 --version
130-
```
131-
132-
If Python is installed correctly, you should have PIP installed. If it isn't, follow the steps in [this guide](https://pip.pypa.io/en/stable/installation?utm_source=ba-deploy-ml-web-workshop) to install PIP.
116+
We will install any of the following:
133117

134-
Hint:
118+
- [Postman Desktop App](https://github.com/pallets/flask?utm_source=ba-deploy-ml-web-workshop): a desktop that allows you use Postman to send API requests.
119+
- [Postman VSCode Extension](https://github.com?utm_source=ba-deploy-ml-web-workshop): a VSCode extension that allows you use Postman to send API requests from within VS Code.
135120

136-
```bash
137-
python get-pip.py
138-
```
121+
Download and install the Postman Desktop Client or VSCode Extension from [this website](https://www.postman.com/downloads?utm_source=ba-deploy-ml-web-workshop) for all operating systems.
139122

140123
</details>
141124

142125
<details>
143126

144-
<summary>Flask</summary>
127+
<summary>Python and PIP</summary>
145128
<br />
146129

147-
> [Flask](https://github.com/pallets/flask?utm_source=ba-deploy-ml-web-workshop) is a lightweight Python micro framework for building web applications.
130+
We will install the following packages:
148131

149-
Install Flask using the command below:
132+
- [Python](https://python.org?utm_source=ba-deploy-ml-web-workshop): a general-purpose programming language that lets you work quickly and integrate systems more effectively.
133+
- [PIP](https://pypi.org/project/pip?utm_source=ba-deploy-ml-web-workshop): a package manager for installing Python packages or modules.
150134

151-
```bash
152-
pip install -U Flask
153-
```
135+
Download and install Python 3 (`>3.10.12`) from [this website](https://python.org/downloads?utm_source=ba-deploy-ml-web-workshop) for all operating systems (this will come with PIP in-built).
136+
137+
> [!IMPORTANT]
138+
>
139+
> Please specifically download and install a version `3.10.12` or higher (I'm using `3.11.5`). Google Colab will install version `3.10.12` which we will use to develope a machine learning model and anything less on your local machine will result in errors when using the serilized model.
154140
155141
Once installed successfully, confirm the version using the command:
156142

157143
```bash
158-
flask --version
144+
python3 --version
159145
```
160146

147+
If Python is installed correctly, you should have PIP installed. If it isn't, follow the steps in [this guide](https://pip.pypa.io/en/stable/installation?utm_source=ba-deploy-ml-web-workshop) to install PIP.
148+
161149
</details>
162150

163151
<details>
164-
<summary>FastAPI</summary>
152+
153+
<summary>Other Python Packages</summary>
165154
<br />
166155

167-
> [FastAPI](https://github.com/tiangolo/fastapi?utm_source=ba-deploy-ml-web-workshop) is a a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints.
156+
We will install the following packages:
168157

169-
Install FastAPI and the required [Uvicorn](https://uvicorn.org?utm_source=ba-deploy-ml-web-workshop) ASGI server using the command below:
158+
- [Python Dotenv](https://pypi.org/project/python-dotenv?utm_source=ba-deploy-ml-web-workshop): a package that reads key-value pairs from a `.env` file and can set them as environment variables.
159+
- [Scikit-Learn](https://scikit-learn.org/stable?utm_source=ba-deploy-ml-web-workshop): a free software machine learning library for the Python programming language, including various classification, regression, and clustering algorithms.
160+
- [Flask](https://github.com/pallets/flask?utm_source=ba-deploy-ml-web-workshop): a lightweight Python micro framework for building web applications.
161+
- [FastAPI](https://github.com/tiangolo/fastapi?utm_source=ba-deploy-ml-web-workshop): a modern and fast web framework for building APIs with Python 3.7+ (with the required [Uvicorn](https://uvicorn.org?utm_source=ba-deploy-ml-web-workshop) ASGI server).
162+
163+
Install all of them using the command below:
170164

171165
```bash
172-
pip install fastapi "uvicorn[standard]"
166+
pip3 install python-dotenv scikit-learn flask fastapi "uvicorn[standard]"
173167
```
174168

175169
Once installed successfully, confirm the version using the command:
176170

177171
```bash
178-
pip show fastapi
172+
pip3 show python-dotenv
173+
174+
pip3 show scikit-learn
175+
176+
pip3 show flask
177+
178+
pip3 show fastapi
179179

180180
uvicorn --version
181181
```
@@ -187,9 +187,10 @@ uvicorn --version
187187
<summary>Nodejs and NPM</summary>
188188
<br />
189189

190-
> [Nodejs](https://nodejs.org?utm_source=ba-deploy-ml-web-workshop) is an open-source, cross-platform JavaScript runtime environment.
191-
>
192-
> [NPM](https://npmjs.com?utm_source=ba-deploy-ml-web-workshop) is a package manager for installing JavaSript packages or modules.
190+
We will install the following packages:
191+
192+
- [Nodejs](https://nodejs.org?utm_source=ba-deploy-ml-web-workshop): an open-source, cross-platform JavaScript runtime environment.
193+
- [NPM](https://npmjs.com?utm_source=ba-deploy-ml-web-workshop): a package manager for installing JavaSript packages or modules.
193194

194195
Download and install Nodejs and NPM from [this website](https://nodejs.org/en/download?utm_source=ba-deploy-ml-web-workshop) for all operating systems or follow this [installation guide](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm?utm_source=ba-deploy-ml-web-workshop).
195196

@@ -205,14 +206,15 @@ npm --version
205206

206207
<details>
207208

208-
<summary>Reactjs 18 and Nextjs 13</summary>
209+
<summary>Reactjs and Nextjs</summary>
209210
<br />
210211

211-
> [Reactjs](https://react.dev?utm_source=ba-deploy-ml-web-workshop) is an open-source web and native JavaScript library for building user interfaces.
212-
>
213-
> [Nextjs](https://nextjs.org?utm_source=ba-deploy-ml-web-workshop) is an open-source React web development framework.
212+
We will install the following packages:
213+
214+
- [Reactjs](https://react.dev?utm_source=ba-deploy-ml-web-workshop): an open-source web and native JavaScript library for building user interfaces.
215+
- [Nextjs](https://nextjs.org?utm_source=ba-deploy-ml-web-workshop): an open-source React web development framework.
214216

215-
Automatically create a project with the latest version of Reactjs and Nextjs using the `create-next-app` command below:
217+
Automatically create a project with the latest version of Reactjs18 and Nextjs13 using the `create-next-app` command below:
216218

217219
```bash
218220
npx create-next-app@latest
@@ -225,15 +227,13 @@ Consider reading Reactjs's [installation guide](https://react.dev/learn/installa
225227
## 👨🏾‍🏫 Workshop Lessons
226228

227229
1. [Introduction to Machine Learning](./lessons/01.md)
228-
2. [Building a ML Model with Python]()
229-
3. [Turning a ML Model into an API —Flask]()
230-
4. [Turning a ML Model into an API —FastAPI]()
231-
5. [Overview of Alternative Frameworks]()
232-
6. [Building a Web Application for the ML Model —Flask/FastAPI]()
233-
7. [Building a Web Application for the ML Model —JavaScript/Nextjs]()
234-
8. [Deploying the Web Application to the Cloud]()
235-
9. Bonus: [Building a ML Model with JavaScript/Tensorflow]()
236-
10. Bonus: [Building Web AI Applications with Pre-trained Models (LLMs) and Vercel AI SDK]()
230+
2. [Building a ML Model with Python](./lessons/02.md)
231+
3. [Building an API for the ML Model](./lessons/03.md)
232+
4. [Building a Web Application for the API]()
233+
4. [Overview of Alternative Frameworks]()
234+
5. [Building a Nextjs Web Application for the ML Model]()
235+
6. [Deploying the API and Web Application to the Cloud]()
236+
7. Bonus: [Building Web AI Applications with Pre-trained Models (LLMs) and Vercel AI SDK]()
237237

238238
## 🚀 What's Next?
239239

@@ -248,6 +248,8 @@ Consider reading Reactjs's [installation guide](https://react.dev/learn/installa
248248
- [How to Deploy a Machine Learning Model to the Web](https://blog.bolajiayodeji.com/how-to-deploy-a-machine-learning-model-to-the-web?utm_source=ba-deploy-ml-web-workshop)
249249
- [Google's Machine Learning Crash Course](https://developers.google.com/machine-learning/crash-course?utm_source=ba-deploy-ml-web-workshop)
250250
- [Google's Machine Learning Glossary](https://developers.google.com/machine-learning/glossary?utm_source=ba-deploy-ml-web-workshop)
251+
- [Flask Quickstart Guide](https://flask.palletsprojects.com/en/2.3.x/quickstart?utm_source=ba-deploy-ml-web-workshop)
252+
- [Handling Flask Application Errors](https://flask.palletsprojects.com/en/2.3.x/errorhandling?utm_source=ba-deploy-ml-web-workshop)
251253

252254
## 👩🏽‍💻 Contributors Guide
253255

3.46 KB
Binary file not shown.
1012 Bytes
Binary file not shown.

app/flask-api/app.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from flask import Flask, request, render_template, jsonify
2+
import pickle
3+
4+
# Create an instance of the Flask class
5+
# with the name of the application’s modules
6+
app = Flask(__name__, template_folder='templates')
7+
8+
# Create the / API route and render the root HTML page
9+
@app.route('/', methods=['GET'])
10+
def main():
11+
return(render_template('main.html'))
12+
13+
# Create the /predict API route
14+
@app.route('/predict', methods=['GET', 'POST'])
15+
def predict():
16+
# Use pickle to load in vectorizer
17+
with open(f'../../model/two/vectorizer.pkl', 'rb') as f:
18+
vectorizer = pickle.load(f)
19+
20+
# Use pickle to load in the pre-trained model
21+
with open(f'../../model/two/model.pkl', 'rb') as f:
22+
model = pickle.load(f)
23+
24+
# Get the message from the API request
25+
message = request.args.get('message')
26+
27+
if request.method == 'GET' and message != None:
28+
mbpti_types = {
29+
0: "ENFJ (Extroversion, Intuition, Feeling, Judging)",
30+
1: "ENFP (Extroversion, Intuition, Feeling, Perceiving)",
31+
2: "ENTJ (Extroversion, Intuition, Thinking, Judging)",
32+
3: "ENTP (Extroversion, Intuition, Thinking, Perceiving)",
33+
4: "ESFJ (Extroversion, Sensing, Feeling, Judging)",
34+
5: "ESFP (Extroversion, Sensing, Feeling, Perceiving)",
35+
6: "ESTJ (Extroversion, Sensing, Thinking, Judging)",
36+
7: "ESTP (Extroversion, Sensing, Thinking, Perceiving)",
37+
8: "INFJ (Introversion, Intuition, Feeling, Judging)",
38+
9: "INFP (Introversion, Intuition, Feeling, Perceiving)",
39+
10: "INTJ (Introversion, Intuition, Thinking, Judging)",
40+
11: "INTP (Introversion, Intuition, Thinking, Perceiving)",
41+
12: "ISFJ (Introversion, Sensing, Feeling, Judging)",
42+
13: "ISFP (Introversion, Sensing, Feeling, Perceiving)",
43+
14: "ISTJ (Introversion, Sensing, Thinking, Judging)",
44+
15: "ISTP (Introversion, Sensing, Thinking, Perceiving)"
45+
}
46+
47+
prediction = model.predict(vectorizer.transform([message]))
48+
result = mbpti_types[prediction[0]]
49+
50+
data = {
51+
"message": message,
52+
"prediction": result
53+
}
54+
return jsonify(data)
55+
56+
else :
57+
data = {
58+
"code": 400,
59+
"description": "Bad Request. No message was provided.",
60+
"message": "Use the message parameter to make a GET request."
61+
}
62+
return jsonify(data), 400
63+
64+
if __name__ == '__main__':
65+
app.run()

app/flask-api/templates/main.html

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>MBPTI Personality Test Checker API</title>
8+
<link
9+
rel="shortcut icon"
10+
type="image/png"
11+
href="https://bolajiayodeji.com/favicon.png"
12+
/>
13+
<link
14+
href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css"
15+
rel="stylesheet"
16+
/>
17+
</head>
18+
19+
<body class="font-mono">
20+
<div class="flex items-center justify-center h-screen">
21+
<div
22+
class="p-12 max-w-xl bg-gray-200 rounded-lg overflow-hidden shadow-lg"
23+
>
24+
<div class="font-bold text-xl mb-2 text-center">
25+
MBPTI Personality Test Checker API
26+
</div>
27+
<hr class="border-1 border-black mb-8" />
28+
29+
<div class="mt-12 text-center">
30+
<p>
31+
Use the /predict API route to make a GET request and pass in the data with a ?message=
32+
parmeter like so:
33+
</p>
34+
<br /><br />
35+
<a
36+
class="text-gray-700 text-center"
37+
href="http://127.0.0.1:5000/predict?message=The last time you considered giving up on your dreams but didn't… what kept you going?"
38+
>http://127.0.0.1:5000/predict?message=The last time you considered
39+
giving up on your dreams but didn't… what kept you going?</a
40+
>
41+
</div>
42+
</div>
43+
</body>
44+
</html>
3.72 KB
Binary file not shown.
1012 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)