AWS Textract start_document_analysis 不允许我在 Lambda 函数中使用 AdapterConfig
目前,我正在使用textract查询从上传的PDF文档中提取特定信息。每当有文档上传到S3存储桶时,我会触发一个叫做textract_async_job_creation
的Lambda函数。这个函数会运行textract的start_document_analysis
方法,并将响应结果存储到另一个S3存储桶中。这个过程会触发SNS发送通知给另一个Lambda函数,叫做textract-response-process
,它会把查询结果以json格式存储到另一个存储桶里。我的问题是,第一个Lambda函数textract_async_job_creation
不允许我使用我训练的适配器,并且报错说AdapterConfig
没有被识别(我不记得具体的错误信息了)。我阅读的所有文档都允许在start_document_analysis
中使用AdapterConfig
。有人能告诉我我哪里出错了吗?
import os
import json
import boto3
from botocore.config import Config
from urllib.parse import unquote_plus
my_config = Config(
region_name='us-east-2',
retries={
'max_attempts': 10,
'mode': 'adaptive'
}
)
textract = boto3.client('textract', config=my_config)
OUTPUT_BUCKET_NAME = os.environ["OUTPUT_BUCKET_NAME"]
OUTPUT_S3_PREFIX = os.environ["OUTPUT_S3_PREFIX"]
SNS_TOPIC_ARN = os.environ["SNS_TOPIC_ARN"]
SNS_ROLE_ARN = os.environ["SNS_ROLE_ARN"]
def lambda_handler(event, context):
responses = []
for record in event["Records"]:
file_obj = record["s3"]
bucketname = str(file_obj["bucket"]["name"])
filename = unquote_plus(str(file_obj["object"]["key"]))
print(f"Bucket: {bucketname} ::: Key: {filename}")
response = textract.start_document_analysis(
DocumentLocation={'S3Object': {'Bucket': bucketname, 'Name': filename}},
FeatureTypes=['QUERIES'],
OutputConfig={'S3Bucket': OUTPUT_BUCKET_NAME, 'S3Prefix': OUTPUT_S3_PREFIX},
NotificationChannel={'SNSTopicArn': SNS_TOPIC_ARN, 'RoleArn': SNS_ROLE_ARN},
QueriesConfig={
'Queries': [
{'Text': 'What is the name of the claimant?', 'Pages': ['1']},
{'Text': 'What is the date on the document?', 'Pages': ['1']},
{'Text': 'What is the phone number?', 'Pages': ['1']},
{'Text': 'What is the address of the office?', 'Pages': ['1']}
]
}
# AdaptersConfig={
# 'Adapters': [
# {'AdapterId': 'xxxxxxxxxxx', 'Version': '1'}
# ]
# }
)
responses.append(response)
successful_responses = [resp for resp in responses if resp["ResponseMetadata"]["HTTPStatusCode"] == 200]
failed_responses = [resp for resp in responses if resp["ResponseMetadata"]["HTTPStatusCode"] != 200]
if successful_responses:
return {"statusCode": 200, "body": json.dumps(f"Job(s) created successfully for {len(successful_responses)} file(s)!")}
else:
return {"statusCode": 500, "body": json.dumps(f"Job creation failed for {len(failed_responses)} file(s)!")}
我尝试使用start_document_analysis
来使用我训练的适配器,以便从我上传到S3存储桶的文档中提取正确的查询响应。然而,每当我尝试在start_document_analysis
方法中包含AdaptersConfig
时,就会出现错误,我不明白为什么,因为文档中有使用AdaptersConfig
的示例。
1 个回答
0
我之前在用Python 3.9这个版本,它里面用的boto3是个旧版本,不支持AdapterConfig这个参数。后来我换成了Python 3.12,这个问题就解决了。