
如何快速實(shí)現(xiàn)REST API集成以優(yōu)化業(yè)務(wù)流程
這一項(xiàng)目是怎樣工作的?具體而言,每當(dāng)用戶運(yùn)行 cortex deploy 時(shí),命令行將配置屬性和代碼發(fā)送到服務(wù)器集群上。每個(gè)模型都載入到一個(gè) Docker 容器中,包括相關(guān)的 Python 包和處理請(qǐng)求的代碼。模型通過(guò)網(wǎng)絡(luò)服務(wù),如 Elastic Load Balancing (ELB)、Flask、TensorFlow Serving 和 ONNX Runtime 公開 API 給用戶使用。容器通過(guò) Elastic Kubernetes Service (EKS) 進(jìn)行控制,而日志文件和評(píng)價(jià)指標(biāo)的記錄和打印工作由 CloudWatch 完成。
使用過(guò)程主要分為以下三個(gè)步驟:
# predictor.py
model = download_my_model()
def predict(sample, metadata):
return model.predict(sample["text"])
如上所示,用戶需要做的是定義代表這個(gè) API 的函數(shù),使其能夠根據(jù)輸入數(shù)據(jù)返回輸出。
# cortex.yaml
- kind: deploymentname: sentiment
- kind: apiname: classifierpredictor:
path: predictor.pytracker:
model_type: classificationcompute:
gpu: 1
第二個(gè)步驟中,用戶需要?jiǎng)?chuàng)建一個(gè)新的 yaml 文件,這個(gè)文件用于配置相關(guān)屬性。具體而言,用戶可以定義部署模型的名稱,本例中名為 classifierpredictor。然后還需要定義 API 的名稱,如 classifierpredictor 以及路徑、模型的類型和使用的 GPU 數(shù)量等。
$ cortex deploy
creating classifier (http://***.amazonaws.com/sentiment/classifier)
以 AWS 為例,以上工作完成后,用戶可以創(chuàng)建這個(gè) API,使其利用 AWS 進(jìn)行工作。
當(dāng)然,用戶還可以實(shí)時(shí)保存推斷結(jié)果,如下所示:
$ curl http://***.amazonaws.com/sentiment/classifier \
-X POST -H "Content-Type: application/json" \
-d '{"text": "the movie was great!"}'
positive
此外,用戶還可以監(jiān)控運(yùn)行結(jié)果。
$ cortex get classifier --watch
status up-to-date available requested last update avg latency
live 1 1 1 8s 123ms
class count
positive 8
negative 4
為了讓用戶更好地使用這一工具,項(xiàng)目作者同時(shí)還提供了一些使用案例。包括:
以使用 BERT 進(jìn)行情感分析為例:首先用戶需要在模型上定義 API 接口函數(shù),使其可以通過(guò)函數(shù)輸入數(shù)據(jù),并返回模型的推理結(jié)果。這一 py 文件被定義為 handler.py:
# handler.py
import tensorflow as tf
import tensorflow_hub as hub
from bert import tokenization, run_classifier
labels = ["negative", "positive"]
with tf.Graph().as_default():
bert_module = hub.Module("https://tfhub.dev/google/bert_uncased_L-12_H-768_A-12/1")
info = bert_module(signature="tokenization_info", as_dict=True)
with tf.Session() as sess:
vocab_file, do_lower_case = sess.run([info["vocab_file"], info["do_lower_case"]])
tokenizer = tokenization.FullTokenizer(vocab_file=vocab_file, do_lower_case=do_lower_case)
def pre_inference(sample, signature, metadata):
input_example = run_classifier.InputExample(guid="", text_a=sample["review"], label=0)
input_feature = run_classifier.convert_single_example(0, input_example, [0, 1], 128, tokenizer)
return {"input_ids": [input_feature.input_ids]}
def post_inference(prediction, signature, metadata):
return labels[prediction["labels"][0]]
接著,用戶需要定義配置 yaml 文件,在文件中指定相關(guān)屬性,這里需要注意,文件名必須定義為 cortex.yaml:
# cortex.yaml
- kind: deployment
name: sentiment
- kind: api
name: classifier
tensorflow:
model: s3://cortex-examples/tensorflow/sentiment-analysis/bert
request_handler: handler.py
tracker:
model_type: classification
從中可以看到,yaml 文件中需要指定出使用的模型,以及作為 API 接口的 py 文件(即第一步中定義了的 py 文件)。
然后進(jìn)行部署即可:
$ cortex deploy
deployment started
如果需要獲得監(jiān)控信息,則需要輸入 cortex get 命令:
$ cortex get classifier --watch
status up-to-date available requested last update avg latency
live 1 1 1 8s
還可以用命令行獲得實(shí)時(shí)預(yù)測(cè):
$ cortex get classifier
url: http://***.amazonaws.com/sentiment/classifier
$ curl http://***.amazonaws.com/sentiment/classifier \
-X POST -H "Content-Type: application/json" \
-d '{"review": "The movie was great!"}'"positive
文章轉(zhuǎn)自微信公眾號(hào)@機(jī)器之心
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)