version: '3.8'

services:
  # Soprano TTS Server (NVIDIA GTX 1660 + CUDA)
  soprano:
    build:
      context: .
      dockerfile: Dockerfile.soprano
    container_name: miku-soprano-tts
    runtime: nvidia
    environment:
      - NVIDIA_VISIBLE_DEVICES=0  # GTX 1660
      - CUDA_VISIBLE_DEVICES=0
    networks:
      - miku-voice
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              device_ids: ['0']  # GTX 1660
              capabilities: [gpu]
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "python3", "-c", "import zmq; ctx = zmq.Context(); s = ctx.socket(zmq.REQ); s.connect('tcp://localhost:5555'); s.close()"]
      interval: 30s
      timeout: 10s
      start_period: 60s
      retries: 3

  # RVC API Server (AMD RX 6800 + ROCm)
  rvc:
    build:
      context: .
      dockerfile: Dockerfile.rvc
    container_name: miku-rvc-api
    command: ["python", "soprano_rvc_api.py"]
    volumes:
      - /opt/rocm:/opt/rocm:ro  # Mount host ROCm (read-only)
    devices:
      - /dev/kfd:/dev/kfd
      - /dev/dri:/dev/dri
    group_add:
      - "989"  # render group (for /dev/dri access)
      - "985"  # video group
    cap_add:
      - SYS_PTRACE
    security_opt:
      - seccomp:unconfined
    environment:
      - HSA_OVERRIDE_GFX_VERSION=10.3.0  # For RX 6800 (gfx1030)
      - HSA_FORCE_FINE_GRAIN_PCIE=1
      - SOPRANO_SERVER=tcp://soprano:5555
      - ROCR_VISIBLE_DEVICES=0
      - GPU_DEVICE=cuda  # Force RVC to use 'cuda' device name for ROCm
      - DISABLE_ADDONS=1  # Disable fairseq/apex addons to avoid C++ extension issues
      - LD_LIBRARY_PATH=/opt/rocm/lib:/opt/rocm/lib64:$LD_LIBRARY_PATH
      - PATH=/opt/rocm/bin:$PATH
    ports:
      - "8765:8765"  # Expose HTTP API externally
    networks:
      - miku-voice
    depends_on:
      soprano:
        condition: service_healthy
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8765/health"]
      interval: 30s
      timeout: 10s
      start_period: 120s
      retries: 3

networks:
  miku-voice:
    name: miku-voice-network
    driver: bridge
