# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker # Dockerfile for Face Swap Video API - GPU Optimized, API Only (No Gradio) FROM python:3.10-slim # Install system dependencies including build tools and libraries RUN apt-get update && apt-get install -y \ ffmpeg \ libgl1 \ libglib2.0-0 \ libsm6 \ libxext6 \ libxrender-dev \ libgomp1 \ libgthread-2.0-0 \ libgtk-3-0 \ libavcodec-dev \ libavformat-dev \ libswscale-dev \ libv4l-dev \ libxvidcore-dev \ libx264-dev \ libjpeg-dev \ libpng-dev \ libtiff-dev \ libopenblas-dev \ libomp-dev \ python3-dev \ git \ build-essential \ gcc \ g++ \ cmake \ pkg-config \ curl \ wget \ ca-certificates \ && rm -rf /var/lib/apt/lists/* # Create user for Hugging Face Spaces (following HF Spaces best practices) RUN useradd -m -u 1000 user # Set working directory WORKDIR /app # Upgrade pip and install build tools RUN pip install --no-cache-dir --upgrade pip setuptools wheel # Install core API dependencies first RUN pip install --no-cache-dir \ fastapi>=0.115.2 \ uvicorn[standard]==0.24.0 \ python-multipart>=0.0.18 \ motor==3.3.2 \ pymongo==4.6.0 \ python-dotenv==1.0.0 \ dnspython==2.4.2 \ firebase-admin>=6.1.0 # Install numpy first (required by other packages) - use compatible version RUN pip install --no-cache-dir "numpy>=1.24.0,<1.27.0" # Install utility dependencies (install these before insightface as they might be dependencies) RUN pip install --no-cache-dir \ psutil==5.9.5 \ tqdm==4.65.0 \ Pillow \ imageio-ffmpeg \ huggingface_hub>=0.23.0 # Install OpenCV first (needed by insightface) RUN pip install --no-cache-dir opencv-python-headless # Install ONNX Runtime - Use CPU version for build compatibility # GPU will be detected automatically at runtime if available # CPU version works for both CPU and GPU, runtime will use GPU if CUDA is available RUN pip install --no-cache-dir onnxruntime || \ pip install --no-cache-dir "onnxruntime>=1.16.0" || \ pip install --no-cache-dir onnxruntime-cpu || \ pip install --no-cache-dir "onnxruntime-cpu>=1.16.0" || \ (echo "ERROR: All ONNX Runtime installation attempts failed" && exit 1) # Verify ONNX Runtime installation RUN python -c "import onnxruntime; print('ONNX Runtime version:', onnxruntime.__version__)" || \ (echo "ERROR: ONNX Runtime installation verification failed" && exit 1) # Install moviepy after other dependencies RUN pip install --no-cache-dir moviepy==1.0.3 # Install insightface - install from git directly for better compatibility RUN pip install --no-cache-dir --no-build-isolation git+https://github.com/deepinsight/insightface.git || \ pip install --no-cache-dir insightface || \ pip install --no-cache-dir "insightface>=0.7.0" || \ (echo "ERROR: Failed to install insightface" && exit 1) # Verify insightface installation RUN python -c "import insightface; print('InsightFace installed successfully')" || \ (echo "ERROR: InsightFace verification failed" && exit 1) # Copy only essential application files (before switching user) COPY --chown=user:user api_server.py /app/ COPY --chown=user:user firebase_app_check.py /app/ COPY --chown=user:user DeepFakeAI/ /app/DeepFakeAI/ # Ensure inswapper_128.onnx is baked into the image if present in repo COPY --chown=user:user DeepFakeAI/.assets/models/inswapper_128.onnx /app/DeepFakeAI/.assets/models/inswapper_128.onnx # Create upload directories with proper permissions before switching to user RUN mkdir -p /app/uploads/source_images /app/uploads/target_videos /app/uploads/result_videos && \ chown -R user:user /app/uploads # Ensure model cache directory is writable for runtime model downloads RUN mkdir -p /app/.assets/models && chown -R user:user /app/.assets # Optional preload of InsightFace inswapper_128.onnx (skips if no URL provided) ARG INSWAPPER_URL="" ARG INSWAPPER_TOKEN="" ENV INSIGHTFACE_HOME="/home/user/.insightface" ENV INSWAPPER_PATH="/app/DeepFakeAI/.assets/models/inswapper_128.onnx" RUN set -e; mkdir -p /app/DeepFakeAI/.assets/models /home/user/.insightface/models; \ if [ -n "$INSWAPPER_URL" ]; then \ echo "Downloading inswapper_128.onnx from $INSWAPPER_URL"; \ if [ -n "$INSWAPPER_TOKEN" ]; then \ curl -fsSL -H "Authorization: Bearer $INSWAPPER_TOKEN" "$INSWAPPER_URL" -o "$INSWAPPER_PATH" || echo "Warning: tokenized download failed, continuing without preloaded model"; \ else \ curl -fsSL "$INSWAPPER_URL" -o "$INSWAPPER_PATH" || echo "Warning: download failed, continuing without preloaded model"; \ fi; \ else \ echo "No INSWAPPER_URL provided; skipping model download"; \ fi; \ chown -R user:user /home/user/.insightface /app/DeepFakeAI/.assets # Switch to user (upload directories already created with proper permissions) USER user ENV PATH="/home/user/.local/bin:$PATH" # Set environment variables ENV PYTHONUNBUFFERED=1 # Expose port 7860 (required for Hugging Face Spaces) EXPOSE 7860 # Run via shell so ${PORT} expands (fallback to 7860) CMD ["/bin/sh", "-lc", "python -m uvicorn api_server:app --host 0.0.0.0 --port ${PORT:-7860}"]