JoshuaCarrera commited on
Commit
c221f76
·
1 Parent(s): 69b7038

Añadir capacidad de debugging mediante hash

Browse files
Files changed (1) hide show
  1. src/app/app.py +28 -6
src/app/app.py CHANGED
@@ -1,8 +1,9 @@
1
  import io
2
  import os
3
  import boto3
4
- import gc # Importamos el recolector de basura de Python
5
  import sys
 
6
  from PIL import Image
7
  from botocore.config import Config
8
  from ultralytics import YOLO
@@ -32,6 +33,14 @@ model_configs = {
32
  }
33
  }
34
 
 
 
 
 
 
 
 
 
35
  # ==========================================
36
  # 2. DESCARGA CONSTRUCTORA Y DESTRUCTIVA (Ahorro de RAM)
37
  # ==========================================
@@ -61,9 +70,11 @@ def download_models_from_r2():
61
  raise e
62
  else:
63
  print(f"El modelo {name} ya existe localmente. Cargando...")
 
 
 
 
64
 
65
- # OPTIMIZACIÓN EXTREMA: Destruir el cliente pesado de Boto3 para liberar RAM
66
- # Esto evita que el servidor haga SWAP al cargar YOLO
67
  del s3_client
68
  gc.collect()
69
 
@@ -71,7 +82,7 @@ def download_models_from_r2():
71
  download_models_from_r2()
72
 
73
  # ==========================================
74
- # 3. CARGA DE MODELOS (Libre del límite de hilos para máxima precisión)
75
  # ==========================================
76
  models = {
77
  "USD": YOLO(model_configs["USD"]["path"]),
@@ -122,10 +133,21 @@ app.add_middleware(
122
  def detection_vef(image: UploadFile):
123
  print("FOTO RECIBIDA")
124
 
125
- # Flujo exacto de lectura con PIL
126
  imageBytes = image.file.read()
 
 
 
 
 
127
  imageStream = io.BytesIO(imageBytes)
128
- imageFile = Image.open(imageStream)
 
 
 
 
 
 
129
 
130
  which_currency = models["INFERENCIA"].predict(imageFile, verbose=False, imgsz=640, conf=0.25)
131
 
 
1
  import io
2
  import os
3
  import boto3
4
+ import gc
5
  import sys
6
+ import hashlib # DEBUG: Importamos hashlib para calcular los MD5
7
  from PIL import Image
8
  from botocore.config import Config
9
  from ultralytics import YOLO
 
33
  }
34
  }
35
 
36
+ # DEBUG: Función auxiliar para calcular el MD5 de un archivo en disco
37
+ def calculate_file_md5(filepath): # DEBUG
38
+ hash_md5 = hashlib.md5() # DEBUG
39
+ with open(filepath, "rb") as f: # DEBUG
40
+ for chunk in iter(lambda: f.read(4096), b""): # DEBUG
41
+ hash_md5.update(chunk) # DEBUG
42
+ return hash_md5.hexdigest() # DEBUG
43
+
44
  # ==========================================
45
  # 2. DESCARGA CONSTRUCTORA Y DESTRUCTIVA (Ahorro de RAM)
46
  # ==========================================
 
70
  raise e
71
  else:
72
  print(f"El modelo {name} ya existe localmente. Cargando...")
73
+
74
+ # DEBUG: Calculamos e imprimimos el MD5 del modelo local para verificar integridad
75
+ local_md5 = calculate_file_md5(config["path"]) # DEBUG
76
+ print(f"[DEBUG] MD5 del modelo {name} en disco: {local_md5}") # DEBUG
77
 
 
 
78
  del s3_client
79
  gc.collect()
80
 
 
82
  download_models_from_r2()
83
 
84
  # ==========================================
85
+ # 3. CARGA DE MODELOS
86
  # ==========================================
87
  models = {
88
  "USD": YOLO(model_configs["USD"]["path"]),
 
133
  def detection_vef(image: UploadFile):
134
  print("FOTO RECIBIDA")
135
 
136
+ # Flujo exacto de lectura
137
  imageBytes = image.file.read()
138
+
139
+ # DEBUG: Calcular MD5 de la foto exacta recibida por la red (bytes puros)
140
+ raw_md5 = hashlib.md5(imageBytes).hexdigest() # DEBUG
141
+ print(f"[DEBUG] MD5 de la foto cruda recibida: {raw_md5}") # DEBUG
142
+
143
  imageStream = io.BytesIO(imageBytes)
144
+ imageFile = Image.open(imageStream).convert("RGB")
145
+
146
+ # DEBUG: Calcular MD5 de la foto después de ser procesada por Pillow
147
+ debug_buffer = io.BytesIO() # DEBUG
148
+ imageFile.save(debug_buffer, format="JPEG") # DEBUG
149
+ pillow_md5 = hashlib.md5(debug_buffer.getvalue()).hexdigest() # DEBUG
150
+ print(f"[DEBUG] MD5 de la foto tras procesar con Pillow (RGB): {pillow_md5}") # DEBUG
151
 
152
  which_currency = models["INFERENCIA"].predict(imageFile, verbose=False, imgsz=640, conf=0.25)
153