Web Services (Part 2) – Consume Azure ML Studio in Python

Published by FirstAlign

In this blog we are going to use python code to perform sentiment analysis using the web service we created in Web Services (Part 1) – Create and deploy.

Short Recap

In my last blog we took a dataset and created a Machine Learning model to predict the sentiment of the text through Sentiment Analysis. We used Azure Machine Learning studio for this purpose, partly due to its drag and drop capability. After creating, we evaluated the model and determined both the accuracy and other evaluation parameters.

We created the web service using the Machine Learning Studio with a simple step-by-step process. We were able to test that web service within the interface of Azure ML Studio.

Now the goal is consume that web service out of the Azure ML Studio Interface. To do this we are going to call that web service using python on our local machine. 

Azure ML Workflow

What is Web Service?

A Web service is a piece of software that allows itself to be accessed over the internet using some standards such as SOAP (Simple Object Access Protocol). Here in our use case we are going to use RESTful web Service, RESTful webservice is a lightweight scalable service, the API in RESTful webservice are exposed in uniform, secure and stateless way. Here we have created a RESTful web service when we will invoke it with text parameters it will return us the sentiment of the text.

As we have a deployed a web service using Azure ML studio in “Web Services (Part 1) – Create and deploy” your first web service, now its time to consume it from the outside i.e. our local machine or some other virtual machine (VM). To achieve this we are using Python. So lets start.

In the last blog we left while we had a working web service end point, created using Azure ML studio.

Web Service dashboard

Now click on the New Web Services Experience and you will be redirected to the page as shown below;

Quick start page for web service

Click on consume, you will get all the basic items required for consuming the web service.

Keys and URL

In the figure above you can see we have a primary key, secondary key and a URL for the prediction of both batch requests and simple single request response. We will use this in our code to perform the prediction.

On the Consume page you can see the sample code in different languages. We will use the python code and get the results.

Imports

There two required import for the code to run one is request for urllib and other is json imports

Using Data

Here we will specify the way data is sent to make the prediction.

Data supplied

Specify URL and API key

Copy the URL key and paste it here in this snippet.

Key and prediction URL

Specify Request and Header

Here in this snippet, headers are created for the request. This headers api key and body are then used to create the request object.

Headers and Request

Try except block:

Here is this snippet. Try catch block is used to make request and deal with exceptions where/ if they occur.

Try except block for making request

Here is the full code.

Full Code

Run the code

As you run the code you will get a json response. The most important feature to look for is ‘scored labels’ which will show the prediction. Here it is -1 based on the type of text supplied.

Results

Conclusion

This blog is an extension of the previous blog where we created a sentiment analysis web service. We have taken this further in this blog by consuming the web service using python. Results were provided in JSON which can now be utilized in a any way we want.

For more information on consuming web service check the following links.

References

Consume web service – ML Studio (classic) – Azure
Once you deploy an Azure Machine Learning Studio (classic) predictive model as a Web service, you can use a REST API to…www.google.com

Deployment and consumption – ML Studio (classic) – Azure
You can use Azure Machine Learning Studio (classic) to deploy machine learning workflows and models as web services…www.google.com

In the next blog we will look at how we can scale the Web Service so stay tuned until than happy coding ❤.

Click here to connect with us

Spread the word

Related posts