Spaces:
Running
on
T4
Running
on
T4
Create Dockerfile
Browse files- Dockerfile +114 -0
Dockerfile
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================================================
|
| 2 |
+
# FaceSwap API - HuggingFace Spaces Docker SDK
|
| 3 |
+
# Production-ready with Gunicorn + Uvicorn workers
|
| 4 |
+
# ============================================================================
|
| 5 |
+
FROM nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04
|
| 6 |
+
|
| 7 |
+
ENV DEBIAN_FRONTEND=noninteractive
|
| 8 |
+
ENV PYTHONUNBUFFERED=1
|
| 9 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 10 |
+
|
| 11 |
+
# ---------------------------------------------------------------------------
|
| 12 |
+
# System dependencies + Python 3.11
|
| 13 |
+
# ---------------------------------------------------------------------------
|
| 14 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 15 |
+
software-properties-common \
|
| 16 |
+
curl \
|
| 17 |
+
git \
|
| 18 |
+
build-essential \
|
| 19 |
+
libgl1-mesa-glx \
|
| 20 |
+
libglib2.0-0 \
|
| 21 |
+
libsm6 \
|
| 22 |
+
libxext6 \
|
| 23 |
+
libxrender-dev \
|
| 24 |
+
ffmpeg \
|
| 25 |
+
&& add-apt-repository ppa:deadsnakes/ppa \
|
| 26 |
+
&& apt-get update && apt-get install -y --no-install-recommends \
|
| 27 |
+
python3.11 \
|
| 28 |
+
python3.11-venv \
|
| 29 |
+
python3.11-dev \
|
| 30 |
+
python3.11-distutils \
|
| 31 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 32 |
+
|
| 33 |
+
# ---------------------------------------------------------------------------
|
| 34 |
+
# Set Python 3.11 as default + install pip
|
| 35 |
+
# ---------------------------------------------------------------------------
|
| 36 |
+
RUN ln -sf /usr/bin/python3.11 /usr/bin/python \
|
| 37 |
+
&& ln -sf /usr/bin/python3.11 /usr/bin/python3 \
|
| 38 |
+
&& curl -sS https://bootstrap.pypa.io/get-pip.py | python3.11 \
|
| 39 |
+
&& python -m pip install --no-cache-dir --upgrade pip setuptools wheel
|
| 40 |
+
|
| 41 |
+
# ---------------------------------------------------------------------------
|
| 42 |
+
# Create non-root user (HuggingFace Spaces requirement)
|
| 43 |
+
# ---------------------------------------------------------------------------
|
| 44 |
+
RUN useradd -m -u 1000 user
|
| 45 |
+
ENV HOME=/home/user
|
| 46 |
+
ENV PATH=/home/user/.local/bin:$PATH
|
| 47 |
+
|
| 48 |
+
WORKDIR /app
|
| 49 |
+
|
| 50 |
+
# ---------------------------------------------------------------------------
|
| 51 |
+
# Install Python dependencies (cached Docker layer)
|
| 52 |
+
# ---------------------------------------------------------------------------
|
| 53 |
+
COPY requirements.txt .
|
| 54 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 55 |
+
|
| 56 |
+
# ---------------------------------------------------------------------------
|
| 57 |
+
# Copy full application code
|
| 58 |
+
# ---------------------------------------------------------------------------
|
| 59 |
+
COPY . .
|
| 60 |
+
|
| 61 |
+
# ---------------------------------------------------------------------------
|
| 62 |
+
# Install BasicSR from local source (required by CodeFormer)
|
| 63 |
+
# ---------------------------------------------------------------------------
|
| 64 |
+
RUN cd /app && python CodeFormer/basicsr/setup.py develop 2>&1 || \
|
| 65 |
+
echo "WARNING: BasicSR install failed (will retry at runtime)"
|
| 66 |
+
|
| 67 |
+
# ---------------------------------------------------------------------------
|
| 68 |
+
# Install realesrgan (depends on BasicSR)
|
| 69 |
+
# ---------------------------------------------------------------------------
|
| 70 |
+
RUN pip install --no-cache-dir realesrgan 2>&1 || \
|
| 71 |
+
echo "WARNING: realesrgan install failed (will retry at runtime)"
|
| 72 |
+
|
| 73 |
+
# ---------------------------------------------------------------------------
|
| 74 |
+
# Pre-download CodeFormer pretrained models
|
| 75 |
+
# ---------------------------------------------------------------------------
|
| 76 |
+
RUN python CodeFormer/scripts/download_pretrained_models.py facelib 2>&1 || \
|
| 77 |
+
echo "WARNING: facelib models download failed (will retry at runtime)"
|
| 78 |
+
RUN python CodeFormer/scripts/download_pretrained_models.py CodeFormer 2>&1 || \
|
| 79 |
+
echo "WARNING: CodeFormer models download failed (will retry at runtime)"
|
| 80 |
+
|
| 81 |
+
# ---------------------------------------------------------------------------
|
| 82 |
+
# Ensure directories + permissions for non-root user
|
| 83 |
+
# ---------------------------------------------------------------------------
|
| 84 |
+
RUN mkdir -p /app/models /tmp/faceswap \
|
| 85 |
+
&& chown -R user:user /app /tmp/faceswap /home/user
|
| 86 |
+
|
| 87 |
+
# ---------------------------------------------------------------------------
|
| 88 |
+
# Switch to non-root user
|
| 89 |
+
# ---------------------------------------------------------------------------
|
| 90 |
+
USER user
|
| 91 |
+
|
| 92 |
+
EXPOSE 7860
|
| 93 |
+
|
| 94 |
+
# ---------------------------------------------------------------------------
|
| 95 |
+
# Production: Gunicorn with Uvicorn ASGI workers
|
| 96 |
+
#
|
| 97 |
+
# --workers 2 : 2 processes (each loads ML models ~1-2GB GPU)
|
| 98 |
+
# --worker-class : UvicornWorker for async FastAPI support
|
| 99 |
+
# --timeout 300 : Face swap + enhancement can take 30-120s
|
| 100 |
+
# --graceful-timeout : Allow 120s for in-flight requests on shutdown
|
| 101 |
+
# --max-requests 500 : Restart workers to prevent GPU memory leaks
|
| 102 |
+
# ---------------------------------------------------------------------------
|
| 103 |
+
CMD ["gunicorn", "app:fastapi_app", \
|
| 104 |
+
"--workers", "2", \
|
| 105 |
+
"--worker-class", "uvicorn.workers.UvicornWorker", \
|
| 106 |
+
"--bind", "0.0.0.0:7860", \
|
| 107 |
+
"--timeout", "300", \
|
| 108 |
+
"--graceful-timeout", "120", \
|
| 109 |
+
"--keep-alive", "5", \
|
| 110 |
+
"--max-requests", "500", \
|
| 111 |
+
"--max-requests-jitter", "50", \
|
| 112 |
+
"--access-logfile", "-", \
|
| 113 |
+
"--error-logfile", "-", \
|
| 114 |
+
"--log-level", "info"]
|