remove.bg / Dockerfile
srayuth
Use PyTorch cu124 to match system CUDA 12.4
bd50768
raw
history blame
2.1 kB
# 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"]