# Omnizart Sidecar
#
# Wraps the Omnizart CLI as an HTTP service consumed by the main StemEngine
# backend.  The base image must have Omnizart and its dependencies installed.
#
# Build:
#   docker build -t omnizart-sidecar omnizart-sidecar/
#
# The easiest base is the official Omnizart Docker image (if published), or
# build from the Omnizart-UI backend image which already has Omnizart + deps.
# Adjust FROM as needed for your Omnizart installation.

# ── Stage 1: use an image that already has Omnizart installed ───────────────
# Replace this with the actual image that has omnizart on PATH.
# Example options:
#   FROM music-ai-ntu/omnizart:latest        (official, if available)
#   FROM your-registry/omnizart-base:latest
# For development you can also build from the Omnizart-UI backend Dockerfile.
FROM python:3.8-slim AS base

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
      ffmpeg \
      build-essential \
      git \
      portaudio19-dev \
      libsndfile1 \
    && rm -rf /var/lib/apt/lists/*

# Omnizart's dependency chain includes packages (notably `vamp`) that import
# numpy during setup.py metadata generation, so numpy must already be present.
RUN pip install --no-cache-dir --upgrade pip setuptools wheel \
  && pip install --no-cache-dir "numpy<2" "Cython<3"

# Install Omnizart and its Python dependencies
# NOTE: Omnizart requires Python 3.8 and specific TensorFlow/numpy versions.
# This step can take 10-20 min on first build due to TensorFlow.
RUN pip install --no-cache-dir omnizart

# ── Stage 2: install sidecar app dependencies ────────────────────────────────
RUN pip install --no-cache-dir fastapi uvicorn[standard] python-multipart

WORKDIR /app
COPY app.py /app/app.py

EXPOSE 9765

# Download Omnizart checkpoints at build time (best effort).
# NOTE: the runtime volume mounted at /root/.omnizart can shadow these,
# so app.py also performs on-demand checkpoint bootstrap when needed.
RUN omnizart download-checkpoints --output-path /root/.omnizart || true

HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
  CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:9765/health')"

CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "9765", "--workers", "1"]
