Skip to content

In this workshop, you will learn how to build a machine learning model using Python/Scikit-Learn, turn the model into an API using Python/Flask, test the API, build web applications using HTML/CSS/JavaScript/Nextjs, and deploy it to the web for global usage by end-users.

License

Notifications You must be signed in to change notification settings

BolajiAyodeji/deploy-ml-web-workshop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

77 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Deploying Machine Learning Models to the Web

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.

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.



Table of Contents


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.

In this workshop, we will build the following:

  • A minimal machine learning model that predicts the personality type of an individual using Python, Scikit-learn, and other libraries (/model).
  • A backend and API for the model using Python and Flask (/app/flask-api).
  • A web application that consumes the model/API using HTML, CSS, JavaScript, and Flask (/app/flask-app).
  • A web application and API that consumes the model/API using HTML, CSS, JavaScript, Nextjs, and Flask (/app/nextjs-flask-app).

You can test the deployed applications here:


๐ŸŽง Overview

Workshop duration: ~three (3) hours.

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

  • Explain what machine learning and a machine learning model are to your friends and family.
  • Understand how the CRISP-DM data framework works.
  • Understand the fundamentals of building a machine learning model using the Python programming language.
  • Understand how to serialize (export/import) a machine learning model.
  • Understand how to turn a machine learning model into an API using the Flask Python framework.
  • Understand how to build a web application using the Flask Python framework.
  • Understand how to build a web application using the Nextjs JavaScript framework.
  • Understand how to deploy a Python/Flask application to the cloud using Fly.
  • Understand how to deploy a Nextjs/Flask application to the cloud using Vercel.

This workshop has been presented at the following events:

๐Ÿ›  Prerequisites and Installation Guide

Generally, you need the following:

  • A curious mind and desire to learn.
  • An understanding of or familiarity with the first five phases of the CRISP-DM data-mining framework.
  • A working laptop running on any operating system (Windows, Linux, or macOS).
  • Ability to navigate through the command-line interface (CLI).
  • Some existing machine learning model development knowledge (or an interest at least).
  • Some basic understanding of the Python programming language.
  • Some basic understanding of the JavaScript programming language.
  • Experience using Reactjs and Nextjs frameworks.

To make the workshop a smooth process, we will use Google Colab for the machine learning model development. This will enable us to run a hosted Jupyter Notebook on the cloud (with GPU and TPU computing resources) and avoid issues with installing all the Python packages locally (the future is cloud!). All you have to do is create a free Google account if you don't have one already and open the Notebook in Google Colab.

But if you prefer local (I strongly advise against this; at least for this workshop), you should install Anancoda 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.

Note

Jupyter Notebooks allow you to combine executable code and rich text in a single document, along with images, HTML, LaTeX, and more.


You should have the following software and packages installed on your computer. Kindly follow the guide in each toggle to install them.

Git and GitHub

We will install/use the following packages:

  • Git: a free and open-sourced distributed version control system.
  • GitHub: a code hosting platform for version control and collaboration.

Download and install Git from this website for all operating systems or follow this installation guide.

Once installed successfully, confirm the version using the command:

git --version

Next, create a free GitHub account if you don't have one already.

[!NOTE]

Bonus: If you're a student, you should check out the GitHub Student Developer Pack, 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.

Postman Desktop App or VSCode Extension

We will install any of the following:

Download and install the Postman Desktop Client or VSCode Extension from this website for all operating systems.

Python and PIP

We will install the following packages:

  • Python: a general-purpose programming language that lets you work quickly and integrate systems more effectively.
  • PIP: a package manager for installing Python packages or modules.

Download and install Python 3 (>3.10.12) from this website for all operating systems (this will come with PIP in-built).

[!IMPORTANT]

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.

Once installed successfully, confirm the version using the command:

python3 --version

If Python is installed correctly, you should have PIP installed. If it isn't, follow the steps in this guide to install PIP.

Other Python Packages

We will install the following packages:

  • Python Dotenv: a package that reads key-value pairs from a .env file and can set them as environment variables.
  • Scikit-Learn: a free software machine learning library for the Python programming language, including various classification, regression, and clustering algorithms.
  • Flask: a lightweight Python micro framework for building web applications.
  • Gunicorn: a Python WSGI HTTP Server for UNIX.

[!IMPORTANT]

Eventually, we will install the above mentioned libraries in a virtual environment to ensure we use independent groups of Python libraries for each project. This is a recommended practice when building Python aplications in both development and production.

In the project directory, create the environment with the command python3 -m venv .venv and activate the envirnment with the command source .venv/bin/activate.

Now, install all of the packages using the command below:

pip3 install python-dotenv scikit-learn flask gunicorn

Once installed successfully, confirm the version using the command:

pip3 show python-dotenv

pip3 show scikit-learn

pip3 show flask

pip3 show gunicorn
Nodejs and NPM

We will install the following packages:

  • Nodejs: an open-source, cross-platform JavaScript runtime environment.
  • NPM: a package manager for installing JavaSript packages or modules.

Download and install Nodejs and NPM from this website for all operating systems or follow this installation guide.

Once installed successfully, confirm the version using the command:

node --version

npm --version
Reactjs and Nextjs

We will install the following packages:

  • Reactjs: an open-source web and native JavaScript library for building user interfaces.
  • Nextjs: an open-source React web development framework.

Automatically create a project with the latest version of Reactjs18 and Nextjs13 using the create-next-app command below:

npx create-next-app@latest

Consider reading Reactjs's installation guide to learn more.

Flyctl and Vercel

We will install the following packages:

  • Flyctl: the command-line utility that lets you work on Fly from your terminal locally.
  • Vercel: a frontend cloud platform for deploying web apps.

Sign up for a free account on Fly.io and Vercel if you haven't yet. Next, read this guide to install flyctl for any operating system.

๐Ÿ“š Workshop Lessons

  1. Introduction to Machine Learning
  2. Building a ML Model with Python
  3. Building an API for the ML Model
  4. Building a Web Application for the API
  5. Overview of Alternative Frameworks
  6. Building a Nextjs Web Application for the ML Model
  7. Deploying the API and Web Application to the Cloud
  8. Bonus: Building Web AI Applications with Pre-trained Models (LLMs) and Vercel AI SDK (TBA)

๐Ÿš€ What's Next?

  • Challenge: as discussed in lesson seven, update the flask-api app to support CORS, change how we get the request data, deploy to Fly again, and then use the live API directly in the Nextjs app.
  • Build something nice for fun to test all you've learned, open-source the code, and write about it if you want.
  • Here's a list of different projects built by participants of this workshop. If you've built something nice, feel free to create a new discussion and talk about your project.
  • Tag me on X (Twitter) @iambolajiayo when you share your project.
  • Open an issue in this repository or DM me @iambolajiayo if you have any questions or need help with anything.

๐Ÿ“‘ Further Resources

๐Ÿ‘ฉ๐Ÿฝโ€๐Ÿ’ป Contributors Guide

  1. Fork this repository (learn how to do this here).

  2. Clone the forked repository like so:

git clone https://github.com/<your username>/deploy-ml-web-workshop.git && cd deploy-ml-web-workshop
  1. Make your changes and create a pull request (learn how to do this).

  2. I will attend to your pull request, provide some feedback, request some changes, or eventually merge the PR!

โš–๏ธ License

The content of this repository ยฉ 2023 by Bolaji Ayodeji is published under the Creative Commons Attribution-ShareAlike 4.0 International license and the underlying code examples are licensed under the MIT license.

About

In this workshop, you will learn how to build a machine learning model using Python/Scikit-Learn, turn the model into an API using Python/Flask, test the API, build web applications using HTML/CSS/JavaScript/Nextjs, and deploy it to the web for global usage by end-users.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published