Elastic Algorithm Service (EAS) allows you to call a service over a common public endpoint by using the official SDK for Python, official SDK for Java, or custom call logic. EAS also allows you to call a service over a public endpoint that is generated by API Gateway. This topic describes these call methods in detail.

EAS allows you to use one of the following methods to call a service over a common public endpoint that does not belong to a virtual private cloud (VPC):
  • Call a service over a common public endpoint
    • By using the official SDK for Python
    • By using the official SDK for Java
    • By using custom call logic
  • Call a service over a public endpoint generated by API Gateway
If you have no special requirements, you can call a service over a common public endpoint. If you use API Gateway to create an API operation for a service and generate a public endpoint for the API operation, you can call the API operation over the public endpoint that is generated by API Gateway.

Call a service over a common public endpoint

After a model is deployed as a service in EAS, a public endpoint is automatically generated for the service. You can perform the following steps to obtain the public endpoint of a service: On the Elastic Algorithm Service page, click Invoke Intro in the Service Method column of the service that you want to call. In the Invoke Intro dialog box, click the Public Network Invoke tab and check the values of the Access address and Token parameters. Based on the call information, you can perform a call test. The following code provides an example on a call test:
$ curl http://166408185518****.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/heart_predict_online -H 'Authorization: ZGI1YWYxNmQwYjMzMDM1YTNlMmFmNmEzYjIzZTVlZGQ0NDJhYTRm****' -d '[{"sex":0,"cp":0,"fbs":0,"restecg":0,"exang":0,"slop":0,"thal":0,"age":0,"trestbps":0,"chol":0,"thalach":0,"oldpeak":0,"ca":0}]'
After the call test succeeds, you can call the service. To facilitate service calls, EAS allows you to use one of the following methods to call a service over a common public endpoint:

Use the official SDK for Python

  1. Install the SDK.
    pip install -U eas-prediction --user
    For more information about how to use the SDK for Python, visit GitHub.
  2. Compile a call program.
    Take a program that uses strings as input and output as an example. For information about sample programs with other input and output formats, such as TensorFlow and PyTorch programs, visit GitHub.
    #!/usr/bin/env python
    from eas_prediction import PredictClient
    from eas_prediction import StringRequest
    if __name__ == '__main__':
        # Set the input of client = PredictClient() to the public endpoint that is used to call the service.
        client = PredictClient('http://166408185518****.cn-hangzhou.pai-eas.aliyuncs.com','heart_predict_online')
        # Use the token that is obtained on the Public Network Invoke tab. For more information, see the "Call a service over a common public endpoint" section of this topic.
        client.set_token('ZGI1YWYxNmQwYjMzMDM1YTNlMmFmNmEzYjIzZTVlZGQ0NDJhYTRm****')    
        client.init()
        # Construct the request input based on the model that you want to use. In this example, the input and output are of the string type.
        request = StringRequest('[{"sex":0,"cp":0,"fbs":0,"restecg":0,"exang":0,"slop":0,"thal":0,"age":0,"trestbps":0,"chol":0,"thalach":0,"oldpeak":0,"ca":0}]')    
        for x in range(0, 1):
            resp = client.predict(request)
            print(resp)
    The input of the client = PredictClient() function is the public endpoint that is used to call the service. For example, if the public endpoint is http://166408185518****.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/heart_predict_online, the format that is used to call the PredictClient() function is client = PredictClient('http://166408185518****.cn-hangzhou.pai-eas.aliyuncs.com','heart_predict_online').
  3. Run the call program.
    $ python heart_predict.py
    The heart_predict.py parameter specifies the name of the Python program. You can replace it with the actual program name. The following prediction result is returned after the call program is run:
    IT-C02YJ0V8JHD2:Desktop wowei$ python heart_predict.py
    [{"p_0":0.9941226679708888,"p_1":0.005877332029111252}]

Use the official SDK for Java

  1. Add the required dependency.
    You must use Maven to manage projects when you define code that runs on a Java client. Therefore, you must add the client dependency eas-sdk to the pom.xml file. The latest version of the dependency is 2.0.1. The following sample code shows how to add the dependency:
    <dependency>
      <groupId>com.aliyun.openservices.eas</groupId>
      <artifactId>eas-sdk</artifactId>
      <version>2.0.1</version>
    </dependency>
    For more information about how to use the SDK for Java, visit GitHub.
  2. Compile a call program.
    Take a program that uses strings as input and output as an example. For information about sample programs with other input and output formats, such as TensorFlow and PyTorch programs, visit GitHub.
    import com.aliyun.openservices.eas.predict.http.PredictClient;
    import com.aliyun.openservices.eas.predict.http.HttpConfig;
    public class Test_String {
        public static void main(String[] args) throws Exception{
            // Start and initialize the client.        
            PredictClient client = new PredictClient(new HttpConfig());
            // Use the token that is obtained on the Public Network Invoke tab. For more information, see the "Call a service over a common public endpoint" section of this topic.    
            client.setToken("ZGI1YWYxNmQwYjMzMDM1YTNlMmFmNmEzYjIzZTVlZGQ0NDJhYTRm****");
            // Set the endpoint to the public endpoint that is used to call the service.
            client.setEndpoint("http://166408185518****.cn-hangzhou.pai-eas.aliyuncs.com");
            // Set the service name.
            client.setModelName("heart_predict_online"); 
            // Define the input string. Construct the request input based on the model that you want to use. In this example, the input and output are of the string type.
            String request = "[{\"sex\":0,\"cp\":0,\"fbs\":0,\"restecg\":0,\"exang\":0,\"slop\":0,\"thal\":0,\"age\":0,\"trestbps\":0,\"chol\":0,\"thalach\":0,\"oldpeak\":0,\"ca\":0}]";
            System.out.println(request);  
            // Return a string by using EAS.      
            try {
                String response = client.predict(request);
                System.out.println(response);
            } catch(Exception e) {
                e.printStackTrace();
            }
            // Shut down the client.
            client.shutdown();
            return;
        }
    }
    The input of the client.setEndpoint() and client.setModelName() functions is the public endpoint that is used to call the service. For example, if the public endpoint is http://166408185518****.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/heart_predict_online, the format that is used to call the client.setEndpoint() function is client.setEndpoint("http://166408185518****.cn-hangzhou.pai-eas.aliyuncs.com"), and the format that is used to call the client.setModelName() function is client.setModelName("heart_predict_online").
  3. Run the call program.
    The following result is returned after the call program is run:
    [{"p_0":0.9941226679708888,"p_1":0.005877332029111252}]

Use custom call logic

EAS supports custom call logic in Python, Java, and other languages. You must customize service requests based on specific frameworks. For more information, see Construct requests for services based on a universal processor. The following code shows an example on how to define call logic:
import requests
# Use the public endpoint that is obtained on the Public Network Invoke tab as the URL. For more information, see the "Call a service over a common public endpoint" section of this topic.
url = 'http://166408185518****.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/heart_predict_online'
# Add the token that is obtained on the Public Network Invoke tab to the header. For more information, see the "Call a service over a common public endpoint" section of this topic.
headers = {"Authorization": 'ZGI1YWYxNmQwYjMzMDM1YTNlMmFmNmEzYjIzZTVlZGQ0NDJhYTRm****'}
# Construct the service request based on the data format required by the model that you want to use. In this example, the input and output are of the string type.
data = '[{"sex":0,"cp":0,"fbs":0,"restecg":0,"exang":0,"slop":0,"thal":0,"age":0,"trestbps":0,"chol":0,"thalach":0,"oldpeak":0,"ca":0}]'
resp = requests.post(url, data=data, headers=headers)
print resp
print resp.content

Call a service over a public endpoint generated by API Gateway

  • Prerequisites

    API Gateway is activated.

  • Call methods
    1. Log on to the Machine Learning Platform for AI console.
    2. In the left-side navigation pane, choose Model Deployment > EAS-Model Serving.
    3. On the Elastic Algorithm Service page, click Invoke Intro in the Service Method column of the service that you want to call.
    4. In the Invoke Intro dialog box, click Click to Create on the Public Network Invoke tab.
      Automatically create an API Gateway instanceEAS automatically creates an API operation for the service and generates a public endpoint on API Gateway. You can call the service by using this public endpoint, as shown in the following figure. API endpointThe API operation is managed by API Gateway. Then, you can call the API operation. You can implement the call code by yourself or refer to the following guidelines:
      • Log on to the API Gateway console. In the left-side navigation pane, choose Publish APIs > APIs. On the API List page, switch to the region of your service, find the API operation of your service, and then check the group of the API operation.
      • Log on to the API Gateway console. In the left-side navigation pane, choose Publish APIs > Owned APIs SDK. On the Owned APIs SDK Auto-Generation page, switch to the region of your service, find the group of the API operation of your service, and then download the SDK in the language that you need. Then, you can develop a call program in the specified language based on the SDK.
      For more information about service calls, see Call an API.
  • Advanced features based on API Gateway
    • View the information about an API operation
      You can perform the following steps to view the information about an API operation:
      1. Log on to the API Gateway console.
      2. In the left-side navigation pane, choose Publish APIs > APIs.
      3. On the API List page, find the API operation that you want to view, click More in the Operation column, and then select Manage.
    • Authorize applications for an API operation

      Only authorized identities can call API operations. These identities are known as applications. When EAS creates an API operation, an application is automatically created and authorized for the API operation. If you want to create other applications, you must manually authorize them. For more information, see 4. Create and authorize an application.

    • Bind an API group to a domain name

      An API operation belongs to an API group. You can manage API operations by group. An API group is assigned a second-level domain name by default. This second-level domain name is only for debugging. The domain name can be accessed up to 1,000 times per day. If you require more accesses, you can bind a domain name to the API group. For more information, see 1. Procedure for binding a domain name to an API group.