Exercise Part 5: Deploy a Model as a Service in Azure ML

Now that you’ve trained some models, it’s showtime! Let’s deploy the best one as a service that client applications can use. Think of it as setting up a lemonade stand, but for predictions!


Step 1: Deployment Options

  • ACI vs. AKS: You can deploy as an Azure Container Instances (ACI) for testing or an Azure Kubernetes Service (AKS) cluster for production. We’ll use ACI for this exercise. It’s like choosing a bike for a leisurely ride instead of a race car.

Step 2: Deploying Your Model

  1. Automated ML Page: In Azure Machine Learning studio, go to your Automated ML experiment’s Details tab.
  2. Deploy the Best Model:
    • Service Name: predict-rentals
    • Description: Predict cycle rentals
    • Compute Type: Azure Container Instance
    • Enable Authentication: Selected
  3. Deployment Process: After clicking Deploy, wait for the status to change from Running to Successful. You might need to hit Refresh now and then.

Step 3: Getting Endpoint Details

  • Endpoints Page: In Azure Machine Learning studio, go to the Endpoints page.
  • Consume Tab: Find the predict-rentals real-time endpoint and note the REST endpoint and Primary Key. These are like the secret codes to access your service.

Step 4: Testing the Deployed Service

  1. Open a New Notebook:
    • In a new Azure Machine Learning studio tab, go to Notebooks (under Author).
    • Create a new file named Test-Bikes.ipynb in your user folder.
    • Make sure your compute instance is running.
  2. Setup the Test Environment:
    • Collapse the file explorer to focus on your notebook.
    • Copy the required code block. Remember to select all, including endpoints and brackets.
  3. Add the Test Code:
    • Paste the provided Python code into the notebook.
    • This code will use your deployed service to predict cycle rentals for a hypothetical five-day period.
  4. Insert Endpoint and Key:
    • Go back to the Consume page and copy the REST endpoint and Primary Key.
    • Paste them into the notebook, replacing YOUR_ENDPOINT and YOUR_KEY.
  5. Run the Code:
    • Save the notebook.
    • Click the run button next to the code cell.
    • Check the predicted number of rentals for each day.

Python Code:

endpoint = 'YOUR_ENDPOINT' #Replace with your endpoint 


key = 'YOUR_KEY' #Replace with your key



import json

import requests



#An array of features based on five-day weather forecast

x = [[1,1,2022,1,0,6,0,2,0.344167,0.363625,0.805833,0.160446],

[2,1,2022,1,0,0,0,2,0.363478,0.353739,0.696087,0.248539],

[3,1,2022,1,0,1,1,1,0.196364,0.189405,0.437273,0.248309],

[4,1,2022,1,0,2,1,1,0.2,0.212122,0.590435,0.160296],

[5,1,2022,1,0,3,1,1,0.226957,0.22927,0.436957,0.1869]]



#Convert the array to JSON format

input_json = json.dumps({"data": x})



#Set the content type and authentication for the request

headers = {"Content-Type":"application/json",

"Authorization":"Bearer " + key}


#Send the request

response = requests.post(endpoint, input_json, headers=headers)


#If we got a valid response, display the predictions

if response.status_code == 200:

y = json.loads(response.json())

print("Predictions:")

for i in range(len(x)):

print (" Day: {}. Predicted rentals: {}".format(i+1, max(0, round(y["result"][i]))))

else:

print(response)

Important Note:

  • Ensure you copy the entire code block accurately to avoid errors.

And there you have it! You’ve successfully deployed a model as a service and tested it. You’re now a bona fide Azure ML service provider!

Leave a Reply

Trending

Discover more from ML Made Simple

Subscribe now to keep reading and get access to the full archive.

Continue reading