File size: 5,261 Bytes
7658264
61d8ed5
7658264
985ca8d
7658264
3e30b9f
7658264
 
1e4ffa5
7658264
3e30b9f
 
 
 
 
9ef67c4
 
 
 
 
 
 
 
 
 
47f3eeb
 
a639f0f
7658264
d140df0
3e30b9f
 
9ef67c4
31fcacb
02f88ee
 
 
7658264
 
 
 
 
 
 
 
53020db
 
 
 
 
d140df0
 
 
 
 
 
dd8dfa2
 
53020db
02f88ee
 
9ef67c4
31fcacb
53020db
d140df0
 
 
 
61d8ed5
7658264
02f88ee
 
 
61d8ed5
 
 
 
6ee66f7
61d8ed5
 
6ee66f7
 
 
 
 
02f88ee
 
 
 
6968b9f
 
02f88ee
6968b9f
 
 
 
 
 
31fcacb
4d1e6bf
205fa6a
c8627a0
205fa6a
4d64af2
 
29cc6b6
11d45f5
 
 
 
510a24d
 
 
6b0a494
 
4d64af2
6b0a494
7ed02ff
 
6b0a494
 
4d64af2
 
 
 
 
6b0a494
 
 
7ed02ff
7ce12d5
11d45f5
29cc6b6
 
7658264
 
 
 
 
 
 
eaea5df
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# 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}"]