Skip to content

Commit 911055e

Browse files
committed
Enhancing A2I template and adding logic for failed review tasks
1 parent 2e920b4 commit 911055e

File tree

7 files changed

+879
-407
lines changed

7 files changed

+879
-407
lines changed

patterns/pattern-1/src/hitl-process-function/index.py

Lines changed: 223 additions & 193 deletions
Large diffs are not rendered by default.

patterns/pattern-1/src/processresults_function/index.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ def start_human_loop(
673673
FlowDefinitionArn = ssm.get_parameter(Name=f"/{os.environ.get('METRIC_NAMESPACE', 'IDP')}/FlowDefinitionArn")['Parameter']['Value']
674674
human_review_id = generate_random_string(2)
675675
response = a2i_runtime_client.start_human_loop(
676-
HumanLoopName=f"review-bda-{execution_id}-{human_review_id}",
676+
HumanLoopName=f"review-bda-{execution_id}-{record_number}-{page_id_num}",
677677
FlowDefinitionArn=FlowDefinitionArn,
678678
HumanLoopInput={"InputContent": json.dumps(human_loop_input)}
679679
)
@@ -929,6 +929,7 @@ def process_segments(
929929
else:
930930
if enable_hitl == 'true':
931931
std_hitl = 'true'
932+
# std_hitl = None # HITL for standard output blueprint match is disabled until we have option to choose Blueprint in A2I
932933
else:
933934
std_hitl = None
934935
# Process standard output if no custom output match
@@ -940,7 +941,8 @@ def process_segments(
940941
page_array = list(range(start_page, end_page + 1))
941942
item.update({
942943
"page_array": page_array,
943-
"hitl_triggered": std_hitl,
944+
# "hitl_triggered": std_hitl,
945+
"hitl_triggered": None,
944946
"extraction_bp_name": "None",
945947
"extracted_result": std_output
946948
})
@@ -950,31 +952,34 @@ def process_segments(
950952
record_number=record_number,
951953
bp_match=segment.get('custom_output_status'),
952954
extraction_bp_name="None",
953-
hitl_triggered=std_hitl,
955+
# hitl_triggered=std_hitl,
956+
hitl_triggered=None,
954957
page_array=page_array,
955958
review_portal_url=SAGEMAKER_A2I_REVIEW_PORTAL_URL
956959
)
957960

958-
hitl_triggered = std_hitl
959-
if enable_hitl == 'true':
960-
for page_number in range(start_page, end_page + 1):
961-
ImageUri = f"s3://{output_bucket}/{object_key}/pages/{page_number}/image.jpg"
962-
try:
963-
human_loop_response = start_human_loop(
964-
execution_id=execution_id,
965-
kv_pairs=[],
966-
source_image_uri=ImageUri,
967-
bounding_boxes=[],
968-
blueprintName="",
969-
bp_confidence=0.00,
970-
confidenceThreshold=confidence_threshold,
971-
page_id=page_number,
972-
page_indices=page_array,
973-
record_number=record_number
974-
)
975-
logger.info(f"Triggered human loop for page {page_number}: {human_loop_response}")
976-
except Exception as e:
977-
logger.error(f"Failed to start human loop for page {page_number}: {str(e)}")
961+
# hitl_triggered = std_hitl
962+
hitl_triggered = None
963+
# if enable_hitl == 'true':
964+
# # if std_hitl: # HITL for standard output blueprint match is disabled until we have option to choose Blueprint in A2I
965+
# for page_number in range(start_page, end_page + 1):
966+
# ImageUri = f"s3://{output_bucket}/{object_key}/pages/{page_number}/image.jpg"
967+
# try:
968+
# human_loop_response = start_human_loop(
969+
# execution_id=execution_id,
970+
# kv_pairs=[],
971+
# source_image_uri=ImageUri,
972+
# bounding_boxes=[],
973+
# blueprintName="",
974+
# bp_confidence=0.00,
975+
# confidenceThreshold=confidence_threshold,
976+
# page_id=page_number,
977+
# page_indices=page_array,
978+
# record_number=record_number
979+
# )
980+
# logger.info(f"Triggered human loop for page {page_number}: {human_loop_response}")
981+
# except Exception as e:
982+
# logger.error(f"Failed to start human loop for page {page_number}: {str(e)}")
978983

979984
document.hitl_metadata.append(hitl_metadata)
980985

@@ -1136,7 +1141,7 @@ def handler(event, context):
11361141
execution_id,
11371142
document
11381143
)
1139-
if hitl_result:
1144+
if hitl_result or hitl_triggered== "true":
11401145
hitl_triggered = "true"
11411146
elif isinstance(output_metadata, dict):
11421147
for asset_id, asset in output_metadata.items():
@@ -1149,7 +1154,7 @@ def handler(event, context):
11491154
execution_id,
11501155
document
11511156
)
1152-
if hitl_result:
1157+
if hitl_result or hitl_triggered== "true":
11531158
hitl_triggered = "true"
11541159
else:
11551160
logger.error("Unexpected output_metadata format in job_metadata.json")

patterns/pattern-1/statemachine/workflow.asl.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@
108108
"BackoffRate": 2
109109
}
110110
],
111+
"Catch": [
112+
{
113+
"ErrorEquals": ["HITLFailedException"],
114+
"Next": "HITLFailState"
115+
}
116+
],
111117
"Next": "HITLStatusUpdate"
112118
},
113119
"HITLStatusUpdate": {
@@ -171,6 +177,11 @@
171177
"Type": "Fail",
172178
"Cause": "Data Automation Job Failed",
173179
"Error": "JobFailedException"
180+
},
181+
"HITLFailState": {
182+
"Type": "Fail",
183+
"Cause": "Human In The Loop Review Failed",
184+
"Error": "HITLFailedException"
174185
}
175186
}
176187
}

patterns/pattern-1/template.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,8 @@ Resources:
878878
detail:
879879
humanLoopStatus:
880880
- 'Completed'
881+
- 'Failed'
882+
- 'Stopped'
881883
State: 'ENABLED'
882884
Targets:
883885
- Arn: !GetAtt HITLProcessLambdaFunction.Arn

0 commit comments

Comments
 (0)