Tutorial: Memulai Menggunakan Amazon A2I API - Amazon SageMaker

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Tutorial: Memulai Menggunakan Amazon A2I API

Tutorial ini menjelaskan operasi API yang dapat Anda gunakan untuk memulai menggunakan Amazon A2I.

Untuk menggunakan Notebook Jupyter untuk menjalankan operasi ini, pilih Notebook Jupyter dari Kasus Penggunaan dan Contoh Menggunakan Amazon A2I dan gunakan Menggunakan Instans SageMaker Notebook dengan Notebook Amazon A2I Jupyter untuk mempelajari cara menggunakannya dalam instance notebook. SageMaker

Untuk mempelajari lebih lanjut tentang operasi API yang dapat Anda gunakan dengan Amazon A2I, lihat. Gunakan API di Amazon Augmented AI

Buat Tim Kerja Pribadi

Anda dapat membuat tim kerja pribadi dan menambahkan diri Anda sebagai pekerja sehingga Anda dapat melihat pratinjau Amazon A2I.

Jika Anda tidak terbiasa dengan Amazon Cognito, kami sarankan Anda menggunakan SageMaker konsol untuk membuat tenaga kerja pribadi dan menambahkan diri Anda sebagai pekerja swasta. Untuk petunjuk, lihat Langkah 1: Buat Tim Kerja.

Jika Anda terbiasa dengan Amazon Cognito, Anda dapat menggunakan instruksi berikut untuk membuat tim kerja pribadi menggunakan API. SageMaker Setelah Anda membuat tim kerja, perhatikan tim kerja ARN ()WorkteamArn.

Untuk mempelajari lebih lanjut tentang tenaga kerja pribadi dan konfigurasi lain yang tersedia, lihat. Gunakan Tenaga Kerja Pribadi

Buat tenaga kerja pribadi

Jika Anda belum membuat tenaga kerja pribadi, Anda dapat melakukannya menggunakan kumpulan pengguna Amazon Cognito. Pastikan Anda telah menambahkan diri Anda ke kumpulan pengguna ini. Anda dapat membuat tim kerja pribadi menggunakan AWS SDK for Python (Boto3) create_workforce fungsi tersebut. Untuk SDK khusus bahasa lainnya, lihat daftar di. CreateWorkforce

response = client.create_workforce( CognitoConfig={ "UserPool": "Pool_ID", "ClientId": "app-client-id" }, WorkforceName="workforce-name" )
Buat tim kerja pribadi

Setelah Anda membuat tenaga kerja pribadi di AWS Wilayah untuk mengonfigurasi dan memulai lingkaran manusia Anda, Anda dapat membuat tim kerja pribadi menggunakan AWS SDK for Python (Boto3) create_workteam fungsi tersebut. Untuk SDK khusus bahasa lainnya, lihat daftar di. 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" }, } ] )

Akses ARN tim kerja Anda sebagai berikut:

workteamArn = response["WorkteamArn"]
Daftar tim kerja pribadi di akun Anda

Jika Anda telah membuat tim kerja pribadi, Anda dapat mencantumkan semua tim kerja di AWS Wilayah tertentu di akun Anda menggunakan AWS SDK for Python (Boto3) list_workteams fungsi tersebut. Untuk SDK khusus bahasa lainnya, lihat daftar di. ListWorkteams

response = client.list_workteams()

Jika Anda memiliki banyak tim kerja di akun Anda, Anda mungkin ingin menggunakanMaxResults,SortBy, dan NameContains memfilter hasil Anda.

Buat Alur Kerja Tinjauan Manusia

Anda dapat membuat alur kerja tinjauan manusia menggunakan operasi Amazon CreateFlowDefinition A2I. Sebelum membuat alur kerja tinjauan manusia, Anda perlu membuat UI tugas manusia. Anda dapat melakukan ini dengan CreateHumanTaskUi operasi.

Jika Anda menggunakan Amazon A2I dengan integrasi Amazon Textract atau Amazon Rekognition, Anda dapat menentukan kondisi aktivasi menggunakan JSON.

Buat UI Tugas Manusia

Jika Anda membuat alur kerja tinjauan manusia untuk digunakan dengan integrasi Amazon Ttract atau Amazon Rekognition, Anda perlu menggunakan dan memodifikasi templat tugas pekerja yang sudah dibuat sebelumnya. Untuk semua integrasi kustom, Anda dapat menggunakan template tugas pekerja kustom Anda sendiri. Gunakan tabel berikut untuk mempelajari cara membuat UI tugas manusia menggunakan templat tugas pekerja untuk dua integrasi bawaan. Ganti template dengan milik Anda sendiri untuk menyesuaikan permintaan ini.

Amazon Textract – Key-value pair extraction

Untuk mempelajari lebih lanjut tentang template ini, lihatContoh Template Kustom untuk Amazon Texttract.

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> """
Amazon Rekognition – Image moderation

Untuk mempelajari lebih lanjut tentang template ini, lihatContoh Template Kustom untuk Amazon Rekognition.

template = r""" <script src="https://assets.crowd.aws/crowd-html-elements.js"></script> {% capture s3_uri %}http://s3.amazonaws.com/{{ task.input.aiServiceRequest.image.s3Object.bucket }}/{{ task.input.aiServiceRequest.image.s3Object.name }}{% endcapture %} <crowd-form> <crowd-rekognition-detect-moderation-labels categories='[ {% for label in task.input.selectedAiServiceResponse.moderationLabels %} { name: "{{ label.name }}", parentName: "{{ label.parentName }}", }, {% endfor %} ]' src="{{ s3_uri | grant_read_access }}" header="Review the image and choose all applicable categories." > <short-instructions header="Instructions"> <style> .instructions { white-space: pre-wrap; } </style> <p class="instructions">Review the image and choose all applicable categories. If no categories apply, choose None. <b>Nudity</b> Visuals depicting nude male or female person or persons <b>Partial Nudity</b> Visuals depicting covered up nudity, for example using hands or pose <b>Revealing Clothes</b> Visuals depicting revealing clothes and poses <b>Physical Violence</b> Visuals depicting violent physical assault, such as kicking or punching <b>Weapon Violence</b> Visuals depicting violence using weapons like firearms or blades, such as shooting <b>Weapons</b> Visuals depicting weapons like firearms and blades </short-instructions> <full-instructions header="Instructions"></full-instructions> </crowd-rekognition-detect-moderation-labels> </crowd-form>"""
Custom Integration

Berikut ini adalah contoh template yang dapat digunakan dalam integrasi kustom. Template ini digunakan dalam notebook ini, menunjukkan integrasi kustom dengan Amazon Comprehend.

template = r""" <script src="https://assets.crowd.aws/crowd-html-elements.js"></script> <crowd-form> <crowd-classifier name="sentiment" categories='["Positive", "Negative", "Neutral", "Mixed"]' initial-value="{{ task.input.initialValue }}" header="What sentiment does this text convey?" > <classification-target> {{ task.input.taskObject }} </classification-target> <full-instructions header="Sentiment Analysis Instructions"> <p><strong>Positive</strong> sentiment include: joy, excitement, delight</p> <p><strong>Negative</strong> sentiment include: anger, sarcasm, anxiety</p> <p><strong>Neutral</strong>: neither positive or negative, such as stating a fact</p> <p><strong>Mixed</strong>: when the sentiment is mixed</p> </full-instructions> <short-instructions> Choose the primary sentiment that is expressed by the text. </short-instructions> </crowd-classifier> </crowd-form> """

Menggunakan template yang ditentukan di atas, Anda dapat membuat template menggunakan AWS SDK for Python (Boto3) create_human_task_ui fungsi. Untuk SDK khusus bahasa lainnya, lihat daftar di. CreateHumanTaskUi

response = client.create_human_task_ui( HumanTaskUiName="human-task-ui-name", UiTemplate={ "Content": template } )

Elemen respon ini berisi tugas manusia UI ARN. Simpan ini sebagai berikut:

humanTaskUiArn = response["HumanTaskUiArn"]

Buat JSON untuk menentukan kondisi aktivasi

Untuk integrasi bawaan Amazon Textract dan Amazon Rekognition, Anda dapat menyimpan kondisi aktivasi di objek JSON dan menggunakannya dalam permintaan Anda. CreateFlowDefinition

Selanjutnya, pilih tab untuk melihat contoh kondisi aktivasi yang dapat Anda gunakan untuk integrasi bawaan ini. Untuk informasi tambahan tentang opsi kondisi aktivasi, lihatSkema JSON untuk Kondisi Aktivasi Loop Manusia di Amazon Augmented AI.

Amazon Textract – Key-value pair extraction

Contoh ini menentukan kondisi untuk kunci tertentu (sepertiMail address) dalam dokumen. Jika kepercayaan Amazon Textract berada di luar ambang batas yang ditetapkan di sini, dokumen dikirim ke manusia untuk ditinjau, dengan kunci spesifik yang memulai loop manusia diminta ke pekerja.

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 } } ] } ] } )
Amazon Rekognition – Image moderation

Kondisi aktivasi loop manusia yang digunakan di sini disesuaikan dengan moderasi konten Rekognition Amazon; mereka didasarkan pada ambang Suggestive kepercayaan untuk label dan moderasi. Female Swimwear Or Underwear

import json humanLoopActivationConditions = json.dumps( { "Conditions": [ { "Or": [ { "ConditionType": "ModerationLabelConfidenceCheck", "ConditionParameters": { "ModerationLabelName": "Suggestive", "ConfidenceLessThan": 98 } }, { "ConditionType": "ModerationLabelConfidenceCheck", "ConditionParameters": { "ModerationLabelName": "Female Swimwear Or Underwear", "ConfidenceGreaterThan": 98 } } ] } ] } )

Buat alur kerja tinjauan manusia

Bagian ini memberikan contoh CreateFlowDefinition AWS SDK for Python (Boto3) permintaan menggunakan sumber daya yang dibuat di bagian sebelumnya. Untuk SDK khusus bahasa lainnya, lihat daftar di Definisi. CreateFlow Gunakan tab di tabel berikut untuk melihat permintaan untuk membuat alur kerja tinjauan manusia untuk integrasi bawaan Amazon Textract dan Amazon Rekognition.

Amazon Textract – Key-value pair extraction

Jika Anda menggunakan integrasi bawaan dengan Amazon Textract, Anda harus menentukan "AWS/Textract/AnalyzeDocument/Forms/V1" untuk "AwsManagedHumanLoopRequestSource" in. HumanLoopRequestSource

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://DOC-EXAMPLE-BUCKET/prefix/", }, RoleArn="arn:aws:iam::<account-number>:role/<role-name>", Tags=[ { "Key": "string", "Value": "string" }, ] )
Amazon Rekognition – Image moderation

Jika Anda menggunakan integrasi bawaan dengan Amazon Rekognition, Anda harus "AWS/Rekognition/DetectModerationLabels/Image/V3" menentukan untuk di. "AwsManagedHumanLoopRequestSource" HumanLoopRequestSource

response = client.create_flow_definition( FlowDefinitionName="human-review-workflow-name", HumanLoopRequestSource={ "AwsManagedHumanLoopRequestSource": "AWS/Rekognition/DetectModerationLabels/Image/V3" }, HumanLoopActivationConfig={ "HumanLoopActivationConditionsConfig": { "HumanLoopActivationConditions": humanLoopActivationConditions } }, HumanLoopConfig={ "WorkteamArn": workteamArn, "HumanTaskUiArn": humanTaskUiArn, "TaskTitle": "Image content moderation", "TaskDescription": "Review the image and instructions. Complete the task", "TaskCount": 1, "TaskAvailabilityLifetimeInSeconds": 43200, "TaskTimeLimitInSeconds": 3600, "TaskKeywords": [ "content moderation", ], }, OutputConfig={ "S3OutputPath": "s3://DOC-EXAMPLE-BUCKET/prefix/", }, RoleArn="arn:aws:iam::<account-number>:role/<role-name>", Tags=[ { "Key": "string", "Value": "string" }, ] )
Custom Integration

Jika Anda menggunakan integrasi kustom, kecualikan parameter berikut:HumanLoopRequestSource,HumanLoopActivationConfig.

response = client.create_flow_definition( FlowDefinitionName="human-review-workflow-name", HumanLoopConfig={ "WorkteamArn": workteamArn, "HumanTaskUiArn": humanTaskUiArn, "TaskTitle": "Image content moderation", "TaskDescription": "Review the image and instructions. Complete the task", "TaskCount": 1, "TaskAvailabilityLifetimeInSeconds": 43200, "TaskTimeLimitInSeconds": 3600, "TaskKeywords": [ "content moderation", ], }, OutputConfig={ "S3OutputPath": "s3://DOC-EXAMPLE-BUCKET/prefix/", }, RoleArn="arn:aws:iam::<account-number>:role/<role-name>", Tags=[ { "Key": "string", "Value": "string" }, ] )

Setelah membuat alur kerja tinjauan manusia, Anda dapat mengambil ARN definisi aliran dari respons:

humanReviewWorkflowArn = response["FlowDefinitionArn"]

Buat Loop Manusia

Operasi API yang Anda gunakan untuk memulai loop manusia bergantung pada integrasi Amazon A2I yang Anda gunakan.

Pilih jenis tugas Anda di tabel berikut untuk melihat contoh permintaan Amazon Textract dan Amazon Rekognition menggunakan file. AWS SDK for Python (Boto3)

Amazon Textract – Key-value pair extraction

Contoh berikut menggunakan AWS SDK for Python (Boto3) to call analyze_document in us-west-2. Ganti teks merah yang dicetak miring dengan sumber daya Anda. Sertakan DataAttributesparameter jika Anda menggunakan tenaga kerja Amazon Mechanical Turk. Untuk informasi selengkapnya, lihat dokumen analyze_document di Referensi API.AWS SDK for Python (Boto)

response = client.analyze_document( Document={"S3Object": {"Bucket": "AWSDOC-EXAMPLE-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"] )

Loop manusia hanya dibuat jika kepercayaan Amazon Texact untuk tugas analisis dokumen memenuhi kondisi aktivasi yang Anda tentukan dalam alur kerja tinjauan manusia Anda. Anda dapat memeriksa response elemen untuk menentukan apakah loop manusia telah dibuat. Untuk melihat semua yang termasuk dalam tanggapan ini, lihat 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"]}"
Amazon Rekognition – Image moderation

Contoh berikut menggunakan AWS SDK for Python (Boto3) to call detect_moderation_labels in us-west-2. Ganti teks merah yang dicetak miring dengan sumber daya Anda. Sertakan DataAttributesparameter jika Anda menggunakan tenaga kerja Amazon Mechanical Turk. Untuk informasi selengkapnya, lihat dokumen detect_moderation_labels di Referensi API.AWS SDK for Python (Boto)

response = client.detect_moderation_labels( Image={"S3Object":{"Bucket": "AWSDOC-EXAMPLE-BUCKET", "Name": "image-name.png"}}, HumanLoopConfig={ "FlowDefinitionArn":"arn:aws:sagemaker:us-west-2:111122223333:flow-definition/flow-definition-name", "HumanLoopName":"human-loop-name", "DataAttributes":{ContentClassifiers:["FreeOfPersonallyIdentifiableInformation"|"FreeOfAdultContent"]} } )

Loop manusia hanya dibuat jika kepercayaan Amazon Rekognition untuk tugas moderasi gambar memenuhi kondisi aktivasi yang Anda tentukan dalam alur kerja tinjauan manusia Anda. Anda dapat memeriksa response elemen untuk menentukan apakah loop manusia telah dibuat. Untuk melihat semua yang termasuk dalam tanggapan ini, lihat HumanLoopActivationOutput.

if "HumanLoopArn" in response["HumanLoopActivationOutput"]: # A human loop has been started! print(f"A human loop has been started with ARN: {response["HumanLoopActivationOutput"]["HumanLoopArn"]}")
Custom Integration

Contoh berikut menggunakan AWS SDK for Python (Boto3) to call start_human_loop in us-west-2. Ganti teks merah yang dicetak miring dengan sumber daya Anda. Sertakan DataAttributesparameter jika Anda menggunakan tenaga kerja Amazon Mechanical Turk. Untuk informasi selengkapnya, lihat dokumen start_human_loop di Referensi API.AWS SDK for Python (Boto)

response = client.start_human_loop( HumanLoopName= "human-loop-name", FlowDefinitionArn= "arn:aws:sagemaker:us-west-2:111122223333:flow-definition/flow-definition-name", HumanLoopInput={"InputContent": inputContentJson}, DataAttributes={"ContentClassifiers":["FreeOfPersonallyIdentifiableInformation"|"FreeOfAdultContent"]} )

Contoh ini menyimpan konten input dalam variabel inputContentJson. Asumsikan bahwa konten input berisi dua elemen: uraian teks dan sentimen (sepertiPositive,, atauNeutral)Negative, dan diformat sebagai berikut:

inputContent = { "initialValue": sentiment, "taskObject": blurb }

Kunci initialValue dan taskObject harus sesuai dengan kunci yang digunakan dalam elemen cair dari template tugas pekerja. Lihat template kustom Buat UI Tugas Manusia untuk melihat contoh.

Untuk membuatinputContentJson, lakukan hal berikut:

import json inputContentJson = json.dumps(inputContent)

Lingkaran manusia dimulai setiap kali Anda meneleponstart_human_loop. Untuk memeriksa status loop manusia Anda, gunakan describe_human_loop:

human_loop_info = a2i.describe_human_loop(HumanLoopName="human_loop_name") print(f"HumanLoop Status: {resp["HumanLoopStatus"]}") print(f"HumanLoop Output Destination: {resp["HumanLoopOutput"]}")