All Products
Search
Document Center

Model serving

Last Updated: Jun 01, 2020

Deploy Auto Learning models

After you generate an Auto Learning model, you can go to Elastic Algorithm Service (EAS) and deploy the model as an online service. The model service can be called through its API from the Internet.

1. Go to EAS to deploy the model.

On the model testing page of Auto Learning, click Go to EAS. You are then navigated to the EAS page.

2. Select a resource type for the model service.

EAS has a built-in Auto Learning processor, allowing you to deploy Auto Learning models with CPU or GPU resources.

3. Enter the model name and select resources.

Enter the name of the model, select resources, and click Next to deploy the model.After you complete the configuration, the model is deployed.

Call the deployed Auto Learning model

1. Find the Auto Learning model to be called.

In the deployed models list, find the Auto Learning model that you want to call, click Call Info in the Call Method column, as shown in the following figure.

2. Obtain the endpoint of the model.

On the Call Info page, click Generate a public endpoint, as shown in the following figure.

3. Call the model to classify images.

After you complete the preceding steps, the API of the model service is generated. You can call the model to classify images with the URL and key pair information of the model service. You can choose to use the public endpoint or a VPC-specific endpoint to call the model.

  • Use the public endpoint to call the model (commonly used).

    Descriptions about the code for calling the model from the Internet:

  1. Environment: python2
  2. Dependency package: pai_tf_predict_proto$ pip install http://eas-data.oss-cn-shanghai.aliyuncs.com/sdk/pai_tf_predict_proto-1.0-py2.py3-none-any.whl
  3. The API Gateway SDK is required: https://github.com/aliyun/api-gateway-demo-sign-python

Sample code:

  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import json
  4. from urlparse import urlparse
  5. from com.aliyun.api.gateway.sdk import client
  6. from com.aliyun.api.gateway.sdk.http import request
  7. from com.aliyun.api.gateway.sdk.common import constant
  8. from pai_tf_predict_proto import tf_predict_pb2
  9. import numpy as np
  10. #predict.jpeg represents the name of the image to be classified.
  11. with open('predict.jpeg', 'rb') as infile:
  12. buf = infile.read()
  13. def predict(url, app_key, app_secret, request_data):
  14. cli = client.DefaultClient(app_key=app_key, app_secret=app_secret)
  15. body = request_data
  16. url_ele = urlparse(url)
  17. host = 'http://' + url_ele.hostname
  18. path = url_ele.path
  19. headers = {'content-type':'charset=utf-8'}
  20. req_post = request.Request(host=host, headers = headers,protocol=constant.HTTP,url=path, method="POST", time_out=6000)
  21. req_post.set_body(body)
  22. req_post.set_content_type(constant.CONTENT_TYPE_STREAM)
  23. stat,header, content = cli.execute(req_post)
  24. return stat, dict(header) if header is not None else {}, content
  25. def demo():
  26. # Specify the model information, including the URL, appKey, and appSecret, as shown in the preceding figure.
  27. app_key = '250477'
  28. app_secret = 'd4e17c1a866ae2a2246fbe23ee8'
  29. url = 'http://3804146ff9844364b0c3d19c821ea229-cn-beijing.alicloudapi.com/EAPI_1295715995194599_autodl_bj_xzh004'
  30. request_data = buf
  31. stat, header, content = predict(url, app_key, app_secret, request_data)
  32. if stat != 200:
  33. print('Http status code: ', stat)
  34. print('Error msg in header: ', header['x-ca-error-message'] if 'x-ca-error-message' in header else '')
  35. print('Error msg in body: ', content)
  36. else:
  37. print(content)
  38. if __name__ == '__main__':
  39. demo()
  • Use a VPC-specific endpoint to call the model
  1. ###The following example shows how to call the model by using a VPC-specific endpoint. You must make sure that the client where you call the model and the model are deployed in VPC networks in the same region.
  2. import requests
  3. import base64
  4. # Set the URL to the endpoint shown on the VPC-specific Endpoint tab.
  5. url="http://pai-eas-vpc.cn-beijing.aliyuncs.com/api/predict/autodl_bj_xzh004"
  6. # Specify the key pair information in the Authorization parameter. To check the key pair information, select your model service, click Call Info, and then click the VPC-specific Endpoint tab.
  7. headers = {"Authorization":"YmJWRjM1OWMxOTViZjJmM2I4NjZlODFiNg=="}
  8. # You can set the image_url parameter to an OSS endpoint where your images are stored. You can also pass Base64-encoded images into the data parameter.
  9. image_url="https://bj-xzh.oss-cn-beijing.aliyuncs.com/Animals/bee_04.jpg"
  10. data = base64.b64encode(requests.get(image_url).content)
  11. resp = requests.post(url, data=data, headers=headers)
  12. if resp.status_code != 200:
  13. print(resp.content)
  14. else:
  15. response = predict_pb2.PredictResponse()
  16. response.ParseFromString(content)
  17. print(response)