Lab 4.1 - Deploy to ACI using Python
An easy way testing your model is to deploy the model in a container to an Azure Container Instance. A Container Instance is the easiest way to deploy a container.

Install the AzureML SDK

In your terminal install the AzureML SDK
1
pip install azureml-sdk
Copied!

Download the scoring script

1
# Create a directory
2
mkdir deploy
3
cd deploy
4
5
# Download the scoring script
6
wget https://raw.githubusercontent.com/GlobalAICommunity/back-together-2021/main/workshop-assets/amls/score.py
Copied!

Create the Python Script

We start with creating a python script called deploy.py, this script will take of deploying your model.
1
code deploy.py
Copied!
and start with importing the dependencies and setting some variables.
1
import azureml
2
from azureml.core.model import Model, InferenceConfig
3
from azureml.core import Workspace, Model, Environment
4
from azureml.core.conda_dependencies import CondaDependencies
5
from azureml.core.webservice import AciWebservice
6
7
# Connect to workspace
8
workspaceName = ""
9
subscriptionId = ""
10
resourceGroup = ""
Copied!
Next we need to connect to our workspace.

Get the subscription ID

The command below shows a list of all your subscription id's
1
az account show --query id -o table
Copied!

Get the Workspace name

The command below lists all the workspaces you have access to
1
az ml workspace list -o table
Copied!

Get the resource group

The command below lists all your resource groups.
1
az rg list
Copied!
Add the values to the variables.

Connect to your workspace

1
ws = Workspace.get(name=workspaceName,
2
subscription_id=subscriptionId,
3
resource_group=resourceGroup)
4
print("Workspace:", ws.name)
Copied!

Load the model

1
# Load the model
2
model = Model(ws, name='SimpsonsClassification-pytorch')
3
print("Loaded model version:",model.version)
Copied!

Define the environment

1
myenv = Environment(name="simpsons-inference")
2
conda_dep = CondaDependencies()
3
conda_dep.add_pip_package("azureml-defaults")
4
conda_dep.add_pip_package("torch")
5
conda_dep.add_pip_package("torchvision")
6
conda_dep.add_pip_package("pillow==5.4.1")
7
myenv.python.conda_dependencies=conda_dep
Copied!

Create the deployment configuration

1
inference_config = InferenceConfig(
2
entry_script="score.py",
3
environment=myenv
4
)
5
6
deploy_config = AciWebservice.deploy_configuration(
7
cpu_cores = 1,
8
memory_gb = 2,
9
description='Simpson Lego Classifier')
Copied!

Deploy the model

1
# Deploy the model to an ACI
2
aci_service = Model.deploy(ws,
3
name="simpsons-pt-aci",
4
models = [model],
5
inference_config = inference_config,
6
deployment_config = deploy_config,
7
overwrite = True)
8
9
aci_service.wait_for_deployment(show_output=True)
Copied!