Поділитися через


How to consume a Machine Learning Studio (classic) web service

APPLIES TO: This is a check mark, which means that this article applies to Machine Learning Studio (classic). Machine Learning Studio (classic) This is an X, which means that this article applies to Azure Machine Learning.Azure Machine Learning

Important

Support for Machine Learning Studio (classic) will end on 31 August 2024. We recommend you transition to Azure Machine Learning by that date.

Beginning 1 December 2021, you will not be able to create new Machine Learning Studio (classic) resources. Through 31 August 2024, you can continue to use the existing Machine Learning Studio (classic) resources.

ML Studio (classic) documentation is being retired and may not be updated in the future.

Once you deploy an Machine Learning Studio (classic) predictive model as a Web service, you can use a REST API to send it data and get predictions. You can send the data in real-time or in batch mode.

You can find more information about how to create and deploy a Machine Learning Web service using Machine Learning Studio (classic) here:

Overview

With the Machine Learning Web service, an external application communicates with a Machine Learning workflow scoring model in real time. A Machine Learning Web service call returns prediction results to an external application. To make a Machine Learning Web service call, you pass an API key that is created when you deploy a prediction. The Machine Learning Web service is based on REST, a popular architecture choice for web programming projects.

Machine Learning Studio (classic) has two types of services:

  • Request-Response Service (RRS) – A low latency, highly scalable service that provides an interface to the stateless models created and deployed from the Machine Learning Studio (classic).
  • Batch Execution Service (BES) – An asynchronous service that scores a batch for data records.

For more information about Machine Learning Web services, see Deploy a Machine Learning Web service.

Get an authorization key

When you deploy your experiment, API keys are generated for the Web service. You can retrieve the keys from several locations.

From the Machine Learning Web Services portal

Sign in to the Machine Learning Web Services portal.

To retrieve the API key for a New Machine Learning Web service:

  1. In the Machine Learning Web Services portal, click Web Services the top menu.
  2. Click the Web service for which you want to retrieve the key.
  3. On the top menu, click Consume.
  4. Copy and save the Primary Key.

To retrieve the API key for a Classic Machine Learning Web service:

  1. In the Azure Machine Learning Web Services portal, click Classic Web Services the top menu.
  2. Click the Web service with which you are working.
  3. Click the endpoint for which you want to retrieve the key.
  4. On the top menu, click Consume.
  5. Copy and save the Primary Key.

Classic Web service

You can also retrieve a key for a Classic Web service from Machine Learning Studio (classic).

Machine Learning Studio (classic)

  1. In Machine Learning Studio (classic), click WEB SERVICES on the left.
  2. Click a Web service. The API key is on the DASHBOARD tab.

Connect to a Machine Learning Web service

You can connect to a Machine Learning Web service using any programming language that supports HTTP request and response. You can view examples in C#, Python, and R from a Machine Learning Web service help page.

Machine Learning API help Machine Learning API help is created when you deploy a Web service. See Tutorial 3: Deploy credit risk model. The Machine Learning API help contains details about a prediction Web service.

  1. Click the Web service with which you are working.
  2. Click the endpoint for which you want to view the API Help Page.
  3. On the top menu, click Consume.
  4. Click API help page under either the Request-Response or Batch Execution endpoints.

To view Machine Learning API help for a New Web service

In the Machine Learning Web Services Portal:

  1. Click WEB SERVICES on the top menu.
  2. Click the Web service for which you want to retrieve the key.

Click Use Web Service to get the URIs for the Request-Response and Batch Execution Services and Sample code in C#, R, and Python.

Click Swagger API to get Swagger based documentation for the APIs called from the supplied URIs.

C# Sample

To connect to a Machine Learning Web service, use an HttpClient passing ScoreData. ScoreData contains a FeatureVector, an n-dimensional vector of numerical features that represents the ScoreData. You authenticate to the Machine Learning service with an API key.

To connect to a Machine Learning Web service, the Microsoft.AspNet.WebApi.Client NuGet package must be installed.

Install Microsoft.AspNet.WebApi.Client NuGet in Visual Studio

  1. Publish the Download dataset from UCI: Adult 2 class dataset Web Service.
  2. Click Tools > NuGet Package Manager > Package Manager Console.
  3. Choose Install-Package Microsoft.AspNet.WebApi.Client.

To run the code sample

  1. Publish "Sample 1: Download dataset from UCI: Adult 2 class dataset" experiment, part of the Machine Learning sample collection.
  2. Assign apiKey with the key from a Web service. See Get an authorization key above.
  3. Assign serviceUri with the Request URI.

Here is what a complete request will look like.

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace CallRequestResponseService
{
    class Program
    {
        static void Main(string[] args)
        {
            InvokeRequestResponseService().Wait();
        }

        static async Task InvokeRequestResponseService()
        {
            using (var client = new HttpClient())
            {
                var scoreRequest = new
                {
                    Inputs = new Dictionary<string, List<Dictionary<string, string>>> () {
                        {
                            "input1",
                            // Replace columns labels with those used in your dataset
                            new List<Dictionary<string, string>>(){new Dictionary<string, string>(){
                                    {
                                        "column1", "value1"
                                    },
                                    {
                                        "column2", "value2"
                                    },
                                    {
                                        "column3", "value3"
                                    }
                                }
                            }
                        },
                    },
                    GlobalParameters = new Dictionary<string, string>() {}
                };

                // Replace these values with your API key and URI found on https://services.azureml.net/
                const string apiKey = "<your-api-key>"; 
                const string apiUri = "<your-api-uri>";
                
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Bearer", apiKey);
                client.BaseAddress = new Uri(apiUri);

                // WARNING: The 'await' statement below can result in a deadlock
                // if you are calling this code from the UI thread of an ASP.NET application.
                // One way to address this would be to call ConfigureAwait(false)
                // so that the execution does not attempt to resume on the original context.
                // For instance, replace code such as:
                //      result = await DoSomeTask()
                // with the following:
                //      result = await DoSomeTask().ConfigureAwait(false)

                HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest);

                if (response.IsSuccessStatusCode)
                {
                    string result = await response.Content.ReadAsStringAsync();
                    Console.WriteLine("Result: {0}", result);
                }
                else
                {
                    Console.WriteLine(string.Format("The request failed with status code: {0}", response.StatusCode));

                    // Print the headers - they include the request ID and the timestamp,
                    // which are useful for debugging the failure
                    Console.WriteLine(response.Headers.ToString());

                    string responseContent = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(responseContent);
                }
            }
        }
    }
}

Python Sample

To connect to a Machine Learning Web service, use the urllib2 library for Python 2.X and urllib.request library for Python 3.X. You will pass ScoreData, which contains a FeatureVector, an n-dimensional vector of numerical features that represents the ScoreData. You authenticate to the Machine Learning service with an API key.

To run the code sample

  1. Deploy "Sample 1: Download dataset from UCI: Adult 2 class dataset" experiment, part of the Machine Learning sample collection.
  2. Assign apiKey with the key from a Web service. See the Get an authorization key section near the beginning of this article.
  3. Assign serviceUri with the Request URI.

Here is what a complete request will look like.

import urllib2 # urllib.request and urllib.error for Python 3.X
import json

data = {
    "Inputs": {
        "input1":
        [
            {
                'column1': "value1",   
                'column2': "value2",   
                'column3': "value3"
            }
        ],
    },
    "GlobalParameters":  {}
}

body = str.encode(json.dumps(data))

# Replace this with the URI and API Key for your web service
url = '<your-api-uri>'
api_key = '<your-api-key>'
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}

# "urllib.request.Request(url, body, headers)" for Python 3.X
req = urllib2.Request(url, body, headers)

try:
    # "urllib.request.urlopen(req)" for Python 3.X
    response = urllib2.urlopen(req)

    result = response.read()
    print(result)
# "urllib.error.HTTPError as error" for Python 3.X
except urllib2.HTTPError, error: 
    print("The request failed with status code: " + str(error.code))

    # Print the headers - they include the request ID and the timestamp, which are useful for debugging the failure
    print(error.info())
    print(json.loads(error.read())) 

R Sample

To connect to a Machine Learning Web Service, use the RCurl and rjson libraries to make the request and process the returned JSON response. You will pass ScoreData, which contains a FeatureVector, an n-dimensional vector of numerical features that represents the ScoreData. You authenticate to the Machine Learning service with an API key.

Here is what a complete request will look like.

library("curl")
library("httr")
library("rjson")

requestFailed = function(response) {
    return (response$status_code >= 400)
}

printHttpResult = function(response, result) {
    if (requestFailed(response)) {
        print(paste("The request failed with status code:", response$status_code, sep=" "))
    
        # Print the headers - they include the request ID and the timestamp, which are useful for debugging the failure
        print(response$headers)
    }
    
    print("Result:") 
    print(fromJSON(result))  
}

req = list(
        Inputs = list( 
            "input1" = list(
                "ColumnNames" = list("Col1", "Col2", "Col3"),
                "Values" = list( list( "0", "value", "0" ),  list( "0", "value", "0" )  )
            )                ),
        GlobalParameters = setNames(fromJSON('{}'), character(0))
)

body = enc2utf8(toJSON(req))
api_key = "abc123" # Replace this with the API key for the web service
authz_hdr = paste('Bearer', api_key, sep=' ')

response = POST(url= "<your-api-uri>",
        add_headers("Content-Type" = "application/json", "Authorization" = authz_hdr),
        body = body)

result = content(response, type="text", encoding="UTF-8")

printHttpResult(response, result)

JavaScript Sample

To connect to a Machine Learning Web Service, use the request npm package in your project. You will also use the JSON object to format your input and parse the result. Install using npm install request --save, or add "request": "*" to your package.json under dependencies and run npm install.

Here is what a complete request will look like.

let req = require("request");

const uri = "<your-api-uri>";
const apiKey = "<your-api-key>";

let data = {
    "Inputs": {
        "input1":
        [
            {
                'column1': "value1",
                'column2': "value2",
                'column3': "value3"
            }
        ],
    },
    "GlobalParameters": {}
}

const options = {
    uri: uri,
    method: "POST",
    headers: {
        "Content-Type": "application/json",
        "Authorization": "Bearer " + apiKey,
    },
    body: JSON.stringify(data)
}

req(options, (err, res, body) => {
    if (!err && res.statusCode == 200) {
        console.log(body);
    } else {
        console.log("The request failed with status code: " + res.statusCode);
    }
});