# Chatterbox Turbo (Resemble AI's 350M-param low-latency English TTS
# with zero-shot voice cloning) served via devnen/Chatterbox-TTS-Server
# — the most actively-maintained OpenAI-compat wrapper that supports
# the Turbo checkpoint.
#
# Why this stack exists alongside the other TTS:
#   * Low-latency English with VOICE CLONING (Kokoro is fast but
#     fixed-voice; this fills the speed-AND-cloning slot).
#   * Native paralinguistic tags inline in text:
#       [laugh] [cough] [sigh] [gasp] [whisper] [breath]
#     Different shape from IndexTTS-2's emotion vector — cleaner for
#     "say it like this" markup directly in the prompt.
#   * MIT weights + code; ~2.5 GB VRAM at fp16; ~75 ms latency.
#   * Mandatory PerTh watermark on outputs (Resemble policy, can't
#     be disabled). Non-issue for internal use.
#
# Image is built locally from the upstream Dockerfile via docker
# buildx git-context (no source vendored on the host).
#
# All tunables live in .env — edit that, not this file.

services:
  chatterbox:
    image: local/chatterbox:${CHATTERBOX_TAG}
    build:
      context: https://github.com/devnen/Chatterbox-TTS-Server.git#${CHATTERBOX_SHA}
      # Upstream moved Dockerfiles from docker/ to repo root and renamed
      # them by CUDA version (Dockerfile.cu128, .cpu, .rocm). The old
      # `docker/Dockerfile.gpu` is gone. cu128 is the GPU build for
      # CUDA 12.8 — fits irv-ml1's 595.58.03 driver (CUDA 13.2 capable,
      # backward-compatible to 12.8 toolkit-built images).
      dockerfile: Dockerfile.cu128
    container_name: chatterbox
    restart: unless-stopped
    runtime: nvidia
    ports:
      - "${CHATTERBOX_BIND:-0.0.0.0}:${CHATTERBOX_PORT}:8004"
    environment:
      - NVIDIA_VISIBLE_DEVICES=${CHATTERBOX_GPU_DEVICES:-0}
      # Switch to ResembleAI/chatterbox-turbo (default) or the base
      # ResembleAI/chatterbox / ResembleAI/chatterbox-multilingual
      # via the wrapper's config hot-swap.
      - CHATTERBOX_MODEL_REPO=${CHATTERBOX_MODEL_REPO:-ResembleAI/chatterbox-turbo}
      - HF_HOME=/app/hf_cache
    volumes:
      - ${CHATTERBOX_REFERENCE_DIR}:/app/reference_audio
      - ${CHATTERBOX_CACHE_DIR}:/app/hf_cache
      # Optional: mount config.yaml as a host file for hot-edit. Default
      # is to use the in-image config + env var overrides.
      # - ${CHATTERBOX_CONFIG}:/app/config.yaml:ro
    healthcheck:
      # The devnen wrapper does NOT expose /health (no such route — use
      # /docs or /openapi.json to enumerate). /api/model-info returns
      # `{"loaded":true,...}` only after the model finishes loading,
      # so it doubles as a liveness + ready probe.
      #
      # Use python urllib instead of curl: devnen's image is built
      # from a python:3.10 base and doesn't ship curl. Bind 127.0.0.1
      # explicitly — `localhost` on this image resolves to both ::1
      # and 127.0.0.1, and uvicorn binds IPv4 only (busybox doesn't
      # always retry on the v4 entry).
      test: ["CMD-SHELL", "python3 -c \"import urllib.request,sys; b=urllib.request.urlopen('http://127.0.0.1:8004/api/model-info', timeout=5).read(); sys.exit(0 if b'\\\"loaded\\\":true' in b else 1)\""]
      interval: 30s
      timeout: 10s
      retries: 3
      # First boot pulls Chatterbox-Turbo (~6 GB total HF assets) and
      # warms torch.compile — give it a generous deadline.
      start_period: 600s
    labels:
      - homepage.group=AI - Speech (TTS)
      - homepage.name=Chatterbox Turbo
      - homepage.icon=mdi-account-music-outline
      - homepage.description=Low-latency English TTS w/ voice cloning + paralinguistics (irv-ml1)
      - homepage.href=http://10.100.79.3:${CHATTERBOX_PORT}
