如何对来自 AutoML 实体提取的文本片段发出预测请求?
How to make a prediction request over a text snippet from AutoML Entity Extraction?
我已经在 AutoML 实体提取中创建了带注释的数据集。它已成功部署。如何使用 google-cloud-automl
库从 Python
发出请求以发出预测请求?
库已经有 an example 代码,但我对有效载荷结构有点困惑
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)
我查看了如何创建负载并找到了 this。我想要对单个句子进行预测并获得结果。例如:'Tim Cook is the CEO of Apple',我想将此文本发送到 AutoML 实体提取进行预测。
所以我仔细研究了一下,发现 this。
我应该如何请求从 python 提取 AutoML 实体?
payload
长什么样子? model_path
的结构是什么?
函数client.predict
的第三个参数中的参数是什么?
Google 已经在用于分析实体的产品页面中发布了文本片段示例 python code。
# 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")
我已经在 AutoML 实体提取中创建了带注释的数据集。它已成功部署。如何使用 google-cloud-automl
库从 Python
发出请求以发出预测请求?
库已经有 an example 代码,但我对有效载荷结构有点困惑
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)
我查看了如何创建负载并找到了 this。我想要对单个句子进行预测并获得结果。例如:'Tim Cook is the CEO of Apple',我想将此文本发送到 AutoML 实体提取进行预测。
所以我仔细研究了一下,发现 this。
我应该如何请求从 python 提取 AutoML 实体?
payload
长什么样子? model_path
的结构是什么?
函数client.predict
的第三个参数中的参数是什么?
Google 已经在用于分析实体的产品页面中发布了文本片段示例 python code。
# 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")