# docker-compose.yml - PaddleOCR REST API
# Usage:
#   CPU:   docker compose up ocr-cpu
#   GPU:   docker compose up ocr-gpu
#   Test:  docker compose run --rm test
#   Build: CUDA_ARCH=120 docker compose --profile build run --rm build-paddle
#
# Auto-detect CUDA arch before building:
#   export CUDA_ARCH=$(nvidia-smi --query-gpu=compute_cap --format=csv,noheader | head -1 | tr -d '.')
#   docker compose --profile build run --rm build-paddle

services:
  # PaddlePaddle GPU wheel builder (ARM64 only, one-time build)
  # Creates ./wheels/paddlepaddle_gpu-*.whl for ARM64 GPU support
  # CUDA_ARCH env var controls target GPU architecture (default: 120 for Blackwell base)
  build-paddle:
    build:
      context: .
      dockerfile: Dockerfile.build-paddle
      args:
        CUDA_ARCH: ${CUDA_ARCH:-120}
    volumes:
      - ./wheels:/wheels
    profiles:
      - build
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]

  # CPU-only service (works on any architecture)
  ocr-cpu:
    build:
      context: .
      dockerfile: Dockerfile.cpu
      args:
        # Models to bake into image (change before building):
        DET_MODEL: PP-OCRv5_server_det
        REC_MODEL: PP-OCRv5_server_rec
    image: paddle-ocr-api:cpu
    container_name: paddle-ocr-cpu
    ports:
      - "8000:8000"
    volumes:
      - ../dataset:/app/dataset:ro
      - ../debugset:/app/debugset:rw          # Your dataset
      - paddlex-cache:/root/.paddlex        # For additional models at runtime
    environment:
      - PYTHONUNBUFFERED=1
      # Override models at runtime (uncomment to use different models):
      # - PADDLE_DET_MODEL=PP-OCRv5_mobile_det
      # - PADDLE_REC_MODEL=PP-OCRv5_mobile_rec
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  # GPU service (requires NVIDIA Container Toolkit)
  ocr-gpu:
    build:
      context: .
      dockerfile: Dockerfile.gpu
      args:
        DET_MODEL: PP-OCRv5_server_det
        REC_MODEL: PP-OCRv5_server_rec
    image: paddle-ocr-api:gpu
    container_name: paddle-ocr-gpu
    ports:
      - "8000:8000"
    volumes:
      - ../dataset:/app/dataset:ro
      - ../debugset:/app/debugset:rw
      - paddlex-cache:/root/.paddlex
    environment:
      - PYTHONUNBUFFERED=1
      - CUDA_VISIBLE_DEVICES=0
      # Override models at runtime:
      # - PADDLE_DET_MODEL=PP-OCRv5_mobile_det
      # - PADDLE_REC_MODEL=PP-OCRv5_mobile_rec
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]
    restart: unless-stopped

  # Test client (runs once and exits)
  test:
    image: python:3.11-slim
    container_name: paddle-ocr-test
    depends_on:
      ocr-cpu:
        condition: service_healthy
    volumes:
      - ./test.py:/app/test.py:ro
    working_dir: /app
    command: >
      sh -c "pip install -q requests && python test.py --url http://ocr-cpu:8000 --dataset /app/dataset"
    network_mode: "service:ocr-cpu"

volumes:
  paddlex-cache:
    name: paddlex-model-cache
