Spaces:
Running
on
T4
Running
on
T4
lcm
Browse files- Dockerfile +17 -15
- app.py +31 -17
Dockerfile
CHANGED
@@ -1,30 +1,32 @@
|
|
1 |
-
#
|
2 |
FROM nvidia/cuda:11.7.1-cudnn8-runtime-ubuntu20.04
|
3 |
|
4 |
-
#
|
5 |
ENV TRANSFORMERS_CACHE="/app/.cache" \
|
6 |
-
HF_HOME="/app/.cache"
|
|
|
7 |
|
8 |
-
#
|
9 |
RUN apt-get update && apt-get install -y \
|
10 |
python3 \
|
11 |
python3-pip \
|
12 |
git \
|
13 |
&& rm -rf /var/lib/apt/lists/*
|
14 |
|
15 |
-
#
|
16 |
-
RUN pip install --upgrade pip
|
17 |
-
RUN pip install torch torchvision diffusers fastapi uvicorn pillow prs-eth-marigold
|
18 |
-
|
19 |
-
# Crea directorios necesarios
|
20 |
WORKDIR /app
|
|
|
21 |
RUN mkdir -p /app/.cache && chmod -R 777 /app/.cache
|
22 |
|
23 |
-
# Copia el
|
24 |
-
COPY . /app
|
25 |
|
26 |
-
#
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
# Comando
|
30 |
-
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "
|
|
|
1 |
+
# Usa una imagen base con soporte para Python y CUDA
|
2 |
FROM nvidia/cuda:11.7.1-cudnn8-runtime-ubuntu20.04
|
3 |
|
4 |
+
# Establece variables de entorno necesarias
|
5 |
ENV TRANSFORMERS_CACHE="/app/.cache" \
|
6 |
+
HF_HOME="/app/.cache" \
|
7 |
+
PATH="/opt/conda/bin:$PATH"
|
8 |
|
9 |
+
# Instala dependencias b谩sicas
|
10 |
RUN apt-get update && apt-get install -y \
|
11 |
python3 \
|
12 |
python3-pip \
|
13 |
git \
|
14 |
&& rm -rf /var/lib/apt/lists/*
|
15 |
|
16 |
+
# Establece el directorio de trabajo
|
|
|
|
|
|
|
|
|
17 |
WORKDIR /app
|
18 |
+
|
19 |
RUN mkdir -p /app/.cache && chmod -R 777 /app/.cache
|
20 |
|
21 |
+
# Copia el archivo requirements.txt al contenedor
|
22 |
+
COPY requirements.txt /app/requirements.txt
|
23 |
|
24 |
+
# Instala las dependencias necesarias
|
25 |
+
RUN pip install --upgrade pip
|
26 |
+
RUN pip install -r requirements.txt
|
27 |
+
|
28 |
+
# Copia los archivos de la aplicaci贸n al contenedor
|
29 |
+
COPY . /app
|
30 |
|
31 |
+
# Comando para ejecutar la aplicaci贸n
|
32 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
@@ -1,30 +1,44 @@
|
|
1 |
-
|
|
|
|
|
2 |
from PIL import Image
|
3 |
-
from prs_eth_marigold import MarigoldDepthPipeline
|
4 |
|
5 |
app = FastAPI()
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
@app.post("/
|
13 |
-
async def
|
14 |
-
"""
|
15 |
-
Genera un mapa de profundidad a partir de una imagen.
|
16 |
-
"""
|
17 |
try:
|
18 |
-
|
|
|
|
|
19 |
|
20 |
-
|
|
|
21 |
|
22 |
-
|
23 |
-
|
|
|
24 |
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
26 |
except Exception as e:
|
27 |
-
|
|
|
|
|
28 |
|
29 |
@app.get("/")
|
30 |
async def root():
|
|
|
1 |
+
import diffusers
|
2 |
+
import torch
|
3 |
+
from fastapi import FastAPI, UploadFile, HTTPException
|
4 |
from PIL import Image
|
|
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
+
# Inicializa el pipeline al arrancar el servidor
|
9 |
+
@app.on_event("startup")
|
10 |
+
async def startup_event():
|
11 |
+
global pipe
|
12 |
+
print("[DEBUG] Cargando modelo Marigold...")
|
13 |
+
pipe = diffusers.MarigoldDepthPipeline.from_pretrained(
|
14 |
+
"prs-eth/marigold-depth-lcm-v1-0", variant="fp16", torch_dtype=torch.float16
|
15 |
+
).to("cuda")
|
16 |
+
print("[DEBUG] Modelo Marigold cargado exitosamente.")
|
17 |
|
18 |
+
@app.post("/predict-depth/")
|
19 |
+
async def predict_depth(file: UploadFile):
|
|
|
|
|
|
|
20 |
try:
|
21 |
+
# Verifica si el archivo es una imagen v谩lida
|
22 |
+
if not file.content_type.startswith("image/"):
|
23 |
+
raise HTTPException(status_code=400, detail="El archivo subido no es una imagen.")
|
24 |
|
25 |
+
# Carga la imagen desde el archivo subido
|
26 |
+
image = Image.open(file.file).convert("RGB")
|
27 |
|
28 |
+
# Realiza la predicci贸n de profundidad
|
29 |
+
print("[DEBUG] Realizando predicci贸n de profundidad...")
|
30 |
+
depth = pipe(image)
|
31 |
|
32 |
+
# Visualiza la profundidad
|
33 |
+
vis = pipe.image_processor.visualize_depth(depth.prediction)
|
34 |
+
output_path = "predicted_depth.png"
|
35 |
+
vis[0].save(output_path)
|
36 |
+
|
37 |
+
return {"message": "Predicci贸n completada", "output_file": output_path}
|
38 |
except Exception as e:
|
39 |
+
print(f"[ERROR] {str(e)}")
|
40 |
+
raise HTTPException(status_code=500, detail="Error procesando la imagen.")
|
41 |
+
|
42 |
|
43 |
@app.get("/")
|
44 |
async def root():
|