-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function_OLD.py
More file actions
282 lines (232 loc) · 7.81 KB
/
lambda_function_OLD.py
File metadata and controls
282 lines (232 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""
Lambda Function - Processamento de Exames Médicos
Versão otimizada com estrutura modular
Economia estimada: 50-60% em custos de IA
"""
import os
import json
import boto3
import requests
from pathlib import Path
from anthropic import Anthropic
# Imports do sistema modular
from src.config import *
from src.utils import (
HeaderCacheS3,
download_from_s3,
cleanup_temp_files,
calculate_exam_stats,
validate_patient_name,
validate_birth_date
)
from src.processors import (
extract_text_hybrid,
extract_header_with_cache,
parse_exams_from_text,
clean_reference_values,
deduplicate_exams,
assign_biomarker_ids
)
# ========================================
# CONFIGURAÇÃO DE CLIENTES AWS
# ========================================
s3_client = boto3.client('s3')
textract_client = boto3.client('textract')
anthropic_client = Anthropic(api_key=os.environ['ANTHROPIC_API_KEY'])
# Cache global para headers
header_cache = HeaderCacheS3(
bucket_name=os.environ.get('CACHE_BUCKET'),
s3_client=s3_client
)
# ========================================
# WEBHOOK PARA SUPABASE
# ========================================
def send_webhook_to_supabase(exam_id: str, s3_key: str, status: str, data: dict = None) -> bool:
"""
Envia resultado do processamento para Supabase via webhook
Args:
exam_id: ID do exame
s3_key: Chave do objeto no S3
status: 'completed' ou 'failed'
data: Dados processados (opcional)
Returns:
bool: True se envio bem-sucedido
"""
webhook_url = os.environ.get('WEBHOOK_URL')
if not webhook_url:
print('⚠️ WEBHOOK_URL não configurada')
return False
payload = {
'examId': exam_id,
's3Key': s3_key,
'status': status
}
if data:
payload.update(data)
try:
response = requests.post(
webhook_url,
json=payload,
timeout=10
)
if response.status_code == 200:
print(f'✅ Webhook enviado: {status}')
return True
else:
print(f'⚠️ Webhook retornou status {response.status_code}')
return False
except Exception as e:
print(f'❌ Erro ao enviar webhook: {e}')
return False
# ========================================
# FUNÇÃO PRINCIPAL DE PROCESSAMENTO
# ========================================
def process_exam_hybrid(exam_id: str, s3_bucket: str, s3_key: str) -> dict:
"""
Processamento híbrido completo de um exame
Fluxo:
1. Download do PDF do S3
2. Extração híbrida de texto (PyPDF2 -> Textract)
3. Extração de header com cache (Vision apenas se necessário)
4. Parsing de exames com Claude Haiku
5. Limpeza e deduplicação
6. Montagem do payload final
Args:
exam_id: ID único do exame
s3_bucket: Nome do bucket S3
s3_key: Chave do objeto no S3
Returns:
dict: Payload completo para o webhook
"""
temp_files = []
try:
# Passo 1: Download do PDF
print(f'\n📥 Processando exame: {exam_id}')
local_pdf = f'/tmp/{exam_id}.pdf'
temp_files.append(local_pdf)
if not download_from_s3(s3_client, s3_bucket, s3_key, local_pdf):
raise Exception('Falha ao baixar PDF do S3')
# Passo 2: Extração híbrida de texto
print('\n📄 Extraindo texto...')
extracted_text, extraction_method = extract_text_hybrid(
local_pdf,
textract_client,
s3_bucket,
s3_key
)
if not extracted_text:
raise Exception('Falha na extração de texto')
print(f' Método usado: {extraction_method}')
print(f' Caracteres extraídos: {len(extracted_text)}')
# Passo 3: Extração de header com cache
print('\n👤 Extraindo header do paciente...')
header = extract_header_with_cache(
local_pdf,
extracted_text,
anthropic_client,
header_cache
)
# Validar header
nome = header.get('nome', '')
data_nasc = header.get('data_nascimento', '')
if not validate_patient_name(nome):
print(f'⚠️ Nome inválido: {nome}')
if not validate_birth_date(data_nasc):
print(f'⚠️ Data de nascimento inválida: {data_nasc}')
# Passo 4: Parsing de exames
print('\n🧪 Parseando exames...')
exames = parse_exams_from_text(extracted_text, anthropic_client)
if not exames:
print('⚠️ Nenhum exame encontrado')
# Passo 5: Limpeza e deduplicação
print('\n🔧 Limpando e deduplicando...')
exames = clean_reference_values(exames)
exames = deduplicate_exams(exames)
exames = assign_biomarker_ids(exames)
# Passo 6: Estatísticas
stats = calculate_exam_stats(exames)
print(f'\n📊 Estatísticas:')
print(f' Total: {stats["total"]} exames')
print(f' Normal: {stats["normal"]}')
print(f' Alterado: {stats["alterado"]}')
# Passo 7: Montar payload final
payload = {
'header': header,
'exams': exames,
'stats': stats,
'metadata': {
'extraction_method': extraction_method,
'text_length': len(extracted_text),
'cache_used': header_cache.enabled
}
}
return payload
finally:
# Sempre limpar arquivos temporários
cleanup_temp_files(temp_files)
# ========================================
# LAMBDA HANDLER (ENTRY POINT)
# ========================================
def lambda_handler(event, context):
"""
Entry point da Lambda
Event esperado:
{
"examId": "uuid",
"s3Bucket": "bucket-name",
"s3Key": "path/to/file.pdf"
}
"""
print('=' * 60)
print('🚀 Lambda Function Iniciada')
print('=' * 60)
try:
# Parse do evento
exam_id = event.get('examId')
s3_bucket = event.get('s3Bucket')
s3_key = event.get('s3Key')
if not all([exam_id, s3_bucket, s3_key]):
raise ValueError('Parâmetros obrigatórios faltando: examId, s3Bucket, s3Key')
print(f'\n📋 Parâmetros:')
print(f' Exam ID: {exam_id}')
print(f' Bucket: {s3_bucket}')
print(f' Key: {s3_key}')
# Processar exame
result = process_exam_hybrid(exam_id, s3_bucket, s3_key)
# Enviar webhook de sucesso
send_webhook_to_supabase(
exam_id=exam_id,
s3_key=s3_key,
status='completed',
data=result
)
print('\n✅ Processamento concluído com sucesso!')
return {
'statusCode': 200,
'body': json.dumps({
'message': 'Exame processado com sucesso',
'examId': exam_id,
'stats': result.get('stats')
})
}
except Exception as e:
print(f'\n❌ Erro no processamento: {e}')
import traceback
traceback.print_exc()
# Enviar webhook de falha
try:
send_webhook_to_supabase(
exam_id=event.get('examId'),
s3_key=event.get('s3Key'),
status='failed',
data={'error': str(e)}
)
except:
pass
return {
'statusCode': 500,
'body': json.dumps({
'message': 'Erro no processamento',
'error': str(e)
})
}