FROM nvidia/cuda:12.9.0-runtime-ubuntu24.04

# Avoid interactive prompts during package installation
ENV DEBIAN_FRONTEND=noninteractive

# Install Python and system dependencies. libsndfile1 for soundfile (wav writing); ffmpeg for
# torchaudio/librosa audio I/O.
RUN apt-get update && apt-get install -y --no-install-recommends \
    python3 \
    python3-pip \
    python3-dev \
    libsndfile1 \
    ffmpeg \
    && rm -rf /var/lib/apt/lists/*

# Set Python alias (Ubuntu 24.04 ships Python 3.12, which liquid-audio requires)
RUN ln -sf /usr/bin/python3 /usr/bin/python
ENV PIP_BREAK_SYSTEM_PACKAGES=1

WORKDIR /app

# Install PyTorch (cu128 wheels for CUDA 12.8+/12.9 compat). 2.8.0 is the version liquid-audio's
# uv.lock tests against (pyproject: torch>=2.8.0).
RUN pip install --no-cache-dir \
    torch==2.8.0 \
    torchaudio==2.8.0 \
    --index-url https://download.pytorch.org/whl/cu128

# liquid-audio (LFM2.5-Audio inference). Its pyproject only sets lower bounds, so pip would pull
# the latest transformers; pin the versions from its uv.lock instead, since the model drives
# transformers' Lfm2Model with its own KV/conv cache loop.
# flash-attn is deliberately NOT installed (optional upstream; it falls back to torch SDPA).
RUN pip install --no-cache-dir \
    "liquid-audio==1.3.0" \
    "transformers==4.56.1" \
    "accelerate==1.10.1" \
    soundfile \
    tqdm

# Build-time smoke test, so a broken dependency fails the BUILD rather than a GPU job.
RUN python -c "from liquid_audio import ChatState, LFM2AudioModel, LFM2AudioProcessor; print('liquid-audio OK')"

# Weights are NOT baked in: they are pulled from the Hub (LiquidAI/LFM2.5-Audio-1.5B, ungated)
# into the HF cache at first run.

# Copy the full repository
COPY . /app

# Default entrypoint
ENTRYPOINT ["bash"]

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