File size: 2,100 Bytes
8836a6d
 
 
56927ac
8836a6d
 
 
 
 
 
7bebf4c
8836a6d
7bebf4c
 
 
 
 
 
 
 
 
 
 
 
8836a6d
7bebf4c
8836a6d
bd50768
 
6a07f6e
 
7bebf4c
8836a6d
 
 
 
 
 
 
7bebf4c
 
 
 
 
 
 
 
 
 
b917401
8836a6d
 
7bebf4c
 
8836a6d
 
7bebf4c
 
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
# Hugging Face Spaces Dockerfile
# GPU-enabled Django API for background removal

FROM nvidia/cuda:12.4.1-cudnn-runtime-ubuntu22.04

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV DEBIAN_FRONTEND=noninteractive
ENV PORT=7860
ENV OMP_NUM_THREADS=4

# Install Python 3.11 and system dependencies
RUN apt-get update && \
    apt-get install -y --no-install-recommends software-properties-common && \
    add-apt-repository ppa:deadsnakes/ppa && \
    apt-get update && \
    apt-get install -y --no-install-recommends \
    python3.11 python3.11-venv python3.11-distutils \
    libgl1 libglib2.0-0 libsm6 libxext6 libxrender1 libgomp1 curl && \
    rm -rf /var/lib/apt/lists/* && \
    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 | python

WORKDIR /app

# Install PyTorch matching system CUDA 12.4 (avoids version conflict)
RUN pip install --no-cache-dir torch torchvision --index-url https://download.pytorch.org/whl/cu124

# Install remaining Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Create non-root user (HF Spaces requirement)
RUN useradd -m -u 1000 user
ENV HOME=/home/user
ENV HF_HOME=/home/user/.cache/huggingface

# Pre-download AI models so they don't re-download on every restart
USER user
RUN mkdir -p /home/user/.u2net /home/user/.config/Ultralytics && \
    python -c "from rembg import new_session; new_session('birefnet-general', providers=['CPUExecutionProvider']); new_session('isnet-general-use', providers=['CPUExecutionProvider'])"

# Copy application code
USER root
COPY . .
RUN chown -R user:user /app
USER user

EXPOSE 7860

# Health check (shorter start period since models are pre-cached)
HEALTHCHECK --interval=30s --timeout=30s --start-period=120s --retries=5 \
    CMD curl -f http://localhost:7860/api/transparent/health/ || exit 1

# --noreload prevents double model loading from Django's autoreload
CMD ["python", "manage.py", "runserver", "0.0.0.0:7860", "--noreload"]