這一項目是怎樣工作的?具體而言,每當用戶運行 cortex deploy 時,命令行將配置屬性和代碼發送到服務器集群上。每個模型都載入到一個 Docker 容器中,包括相關的 Python 包和處理請求的代碼。模型通過網絡服務,如 Elastic Load Balancing (ELB)、Flask、TensorFlow Serving 和 ONNX Runtime 公開 API 給用戶使用。容器通過 Elastic Kubernetes Service (EKS) 進行控制,而日志文件和評價指標的記錄和打印工作由 CloudWatch 完成。

使用方法

使用過程主要分為以下三個步驟:

定義模型的 API

# predictor.py

model = download_my_model()
def predict(sample, metadata):
return model.predict(sample["text"])

如上所示,用戶需要做的是定義代表這個 API 的函數,使其能夠根據輸入數據返回輸出。

配置部署

# cortex.yaml

- kind: deploymentname: sentiment

- kind: apiname: classifierpredictor:
path: predictor.pytracker:
model_type: classificationcompute:
gpu: 1

第二個步驟中,用戶需要創建一個新的 yaml 文件,這個文件用于配置相關屬性。具體而言,用戶可以定義部署模型的名稱,本例中名為 classifierpredictor。然后還需要定義 API 的名稱,如 classifierpredictor 以及路徑、模型的類型和使用的 GPU 數量等。

AWS 部署

$ cortex deploy

creating classifier (http://***.amazonaws.com/sentiment/classifier)

以 AWS 為例,以上工作完成后,用戶可以創建這個 API,使其利用 AWS 進行工作。

當然,用戶還可以實時保存推斷結果,如下所示:

$ curl http://***.amazonaws.com/sentiment/classifier \
-X POST -H "Content-Type: application/json" \
-d '{"text": "the movie was great!"}'

positive

此外,用戶還可以監控運行結果。

$ 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

使用教程

為了讓用戶更好地使用這一工具,項目作者同時還提供了一些使用案例。包括:

以使用 BERT 進行情感分析為例:首先用戶需要在模型上定義 API 接口函數,使其可以通過函數輸入數據,并返回模型的推理結果。這一 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 文件,在文件中指定相關屬性,這里需要注意,文件名必須定義為 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 文件)。

然后進行部署即可:

$ cortex deploy

deployment started

如果需要獲得監控信息,則需要輸入 cortex get 命令:

$ cortex get classifier --watch

status up-to-date available requested last update avg latency
live 1 1 1 8s

還可以用命令行獲得實時預測:

$ 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

文章轉自微信公眾號@機器之心

上一篇:

Web Audio API實現簡單變聲效果

下一篇:

用 Flair 輕松實現強大文本分析與深度學習應用
#你可能也喜歡這些API文章!

我們有何不同?

API服務商零注冊

多API并行試用

數據驅動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內容創意新穎性、情感共鳴力、商業轉化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費