<jupyter_start><jupyter_text>Textract Anazlyze API Invoke with human in the loop<jupyter_code>import boto3
import uuid
import time
import re
import pprint
import json
pp = pprint.PrettyPrinter(indent=4)
# Amazon Textract client
textract = boto3.client('textract')

# Amazon S3 client 
s3 = boto3.client('s3')
humanLoopName = str(uuid.uuid4())<jupyter_output><empty_output><jupyter_text>Enter the name of the S3 bucket you craeted and uplaoded the Samplecheck document<jupyter_code>bucket="<enter s3 bucket name>"<jupyter_output><empty_output><jupyter_text>Enter the flow definition ARN or human review workflow arnby copying the arn from Console https://console.aws.amazon.com/a2i/home?region=us-east-1/human-review-workflows<jupyter_code>humanLoopConfig = {
    'FlowDefinitionArn':"<enter flow definition or human review workflow arn>",
    'HumanLoopName':humanLoopName, 
    'DataAttributes': { 'ContentClassifiers': [ 'FreeOfPersonallyIdentifiableInformation' ]}
}<jupyter_output><empty_output><jupyter_text>enter the bucket name in Bucket and sample document name in Name<jupyter_code>response = textract.analyze_document(
        Document={'S3Object': {'Bucket': bucket, 'Name':  "samplecheck.PNG"}},
        FeatureTypes=["FORMS"], 
        HumanLoopConfig=humanLoopConfig
    )
print(response)<jupyter_output><empty_output><jupyter_text>Paste the workteam arn below which you copied from craeting private workteam or you can find it in this link https://console.aws.amazon.com/sagemaker/groundtruth?region=us-east-1/labeling-workforces<jupyter_code>WORKTEAM_ARN= "<enter workteam arn>"
# Amazon SageMaker client
sagemaker = boto3.client('sagemaker')

# Amazon Augment AI (A2I) client
a2i = boto3.client('sagemaker-a2i-runtime')


workteamName = WORKTEAM_ARN[WORKTEAM_ARN.rfind('/') + 1:]
print("Navigate to the private worker portal and do the tasks. Make sure you've invited yourself to your workteam!")
print('https://' + sagemaker.describe_workteam(WorkteamName=workteamName)['Workteam']['SubDomain'])


completed_human_loops = []
resp = a2i.describe_human_loop(HumanLoopName=humanLoopName)
print(f'HumanLoop Name: {humanLoopName}')
print(f'HumanLoop Status: {resp["HumanLoopStatus"]}')
#print(f'HumanLoop Output Destination: {resp["HumanLoopOutput"]}')
print('\n')
    
if resp["HumanLoopStatus"] == "Completed":
    completed_human_loops.append(resp)

for resp in completed_human_loops:
    splitted_string = re.split('s3://' + bucket  + '/', resp['HumanLoopOutput']['OutputS3Uri'])
    output_bucket_key = splitted_string[1]
    print(output_bucket_key)
    response = s3.get_object(Bucket= bucket, Key=output_bucket_key)
    content = response["Body"].read()
    json_output = json.loads(content)
    pp.pprint(json_output)
    print('\n')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1192100538599641, 'fraction_numerical': 0.008976660682226212, 'mean_word_length': 6.509433962264151, 'pattern_counts': {'":': 1, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '394094', 'license': 'MIT', 'n_tokens_mistral': 947, 'n_tokens_neox': 915, 'n_words': 195, 'path': 'Chapter 13/chapter13 Improving accuracy of document processing .ipynb', 'repo_name': 'iamkamleshrangi/Natural-Language-Processing-with-AWS-AI-Services', 'size': '2785'}