

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# ヒューマンループを作成および開始する
<a name="a2i-start-human-loop"></a>

*ヒューマンループ*は、ヒューマンレビューワークフローを開始し、ヒューマンワーカーにデータレビュータスクを送信します。Amazon A2I 組み込みタスクタイプのいずれかを使用すると、フロー定義で指定された条件が満たされると、対応する AWS サービスがユーザーに代わってヒューマンループを作成して開始します。フロー定義で条件が指定されていない場合、オブジェクトごとにヒューマンループが作成されます。カスタムタスクに Amazon A2I を使用する場合、アプリケーションで `StartHumanLoop` が呼び出された時点で、ヒューマンループが開始されます。

次の手順に従って、Amazon Rekognition または Amazon Textract 組み込みタスクタイプとカスタムタスクタイプでヒューマンループを構成します。

**前提条件**

ヒューマンループを作成して開始するには、ヒューマンループを設定または開始する AWS Identity and Access Management (IAM) ユーザーまたはロールに`AmazonAugmentedAIFullAccess`ポリシーをアタッチする必要があります。これは、組み込みタスクタイプに `HumanLoopConfig` を使用してヒューマンループを設定するために使用する ID になります。カスタムタスクタイプの場合、`StartHumanLoop` を呼び出す際に使用する ID になります。

さらに、組み込みタスクタイプを使用する場合、ユーザーまたはロールには、タスクタイプに関連付けられた AWS サービスの API オペレーションを呼び出すアクセス許可が必要です。例えば、Augmented AI で Amazon Rekognition を使用する場合、`DetectModerationLabels` を呼び出すのに必要なアクセス許可をアタッチする必要があります。これらのアクセス許可を付与するために使用できるアイデンティティベースのポリシーの例については、「[Amazon Rekognition アイデンティティベースのポリシーの例](https://docs.aws.amazon.com/rekognition/latest/dg/security_iam_id-based-policy-examples.html)」と「[Amazon Textract アイデンティティベースのポリシーの例](https://docs.aws.amazon.com/textract/latest/dg/security_iam_id-based-policy-examples.html)」を参照してください。また、より一般的なポリシー `AmazonAugmentedAIIntegratedAPIAccess` を使用して、これらのアクセス許可を付与することもできます。詳細については、「[Amazon A2I、Amazon Textract、Amazon Rekognition API オペレーションを呼び出すアクセス権限を持つユーザーを作成する](a2i-permissions-security.md#a2i-grant-general-permission)」を参照してください。

ヒューマンループを作成して開始するには、フロー定義 ARN が必要です。フロー定義 (またはヒューマンレビューワークフロー) の作成方法については、「[人間によるレビューワークフローを作成する](a2i-create-flow-definition.md)」を参照してください。

**重要**  
Amazon A2I では、ヒューマンループ入力イメージデータを含むすべての S3 バケットに CORS ポリシーが添付されている必要があります。この変更の詳細については、「[CORS アクセス許可要件](a2i-permissions-security.md#a2i-cors-update)」を参照してください。

## 組み込みタスクタイプのヒューマンループを作成および開始する
<a name="a2i-human-loop-built-in-task-type"></a>

組み込みタスクタイプを使用してヒューマンループを開始するには、対応するサービスの API を使用して入力データを提供し、ヒューマンループを設定します。Amazon Textract の場合、`AnalyzeDocument` API オペレーションを使用します。Amazon Rekognition の場合、`DetectModerationLabels` API オペレーションを使用します。 AWS CLI または言語固有の SDK を使用して、これらの API オペレーションを使用してリクエストを作成できます。

**重要**  
組み込みタスクタイプを使用してヒューマンループを作成する場合、`DataAttributes` を使用して `StartHumanLoop` オペレーションに提供された入力に関連する一連の `ContentClassifiers` を指定できます。コンテンツ分類子を使用して、コンテンツに個人を特定できる情報やアダルトコンテンツが含まれていないことを宣言します。  
Amazon Mechanical Turk を使用するには、HIPAA で保護された健康情報を含む個人を特定できる情報がデータに含まれていないことを確認してください。​`FreeOfPersonallyIdentifiableInformation` コンテンツ分類子を含めます。このコンテンツ分類子を使用しない場合、SageMaker AI はタスクを Mechanical Turk に送信しません。データにアダルトコンテンツが含まれていない場合、`'FreeOfAdultContent'` 分類子も含めます。これらのコンテンツ分類子を使用しない場合、SageMaker AI が、タスクを表示できる Mechanical Turk ワーカーを制限する可能性があります。

組み込みタスクタイプの AWS サービス API を使用して ML ジョブを開始すると、Amazon A2I はそのサービスの推論結果をモニタリングします。例えば、Amazon Rekognition を使用してジョブを実行する場合、Amazon A2I は各イメージの推論信頼スコアをチェックし、フロー定義で指定されている信頼性のしきい値と比較します。ヒューマンレビュータスクを開始する条件が満たされている場合、またはフロー定義で条件を指定しなかった場合は、ヒューマンレビュータスクがワーカーに送信されます。

### Amazon Textract のヒューマンループを作成する
<a name="a2i-human-loop-textract"></a>

Amazon A2I は Amazon Textract と統合されており、Amazon Textract API を使用してヒューマンループを設定および開始できます。ドキュメント分析のためにドキュメントファイルを Amazon Textract に送信するには、[`AnalyzeDocument` API オペレーション](https://docs.aws.amazon.com/textract/latest/dg/API_AnalyzeDocument.html)を使用します。このドキュメント分析ジョブにヒューマンループを追加するには、パラメータ `HumanLoopConfig` を設定する必要があります。

ヒューマンループを設定する場合、`HumanLoopConfig` の `FlowDefinitionArn` で指定したフロー定義は `Document` パラメータの `Bucket` で識別されるバケットと同じ AWS リージョンにあることが必要です。

次の表は、 AWS CLI および でこのオペレーションを使用する方法の例を示しています AWS SDK for Python (Boto3)。

------
#### [ AWS SDK for Python (Boto3) ]

次のリクエスト例では、SDK for Python (Boto3) を使用します。詳細については、*AWS SDK for Python (Boto) APIリファレンス*の「[analyze\_document](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/textract.html#Textract.Client.analyze_document)」を参照してください。

```
import boto3

textract = boto3.client('textract', {{aws_region}})

response = textract.analyze_document(
            Document={'S3Object': {'Bucket': {{bucket_name}}, 'Name': {{document_name}}}},
            FeatureTypes=["TABLES", "FORMS"],
            HumanLoopConfig={
                'FlowDefinitionArn': 'arn:aws:sagemaker:{{aws_region}}:{{aws_account_number}}:flow-definition/{{flow_def_name}}',
                'HumanLoopName': '{{human_loop_name}}',
                'DataAttributes': {'ContentClassifiers': ['FreeOfPersonallyIdentifiableInformation','FreeOfAdultContent']}
            }
          )
```

------
#### [ AWS CLI ]

次のリクエスト例では、 CLI AWS を使用しています。詳細については、*[AWS CLI コマンドリファレンス](https://docs.aws.amazon.com/cli/latest/reference/)*の「[analyze-document](https://docs.aws.amazon.com/cli/latest/reference/textract/analyze-document.html)」を参照してください。

```
$ aws textract analyze-document \
     --document '{"S3Object":{"Bucket":"{{bucket_name}}","Name":"{{document_name}}"}}' \
     --human-loop-config HumanLoopName="{{human_loop_name}}",FlowDefinitionArn="arn:aws:sagemaker:{{aws-region}}:{{aws_account_number}}:flow-definition/{{flow_def_name}}",DataAttributes='{ContentClassifiers=["FreeOfPersonallyIdentifiableInformation", "FreeOfAdultContent"]}' \
     --feature-types '["TABLES", "FORMS"]'
```

```
$ aws textract analyze-document \
     --document '{"S3Object":{"Bucket":"{{bucket_name}}","Name":"{{document_name}}"}}' \
     --human-loop-config \
          '{"HumanLoopName":"{{human_loop_name}}","FlowDefinitionArn":"arn:aws:sagemaker:{{aws_region}}:{{aws_account_number}}:flow-definition/{{flow_def_name}}","DataAttributes": {"ContentClassifiers":["FreeOfPersonallyIdentifiableInformation","FreeOfAdultContent"]}}' \
     --feature-types '["TABLES", "FORMS"]'
```

------

ヒューマンループを設定して `AnalyzeDocument` を実行した後、Amazon A2I が `AnalyzeDocument` の結果をモニタリングし、フロー定義のアクティベーション条件と照合します。1 つ以上のキー値のペアの Amazon Textract 推論信頼スコアがレビューの条件を満たしている場合、Amazon A2I は人間によるレビューループを開始し、その [https://docs.aws.amazon.com/textract/latest/dg/API_HumanLoopActivationOutput.html](https://docs.aws.amazon.com/textract/latest/dg/API_HumanLoopActivationOutput.html) オブジェクトを `AnalyzeDocument` のレスポンスに含めます。

### Amazon Rekognition のヒューマンループを作成する
<a name="a2i-human-loop-rekognition"></a>

Amazon A2I は Amazon Rekognition と統合されており、Amazon Rekognition API を使用してヒューマンループを設定および開始できます。コンテンツモデレーションのためにイメージを Amazon Rekognition に送信するには、[`DetectModerationLabels` API オペレーション](https://docs.aws.amazon.com/rekognition/latest/dg/API_DetectModerationLabels.html)を使用します。ヒューマンループを設定するには、`DetectModerationLabels` を設定する際に `HumanLoopConfig` パラメータを設定します。

ヒューマンループを設定する場合、`HumanLoopConfig` の `FlowDefinitionArn` で指定したフロー定義は `Image` パラメータの `Bucket` で識別される S3 バケットと同じ AWS リージョンにあることが必要です。

次の表は、 AWS CLI および でこのオペレーションを使用する方法の例を示しています AWS SDK for Python (Boto3)。

------
#### [ AWS SDK for Python (Boto3) ]

以下の リクエスト例では、 SDK for Python (Boto3) を使用します。詳細については、*AWS SDK for Python (Boto) API リファレンス*の「[detect\_moderation\_labels](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/rekognition.html#Rekognition.Client.detect_moderation_labels)」を参照してください。

```
import boto3

rekognition = boto3.client("rekognition", {{aws_region}})

response = rekognition.detect_moderation_labels( \
        Image={'S3Object': {'Bucket': {{bucket_name}}, 'Name': {{image_name}}}}, \
        HumanLoopConfig={ \
            'HumanLoopName': '{{human_loop_name}}', \
            'FlowDefinitionArn': , "arn:aws:sagemaker:{{aws_region}}:{{aws_account_number}}:flow-definition/{{flow_def_name}}" \
            'DataAttributes': {'ContentClassifiers': ['FreeOfPersonallyIdentifiableInformation','FreeOfAdultContent']}
         })
```

------
#### [ AWS CLI ]

次のリクエスト例では、 CLI AWS を使用しています。詳細については、*[AWS CLI コマンドリファレンス](https://docs.aws.amazon.com/cli/latest/reference/)*の「[detect-moderation-labels](https://docs.aws.amazon.com/cli/latest/reference/rekognition/detect-moderation-labels.html)」を参照してください。

```
$ aws rekognition detect-moderation-labels \
    --image "S3Object={Bucket='{{bucket_name}}',Name='{{image_name}}'}" \
    --human-loop-config HumanLoopName="{{human_loop_name}}",FlowDefinitionArn="arn:aws:sagemaker:{{aws_region}}:{{aws_account_number}}:flow-definition/{{flow_def_name}}",DataAttributes='{ContentClassifiers=["FreeOfPersonallyIdentifiableInformation", "FreeOfAdultContent"]}'
```

```
$ aws rekognition detect-moderation-labels \
    --image "S3Object={Bucket='{{bucket_name}}',Name='{{image_name}}'}" \
    --human-loop-config \
        '{"HumanLoopName": "{{human_loop_name}}", "FlowDefinitionArn": "arn:aws:sagemaker:{{aws_region}}:{{aws_account_number}}:flow-definition/{{flow_def_name}}", "DataAttributes": {"ContentClassifiers": ["FreeOfPersonallyIdentifiableInformation", "FreeOfAdultContent"]}}'
```

------

ヒューマンループを設定して `DetectModerationLabels` を実行した後、Amazon A2I が `DetectModerationLabels` の結果をモニタリングし、フロー定義のアクティベーション条件と照合します。イメージの Amazon Rekognition 推論信頼スコアがレビューの条件を満たしている場合、Amazon A2I が人間によるレビューループを開始し、応答要素 `HumanLoopActivationOutput` を `DetectModerationLabels` の応答に含めます。

## カスタムタスクタイプのヒューマンループを作成および開始する
<a name="a2i-instructions-starthumanloop"></a>

カスタムヒューマンレビュータスクのヒューマンループを設定するには、アプリケーション内で `StartHumanLoop` オペレーションを使用します。このセクションでは、 AWS SDK for Python (Boto3) と AWS Command Line Interface () を使用したヒューマンループリクエストの例を示しますAWS CLI。`StartHumanLoop` をサポートしている他の言語固有の SDK に関するドキュメントについては、 Amazon Augmented AI ランタイム API のドキュメントで、**[See Also]** (その他の資料) セクションの [StartHumanLoop](https://docs.aws.amazon.com/augmented-ai/2019-11-07/APIReference/API_StartHumanLoop.html) を使用してください。カスタムタスクタイプで Amazon A2I を使用する方法を示す例については、「[Amazon A2I を使用したユースケースと例](a2i-task-types-general.md)」を参照してください。

**前提条件**

この手順を完了するには、以下が必要です。
+ JSON 形式のファイルの文字列表現としてフォーマットされた入力データ
+ フロー定義の Amazon リソースネーム (ARN)

**ヒューマンループを設定するには**

1. `DataAttributes` の場合、`StartHumanLoop` オペレーションに提供される入力に関連する `ContentClassifiers` のセットを指定します。コンテンツ分類子を使用して、コンテンツに個人を特定できる情報やアダルトコンテンツが含まれていないことを宣言します。

   Amazon Mechanical Turk を使用するには、HIPAA で保護された健康情報を含む個人を特定できる情報がデータに含まれていないことを確認し、`FreeOfPersonallyIdentifiableInformation` コンテンツ識別子を含めます。このコンテンツ分類子を使用しない場合、SageMaker AI はタスクを Mechanical Turk に送信しません。データにアダルトコンテンツが含まれていない場合、`'FreeOfAdultContent'` 分類子も含めます。これらのコンテンツ分類子を使用しない場合、SageMaker AI が、タスクを表示できる Mechanical Turk ワーカーを制限する可能性があります。

1. `FlowDefinitionArn` の場合、フロー定義の Amazon リソースネーム (ARN) を入力します。

1. `HumanLoopInput` の場合、JSON 形式のファイルの文字列表現として入力データを入力します。ヒューマンループを開始したときに、入力データがヒューマンワーカーに適切に表示されるように、入力データとカスタムワーカータスクテンプレートを構造化します。カスタムワーカータスクテンプレートをプレビューする方法については、「[ワーカータスクテンプレートをプレビューする](a2i-custom-templates.md#a2i-preview-your-custom-template)」を参照してください。

1. `HumanLoopName` の場合、ヒューマンループの名前を入力します。名前は、アカウントのリージョン内で一意である必要があり、63 文字以内で指定できます。有効な文字は、a ～ z、0 ～ 9、- (ハイフン) です。

**ヒューマンループを開始するには**
+ ヒューマンループを始めるには、希望する言語固有の SDK を使用して、次の例のようなリクエストを送信します。

------
#### [ AWS SDK for Python (Boto3) ]

次のリクエスト例では、SDK for Python (Boto3) を使用します。詳細については、*AWS SDK for Python (Boto) API リファレンス*の「[Boto 3 Augmented AI ランタイム](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker-a2i-runtime.html#AugmentedAIRuntime.Client.start_human_loop)」を参照してください。

```
import boto3

a2i_runtime_client = boto3.client('sagemaker-a2i-runtime')

response = a2i_runtime_client.start_human_loop(
    HumanLoopName='{{human_loop_name}}',
    FlowDefinitionArn='arn:aws:sagemaker:{{aws-region}}:{{xyz}}:flow-definition/{{flow_def_name}}',
    HumanLoopInput={
        'InputContent': {{'{"InputContent": {\"prompt\":\"What is the answer?\"}}'}}    
    },
    DataAttributes={
        'ContentClassifiers': [
            {{'FreeOfPersonallyIdentifiableInformation'|'FreeOfAdultContent'}},
        ]
    }
)
```

------
#### [ AWS CLI ]

次のリクエスト例では、 CLI AWS を使用しています。詳細については、*[AWS CLI コマンドリファレンス](https://docs.aws.amazon.com/cli/latest/reference/)*の「[start-human-loop](https://docs.aws.amazon.com/cli/latest/reference/sagemaker-a2i-runtime/start-human-loop.html)」を参照してください。

```
$ aws sagemaker-a2i-runtime start-human-loop
        --flow-definition-arn 'arn:aws:sagemaker:{{aws_region}}:{{xyz}}:flow-definition/{{flow_def_name}}' \
        --human-loop-name '{{human_loop_name}}' \
        --human-loop-input '{{{"InputContent": "{\"prompt\":\"What is the answer?\"}"}}}' \
        --data-attributes ContentClassifiers={{"FreeOfPersonallyIdentifiableInformation","FreeOfAdultContent"}} \
```

------

`StartHumanLoop` を直接呼び出すことによってヒューマンループを正常に開始すると、レスポンスには `HumanLoopARN` と、`NULL` に設定される `HumanLoopActivationResults` オブジェクトが含まれます。このヒューマンループ名を使用して、ヒューマンループを監視および管理できます。

## 次のステップ:
<a name="a2i-next-step-starthumanloop"></a>

ヒューマンループを開始したら、Amazon Augmented AI ランタイム API と Amazon CloudWatch Events を使用して管理およびモニタリングできます。詳細については[ヒューマンループの監視と管理](a2i-monitor-humanloop-results.md)を参照してください。