/blog-mlopsapim-git

Primary LanguageJupyter Notebook

mlops, secure endpoints

This repo shows some introduction examples to Azure Machine Learning and a simple MLOps implemenation in which endpoints are secured by Azure API Management. Elaborating on this git repo of Clemens Siebler. For more details, see also my blogpost here:

Setup & Demo Flow

This gives a short, high-level overview of how this repo may be used.

Architecture

Simple MLOps pipelines

  1. Create a new project in Azure DevOps
  2. Fork this repo or import it into Azure DevOps (so that you can make changes to the repo)
  3. Create a service connection to your Azure Machine Learning workspace and use the name aml-workspace-connection
  4. Edit pipelines/german-credit-config.yml and adapt the values to point to your workspace
  5. Import the following pipelines into DevOps

Conventions

This repo is fully based on conventions in order to make MLOps reusable and easily scaleable. The directory structure is as follows:

pipelines
    \- german-credit-config.yml - Configuration for german credit model
    \- german-credit-deploy.yml - Deployment pipeline for german credit model
    \- german-credit-train-and-register.yml - Pipline for training and registering the base german credit model
models
    \- model1
        train.py (entry file for training)
        score.py (entry file for scoring)
        \- config
            deployment-config-aks.yml - Deployment infrastructure definition (e.g., AKS configuration)
            inference-conda.yml - Conda environement definition for inferencing/scoring
            inference-config.yml - Azure Machine Learning config for inferencing
            train-conda.yml - Conda environement definition for training
    \- model2
        ...same file and folder structure...

Testing

This snipped can be used to manually showcase/test the deployed model on AKS using the APIM exposed endpoint:

import requests
import json

import json
import requests
#
# creating bearer token using SPN (can also be created using Managed Identity or logging in as a user)
tenant ='<<your tenant id>>'
client_id = '<<spn client id>>'
client_secret = '<<spn secret>>'

url = 'https://login.microsoftonline.com/%s/oauth2/token' % tenant
data = {
  'grant_type': 'client_credentials',
  'client_id': client_id,
  'client_secret': client_secret
}

token = resp.json()['access_token']
#
url = 'https://<<your apim>>.azure-api.net/testprivv2/score'
#
test_data = {
  'data': [{
    "Age": 20,
    "Sex": "male",
    "Job": 0,
    "Housing": "own",
    "Saving accounts": "little",
    "Checking account": "little",
    "Credit amount": 100,
    "Duration": 48,
    "Purpose": "radio/TV"
  }]
}

headers = {'Content-Type':'application/json', 'Authorization': 'bearer ' + token}
resp = requests.post(url, json=test_data, headers=headers)

print("Prediction (good, bad):", resp.text)

Further Work

⭐ A fully documented starting template for Azure Machine Leraning with MLOps can be found here: microsoft/aml-acceleration-template. This includes model training, validation, testing, deployment, pipelines, and several other production-grade capabilties.