首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对来自AutoML实体提取的文本片段提出预测请求?

如何对来自AutoML实体提取的文本片段提出预测请求?
EN

Stack Overflow用户
提问于 2019-08-29 05:43:47
回答 1查看 434关注 0票数 0

我已经在AutoML实体提取中创建了一个注意到的数据集。它已成功部署。如何使用Python库从google-cloud-automl发出请求以发出预测请求?

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

代码语言:javascript
复制
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的第三个参数中的参数是什么

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-29 06:07:10

谷歌已经在产品页面中发布了一个用于分析实体的文本片段的示例python代码

代码语言:javascript
复制
# 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")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57703681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档