FROM python:3.10-slim

WORKDIR /app

# Install system dependencies
# We need ffmpeg for video processing and libsm6 libxext6 for opencv (if needed by backgroundremover)
RUN apt-get update && apt-get install -y \
    ffmpeg \
    libsm6 \
    libxext6 \
    wget \
    && rm -rf /var/lib/apt/lists/*

# Pre-create the directory for u2net models so they persist if mounted,
# or at least get downloaded to a known location
ENV U2NET_HOME=/root/.u2net
RUN mkdir -p /root/.u2net

COPY requirements.txt .

RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# We explicitly pre-download the model by running backgroundremover on a dummy image if we wanted,
# but the script downloads it on first run. To avoid huge first-request times,
# you could add a script here to download the u2net model directly.
# wget https://github.com/nadermx/backgroundremover/raw/main/models/u2net.pth -O /root/.u2net/u2net.pth (if available)

COPY . .

EXPOSE 8000

# Using shm-size is handled in docker-compose, but we set workers to 1 
# because video processing is extremely heavy and multiprocessing can crash without enough shm
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]
