# syntax=docker/dockerfile:1 FROM python:3.10-slim # --- System dependencies --- # libgl1/libglib2.0-0 are needed by Pillow/torchvision/opencv image codecs on slim images # git is needed only at build time, to vendor the model repo below RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ libgl1 \ libglib2.0-0 \ git \ && rm -rf /var/lib/apt/lists/* # --- App setup --- WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY app.py . # --- Vendor the AnimeGANv2 model code at BUILD time --- # torch.hub.load() normally calls GitHub's API at every container startup to # check the repo isn't a fork — under shared/free hosting this regularly hits # GitHub's rate limit (HTTP 403) and crashes the app before it can even start. # Cloning the repo here, once, at build time avoids that runtime network call # entirely. app.py loads from this local path with source="local" instead. RUN git clone --depth 1 https://github.com/bryandlee/animegan2-pytorch.git /app/animegan2-pytorch # --- Hugging Face Spaces (Docker SDK) requirements --- # Spaces run containers as a non-root user and expect the app on port 7860. # Create the user and point TORCH_HOME at its cache dir BEFORE downloading # weights below, so the cached files land somewhere this user can read. RUN useradd -m -u 1000 appuser ENV HOME=/home/appuser \ TORCH_HOME=/home/appuser/.cache/torch RUN mkdir -p $TORCH_HOME && chown -R appuser:appuser /app /home/appuser # --- Pre-download model weights at BUILD time too --- # Same rate-limit risk applies to weight downloads, plus this removes the # ~30-60s cold-start delay on every fresh container. Runs as appuser so the # cache lands in $TORCH_HOME (set above) where the running app will look. USER appuser RUN python -c "\ import torch; \ torch.hub.load('/app/animegan2-pytorch', 'generator', source='local', pretrained='face_paint_512_v2', device='cpu'); \ torch.hub.load('/app/animegan2-pytorch', 'generator', source='local', pretrained='face_paint_512_v1', device='cpu'); \ torch.hub.load('/app/animegan2-pytorch', 'generator', source='local', pretrained='celeba_distill', device='cpu'); \ torch.hub.load('/app/animegan2-pytorch', 'generator', source='local', pretrained='paprika', device='cpu'); \ " EXPOSE 7860 CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]