FROM nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04 ENV DEBIAN_FRONTEND=noninteractive ENV PYTHONUNBUFFERED=1 ENV PYTHONDONTWRITEBYTECODE=1 # --------------------------------------------------------------------------- # System dependencies + Python 3.11 # --------------------------------------------------------------------------- RUN apt-get update && apt-get install -y --no-install-recommends \ software-properties-common \ curl \ git \ build-essential \ libgl1-mesa-glx \ libglib2.0-0 \ libsm6 \ libxext6 \ libxrender-dev \ ffmpeg \ && add-apt-repository ppa:deadsnakes/ppa \ && apt-get update && apt-get install -y --no-install-recommends \ python3.11 \ python3.11-venv \ python3.11-dev \ python3.11-distutils \ && rm -rf /var/lib/apt/lists/* # --------------------------------------------------------------------------- # Set Python 3.11 as default + pip # --------------------------------------------------------------------------- RUN ln -sf /usr/bin/python3.11 /usr/bin/python \ && ln -sf /usr/bin/python3.11 /usr/bin/python3 \ && curl -sS https://bootstrap.pypa.io/get-pip.py | python3.11 \ && python -m pip install --no-cache-dir --upgrade pip setuptools wheel # --------------------------------------------------------------------------- # Create non-root user (HF Spaces requirement) # --------------------------------------------------------------------------- RUN useradd -m -u 1000 user ENV HOME=/home/user ENV PATH=/home/user/.local/bin:$PATH WORKDIR /app # --------------------------------------------------------------------------- # Install Python deps # --------------------------------------------------------------------------- COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # --------------------------------------------------------------------------- # Copy app code # --------------------------------------------------------------------------- COPY . . # --------------------------------------------------------------------------- # Install realesrgan (pulls PyPI basicsr==1.4.2 as dependency) # --------------------------------------------------------------------------- RUN pip install --no-cache-dir realesrgan 2>&1 || true # --------------------------------------------------------------------------- # FIX: Uninstall broken PyPI basicsr (it imports removed # torchvision.transforms.functional_tensor). CodeFormer's own local # basicsr (at /app/CodeFormer/basicsr/) works fine and is used instead # via PYTHONPATH below. # --------------------------------------------------------------------------- RUN pip uninstall -y basicsr 2>&1 || true # --------------------------------------------------------------------------- # Use CodeFormer's local basicsr + facelib via PYTHONPATH # This makes `import basicsr` and `import facelib` resolve to the # CodeFormer-bundled versions that are compatible with modern torchvision. # --------------------------------------------------------------------------- ENV PYTHONPATH="/app/CodeFormer:${PYTHONPATH}" # --------------------------------------------------------------------------- # Download pretrained models (now uses local basicsr — no import errors) # --------------------------------------------------------------------------- RUN python CodeFormer/scripts/download_pretrained_models.py facelib 2>&1 || true RUN python CodeFormer/scripts/download_pretrained_models.py CodeFormer 2>&1 || true # --------------------------------------------------------------------------- # Directories + permissions # --------------------------------------------------------------------------- RUN mkdir -p /app/models /tmp/faceswap \ && chown -R user:user /app /tmp/faceswap /home/user # --------------------------------------------------------------------------- # Switch to non-root # --------------------------------------------------------------------------- USER user EXPOSE 7860 # --------------------------------------------------------------------------- # PRODUCTION CMD (GPU SAFE) # --------------------------------------------------------------------------- CMD ["gunicorn","app:fastapi_app","--workers=1","--worker-class=uvicorn.workers.UvicornWorker","--bind=0.0.0.0:7860","--timeout=600","--graceful-timeout=300","--keep-alive=5","--max-requests=200","--max-requests-jitter=50","--access-logfile=-","--error-logfile=-","--log-level=info"] # # ============================================================================ # # FaceSwap API - HuggingFace Spaces Docker SDK (FIXED) # # Production-ready with Gunicorn + Uvicorn workers # # ============================================================================ # FROM nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04 # ENV DEBIAN_FRONTEND=noninteractive # ENV PYTHONUNBUFFERED=1 # ENV PYTHONDONTWRITEBYTECODE=1 # # --------------------------------------------------------------------------- # # System dependencies + Python 3.11 # # --------------------------------------------------------------------------- # RUN apt-get update && apt-get install -y --no-install-recommends \ # software-properties-common \ # curl \ # git \ # build-essential \ # libgl1-mesa-glx \ # libglib2.0-0 \ # libsm6 \ # libxext6 \ # libxrender-dev \ # ffmpeg \ # && add-apt-repository ppa:deadsnakes/ppa \ # && apt-get update && apt-get install -y --no-install-recommends \ # python3.11 \ # python3.11-venv \ # python3.11-dev \ # python3.11-distutils \ # && rm -rf /var/lib/apt/lists/* # # --------------------------------------------------------------------------- # # Set Python 3.11 as default + pip # # --------------------------------------------------------------------------- # RUN ln -sf /usr/bin/python3.11 /usr/bin/python \ # && ln -sf /usr/bin/python3.11 /usr/bin/python3 \ # && curl -sS https://bootstrap.pypa.io/get-pip.py | python3.11 \ # && python -m pip install --no-cache-dir --upgrade pip setuptools wheel # # --------------------------------------------------------------------------- # # Create non-root user (HF Spaces requirement) # # --------------------------------------------------------------------------- # RUN useradd -m -u 1000 user # ENV HOME=/home/user # ENV PATH=/home/user/.local/bin:$PATH # WORKDIR /app # # --------------------------------------------------------------------------- # # Install Python deps # # --------------------------------------------------------------------------- # COPY requirements.txt . # RUN pip install --no-cache-dir -r requirements.txt # # --------------------------------------------------------------------------- # # Copy app code # # --------------------------------------------------------------------------- # COPY . . # # --------------------------------------------------------------------------- # # Install BasicSR (required for CodeFormer) # # --------------------------------------------------------------------------- # RUN cd /app && python CodeFormer/basicsr/setup.py develop 2>&1 || true # # --------------------------------------------------------------------------- # # Install realesrgan # # --------------------------------------------------------------------------- # RUN pip install --no-cache-dir realesrgan 2>&1 || true # # --------------------------------------------------------------------------- # # Download pretrained models # # --------------------------------------------------------------------------- # RUN python CodeFormer/scripts/download_pretrained_models.py facelib 2>&1 || true # RUN python CodeFormer/scripts/download_pretrained_models.py CodeFormer 2>&1 || true # # --------------------------------------------------------------------------- # # Directories + permissions # # --------------------------------------------------------------------------- # RUN mkdir -p /app/models /tmp/faceswap \ # && chown -R user:user /app /tmp/faceswap /home/user # # --------------------------------------------------------------------------- # # Switch to non-root # # --------------------------------------------------------------------------- # USER user # EXPOSE 7860 # # --------------------------------------------------------------------------- # # PRODUCTION CMD (GPU SAFE) # # --------------------------------------------------------------------------- # CMD ["gunicorn","app:fastapi_app","--workers=1","--worker-class=uvicorn.workers.UvicornWorker","--bind=0.0.0.0:7860","--timeout=300","--graceful-timeout=120","--keep-alive=5","--max-requests=200","--max-requests-jitter=50","--access-logfile=-","--error-logfile=-","--log-level=info"] # # # ============================================================================ # # # FaceSwap API - HuggingFace Spaces Docker SDK # # # Production-ready with Gunicorn + Uvicorn workers # # # ============================================================================ # # FROM nvidia/cuda:11.8.0-cudnn8-devel-ubuntu22.04 # # ENV DEBIAN_FRONTEND=noninteractive # # ENV PYTHONUNBUFFERED=1 # # ENV PYTHONDONTWRITEBYTECODE=1 # # # --------------------------------------------------------------------------- # # # System dependencies + Python 3.11 # # # --------------------------------------------------------------------------- # # RUN apt-get update && apt-get install -y --no-install-recommends \ # # software-properties-common \ # # curl \ # # git \ # # build-essential \ # # libgl1-mesa-glx \ # # libglib2.0-0 \ # # libsm6 \ # # libxext6 \ # # libxrender-dev \ # # ffmpeg \ # # && add-apt-repository ppa:deadsnakes/ppa \ # # && apt-get update && apt-get install -y --no-install-recommends \ # # python3.11 \ # # python3.11-venv \ # # python3.11-dev \ # # python3.11-distutils \ # # && rm -rf /var/lib/apt/lists/* # # # --------------------------------------------------------------------------- # # # Set Python 3.11 as default + install pip # # # --------------------------------------------------------------------------- # # RUN ln -sf /usr/bin/python3.11 /usr/bin/python \ # # && ln -sf /usr/bin/python3.11 /usr/bin/python3 \ # # && curl -sS https://bootstrap.pypa.io/get-pip.py | python3.11 \ # # && python -m pip install --no-cache-dir --upgrade pip setuptools wheel # # # --------------------------------------------------------------------------- # # # Create non-root user (HuggingFace Spaces requirement) # # # --------------------------------------------------------------------------- # # RUN useradd -m -u 1000 user # # ENV HOME=/home/user # # ENV PATH=/home/user/.local/bin:$PATH # # WORKDIR /app # # # --------------------------------------------------------------------------- # # # Install Python dependencies (cached Docker layer) # # # --------------------------------------------------------------------------- # # COPY requirements.txt . # # RUN pip install --no-cache-dir -r requirements.txt # # # --------------------------------------------------------------------------- # # # Copy full application code # # # --------------------------------------------------------------------------- # # COPY . . # # # --------------------------------------------------------------------------- # # # Install BasicSR from local source (required by CodeFormer) # # # --------------------------------------------------------------------------- # # RUN cd /app && python CodeFormer/basicsr/setup.py develop 2>&1 || \ # # echo "WARNING: BasicSR install failed (will retry at runtime)" # # # --------------------------------------------------------------------------- # # # Install realesrgan (depends on BasicSR) # # # --------------------------------------------------------------------------- # # RUN pip install --no-cache-dir realesrgan 2>&1 || \ # # echo "WARNING: realesrgan install failed (will retry at runtime)" # # # --------------------------------------------------------------------------- # # # Pre-download CodeFormer pretrained models # # # --------------------------------------------------------------------------- # # RUN python CodeFormer/scripts/download_pretrained_models.py facelib 2>&1 || \ # # echo "WARNING: facelib models download failed (will retry at runtime)" # # RUN python CodeFormer/scripts/download_pretrained_models.py CodeFormer 2>&1 || \ # # echo "WARNING: CodeFormer models download failed (will retry at runtime)" # # # --------------------------------------------------------------------------- # # # Ensure directories + permissions for non-root user # # # --------------------------------------------------------------------------- # # RUN mkdir -p /app/models /tmp/faceswap \ # # && chown -R user:user /app /tmp/faceswap /home/user # # # --------------------------------------------------------------------------- # # # Switch to non-root user # # # --------------------------------------------------------------------------- # # USER user # # EXPOSE 7860 # # # --------------------------------------------------------------------------- # # # Production: Gunicorn with Uvicorn ASGI workers # # # # # # --workers 2 : 2 processes (each loads ML models ~1-2GB GPU) # # # --worker-class : UvicornWorker for async FastAPI support # # # --timeout 300 : Face swap + enhancement can take 30-120s # # # --graceful-timeout : Allow 120s for in-flight requests on shutdown # # # --max-requests 500 : Restart workers to prevent GPU memory leaks # # # --------------------------------------------------------------------------- # # # CMD ["gunicorn", "app:fastapi_app", \ # # # "--workers", "2", \ # # # "--worker-class", "uvicorn.workers.UvicornWorker", \ # # # "--bind", "0.0.0.0:7860", \ # # # "--timeout", "300", \ # # # "--graceful-timeout", "120", \ # # # "--keep-alive", "5", \ # # # "--max-requests", "500", \ # # # "--max-requests-jitter", "50", \ # # # "--access-logfile", "-", \ # # # "--error-logfile", "-", \ # # # "--log-level", "info"] # # CMD ["gunicorn", "app:fastapi_app", # # "--workers=1", # # "--worker-class=uvicorn.workers.UvicornWorker", # # "--bind=0.0.0.0:7860", # # "--timeout=300", # # "--graceful-timeout=120", # # "--keep-alive=5", # # "--max-requests=200", # # "--max-requests-jitter=50", # # "--access-logfile=-", # # "--error-logfile=-", # # "--log-level=info"]