Python SDK提供TensorFlow、PyTorch和字串等多種輸入輸出格式的封裝,以及佇列服務的非同步呼叫能力。本文介紹各介面詳情並提供完整程式樣本。
關於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的"調用異常排查"章節。
完整的服務狀態代碼、錯誤資訊含義與處理建議請參見附錄:服務狀態代碼與常見報錯。