Skip to content

Commit

Permalink
Export whisper models to torchscript. (#693)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Jan 8, 2025
1 parent e5b91cd commit 2ffe462
Show file tree
Hide file tree
Showing 10 changed files with 1,254 additions and 1 deletion.
168 changes: 168 additions & 0 deletions .github/workflows/export-whisper.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
name: export-whisper

on:
push:
branches:
- export-whisper
workflow_dispatch:

concurrency:
group: export-whisper-${{ github.ref }}
cancel-in-progress: true

jobs:
export-whisper:
if: github.repository_owner == 'k2-fsa' || github.repository_owner == 'csukuangfj'
name: export whisper ${{ matrix.model }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest]
model: ["turbo", "distil-medium.en", "distil-small.en", "tiny.en", "base.en", "small.en", "medium.en", "tiny", "base", "small", "medium", "medium-aishell", "large", "large-v1", "large-v2", "large-v3", "distil-large-v2"]
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 dependencies
shell: bash
run: |
python3 -m pip install torch==1.13.0 torchaudio==0.13.0 -f https://download.pytorch.org/whl/cpu/torch_stable.html numpy==1.26.4 kaldi-native-fbank
python3 -m pip install -U openai-whisper
python3 -m pip install onnx soundfile librosa
- name: Download model files for ${{ matrix.model }}
shell: bash
run: |
pushd scripts/whisper
model=${{ matrix.model }}
echo "model: $model"
if [[ $model == distil-medium.en ]]; then
wget -q -O distil-medium-en-original-model.bin https://huggingface.co/distil-whisper/distil-medium.en/resolve/main/original-model.bin
ls -lh
elif [[ $model == distil-large-v2 ]]; then
wget -q -O distil-large-v2-original-model.bin https://huggingface.co/distil-whisper/distil-large-v2/resolve/main/original-model.bin
ls -lh
elif [[ $model == distil-small.en ]]; then
wget -q -O distil-small-en-original-model.bin https://huggingface.co/distil-whisper/distil-small.en/resolve/main/original-model.bin
ls -lh
elif [[ $model == medium-aishell ]]; then
wget -q -O medium-aishell.pt https://huggingface.co/yuekai/icefall_asr_aishell_whisper/resolve/main/exp_medium/whisper-medium-aishell1-epoch-10-avg-4.pt
ls -lh
fi
- name: Export ${{ matrix.model }}
shell: bash
run: |
pushd scripts/whisper
export name=${{ matrix.model }}
./run.sh
- name: Test ${{ matrix.model }}
shell: bash
run: |
pushd scripts/whisper
curl -SL -O https://huggingface.co/csukuangfj/sherpa-onnx-streaming-zipformer-en-2023-02-21/resolve/main/test_wavs/0.wav
./test.py
- name: Collect results
shell: bash
run: |
d=sherpa-whisper-${{ matrix.model }}
mkdir $d
mv -v scripts/whisper/model.pt $d/
mv -v scripts/whisper/tokens.txt $d/
mv -v scripts/whisper/README.md $d/
pushd $d
mkdir test_wavs
cd test_wavs
curl -SL -O https://huggingface.co/csukuangfj/sherpa-onnx-streaming-zipformer-en-2023-02-21/resolve/main/test_wavs/0.wav
curl -SL -O https://huggingface.co/csukuangfj/sherpa-onnx-streaming-zipformer-en-2023-02-21/resolve/main/test_wavs/1.wav
curl -SL -O https://huggingface.co/csukuangfj/sherpa-onnx-streaming-zipformer-en-2023-02-21/resolve/main/test_wavs/8k.wav
curl -SL -O https://huggingface.co/csukuangfj/sherpa-onnx-streaming-zipformer-en-2023-02-21/resolve/main/test_wavs/trans.txt
popd
ls -lh $d
tar cjvf $d.tar.bz2 $d
echo "---"
ls -lh
- name: Release
if: matrix.model != 'large' && matrix.model != 'large-v1' && matrix.model != 'large-v2' && matrix.model != 'large-v3' && matrix.model != 'medium-aishell'
uses: svenstaro/upload-release-action@v2
with:
file_glob: true
file: ./*.tar.bz2
overwrite: true
repo_name: k2-fsa/sherpa
repo_token: ${{ secrets.UPLOAD_GH_SHERPA_TOKEN }}
tag: asr-models

- name: Publish ${{ matrix.model }} to huggingface
shell: bash
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
run: |
src=sherpa-whisper-${{ matrix.model }}
git config --global user.email "[email protected]"
git config --global user.name "Fangjun Kuang"
export GIT_CLONE_PROTECTION_ACTIVE=false
export GIT_LFS_SKIP_SMUDGE=1
rm -rf huggingface
git clone https://csukuangfj:[email protected]/csukuangfj/$src huggingface
rm -rf huggingface/*
cp -av $src/* ./huggingface/
cd huggingface
git status
ls -lh
git lfs track "*.pt*"
git add .
git commit -m "upload $src" || true
git push https://csukuangfj:[email protected]/csukuangfj/$src main || true
- name: Publish ${{ matrix.model }} to huggingface
shell: bash
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
run: |
src=sherpa-whisper-${{ matrix.model }}
git config --global user.email "[email protected]"
git config --global user.name "Fangjun Kuang"
export GIT_CLONE_PROTECTION_ACTIVE=false
export GIT_LFS_SKIP_SMUDGE=1
rm -rf huggingface
git clone https://csukuangfj:[email protected]/k2-fsa/sherpa-models huggingface
cp -av $src.tar.bz2 ./huggingface/non-streaming-asr
cd huggingface
git status
ls -lh
git lfs track "*.tar.bz2*"
git add .
git commit -m "upload $src" || true
git push https://csukuangfj:[email protected]/k2-fsa/sherpa-models main || true
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ sherpa-nemo-ctc*
/.cache
/.idea
/.vscode
*.pt
tokens.txt
7 changes: 6 additions & 1 deletion docs/source/onnx/kws/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ usage as follows:

.. code-block::
# Note: You need to run pip install sherpa-onnx to get the commandline tool: sherpa-onnx-cli
sherpa-onnx-cli text2token --help
Usage: sherpa-onnx-cli text2token [OPTIONS] INPUT OUTPUT
Expand Down Expand Up @@ -88,7 +91,9 @@ usage as follows:
.. note::

If you install sherpa-onnx from sources (i.e. not by pip), you can use the
alternative script in `scripts`, the usage is almost the same as the command
`alternative script <https://github.com/k2-fsa/sherpa-onnx/blob/master/scripts/text2token.py>`_
in `scripts <https://github.com/k2-fsa/sherpa-onnx/tree/master/scripts>`_,
the usage is almost the same as the command
line tool, read the help information by:

.. code-block::
Expand Down
6 changes: 6 additions & 0 deletions docs/source/onnx/pretrained_models/whisper/export-onnx.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ from the following huggingface repositories:
* - ``large-v3``
- `<https://huggingface.co/csukuangfj/sherpa-onnx-whisper-large-v3>`_
- `点此 <https://hf-mirror.com/csukuangfj/sherpa-onnx-whisper-large-v3>`_
* - ``turbo``
- `<https://huggingface.co/csukuangfj/sherpa-onnx-whisper-turbo>`_
- `点此 <https://hf-mirror.com/csukuangfj/sherpa-onnx-whisper-turbo>`_
* - ``distil-large-v2``
- `<https://huggingface.co/csukuangfj/sherpa-onnx-whisper-distil-large-v2>`_
- `点此 <https://hf-mirror.com/csukuangfj/sherpa-onnx-whisper-distil-large-v2>`_
* - ``medium-aishell``
- `<https://huggingface.co/csukuangfj/sherpa-onnx-whisper-medium-aishell>`_
- `点此 <https://hf-mirror.com/csukuangfj/sherpa-onnx-whisper-medium-aishell2>`_

.. note::

Expand Down
2 changes: 2 additions & 0 deletions scripts/sense-voice/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ cat >README.md << EOF
Models in this file are converted from
https://www.modelscope.cn/models/iic/SenseVoiceSmall/summary
using the following script
https://github.com/k2-fsa/sherpa/blob/master/scripts/sense-voice/run.sh
EOF
7 changes: 7 additions & 0 deletions scripts/whisper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Introduction

Models in this file are converted from
https://github.com/openai/whisper
using the following script
https://github.com/k2-fsa/sherpa/blob/master/scripts/whisper/run.sh

Loading

0 comments on commit 2ffe462

Please sign in to comment.