name: 'model downloader'

on:
  pull_request:
    paths:
      - '.github/workflows/misc/model_list.json'
      - '.github/workflows/labled_download_model.yaml'
    types: [labeled, synchronize]

defaults:
  run:
    shell: bash -el {0}

concurrency:
  group: ascend-${{ github.workflow_ref }}
  cancel-in-progress: true

jobs:
  download-models:
    if: contains(github.event.pull_request.labels.*.name, 'model-download')
    name: Download models from ModelScope
    runs-on: linux-aarch64-a2-0
    container:
      image: swr.cn-southwest-2.myhuaweicloud.com/base_image/ascend-ci/vllm-ascend:nightly-cpu

    steps:
      - name: Install dependencies
        run: |
          apt-get update -y && apt-get install git jq -y
          pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
          pip install modelscope

      - name: Show Current Disk Usage
        run: |
          df -h /root/.cache | grep -v Filesystem | \
          awk '{print "Mount point: "$6, "Total: "$2, "Used: "$3, "Available: "$4, "Usage: "$5}'
      - name: Checkout PR branch
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Extract new models from PR
        id: diff
        run: |
          set -euo pipefail

          git config --global --add safe.directory /__w/vllm-ascend/vllm-ascend
          JSON_PATH=".github/workflows/misc/model_list.json"

          git fetch origin main

          git show origin/main:$JSON_PATH > /tmp/models_main.json || \
            echo '{"models":[]}' > /tmp/models_main.json

          cp $JSON_PATH /tmp/models_pr.json

          jq -r '
            (.models // []) as $pr
            | input
            | (.models // []) as $main
            | ($pr - $main)[]
          ' /tmp/models_pr.json /tmp/models_main.json > /tmp/new_models.txt

          echo "New models:"
          cat /tmp/new_models.txt || true

      - name: Download new models (CLI)
        run: |
          set -euo pipefail
          if [ ! -s /tmp/new_models.txt ]; then
            echo "No new models to download."
            exit 0
          fi

          while read -r model; do
            [ -z "$model" ] && continue
            echo "▶ Downloading $model"
            modelscope download "$model"
          done < /tmp/new_models.txt

      - name: Summary
        run: |
          echo "Downloaded models:"
          cat /tmp/new_models.txt || echo "No new models"
