One great advantage of Azure Machine Learning studio is the ability to easily deploy your trained model as a web service.
Set Up
For instance we have created a sentiment analysis experiment by following this tutorial.
After publishing your prediction model as a web service you have a RESTful API that takes a text as an input and sends you back the sentiment score.
Here is the sample request
{
"Inputs": {
"input1": {
"ColumnNames": [
"UserRequest"
],
"Values": [
[
"value"
],
[
"value"
]
]
}
},
"GlobalParameters": {}
}
Moreover there is also a code snippet in 3 programming languages provided by Azure ML team to create a client application. If we have a closer look we may notice that there is a possibility to send several lines simultaneously
data = {
"Inputs": {
"input1": {
"ColumnNames": [
"UserRequest"
],
"Values": [
[
"value"
],
[
"value"
],
]
},
},
"GlobalParameters": {}
}
That is actually great as it allows us to tremendously speed our application.
I have recently tested my application sending the data line by line and in chunks and the difference was dramatic, which is not surprising as you do not have re-open a url connection each time you need to predict your data thus economizing several seconds per request that may turn out to be really crucial during the execution.
However, it took me a lot of time to form my http request properly as there are only few documentations on the web so I decided to share my code.
Hope you will find this useful. Enjoy.
1. First you need to create an array of a data to be predicted (an array of short texts if take atext classification sample as a reference)
web_input = []
# in this example I used a column with a textual data from a pandas dataframe
for item in items_df['Text Column']:
web_input.append([item])
2. Now construct a dictionary that represents your web input
data = {
"Inputs": {
"input1": {
"ColumnNames": [
"UserRequest"
],
"Values": web_input
},
},
"GlobalParameters": {}
}
3. Now convert your input into a json object.
body = str.encode(json.dumps(data))
4. Now you can send the array to the AML Studio web service and get the response
url = 'ypur-web-service-url'
api_key = 'your-api-key'
headers = {'Content-Type': 'application/json', 'Authorization': ('Bearer ' + api_key)}
req = urllib.request.Request(url, body, headers)
output = []
try:
response = urllib.request.urlopen(req)
result = json.loads(response.read())
responses = result['Results']['output1']['value']['Values']
for response in responses:
output += response
except:
pass
And here is the full code.
Comments