Spaces:
Sleeping
Sleeping
JoshuaCarrera commited on
Commit 路
3e7a27d
1
Parent(s): 7947e09
Cambiar Dockerfile y requerimientos
Browse files- Dockerfile +21 -26
- requirements.txt +3 -3
- src/app/app.py +0 -15
Dockerfile
CHANGED
|
@@ -1,44 +1,39 @@
|
|
| 1 |
-
# 1. Usamos
|
| 2 |
-
FROM python:3.
|
| 3 |
|
| 4 |
-
# 2. Instalamos
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
ffmpeg \
|
| 9 |
-
libsm6 \
|
| 10 |
-
libxext6 \
|
| 11 |
-
build-essential \
|
| 12 |
&& rm -rf /var/lib/apt/lists/*
|
| 13 |
|
| 14 |
# --- CONFIGURACI脫N DE HUGGING FACE ---
|
| 15 |
-
# 3. Creamos un usuario no-root con ID 1000 (Requisito estricto de HF)
|
| 16 |
RUN useradd -m -u 1000 user
|
| 17 |
-
|
| 18 |
-
# 4. Cambiamos al nuevo usuario y establecemos su entorno
|
| 19 |
USER user
|
|
|
|
|
|
|
| 20 |
ENV HOME=/home/user \
|
| 21 |
-
PATH=/home/user/.local/bin:$PATH
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
WORKDIR $HOME/app
|
| 23 |
# -------------------------------------
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
# (Se instalar谩 en ~/.local/bin del usuario 1000)
|
| 27 |
-
RUN pip install --no-cache-dir --upgrade pip && \
|
| 28 |
-
pip install --no-cache-dir torch torchvision --index-url https://download.pytorch.org/whl/cpu
|
| 29 |
-
|
| 30 |
-
# 6. Copiamos el archivo de requerimientos
|
| 31 |
-
# OJO: Como es un monorepo y HF construye desde la ra铆z, usamos "backend/"
|
| 32 |
COPY --chown=user requirements.txt .
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
RUN pip install --no-cache-dir -
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
#
|
| 38 |
COPY --chown=user . .
|
| 39 |
|
| 40 |
-
# 9. Exponemos el puerto que exige Hugging Face
|
| 41 |
EXPOSE 7860
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
CMD ["uvicorn", "src.app.app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 1 |
+
# 1. Usamos 3.11 por estabilidad absoluta con PyTorch y YOLO sin compilar desde cero
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
|
| 4 |
+
# 2. Instalamos dependencias del sistema m铆nimas.
|
| 5 |
+
# NOTA: Asumimos que usar谩s 'opencv-python-headless' en requirements.txt
|
| 6 |
+
# Si estrictamente procesas video, deja ffmpeg. Si son solo im谩genes, puedes borrar el RUN completo.
|
| 7 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 8 |
ffmpeg \
|
|
|
|
|
|
|
|
|
|
| 9 |
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
|
| 11 |
# --- CONFIGURACI脫N DE HUGGING FACE ---
|
|
|
|
| 12 |
RUN useradd -m -u 1000 user
|
|
|
|
|
|
|
| 13 |
USER user
|
| 14 |
+
|
| 15 |
+
# 3. Variables de entorno: PATH y Optimizaci贸n estricta para CPU
|
| 16 |
ENV HOME=/home/user \
|
| 17 |
+
PATH=/home/user/.local/bin:$PATH \
|
| 18 |
+
OMP_NUM_THREADS=1 \
|
| 19 |
+
MKL_NUM_THREADS=1 \
|
| 20 |
+
OPENBLAS_NUM_THREADS=1
|
| 21 |
+
|
| 22 |
WORKDIR $HOME/app
|
| 23 |
# -------------------------------------
|
| 24 |
|
| 25 |
+
# 4. Copiamos requerimientos primero para aprovechar el cach茅 de capas de Docker
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
COPY --chown=user requirements.txt .
|
| 27 |
|
| 28 |
+
# 5. Instalamos todo en una sola capa (reduce el tama帽o de la imagen final)
|
| 29 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
| 30 |
+
pip install --no-cache-dir torch torchvision --index-url https://download.pytorch.org/whl/cpu && \
|
| 31 |
+
pip install --no-cache-dir -r requirements.txt
|
| 32 |
|
| 33 |
+
# 6. Copiamos el resto del c贸digo
|
| 34 |
COPY --chown=user . .
|
| 35 |
|
|
|
|
| 36 |
EXPOSE 7860
|
| 37 |
|
| 38 |
+
# 7. Arranque
|
| 39 |
+
CMD ["uvicorn", "src.app.app:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "16"]
|
requirements.txt
CHANGED
|
@@ -2,7 +2,7 @@ annotated-doc==0.0.4
|
|
| 2 |
annotated-types==0.7.0
|
| 3 |
anyio==4.13.0
|
| 4 |
beautifulsoup4==4.14.3
|
| 5 |
-
boto3==1.43.19
|
| 6 |
botocore==1.43.19
|
| 7 |
certifi==2026.5.20
|
| 8 |
charset-normalizer==3.4.7
|
|
@@ -23,7 +23,7 @@ matplotlib==3.10.9
|
|
| 23 |
mpmath==1.3.0
|
| 24 |
networkx==3.6.1
|
| 25 |
numpy==2.4.4
|
| 26 |
-
opencv-python==4.13.0.92
|
| 27 |
packaging==26.2
|
| 28 |
pillow==12.2.0
|
| 29 |
polars==1.41.2
|
|
@@ -50,4 +50,4 @@ typing_extensions==4.15.0
|
|
| 50 |
ultralytics==8.4.57
|
| 51 |
ultralytics-thop==2.0.19
|
| 52 |
urllib3==2.7.0
|
| 53 |
-
uvicorn==0.48.0
|
|
|
|
| 2 |
annotated-types==0.7.0
|
| 3 |
anyio==4.13.0
|
| 4 |
beautifulsoup4==4.14.3
|
| 5 |
+
boto3==1.43.19
|
| 6 |
botocore==1.43.19
|
| 7 |
certifi==2026.5.20
|
| 8 |
charset-normalizer==3.4.7
|
|
|
|
| 23 |
mpmath==1.3.0
|
| 24 |
networkx==3.6.1
|
| 25 |
numpy==2.4.4
|
| 26 |
+
opencv-python-headless==4.13.0.92
|
| 27 |
packaging==26.2
|
| 28 |
pillow==12.2.0
|
| 29 |
polars==1.41.2
|
|
|
|
| 50 |
ultralytics==8.4.57
|
| 51 |
ultralytics-thop==2.0.19
|
| 52 |
urllib3==2.7.0
|
| 53 |
+
uvicorn==0.48.0
|
src/app/app.py
CHANGED
|
@@ -37,13 +37,6 @@ model_configs = {
|
|
| 37 |
# ==========================================
|
| 38 |
# 2. FUNCIONES AUXILIARES
|
| 39 |
# ==========================================
|
| 40 |
-
def calculate_file_md5(filepath):
|
| 41 |
-
hash_md5 = hashlib.md5()
|
| 42 |
-
with open(filepath, "rb") as f:
|
| 43 |
-
for chunk in iter(lambda: f.read(4096), b""):
|
| 44 |
-
hash_md5.update(chunk)
|
| 45 |
-
return hash_md5.hexdigest()
|
| 46 |
-
|
| 47 |
def get_s3_client():
|
| 48 |
"""Retorna un cliente s3 configurado para Cloudflare R2"""
|
| 49 |
return boto3.client(
|
|
@@ -78,9 +71,6 @@ def download_models_from_r2():
|
|
| 78 |
else:
|
| 79 |
print(f"El modelo {name} ya existe localmente. Cargando...")
|
| 80 |
|
| 81 |
-
local_md5 = calculate_file_md5(config["path"])
|
| 82 |
-
print(f"[DEBUG] MD5 del modelo {name} en disco: {local_md5}")
|
| 83 |
-
|
| 84 |
del s3_client
|
| 85 |
gc.collect()
|
| 86 |
|
|
@@ -134,16 +124,11 @@ def detection_vef(image: UploadFile):
|
|
| 134 |
|
| 135 |
imageBytes = image.file.read()
|
| 136 |
|
| 137 |
-
raw_md5 = hashlib.md5(imageBytes).hexdigest()
|
| 138 |
-
print(f"[DEBUG] MD5 de la foto cruda recibida: {raw_md5}")
|
| 139 |
-
|
| 140 |
imageStream = io.BytesIO(imageBytes)
|
| 141 |
imageFile = Image.open(imageStream).convert("RGB")
|
| 142 |
|
| 143 |
debug_buffer = io.BytesIO()
|
| 144 |
imageFile.save(debug_buffer, format="JPEG")
|
| 145 |
-
pillow_md5 = hashlib.md5(debug_buffer.getvalue()).hexdigest()
|
| 146 |
-
print(f"[DEBUG] MD5 de la foto tras procesar con Pillow (RGB): {pillow_md5}")
|
| 147 |
|
| 148 |
which_currency = models["INFERENCIA"].predict(imageFile, verbose=False, imgsz=640, conf=0.25)
|
| 149 |
|
|
|
|
| 37 |
# ==========================================
|
| 38 |
# 2. FUNCIONES AUXILIARES
|
| 39 |
# ==========================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
def get_s3_client():
|
| 41 |
"""Retorna un cliente s3 configurado para Cloudflare R2"""
|
| 42 |
return boto3.client(
|
|
|
|
| 71 |
else:
|
| 72 |
print(f"El modelo {name} ya existe localmente. Cargando...")
|
| 73 |
|
|
|
|
|
|
|
|
|
|
| 74 |
del s3_client
|
| 75 |
gc.collect()
|
| 76 |
|
|
|
|
| 124 |
|
| 125 |
imageBytes = image.file.read()
|
| 126 |
|
|
|
|
|
|
|
|
|
|
| 127 |
imageStream = io.BytesIO(imageBytes)
|
| 128 |
imageFile = Image.open(imageStream).convert("RGB")
|
| 129 |
|
| 130 |
debug_buffer = io.BytesIO()
|
| 131 |
imageFile.save(debug_buffer, format="JPEG")
|
|
|
|
|
|
|
| 132 |
|
| 133 |
which_currency = models["INFERENCIA"].predict(imageFile, verbose=False, imgsz=640, conf=0.25)
|
| 134 |
|