Spaces:
Paused
Paused
fix: 400 ante imagen vacia/corrupta e import diferido de llama_cpp
Browse files- modules/llm/qwen.py +9 -2
- routes/upload.py +9 -1
modules/llm/qwen.py
CHANGED
|
@@ -9,11 +9,15 @@ Descarga:
|
|
| 9 |
"""
|
| 10 |
import os
|
| 11 |
import threading
|
|
|
|
|
|
|
| 12 |
import torch
|
| 13 |
-
from llama_cpp import Llama
|
| 14 |
from modules.llm.prompt_builder import SYSTEM
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
_load_lock = threading.Lock()
|
| 18 |
_infer_lock = threading.Lock() # llama-cpp no es thread-safe
|
| 19 |
|
|
@@ -44,6 +48,9 @@ def _load() -> None:
|
|
| 44 |
with _load_lock:
|
| 45 |
if _model is not None: # re-check inside lock
|
| 46 |
return
|
|
|
|
|
|
|
|
|
|
| 47 |
from config import QWEN_MODEL_PATH
|
| 48 |
model_path = _resolve_model_path(QWEN_MODEL_PATH)
|
| 49 |
cuda = torch.cuda.is_available()
|
|
|
|
| 9 |
"""
|
| 10 |
import os
|
| 11 |
import threading
|
| 12 |
+
from typing import TYPE_CHECKING
|
| 13 |
+
|
| 14 |
import torch
|
|
|
|
| 15 |
from modules.llm.prompt_builder import SYSTEM
|
| 16 |
|
| 17 |
+
if TYPE_CHECKING: # solo para los type checkers
|
| 18 |
+
from llama_cpp import Llama
|
| 19 |
+
|
| 20 |
+
_model: "Llama | None" = None
|
| 21 |
_load_lock = threading.Lock()
|
| 22 |
_infer_lock = threading.Lock() # llama-cpp no es thread-safe
|
| 23 |
|
|
|
|
| 48 |
with _load_lock:
|
| 49 |
if _model is not None: # re-check inside lock
|
| 50 |
return
|
| 51 |
+
# llama_cpp se importa aquí y no arriba: así el backend arranca sin la
|
| 52 |
+
# dependencia cuando COACH_MODEL=deterministic (experimento A).
|
| 53 |
+
from llama_cpp import Llama
|
| 54 |
from config import QWEN_MODEL_PATH
|
| 55 |
model_path = _resolve_model_path(QWEN_MODEL_PATH)
|
| 56 |
cuda = torch.cuda.is_available()
|
routes/upload.py
CHANGED
|
@@ -34,10 +34,18 @@ def upload():
|
|
| 34 |
return jsonify({'error': 'Formato no soportado. Use JPG o PNG'}), 400
|
| 35 |
|
| 36 |
file_bytes = file.read()
|
|
|
|
|
|
|
| 37 |
if len(file_bytes) > MAX_UPLOAD_MB * 1024 * 1024:
|
| 38 |
return jsonify({'error': f'Archivo demasiado grande. Maximo {MAX_UPLOAD_MB}MB'}), 413
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
if img is None:
|
| 42 |
return jsonify({'error': 'No se pudo leer la imagen'}), 400
|
| 43 |
|
|
|
|
| 34 |
return jsonify({'error': 'Formato no soportado. Use JPG o PNG'}), 400
|
| 35 |
|
| 36 |
file_bytes = file.read()
|
| 37 |
+
if not file_bytes:
|
| 38 |
+
return jsonify({'error': 'No se pudo leer la imagen'}), 400
|
| 39 |
if len(file_bytes) > MAX_UPLOAD_MB * 1024 * 1024:
|
| 40 |
return jsonify({'error': f'Archivo demasiado grande. Maximo {MAX_UPLOAD_MB}MB'}), 413
|
| 41 |
|
| 42 |
+
# cv2.imdecode devuelve None ante datos no-imagen, pero LANZA cv2.error si el
|
| 43 |
+
# buffer llega vacio o corrupto. Atrapamos ambos para responder siempre el
|
| 44 |
+
# 400 amigable en vez de dejar que la excepcion se vuelva un 500 crudo.
|
| 45 |
+
try:
|
| 46 |
+
img = cv2.imdecode(np.frombuffer(file_bytes, np.uint8), cv2.IMREAD_COLOR)
|
| 47 |
+
except cv2.error:
|
| 48 |
+
img = None
|
| 49 |
if img is None:
|
| 50 |
return jsonify({'error': 'No se pudo leer la imagen'}), 400
|
| 51 |
|