0

我在 AutoML Entity Extraction 中创建了带注释的数据集。它已成功部署。如何通过Python使用google-cloud-automl库发出预测请求?

库已经有一个示例代码,但我对有效负载结构有点困惑

from google.cloud.automl_v1beta1 import PredictionServiceClient

client = PredictionServiceClient()
model_path = client.model_path('my-project-123', 'us-central', 'model-name')
payload = {...}
params = {'foo': 1}
response = client.predict(model_path, payload, params=params)

我查看了如何创建有效负载并找到了这个. 我想要一个句子的预测并得到它的结果。例如:“Tim Cook 是 Apple 的 CEO”,我想将此文本发送到 AutoML Entity Extraction 进行预测。

所以我挖了一下,发现了这个

我应该如何从 python 请求 AutoML 实体提取?

payload外观如何?的结构是model_path什么?

函数的第三个参数中的参数是client.predict什么?

4

1 回答 1

1

Google 已经在产品页面中发布了用于分析实体的文本片段的示例Python 代码。

# TODO(developer): Uncomment and set the following variables
# project_id = '[PROJECT_ID]'
# compute_region = '[COMPUTE_REGION]'
# model_id = '[MODEL_ID]'
# file_path = '/local/path/to/file'

from google.cloud import automl_v1beta1 as automl

automl_client = automl.AutoMlClient()

# Create client for prediction service.
prediction_client = automl.PredictionServiceClient()

# Get the full path of the model.
model_full_id = automl_client.model_path(
    project_id, compute_region, model_id
)

# Read the file content for prediction.
with open(file_path, "rb") as content_file:
    snippet = content_file.read()

# Set the payload by giving the content and type of the file.
payload = {"text_snippet": {"content": snippet, "mime_type": "text/plain"}}

# params is additional domain-specific parameters.
# currently there is no additional parameters supported.
params = {}
response = prediction_client.predict(model_full_id, payload, params)
print("Prediction results:")
for result in response.payload:
    print("Predicted entity label: {}".format(result.display_name))
    print("Predicted confidence score: {}".format(result.text_extraction.score))
    print("Predicted text segment: {}".format(result.text_extraction.text_segment.content))
    print("Predicted text segment start offset: {}".format(result.text_extraction.text_segment.start_offset))
    print("Predicted text segment end offset : {}".format(result.text_extraction.text_segment.end_offset))
    print("\n")
于 2019-08-29T06:07:10.927 回答