翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
このチュートリアルでは、Amazon A2I で使用開始に使用できる API オペレーションについて説明します。
Jupyter Notebook を使用してこれらのオペレーションを実行するには、 から Jupyter Notebook を選択しAmazon A2I を使用したユースケースと例、 Amazon A2I Jupyter ノートブックで SageMaker ノートブックインスタンスを使用するを使用して SageMaker AI ノートブックインスタンスで使用する方法を確認します。
Amazon A2I で使用できる API オペレーションの詳細については、「Amazon Augmented AI で API を使用する」を参照してください。
プライベートワークチームを作成する
プライベートワークチームを作成し、自分自身をワーカーとして追加して Amazon A2I をプレビューできます。
Amazon Cognito に慣れていない場合は、SageMaker AI コンソールを使用してプライベートワークフォースを作成し、自分自身をプライベートワーカーとして追加することをお勧めします。手順については、ステップ 1: ワークチームを作成する を参照してください。
Amazon Cognito に精通している場合は、次の手順に従い、SageMaker API を使用してプライベートワークチームを作成できます。ワークチームを作成したら、ワークチーム ARN (WorkteamArn
) を確認します。
プライベートワークフォースと、利用可能な他の設定の詳細については、「プライベートワークフォース」を参照してください。
プライベートワークフォースを作成する
プライベートワークフォースを作成していない場合、Amazon Cognito ユーザープールを使用して作成できます。このユーザープールに自分自身を追加してください。create_workforce
関数を使用して AWS SDK for Python (Boto3) プライベートワークチームを作成できます。その他の言語固有の SDK については、「CreateWorkforce」のリストを参照してください。
response = client.create_workforce(
CognitoConfig={
"UserPool": "Pool_ID
",
"ClientId": "app-client-id
"
},
WorkforceName="workforce-name
"
)
プライベートワークチームを作成する
ヒューマンループを設定して開始するプライベートワークフォースを AWS リージョンに作成したら、 AWS SDK for Python (Boto3) create_workteam
関数を使用してプライベートワークチームを作成できます。その他の言語固有の SDK については、「CreateWorkteam
」のリストを参照してください。
response = client.create_workteam(
WorkteamName="work-team-name
",
WorkforceName= "workforce-name
",
MemberDefinitions=[
{
"CognitoMemberDefinition": {
"UserPool": "<aws-region>_ID
",
"UserGroup": "user-group
",
"ClientId": "app-client-id
"
},
}
]
)
次のように、ワークチーム ARN にアクセスします。
workteamArn = response["WorkteamArn"]
アカウント内のプライベートワークチームを一覧表示する
プライベートワークチームをすでに作成している場合は、 関数を使用して AWS SDK for Python (Boto3) list_workteams
、アカウント内の特定の AWS リージョンのすべてのワークチームを一覧表示できます。その他の言語固有の SDK については、「ListWorkteams
」のリストを参照してください。
response = client.list_workteams()
アカウントに多数のワークチームがある場合、MaxResults
、SortBy
、NameContains
を使用して結果をフィルタリングすることもできます。
人間によるレビューワークフローを作成する
Amazon A2I CreateFlowDefinition
オペレーションを使用して、人間によるレビューワークフローを作成できます。人間によるレビューワークフローを作成する前に、ヒューマンタスク UI を作成する必要があります。これは、CreateHumanTaskUi
オペレーションで実行できます。
Amazon Textract または Amazon Rekognition 統合で Amazon A2I を使用している場合、JSON を使用してアクティベーション条件を指定できます。
ヒューマンタスク UI を作成する
Amazon Textract または Amazon Rekognition 統合で使用する人間によるレビューワークフローを作成する場合、事前に作成されたワーカータスクテンプレートを使用して変更する必要があります。すべてのカスタム統合で独自のカスタムワーカータスクテンプレートを使用できます。次の表では、2 つの組み込み統合のワーカータスクテンプレートを使用してヒューマンタスク UI を作成する方法について説明します。テンプレートを独自のテンプレートに置き換えて、このリクエストをカスタマイズしてください。
このテンプレートの詳細については、「Amazon Textract のカスタムテンプレートの例」を参照してください。
template
= r"""
<script src="https://assets.crowd.aws/crowd-html-elements.js"></script>
{% capture s3_uri %}http://s3.amazonaws.com/{{ task.input.aiServiceRequest.document.s3Object.bucket }}/{{ task.input.aiServiceRequest.document.s3Object.name }}{% endcapture %}
<crowd-form>
<crowd-textract-analyze-document
src="{{ s3_uri | grant_read_access }}"
initial-value="{{ task.input.selectedAiServiceResponse.blocks }}"
header="Review the key-value pairs listed on the right and correct them if they don"t match the following document."
no-key-edit=""
no-geometry-edit=""
keys="{{ task.input.humanLoopContext.importantFormKeys }}"
block-types='["KEY_VALUE_SET"]'>
<short-instructions header="Instructions">
<p>Click on a key-value block to highlight the corresponding key-value pair in the document.
</p><p><br></p>
<p>If it is a valid key-value pair, review the content for the value. If the content is incorrect, correct it.
</p><p><br></p>
<p>The text of the value is incorrect, correct it.</p>
<p><img src="https://assets.crowd.aws/images/a2i-console/correct-value-text.png">
</p><p><br></p>
<p>A wrong value is identified, correct it.</p>
<p><img src="https://assets.crowd.aws/images/a2i-console/correct-value.png">
</p><p><br></p>
<p>If it is not a valid key-value relationship, choose No.</p>
<p><img src="https://assets.crowd.aws/images/a2i-console/not-a-key-value-pair.png">
</p><p><br></p>
<p>If you can’t find the key in the document, choose Key not found.</p>
<p><img src="https://assets.crowd.aws/images/a2i-console/key-is-not-found.png">
</p><p><br></p>
<p>If the content of a field is empty, choose Value is blank.</p>
<p><img src="https://assets.crowd.aws/images/a2i-console/value-is-blank.png">
</p><p><br></p>
<p><strong>Examples</strong></p>
<p>Key and value are often displayed next or below to each other.
</p><p><br></p>
<p>Key and value displayed in one line.</p>
<p><img src="https://assets.crowd.aws/images/a2i-console/sample-key-value-pair-1.png">
</p><p><br></p>
<p>Key and value displayed in two lines.</p>
<p><img src="https://assets.crowd.aws/images/a2i-console/sample-key-value-pair-2.png">
</p><p><br></p>
<p>If the content of the value has multiple lines, enter all the text without line break.
Include all value text even if it extends beyond the highlight box.</p>
<p><img src="https://assets.crowd.aws/images/a2i-console/multiple-lines.png"></p>
</short-instructions>
<full-instructions header="Instructions"></full-instructions>
</crowd-textract-analyze-document>
</crowd-form>
"""
上記のテンプレートを使用して、 AWS SDK for Python (Boto3) create_human_task_ui
関数を使用してテンプレートを作成できます。その他の言語固有の SDK については、「CreateHumanTaskUi
」のリストを参照してください。
response = client.create_human_task_ui(
HumanTaskUiName="human-task-ui-name
",
UiTemplate={
"Content": template
}
)
このレスポンス要素には、ヒューマンタスク UI ARN が含まれます。これを次のように保存します。
humanTaskUiArn = response["HumanTaskUiArn"]
JSON を作成してアクティベーション条件を指定する
Amazon Textract と Amazon Rekognition の組み込み統合では、アクティベーション条件を JSON オブジェクトに保存し、それを CreateFlowDefinition
リクエストで使用できます。
次に、タブを選択して、これらの組み込み統合に使用できるアクティベーション条件の例を確認します。アクティベーション条件オプションの詳細については、「Amazon Augmented AI のヒューマンループのアクティベーション条件に対する JSON スキーマ」を参照してください。
この例では、ドキュメント内の特定のキー (Mail address
など) の条件を指定します。Amazon Textract の信頼度がこちらで設定したしきい値を超えている場合、ドキュメントは人間によるレビュー用に送られ、ヒューマンループを開始した特定のキーによりそのワーカーにプロンプト表示されます。
import json
humanLoopActivationConditions = json.dumps(
{
"Conditions": [
{
"Or": [
{
"ConditionType": "ImportantFormKeyConfidenceCheck",
"ConditionParameters": {
"ImportantFormKey": "Mail address",
"ImportantFormKeyAliases": ["Mail Address:","Mail address:", "Mailing Add:","Mailing Addresses"],
"KeyValueBlockConfidenceLessThan": 100,
"WordBlockConfidenceLessThan": 100
}
},
{
"ConditionType": "MissingImportantFormKey",
"ConditionParameters": {
"ImportantFormKey": "Mail address",
"ImportantFormKeyAliases": ["Mail Address:","Mail address:","Mailing Add:","Mailing Addresses"]
}
},
{
"ConditionType": "ImportantFormKeyConfidenceCheck",
"ConditionParameters": {
"ImportantFormKey": "Phone Number",
"ImportantFormKeyAliases": ["Phone number:", "Phone No.:", "Number:"],
"KeyValueBlockConfidenceLessThan": 100,
"WordBlockConfidenceLessThan": 100
}
},
{
"ConditionType": "ImportantFormKeyConfidenceCheck",
"ConditionParameters": {
"ImportantFormKey": "*",
"KeyValueBlockConfidenceLessThan": 100,
"WordBlockConfidenceLessThan": 100
}
},
{
"ConditionType": "ImportantFormKeyConfidenceCheck",
"ConditionParameters": {
"ImportantFormKey": "*",
"KeyValueBlockConfidenceGreaterThan": 0,
"WordBlockConfidenceGreaterThan": 0
}
}
]
}
]
}
)
人間によるレビューワークフローを作成する
このセクションでは、前のセクションで作成したリソースを使用したCreateFlowDefinition
AWS SDK for Python (Boto3) リクエストの例を示します。その他の言語固有の SDK については、「CreateFlowDefinition」のリストを参照してください。次の表にあるタブで、Amazon Textract と Amazon Rekognition の組み込み統合に対する人間によるレビューワークフローの作成リクエストを確認します。
Amazon Textract との組み込み統合を使用する場合、HumanLoopRequestSource
で "AWS/Textract/AnalyzeDocument/Forms/V1"
と "AwsManagedHumanLoopRequestSource"
を指定する必要があります。
response = client.create_flow_definition(
FlowDefinitionName="human-review-workflow-name
",
HumanLoopRequestSource={
"AwsManagedHumanLoopRequestSource": "AWS/Textract/AnalyzeDocument/Forms/V1"
},
HumanLoopActivationConfig={
"HumanLoopActivationConditionsConfig": {
"HumanLoopActivationConditions": humanLoopActivationConditions
}
},
HumanLoopConfig={
"WorkteamArn": workteamArn
,
"HumanTaskUiArn": humanTaskUiArn
,
"TaskTitle": "Document entry review
",
"TaskDescription": "Review the document and instructions. Complete the task
",
"TaskCount": 1
,
"TaskAvailabilityLifetimeInSeconds": 43200
,
"TaskTimeLimitInSeconds": 3600
,
"TaskKeywords": [
"document review
",
],
},
OutputConfig={
"S3OutputPath": "s3://amzn-s3-demo-bucket/prefix/
",
},
RoleArn="arn:aws:iam::<account-number>:role/<role-name>
",
Tags=[
{
"Key": "string
",
"Value": "string
"
},
]
)
人間によるレビューワークフローを作成した後、レスポンスからフロー定義 ARN を取得できます。
humanReviewWorkflowArn
= response["FlowDefinitionArn"]
ヒューマンループを作成する
ヒューマンループの開始に使用する API オペレーションは、使用する Amazon A2I 統合によって異なります。
-
Amazon Textract 組み込み統合を使用する場合、AnalyzeDocument オペレーションを使用します。
-
Amazon Rekognition 組み込み統合を使用する場合、DetectModerationLabels オペレーションを使用します。
-
カスタム統合を使用する場合、StartHumanLoop オペレーションを使用します。
AWS SDK for Python (Boto3)を使用した Amazon Textract および Amazon Rekognition のリクエスト例については、次の表からタスクタイプを選択して参照してください。
次の例では、 を使用して us-west-2 analyze_document
で AWS SDK for Python (Boto3) を呼び出します。斜体の赤いテキストはリソースに置き換えます。Amazon Mechanical Turk ワークフォースを使用している場合は、DataAttributes
パラメータを含めます。詳細については、AWS SDK for Python (Boto) APIリファレンスの「analyze_document
response = client.analyze_document(
Document={"S3Object": {"Bucket": "amzn-s3-demo-bucket
", "Name": "document-name.pdf
"},
HumanLoopConfig={
"FlowDefinitionArn":"arn:aws:sagemaker:us-west-2:111122223333:flow-definition/flow-definition-name
",
"HumanLoopName":"human-loop-name
",
"DataAttributes" : {ContentClassifiers:["FreeOfPersonallyIdentifiableInformation"
|"FreeOfAdultContent"
]}
}
FeatureTypes=["FORMS"]
)
ヒューマンループは、Amazon Textract のドキュメント分析に対する信頼度タスクが人間によるレビューワークフローで指定したアクティベーション条件を満たしている場合にのみ作成されます。response
要素をチェックし、ヒューマンループが作成されたかどうかを判断できます。このレスポンスに含まれるものすべてを表示するには、HumanLoopActivationOutput
を確認します。
if "HumanLoopArn" in analyzeDocumentResponse["HumanLoopActivationOutput"]:
# A human loop has been started!
print(f"A human loop has been started with ARN: {analyzeDocumentResponse["HumanLoopActivationOutput"]["HumanLoopArn"]}"