我已经在AutoML实体提取中创建了一个注意到的数据集。它已成功部署。如何使用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)我查看了如何创建有效负载并找到了这。我想要一个句子的预测,并得到结果。例如:“蒂姆·库克是苹果公司的首席执行官”,我想把这段文字发送给AutoML实体提取进行预测。
所以我挖了一下,找到了这。
我应该如何请求从python中提取AutoML实体?
payload看起来怎么样?model_path的结构是什么?
函数client.predict的第三个参数中的参数是什么
发布于 2019-08-29 06:07:10
谷歌已经在产品页面中发布了一个用于分析实体的文本片段的示例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")https://stackoverflow.com/questions/57703681
复制相似问题