import boto3 # Initialize AWS Textract client once textract_client = boto3.client('textract', region_name='us-west-2') def extract_text(file): # Extract text using AWS Textract response = textract_client.detect_document_text(Document={'Bytes': file.getvalue()}) # Use list comprehension to extract and join text return " ".join([item['Text'] for item in response['Blocks'] if item['BlockType'] == 'WORD'])