# Underdog Woof is numpy-only CPU inference (no torch, no CUDA), so a slim Python base is enough.
# The leaderboard still runs stage 1 on the h200 flavor (for comparability); the model just uses
# that host's CPU.
FROM python:3.12-slim

ENV DEBIAN_FRONTEND=noninteractive PIP_NO_CACHE_DIR=1

# libsndfile1: soundfile (wav writing in run_eval.py). git: none needed (weights come from the Hub).
RUN apt-get update && apt-get install -y --no-install-recommends libsndfile1 \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# The runtime itself needs only numpy (requirements.txt: numpy>=1.24). The rest is the eval loop:
# datasets/soundfile/tqdm, threadpoolctl to pin the BLAS thread count from run_eval.py --threads,
# and huggingface_hub (pulled in by datasets) for the download below.
RUN pip install --upgrade pip && pip install \
    "numpy>=1.24,<3" soundfile datasets tqdm threadpoolctl huggingface_hub

# Woof ships as a repo, not a pip package: the `underdog_woof` package dir loads `woof_int8.npz`
# from its parent dir. Bake the repo at a pinned revision and put it on PYTHONPATH.
ARG WOOF_REVISION=be702960539777fc3f698e54335276a8391b00f9
ENV WOOF_DIR=/opt/underdog-woof WOOF_REVISION=${WOOF_REVISION}
RUN python -c "\
from huggingface_hub import snapshot_download; \
snapshot_download('ConwayResearch/Underdog-Woof', revision='${WOOF_REVISION}', local_dir='${WOOF_DIR}', \
                  allow_patterns=['underdog_woof/*', 'woof_int8.npz', 'LICENSE', 'NOTICE', 'requirements.txt'])" \
    && rm -rf ${WOOF_DIR}/.cache && ls -la ${WOOF_DIR} ${WOOF_DIR}/underdog_woof
ENV PYTHONPATH=${WOOF_DIR}

# Smoke-test both voices so a broken download or numpy incompatibility fails the BUILD, not a job.
RUN python -c "\
from underdog_woof import Woof, SR; \
import numpy as np; \
[print(v, (lambda a: (a.dtype, a.shape, round(len(a) / SR, 2)))(Woof(voice=v)('Call 415-555-0199 before 5:30 p.m. on Friday.', seed=0))) for v in ('juno', 'mira')]; \
np.show_config()"

# Copy the full repository (the eval scripts are injected at runtime by submit_jobs.sh)
COPY . /app

ENTRYPOINT ["bash"]

# Keep-alive CMD so the Space runtime stays healthy; `docker run` overrides it.
EXPOSE 7860
CMD ["-c", "python3 -m http.server 7860"]
