Automate Machine Studying Deployment with GitHub Actions | by Khuyen Tran | Apr, 2023

Within the earlier article, we realized about utilizing steady integration to securely and effectively merge a brand new machine-learning mannequin into the principle department.

Picture by Writer

Nonetheless, as soon as the mannequin is in the principle department, how can we deploy it into manufacturing?

Picture by Writer

Counting on an engineer to deploy the mannequin so can have some drawbacks, corresponding to:

  • Slowing down the discharge course of
  • Consuming useful engineering time that could possibly be used for different duties

These issues turn out to be extra pronounced if the mannequin undergoes frequent updates.

Picture by Writer

Wouldn’t or not it’s good if the mannequin is routinely deployed into manufacturing each time a brand new mannequin is pushed to the principle department? That’s when steady integration is useful.

Steady deployment (CD) is the follow of routinely deploying software program modifications to manufacturing after they move a sequence of automated exams. In a machine studying mission, steady deployment can supply a number of advantages:

  1. Sooner time-to-market: Steady deployment reduces the time wanted to launch new machine studying fashions to manufacturing.
  2. Elevated effectivity: Automating the deployment course of reduces the assets required to deploy machine studying fashions to manufacturing.

This text will present you how one can create a CD pipeline for a machine-learning mission.

Be happy to play and fork the supply code of this text right here:

Earlier than constructing a CD pipeline, let’s determine the workflow for the pipeline:

  • After a sequence of exams, a brand new machine-learning mannequin is merged into the principle department
  • A CD pipeline is triggered and a brand new mannequin is deployed into manufacturing
Picture by Writer

To construct a CD pipeline, we are going to carry out the next steps:

  1. Save mannequin object and mannequin metadata
  2. Serve the mannequin regionally
  3. Add the mannequin to a distant storage
  4. Arrange a platform to deploy your mannequin
  5. Create a GitHub workflow to deploy fashions into manufacturing

Let’s discover every of those steps intimately.

Save mannequin

We’ll use MLEM, an open-source device, to avoid wasting and deploy the mannequin.

To save lots of an experiment’s mannequin utilizing MLEM, start by calling its save technique.

from mlem.api import save

# as a substitute of joblib.dump(mannequin, "mannequin/svm")
save(mannequin, "mannequin/svm", sample_data=X_train)

Full script.

Working this script will create two recordsdata: a mannequin file and a metadata file.

Picture by Writer

The metadata file captures varied info from a mannequin object, together with:

  • Mannequin artifacts such because the mannequin’s dimension and hash worth, that are helpful for versioning
  • Mannequin strategies corresponding topredict and predict_proba
  • Enter information schema
  • Python necessities used to coach the mannequin
hash: ba0c50b412f6b5d5c5bd6c0ef163b1a1
dimension: 148163
uri: svm
- - mannequin
- predict
object_type: mannequin
- title: X
- ''
- mounted acidity
- unstable acidity
- citric acid
- residual sugar
- ...
- int64
- float64
- float64
- float64
- float64
- ...
- ''
kind: dataframe
title: predict
dtype: int64
- null
kind: ndarray
varkw: predict_params
kind: sklearn_pipeline
- module: numpy
model: 1.24.2
- module: pandas
model: 1.5.3
- module: sklearn
package_name: scikit-learn
model: 1.2.2

View the metadata file.

Serve the mannequin regionally

Let’s check out the mannequin by serving it regionally. To launch a FastAPI mannequin server regionally, merely run:

mlem serve fastapi --model mannequin/svm

Go to to view the mannequin. Click on “Attempt it out” to check out the mannequin on a pattern dataset.

Picture by Writer

Push the mannequin to a distant storage

By pushing the mannequin to distant storage, we are able to retailer our fashions and information in a centralized location that may be accessed by the GitHub workflow.

Picture by Writer

We’ll use DVC for mannequin administration as a result of it presents the next advantages:

  1. Model management: DVC permits maintaining monitor of modifications to fashions and information over time, making it straightforward to revert to earlier variations.
  2. Storage: DVC can retailer fashions and information in various kinds of storage programs, corresponding to Amazon S3, Google Cloud Storage, and Microsoft Azure Blob Storage.
  3. Reproducibility: By versioning information and fashions, experiments could be simply reproduced with the very same information and mannequin variations.

To combine DVC with MLEM, we are able to use DVC pipeline. With the DVC pipeline, we are able to specify the command, dependencies, and parameters wanted to create sure outputs within the dvc.yaml file.

cmd: python src/
- information/intermediate
- src/
- information
- mannequin
- prepare
- mannequin/svm
- mannequin/svm.mlem:
cache: false

View the full file.

Within the instance above, we specify the outputs to be the recordsdata mannequin/svm and mannequin/svm.mlem beneath the outs area. Particularly,

  • The mannequin/svm is cached, so it is going to be uploaded to a DVC distant storage, however not dedicated to Git. This ensures that enormous binary recordsdata don’t decelerate the efficiency of the repository.
  • The mode/svm.mlem shouldn’t be cached, so it received’t be uploaded to a DVC distant storage however will probably be dedicated to Git. This permits us to trace modifications within the mannequin whereas nonetheless maintaining the repository’s dimension small.
Picture by Writer

To run the pipeline, kind the next command in your terminal:

$ dvc exp run

Working stage 'prepare':
> python src/

Subsequent, specify the distant storage location the place the mannequin will probably be uploaded to within the file .dvc/config :

['remote "read"']
url =
['remote "read-write"']
url = s3://your-s3-bucket/

To push the modified recordsdata to the distant storage location named “read-write”, merely run:

dvc push -r read-write

Arrange a platform to deploy your mannequin

Subsequent, let’s work out a platform to deploy our mannequin. MLEM helps deploying your mannequin to the next platforms:

  • Docker
  • Heroku
  • Kubernetes
  • Sagemaker

This mission chooses as a deployment platform because it’s straightforward and low-cost to get began.

To create purposes on in a GitHub workflow, you’ll want an entry token. Right here’s how one can get one:

  1. Join a account (you’ll want to supply a bank card, however they received’t cost you till you exceed free limits).
  2. Log in and click on “Entry Tokens” beneath the “Account” button within the high proper nook.
  3. Create a brand new entry token and duplicate it for later use.
Picture by Writer

Create a GitHub workflow

Now it involves the thrilling half: Making a GitHub workflow to deploy your mannequin! In case you are not aware of GitHub workflow, I like to recommend studying this article for a fast overview.

We’ll create the workflow referred to as publish-model within the file .github/workflows/publish.yaml :

Picture by Writer

Right here’s what the file appears like:

title: publish-model

- essential
- mannequin/svm.mlem

runs-on: ubuntu-latest

- title: Checkout
makes use of: actions/checkout@v2

- title: Setting setup
makes use of: actions/setup-python@v2
python-version: 3.8

- title: Set up dependencies
run: pip set up -r necessities.txt

- title: Obtain mannequin
AWS_ACCESS_KEY_ID: ${{ secrets and techniques.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets and techniques.AWS_SECRET_ACCESS_KEY }}
run: dvc pull mannequin/svm -r read-write

- title: Setup flyctl
makes use of: superfly/flyctl-actions/setup-flyctl@grasp

- title: Deploy mannequin
FLY_API_TOKEN: ${{ secrets and techniques.FLY_API_TOKEN }}
run: mlem deployment run flyio svm-app --model mannequin/svm

The on area specifies that the pipeline is triggered on a push occasion to the principle department.

The publish-model job contains the next steps:

  • Testing the code
  • Establishing the Python surroundings
  • Putting in dependencies
  • Pulling a mannequin from a distant storage location utilizing DVC
  • Establishing flyctl to make use of
  • Deploying the mannequin to

Word that for the job to operate correctly, it requires the next:

  • AWS credentials to drag the mannequin
  •’s entry token to deploy the mannequin

To make sure the safe storage of delicate info in our repository and allow GitHub Actions to entry them, we are going to use encrypted secrets.

To create encrypted secrets and techniques, click on “Settings” -> “Actions” -> “New repository secret.”

Picture by Writer

That’s it! Now let’s check out this mission and see if it really works as anticipated.


To check out this mission, begin with creating a brand new repository utilizing the mission template.

Picture by Writer

Clone the brand new repository to your native machine:

git clone

Arrange the surroundings:

# Go to the mission listing
cd cicd-mlops-demo

# Create a brand new department
git checkout -b experiment

# Set up dependencies
pip set up -r necessities.txt

Pull information from the distant storage location referred to as “learn”:

dvc pull -r learn

Create a brand new mannequin

svm_kernel is an inventory of values used to check the kernel hyperparameter whereas tuning the SVM mannequin. To generate a brand new mannequin, add rbf to svm__kernel within the params.yaml file.

Picture by Writer

Run a brand new experiment with the change:

dvc exp run

Push the modified mannequin to distant storage referred to as “read-write”:

dvc push -r read-write

Add, commit, and push modifications to the repository within the “experiment” department:

git add .
git commit -m 'change svm kernel'
git push origin experiment

Create a pull request

Subsequent, create a pull request by clicking the Contribute button.

Picture by Writer

After making a pull request within the repository, a GitHub workflow will probably be triggered to run exams on the code and mannequin.

After all of the exams have handed, click on “Merge pull request.”

Picture by Writer

Deploy the mannequin

As soon as the modifications are merged, a CD pipeline will probably be triggered to deploy the ML mannequin.

To view the workflow run, click on the workflow then click on the publish-model job.

Picture by Writer
Picture by Writer

Click on the hyperlink beneath the “Deploy mannequin” step to view the web site to which the mannequin is deployed.

Picture by Writer

Right here’s what the web site appears like:

Picture by Writer

View the website.

Congratulations! You’ve simply realized how one can create a CD pipeline to automate your machine-learning workflows. Combining CD with CI will permit your corporations to catch errors early, scale back prices, and scale back time-to-market.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button