自定义分类实时分析 (API) - Amazon Comprehend

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

自定义分类实时分析 (API)

您可以使用 Amazon Comprehend API 使用自定义模型运行实时分类。首先,创建一个终端节点来运行实时分析。创建终端节点后,您可以运行实时分类。

本节中的示例使用适用于 Unix、Linux 和 macOS 的命令格式。对于 Windows,请将每行末尾的反斜杠 (\) Unix 行继续符替换为脱字号 (^)。

有关配置终端节点吞吐量以及相关成本的信息,请参阅 使用 Amazon Comprehend 终端节点

为自定义分类创建终端节点

以下示例显示了使用CreateEndpoint的 API 操作 AWS CLI。

aws comprehend create-endpoint \ --desired-inference-units number of inference units \ --endpoint-name endpoint name \ --model-arn arn:aws:comprehend:region:account-id:model/example \ --tags Key=My1stTag,Value=Value1

Amazon Comprehend 的响应如下:

{ "EndpointArn": "Arn" }

运行实时自定义分类

为自定义分类模型创建终端节点后,您可以使用该终端节点运行 ClassifyDocumentAPI 操作。您可以使用 textbytes 参数提供文本输入。使用 bytes 参数输入其他输入类型。

对于图像文件和 PDF 文件,您可以使用 DocumentReaderConfig 参数来覆盖默认的文本提取操作。有关详细信息,请参阅 设置文本提取选项

为获得最佳结果,请将输入类型与分类器模型类型相匹配。如果您向纯文本模型提交原生文档,或者向原生文档模型提交纯文本文件,API 响应将包含一条警告。有关更多信息,请参阅 训练分类模型

使用 AWS Command Line Interface

以下示例将演示如何使用分类文档 CLI 命令。

使用对文本进行分类 AWS CLI

以下示例对文本块运行实时分类。

aws comprehend classify-document \ --endpoint-arn arn:aws:comprehend:region:account-id:endpoint/endpoint name \ --text 'From the Tuesday, April 16th, 1912 edition of The Guardian newspaper: The maiden voyage of the White Star liner Titanic, the largest ship ever launched ended in disaster. The Titanic started her trip from Southampton for New York on Wednesday. Late on Sunday night she struck an iceberg off the Grand Banks of Newfoundland. By wireless telegraphy she sent out signals of distress, and several liners were near enough to catch and respond to the call.'

Amazon Comprehend 的响应如下:

{ "Classes": [ { "Name": "string", "Score": 0.9793661236763 } ] }

使用对半结构化文档进行分类 AWS CLI

要分析 PDF、Word 或图像文件的自定义分类,请在 bytes 参数中包含输入文件运行 classify-document 的命令。

以下示例使用图像作为输入文件。它使用 fileb 选项对图像文件字节进行 base-64 编码。有关更多信息,请参阅《 AWS Command Line Interface 用户指南》中的二进制大型对象

此示例还传入名为 config.json 的 JSON 文件以设置文本提取选项。

$ aws comprehend classify-document \ > --endpoint-arn arn \ > --language-code en \ > --bytes fileb://image1.jpg \ > --document-reader-config file://config.json

config.json 文件包含以下代码。

{ "DocumentReadMode": "FORCE_DOCUMENT_READ_ACTION", "DocumentReadAction": "TEXTRACT_DETECT_DOCUMENT_TEXT" }

Amazon Comprehend 的响应如下:

{ "Classes": [ { "Name": "string", "Score": 0.9793661236763 } ] }

有关更多信息,请参阅ClassifyDocument亚马逊 Comprehend API 参考》。