Create an Azure Machine Learning Web Service with Python and Azure DSVM | by Philip Roimon Domingo | Towards Data Science

Last week, while I was reading the documentation about Microsoft CNTK, and checking out the code samples from their GitHub repository, I bumped into another GitHub repository which is about Azure Machine Learning Operationalization.
Azure Machine Learning Operationalization is a component of the Azure CLI that enables operationalization of models that you create through Vienna that use the CNTK, SPARK, and Python machine learning platforms.
I’ve been playing around with Python and Machine Learning recently, so I thought I’d give this one a try and create an AzureML Web Service using a machine learning model built with Python.
Important note: Azure Machine Learning Operationalization is still in preview as of this time of writing. There could be some breaking changes that might happen in the future.
To get started with Azure Machine Learning Operationalization, you need the following:
Azure Account (if you don’t have one, you can get a free trial here)Azure Data Science Virtual Machine (DSVM) - though this one is not exactly required but I would say that this is the fastest and easiest way to get started. You can check the documentation here to create one.
Azure Account (if you don’t have one, you can get a free trial here)
Azure Data Science Virtual Machine (DSVM) - though this one is not exactly required but I would say that this is the fastest and easiest way to get started. You can check the documentation here to create one.
Once you have your Azure DSVM created, first thing we’re going to do is to update our azure-cli, azure-cli-ml and azure-ml-api-sdk packages by uninstalling and installing them.
$ pip uninstall azure-cli azure-cli-ml azure-ml-api-sdk$ pip install azure-cli azure-cli-ml azure-ml-api-sdk
Previously, I tried updating the packages with the --upgrade option in pip install but I keep on having an error upgrading the packages. So uninstalling and installing it will do the fix because later on, we will be using these packages for generating a schema and deploying our trained model to our web service.
Next is we’re going to create a model that will predict whether the person is a male or female based on the given body metrics (height, width and shoe size) using scikit-learn machine learning library. Create a file named train.py and you can copy the code below:
Run the code and it should create a model.pkl in your project folder.
To deploy our model as a web service, we need to create a score.py that will be used to generate a schema JSON file.
Run the score.py and you should see that the service_schema.json is created. Now our project folder should contain the following files:
PROJECT_FOLDER- train.py- score.py- model.pkl- service_schema.json
Now that we have everything setup, we will now create our web service and deploy the model by doing the following:
In order for us to be able to create our environment and model management account using azure cli we will first need to authenticate with this command:
$ az login
This will then show a message that you need to open up your web browser and go to https://aka.ms/devicelogin and enter the code provided in your terminal.
Once you’re able to authenticate your account using azure cli type the following command to create our environment:
$  az ml env setup -c -n YOUR_CLUSTER_NAME -l YOUR_LOCATION
This is what I’ve used for this tutorial:
$ az ml env setup -c -n azmldemo -l australiaeast
It’s also important to keep in mind that the supported location for now is westcentralus, eastus2, and australiaeast.
It will take at least 10–20 minutes for our environment to be completely provisioned, so while waiting, we can now then create our model management account which is a one time setup thing.
$ az ml account modelmanagement create -l YOUR_LOCATION -n YOUR_MODEL_MANAGEMENT_ACCOUNT_NAME -g YOUR_RESOURCE_GROUP
This is what I’ve used for this tutorial:
$ az ml account modelmanagement create -l australiaeast -n azmldemoacc -g azmldemorg
Just in case you’re wondering, the azmldemorg resource group was automatically created when we created our environment earlier, it basically added an rg to the name of our cluster which is azmldemo.
Once the Model Management Account and your environment is successfully created, we will now set the Model Management Account and the environment with the following command:
$ az ml account modelmanagement set -n YOUR_MODEL_MANAGEMENT_ACCOUNT_NAME -g YOUR_RESOURCE_GROUP$ az ml env set -n YOUR_CLUSTER_NAME -g YOUR_RESOURCE_GROUP
This is what I’ve used for this tutorial:
$ az ml account modelmanagement set -n azmldemoacc -g azmldemorg$ az ml env set -n azmldemo -g azmldemorg
It’s also important to keep in mind that your environment Provisioning State should show Succeeded for you to be able to set it successfully. You can check the Provisioning State with the following command:
$ az ml env show -g YOUR_RESOURCE_GROUP -n YOUR_CLUSTER_NAME
Once you’re able to successfully set your Model Management Account and environment, we will now create the web service by:
Go to the project folder in where we’ve created the Gender Classifier Model and JSON schemaType the following command to create the web service:
Go to the project folder in where we’ve created the Gender Classifier Model and JSON schema
Type the following command to create the web service:
$ az ml service create realtime -f score.py --m model.pkl -s service_schema.json -n genderclassifier -r pythonor you can also use --help or -h to know the other arguments available$ az ml service create realtime -h
Once the web service is successfully created, you can use the following command to get all the important details that you can use to test the web service (like the sample CLI command, Swagger URL, Authorization Bearer Key, and etc):
$ az ml service usage realtime -i YOUR_SERVICE_ID
You can get your Service Id with the following command:
$ az ml service list realtime
$ az ml service run realtime -i YOUR_SERVICE_ID -d "{\"input_df\": [{\"width\": 60, \"shoe_size\": 38, \"height\": 190}]}"
It should look like this when your run in your terminal:
Here’s an example on how you can test the web service via Scoring URL
And we’re done, so what do you think about the Azure Machine Learning Operationalization? Again, you can check out their GitHub Repository here for other samples.