本文介绍官方Python SDK接口详情,并提供常见输入输出示例和完整程序示例。
关于SDK的适用场景、调用原理,请参见服务调用SDK。
前置准备
pip install -U eas-prediction --user快速上手
根据您的模型输入数据格式,选择对应的Request类。以下是最小的字符串请求端到端调用示例,更多请参见程序示例:
#!/usr/bin/env python
from eas_prediction import PredictClient
from eas_prediction import StringRequest
if __name__ == '__main__':
client = PredictClient('http://182848887922****.cn-shanghai.pai-eas.aliyuncs.com', 'my_service')
client.set_token('YOUR_SERVICE_TOKEN')
client.init()
request = StringRequest('[{}]')
resp = client.predict(request)
print(resp)接口列表
Python SDK 提供以下接口类,按用途分为三组:
分组 | 类说明 |
客户端主类 | PredictClient:配置服务信息(Endpoint / service_name / Token)、发送请求、接收响应。 |
输入输出 |
|
队列服务 |
|
公共参数说明
endpoint:服务端的Endpoint地址。
如果是普通服务,则设置为默认网关Endpoint。例如
182848887922***.cn-shanghai.pai-eas.aliyuncs.com。如果是VPC高速直连请求,请设置为该服务的VPC直连Endpoint,格式为
<uid>.vpc.<region-id>.pai-eas.aliyuncs.com,例如182848887922****.vpc.cn-shanghai.pai-eas.aliyuncs.com。
类PredictClient
接口 | 描述 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 对PredictClient对象进行初始化。在上述设置参数的接口执行完成后,需要调用 |
|
|
类StringRequest
接口 | 描述 |
|
|
类StringResponse
接口 | 描述 |
|
|
类TFRequest
接口 | 描述 |
|
|
|
|
|
|
|
|
类TFResponse
接口 | 描述 |
|
|
|
|
类TorchRequest
接口 | 描述 |
| TorchRequest类的构造方法。 |
|
|
|
|
|
|
类TorchResponse
接口 | 描述 |
|
|
|
|
类QueueClient
接口 | 描述 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
类Watcher
接口 | 描述 |
|
|
| 功能:关闭一个Watcher对象,用于关闭后端的数据连接。 说明 一个客户端只能启动一个Watcher对象,使用完成后需要将该对象关闭才能启动新的Watcher对象。 |
程序示例
同步推理示例(按输入输出格式)
根据服务的输入输出类型,选择对应的示例代码。
字符串
对于使用自定义Processor部署服务的用户而言,通常采用字符串进行服务调用(例如,PMML模型服务的调用),具体的Demo程序如下。
#!/usr/bin/env python
from eas_prediction import PredictClient
from eas_prediction import StringRequest
if __name__ == '__main__':
client = PredictClient('http://182848887922****.cn-shanghai.pai-eas.aliyuncs.com', 'scorecard_pmml_example')
client.set_token('YWFlMDYyZDNmNTc3M2I3MzMwYmY0MmYwM2Y2MTYxMTY4NzBkNzdj****')
client.init()
request = StringRequest('[{"fea1": 1, "fea2": 2}]')
for x in range(0, 1000000):
resp = client.predict(request)
print(resp)TensorFlow
使用TensorFlow的用户,需要将TFRequest和TFResponse分别作为输入和输出数据格式,具体Demo示例如下。
#!/usr/bin/env python
from eas_prediction import PredictClient
from eas_prediction import StringRequest
from eas_prediction import TFRequest
if __name__ == '__main__':
client = PredictClient('http://182848887922****.cn-shanghai.pai-eas.aliyuncs.com', 'mnist_saved_model_example')
client.set_token('YTg2ZjE0ZjM4ZmE3OTc0NzYxZDMyNmYzMTJjZTQ1YmU0N2FjMTAy****')
client.init()
#request = StringRequest('[{}]')
req = TFRequest('predict_images')
req.add_feed('images', [1, 784], TFRequest.DT_FLOAT, [1] * 784)
for x in range(0, 1000000):
resp = client.predict(req)
print(resp)PyTorch
使用PyTorch的用户,需要将TorchRequest和TorchResponse分别作为输入和输出数据格式,具体Demo示例如下。
#!/usr/bin/env python
from eas_prediction import PredictClient
from eas_prediction import TorchRequest
if __name__ == '__main__':
client = PredictClient('http://182848887922****.cn-shanghai.pai-eas.aliyuncs.com', 'pytorch_gpu_wl')
client.init()
req = TorchRequest()
req.add_feed(0, [1, 3, 224, 224], TorchRequest.DT_FLOAT, [1] * 150528)
# req.add_fetch(0)
import time
st = time.time()
timer = 0
for x in range(0, 10):
resp = client.predict(req)
timer += (time.time() - st)
st = time.time()
print(resp.get_tensor_shape(0))
# print(resp)
print("average response time: %s s" % (timer / 10) )BladeProcessor
使用BladeProcessor的用户,需要将BladeRequest和BladeResponse分别作为输入和输出数据格式,具体Demo示例如下。
#!/usr/bin/env python
from eas_prediction import PredictClient
from eas_prediction import BladeRequest
if __name__ == '__main__':
client = PredictClient('http://182848887922****.cn-shanghai.pai-eas.aliyuncs.com', 'nlp_model_example')
client.init()
req = BladeRequest()
req.add_feed('input_data', 1, [1, 360, 128], BladeRequest.DT_FLOAT, [0.8] * 85680)
req.add_feed('input_length', 1, [1], BladeRequest.DT_INT32, [187])
req.add_feed('start_token', 1, [1], BladeRequest.DT_INT32, [104])
req.add_fetch('output', BladeRequest.DT_FLOAT)
import time
st = time.time()
timer = 0
for x in range(0, 10):
resp = client.predict(req)
timer += (time.time() - st)
st = time.time()
# print(resp)
# print(resp.get_values('output'))
print(resp.get_tensor_shape('output'))
print("average response time: %s s" % (timer / 10) )兼容默认TensorFlow接口的BladeProcessor
BladeProcessor用户可以使用兼容EAS默认TensorFlow接口的TFRequest与TFResponse作为数据的输入输出格式,具体Demo示例如下。
#!/usr/bin/env python
from eas_prediction import PredictClient
from eas_prediction.blade_tf_request import TFRequest # Need Importing blade TFRequest
if __name__ == '__main__':
client = PredictClient('http://182848887922****.cn-shanghai.pai-eas.aliyuncs.com', 'nlp_model_example')
client.init()
req = TFRequest(signature_name='predict_words')
req.add_feed('input_data', [1, 360, 128], TFRequest.DT_FLOAT, [0.8] * 85680)
req.add_feed('input_length', [1], TFRequest.DT_INT32, [187])
req.add_feed('start_token', [1], TFRequest.DT_INT32, [104])
req.add_fetch('output')
import time
st = time.time()
timer = 0
for x in range(0, 10):
resp = client.predict(req)
timer += (time.time() - st)
st = time.time()
# print(resp)
# print(resp.get_values('output'))
print(resp.get_tensor_shape('output'))
print("average response time: %s s" % (timer / 10) )通过VPC网络直连方式调用服务的示例
通过网络直连方式,需要为服务配置专有网络,请参见使用EAS资源组和EAS访问公网或内网资源。该调用方式与普通调用方式相比,仅需增加一行代码client.set_endpoint_type(ENDPOINT_TYPE_DIRECT)即可,特别适合大流量高并发的服务,具体示例如下。
#!/usr/bin/env python
from eas_prediction import PredictClient
from eas_prediction import StringRequest
from eas_prediction import TFRequest
from eas_prediction import ENDPOINT_TYPE_DIRECT
if __name__ == '__main__':
# VPC高速直连Endpoint格式:<uid>.vpc.<region-id>.pai-eas.aliyuncs.com,可在EAS控制台服务详情页的"调用信息"中查看
client = PredictClient('http://182848887922****.vpc.cn-hangzhou.pai-eas.aliyuncs.com', 'mnist_saved_model_example')
client.set_token('M2FhNjJlZDBmMzBmMzE4NjFiNzZhMmUxY2IxZjkyMDczNzAzYjFi****')
client.set_endpoint_type(ENDPOINT_TYPE_DIRECT)
client.init()
request = TFRequest('predict_images')
request.add_feed('images', [1, 784], TFRequest.DT_FLOAT, [1] * 784)
for x in range(0, 1000000):
resp = client.predict(request)
print(resp)队列服务发送、订阅数据示例
通过QueueClient可向队列服务中发送数据、查询数据、查询队列服务的状态以及订阅队列服务中的数据推送。以下方的Demo为例,介绍一个线程向队列服务中推送数据,另外一个线程通过Watcher订阅队列服务中推送过来的数据。
#!/usr/bin/env python
from eas_prediction import QueueClient
import threading
if __name__ == '__main__':
endpoint = '182848887922****.cn-shanghai.pai-eas.aliyuncs.com'
queue_name = 'test_group.qservice/sink'
token = 'YmE3NDkyMzdiMzNmMGM3ZmE4ZmNjZDk0M2NiMDA3OTZmNzc1MTUx****'
queue = QueueClient(endpoint, queue_name)
queue.set_token(token)
queue.init()
queue.set_timeout(30000)
# truncate all messages in the queue
attributes = queue.attributes()
if 'stream.lastEntry' in attributes:
queue.truncate(int(attributes['stream.lastEntry']) + 1)
count = 100
# create a thread to send messages to the queue
def send_thread():
for i in range(count):
index, request_id = queue.put('[{}]')
print('send: ', i, index, request_id)
# create a thread to watch messages from the queue
def watch_thread():
watcher = queue.watch(0, 5, auto_commit=True)
i = 0
for x in watcher.run():
print('recv: ', i, x.index, x.tags['requestId'])
i += 1
if i == count:
break
watcher.close()
thread1 = threading.Thread(target=watch_thread)
thread2 = threading.Thread(target=send_thread)
thread1.start()
thread2.start()
thread1.join()
thread2.join()常见问题排查
Python SDK调用异常的现象、原因与排查方向(鉴权、路由、连接、服务端类等共性问题)请参见服务调用SDK的"调用异常排查"章节。
完整的服务状态码、错误信息含义与处理建议请参见附录:服务状态码与常见报错。