name: export-telespeech-ctc-to-onnx

on:
  workflow_dispatch:

concurrency:
  group: export-telespeech-ctc-to-onnx-${{ github.ref }}
  cancel-in-progress: true

jobs:
  export-telespeech-ctc-to-onnx:
    if: github.repository_owner == 'k2-fsa' || github.repository_owner == 'csukuangfj'
    name: telespeech
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest]
        python-version: ["3.10"]

    steps:
      - uses: actions/checkout@v4

      - name: Setup Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install Python dependencies
        shell: bash
        run: |
          pip install onnx onnxruntime soundfile librosa numpy kaldi-native-fbank

      - name: Run
        shell: bash
        run: |
          cd scripts/tele-speech
          ./run.sh

          ./test.py

      - name: Release
        uses: svenstaro/upload-release-action@v2
        with:
          file_glob: true
          file: ./*.tar.bz2
          overwrite: true
          repo_name: k2-fsa/sherpa-onnx
          repo_token: ${{ secrets.UPLOAD_GH_SHERPA_ONNX_TOKEN }}
          tag: asr-models

      - name: Publish float32 model to huggingface
        shell: bash
        env:
          HF_TOKEN: ${{ secrets.HF_TOKEN }}
        run: |
          src=scripts/tele-speech/sherpa-onnx-telespeech-ctc-zh-2024-06-04
          git config --global user.email "csukuangfj@gmail.com"
          git config --global user.name "Fangjun Kuang"

          export GIT_CLONE_PROTECTION_ACTIVE=false

          GIT_LFS_SKIP_SMUDGE=1 git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-telespeech-ctc-zh-2024-06-04 hf
          cp -a $src/* hf/
          cd hf
          git lfs track "*.pdf"
          git lfs track "*.onnx"
          git add .
          git commit -m 'add model files' || true
          git status
          ls -lh
          git push https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-telespeech-ctc-zh-2024-06-04 main || true
          rm -rf hf

      - name: Publish int8 model to huggingface
        shell: bash
        env:
          HF_TOKEN: ${{ secrets.HF_TOKEN }}
        run: |
          src=scripts/tele-speech/sherpa-onnx-telespeech-ctc-int8-zh-2024-06-04
          git config --global user.email "csukuangfj@gmail.com"
          git config --global user.name "Fangjun Kuang"

          export GIT_CLONE_PROTECTION_ACTIVE=false

          rm -rf hf
          GIT_LFS_SKIP_SMUDGE=1 git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-telespeech-ctc-int8-zh-2024-06-04 hf
          cp -a $src/* hf/
          cd hf
          git lfs track "*.pdf"
          git lfs track "*.onnx"
          git add .
          git commit -m 'add model files' || true
          git status
          ls -lh
          git push https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-telespeech-ctc-int8-zh-2024-06-04 main || true
