From effd5ef2be885f9de2e280faa9798778746e3ae4 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Wed, 23 Oct 2024 12:07:43 +0800 Subject: [PATCH] Add C++ API for streaming ASR. (#1455) It is a wrapper around the C API. --- .github/scripts/test-cxx-api.sh | 21 ++ .../workflows/aarch64-linux-gnu-shared.yaml | 2 +- .../workflows/aarch64-linux-gnu-static.yaml | 2 +- .github/workflows/android.yaml | 2 +- .github/workflows/apk-asr-2pass.yaml | 2 +- .github/workflows/apk-asr.yaml | 2 +- .../workflows/apk-audio-tagging-wearos.yaml | 2 +- .github/workflows/apk-audio-tagging.yaml | 2 +- .github/workflows/apk-kws.yaml | 2 +- .../workflows/apk-speaker-diarization.yaml | 2 +- .../workflows/apk-speaker-identification.yaml | 2 +- .../apk-spoken-language-identification.yaml | 2 +- .github/workflows/apk-tts-engine.yaml | 2 +- .github/workflows/apk-tts.yaml | 2 +- .github/workflows/apk-vad-asr.yaml | 2 +- .github/workflows/apk-vad.yaml | 2 +- .github/workflows/arm-linux-gnueabihf.yaml | 2 +- .github/workflows/build-wheels-aarch64.yaml | 2 +- .github/workflows/build-wheels-armv7l.yaml | 2 +- .../workflows/build-wheels-linux-cuda.yaml | 2 +- .github/workflows/build-wheels-linux.yaml | 2 +- .../workflows/build-wheels-macos-arm64.yaml | 2 +- .../build-wheels-macos-universal2.yaml | 2 +- .github/workflows/build-wheels-macos-x64.yaml | 2 +- .github/workflows/build-wheels-win32.yaml | 2 +- .../workflows/build-wheels-win64-cuda.yaml | 2 +- .github/workflows/build-wheels-win64.yaml | 2 +- .github/workflows/build-xcframework.yaml | 2 +- .github/workflows/c-api.yaml | 2 - .github/workflows/cxx-api.yaml | 117 ++++++++++++ .github/workflows/dot-net.yaml | 2 +- .../workflows/export-3dspeaker-to-onnx.yaml | 2 +- .github/workflows/export-ced-to-onnx.yaml | 2 +- .github/workflows/export-libriheavy.yaml | 4 +- .../workflows/export-melo-tts-to-onnx.yaml | 2 +- ...r-hybrid-transducer-ctc-non-streaming.yaml | 2 +- ...d-transducer-transducer-non-streaming.yaml | 2 +- ...ort-nemo-speaker-verification-to-onnx.yaml | 2 +- .../export-pyannote-segmentation-to-onnx.yaml | 2 +- .../export-revai-segmentation-to-onnx.yaml | 2 +- .../workflows/export-sense-voice-to-onnx.yaml | 2 +- .github/workflows/export-telespeech-ctc.yaml | 4 +- .github/workflows/export-wenet-to-onnx.yaml | 12 +- .../workflows/export-wespeaker-to-onnx.yaml | 2 +- .github/workflows/export-whisper-to-onnx.yaml | 2 +- .github/workflows/flutter-android.yaml | 2 +- .github/workflows/flutter-linux.yaml | 2 +- .github/workflows/flutter-macos.yaml | 4 +- .github/workflows/flutter-windows-x64.yaml | 4 +- .github/workflows/lazarus.yaml | 2 +- .github/workflows/linux.yaml | 14 ++ .github/workflows/macos.yaml | 14 ++ .github/workflows/sanitizer.yaml | 9 +- .github/workflows/windows-x64.yaml | 13 +- .github/workflows/windows-x86.yaml | 13 +- CMakeLists.txt | 9 +- .../streaming-ctc-buffered-tokens-c-api.c | 2 +- ...reaming-paraformer-buffered-tokens-c-api.c | 2 +- c-api-examples/streaming-paraformer-c-api.c | 2 +- ...zipformer-buffered-tokens-hotwords-c-api.c | 2 +- c-api-examples/streaming-zipformer-c-api.c | 2 +- cmake/cmake_extension.py | 1 + cxx-api-examples/CMakeLists.txt | 4 + .../streaming-zipformer-cxx-api.cc | 91 +++++++++ ffmpeg-examples/sherpa-onnx-ffmpeg.c | 4 +- .../StreamingSpeechRecognitionDlg.h | 6 +- scripts/node-addon-api/src/streaming-asr.cc | 5 +- sherpa-onnx/c-api/CMakeLists.txt | 23 ++- sherpa-onnx/c-api/c-api.cc | 3 +- sherpa-onnx/c-api/c-api.h | 3 +- sherpa-onnx/c-api/cxx-api.cc | 159 ++++++++++++++++ sherpa-onnx/c-api/cxx-api.h | 179 ++++++++++++++++++ 72 files changed, 729 insertions(+), 83 deletions(-) create mode 100755 .github/scripts/test-cxx-api.sh create mode 100644 .github/workflows/cxx-api.yaml create mode 100644 cxx-api-examples/CMakeLists.txt create mode 100644 cxx-api-examples/streaming-zipformer-cxx-api.cc create mode 100644 sherpa-onnx/c-api/cxx-api.cc create mode 100644 sherpa-onnx/c-api/cxx-api.h diff --git a/.github/scripts/test-cxx-api.sh b/.github/scripts/test-cxx-api.sh new file mode 100755 index 000000000..89b2f1dd6 --- /dev/null +++ b/.github/scripts/test-cxx-api.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +set -ex + +log() { + # This function is from espnet + local fname=${BASH_SOURCE[1]##*/} + echo -e "$(date '+%Y-%m-%d %H:%M:%S') (${fname}:${BASH_LINENO[0]}:${FUNCNAME[1]}) $*" +} + +echo "CXX_STREAMING_ZIPFORMER_EXE is $CXX_STREAMING_ZIPFORMER_EXE" +echo "PATH: $PATH" + +log "------------------------------------------------------------" +log "Test streaming zipformer CXX API" +log "------------------------------------------------------------" +curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 +tar xvf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 +rm sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 +$CXX_STREAMING_ZIPFORMER_EXE +rm -rf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20 diff --git a/.github/workflows/aarch64-linux-gnu-shared.yaml b/.github/workflows/aarch64-linux-gnu-shared.yaml index 5e82d9b3a..dbba7c132 100644 --- a/.github/workflows/aarch64-linux-gnu-shared.yaml +++ b/.github/workflows/aarch64-linux-gnu-shared.yaml @@ -193,7 +193,7 @@ jobs: rm -rf huggingface export GIT_CLONE_PROTECTION_ACTIVE=false - GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/csukuangfj/sherpa-onnx-libs huggingface + GIT_LFS_SKIP_SMUDGE=1 git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-libs huggingface cd huggingface mkdir -p aarch64 diff --git a/.github/workflows/aarch64-linux-gnu-static.yaml b/.github/workflows/aarch64-linux-gnu-static.yaml index 765e2422f..6cbfc0e27 100644 --- a/.github/workflows/aarch64-linux-gnu-static.yaml +++ b/.github/workflows/aarch64-linux-gnu-static.yaml @@ -184,7 +184,7 @@ jobs: rm -rf huggingface export GIT_CLONE_PROTECTION_ACTIVE=false - GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/csukuangfj/sherpa-onnx-libs huggingface + GIT_LFS_SKIP_SMUDGE=1 git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-libs huggingface cd huggingface mkdir -p aarch64 diff --git a/.github/workflows/android.yaml b/.github/workflows/android.yaml index 35dfd6b26..ebe9cd90f 100644 --- a/.github/workflows/android.yaml +++ b/.github/workflows/android.yaml @@ -121,7 +121,7 @@ jobs: rm -rf huggingface export GIT_CLONE_PROTECTION_ACTIVE=false - GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/csukuangfj/sherpa-onnx-libs huggingface + GIT_LFS_SKIP_SMUDGE=1 git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-libs huggingface cd huggingface diff --git a/.github/workflows/apk-asr-2pass.yaml b/.github/workflows/apk-asr-2pass.yaml index bbe61060a..bfbc2f170 100644 --- a/.github/workflows/apk-asr-2pass.yaml +++ b/.github/workflows/apk-asr-2pass.yaml @@ -163,7 +163,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-apk huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-apk huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/apk-asr.yaml b/.github/workflows/apk-asr.yaml index fc1cd1f5d..c51ce1e20 100644 --- a/.github/workflows/apk-asr.yaml +++ b/.github/workflows/apk-asr.yaml @@ -163,7 +163,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-apk huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-apk huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/apk-audio-tagging-wearos.yaml b/.github/workflows/apk-audio-tagging-wearos.yaml index 0ed823076..bfe9f9ac7 100644 --- a/.github/workflows/apk-audio-tagging-wearos.yaml +++ b/.github/workflows/apk-audio-tagging-wearos.yaml @@ -163,7 +163,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-apk huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-apk huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/apk-audio-tagging.yaml b/.github/workflows/apk-audio-tagging.yaml index f6b85c3b2..c11180c4a 100644 --- a/.github/workflows/apk-audio-tagging.yaml +++ b/.github/workflows/apk-audio-tagging.yaml @@ -160,7 +160,7 @@ jobs: export GIT_LFS_SKIP_SMUDGE=1 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/csukuangfj/sherpa-onnx-apk huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-apk huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/apk-kws.yaml b/.github/workflows/apk-kws.yaml index 524622de8..43cdef49e 100644 --- a/.github/workflows/apk-kws.yaml +++ b/.github/workflows/apk-kws.yaml @@ -160,7 +160,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-apk huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-apk huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/apk-speaker-diarization.yaml b/.github/workflows/apk-speaker-diarization.yaml index 8e422e13f..90bcc7323 100644 --- a/.github/workflows/apk-speaker-diarization.yaml +++ b/.github/workflows/apk-speaker-diarization.yaml @@ -163,7 +163,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-apk huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-apk huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/apk-speaker-identification.yaml b/.github/workflows/apk-speaker-identification.yaml index e32ad3bc9..c88718d6e 100644 --- a/.github/workflows/apk-speaker-identification.yaml +++ b/.github/workflows/apk-speaker-identification.yaml @@ -163,7 +163,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-apk huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-apk huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/apk-spoken-language-identification.yaml b/.github/workflows/apk-spoken-language-identification.yaml index 3cb9c83b2..cc7525cd4 100644 --- a/.github/workflows/apk-spoken-language-identification.yaml +++ b/.github/workflows/apk-spoken-language-identification.yaml @@ -163,7 +163,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-apk huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-apk huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/apk-tts-engine.yaml b/.github/workflows/apk-tts-engine.yaml index d251483e4..b8614cb76 100644 --- a/.github/workflows/apk-tts-engine.yaml +++ b/.github/workflows/apk-tts-engine.yaml @@ -164,7 +164,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-apk huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-apk huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/apk-tts.yaml b/.github/workflows/apk-tts.yaml index dd0aa3f77..1609739c6 100644 --- a/.github/workflows/apk-tts.yaml +++ b/.github/workflows/apk-tts.yaml @@ -164,7 +164,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-apk huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-apk huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/apk-vad-asr.yaml b/.github/workflows/apk-vad-asr.yaml index 8310043a9..4c587f1be 100644 --- a/.github/workflows/apk-vad-asr.yaml +++ b/.github/workflows/apk-vad-asr.yaml @@ -163,7 +163,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-apk huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-apk huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/apk-vad.yaml b/.github/workflows/apk-vad.yaml index d9af75477..f1a4364fc 100644 --- a/.github/workflows/apk-vad.yaml +++ b/.github/workflows/apk-vad.yaml @@ -160,7 +160,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-apk huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-apk huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/arm-linux-gnueabihf.yaml b/.github/workflows/arm-linux-gnueabihf.yaml index a56b2cdad..6a2874910 100644 --- a/.github/workflows/arm-linux-gnueabihf.yaml +++ b/.github/workflows/arm-linux-gnueabihf.yaml @@ -205,7 +205,7 @@ jobs: git config --global user.name "Fangjun Kuang" rm -rf huggingface - GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/csukuangfj/sherpa-onnx-libs huggingface + GIT_LFS_SKIP_SMUDGE=1 git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-libs huggingface cd huggingface mkdir -p arm32 diff --git a/.github/workflows/build-wheels-aarch64.yaml b/.github/workflows/build-wheels-aarch64.yaml index 9d4ac571e..860ccdcda 100644 --- a/.github/workflows/build-wheels-aarch64.yaml +++ b/.github/workflows/build-wheels-aarch64.yaml @@ -99,7 +99,7 @@ jobs: d=cpu/$SHERPA_ONNX_VERSION - git clone https://huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/build-wheels-armv7l.yaml b/.github/workflows/build-wheels-armv7l.yaml index 05c3b196d..584c64687 100644 --- a/.github/workflows/build-wheels-armv7l.yaml +++ b/.github/workflows/build-wheels-armv7l.yaml @@ -102,7 +102,7 @@ jobs: d=cpu/$SHERPA_ONNX_VERSION - git clone https://huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/build-wheels-linux-cuda.yaml b/.github/workflows/build-wheels-linux-cuda.yaml index b1ee89825..3f3f66dda 100644 --- a/.github/workflows/build-wheels-linux-cuda.yaml +++ b/.github/workflows/build-wheels-linux-cuda.yaml @@ -113,7 +113,7 @@ jobs: d=cuda/$SHERPA_ONNX_VERSION - git clone https://huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/build-wheels-linux.yaml b/.github/workflows/build-wheels-linux.yaml index e16f5bb9a..5ca10639a 100644 --- a/.github/workflows/build-wheels-linux.yaml +++ b/.github/workflows/build-wheels-linux.yaml @@ -96,7 +96,7 @@ jobs: d=cpu/$SHERPA_ONNX_VERSION - git clone https://huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/build-wheels-macos-arm64.yaml b/.github/workflows/build-wheels-macos-arm64.yaml index ce899c5d1..181246e8b 100644 --- a/.github/workflows/build-wheels-macos-arm64.yaml +++ b/.github/workflows/build-wheels-macos-arm64.yaml @@ -68,7 +68,7 @@ jobs: d=cpu/$SHERPA_ONNX_VERSION - git clone https://huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/build-wheels-macos-universal2.yaml b/.github/workflows/build-wheels-macos-universal2.yaml index 4578d370e..88c51b3a0 100644 --- a/.github/workflows/build-wheels-macos-universal2.yaml +++ b/.github/workflows/build-wheels-macos-universal2.yaml @@ -68,7 +68,7 @@ jobs: d=cpu/$SHERPA_ONNX_VERSION - git clone https://huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/build-wheels-macos-x64.yaml b/.github/workflows/build-wheels-macos-x64.yaml index b7bf6ff54..695186958 100644 --- a/.github/workflows/build-wheels-macos-x64.yaml +++ b/.github/workflows/build-wheels-macos-x64.yaml @@ -83,7 +83,7 @@ jobs: d=cpu/$SHERPA_ONNX_VERSION - git clone https://huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/build-wheels-win32.yaml b/.github/workflows/build-wheels-win32.yaml index 256084783..04a02d45f 100644 --- a/.github/workflows/build-wheels-win32.yaml +++ b/.github/workflows/build-wheels-win32.yaml @@ -67,7 +67,7 @@ jobs: d=cpu/$SHERPA_ONNX_VERSION - git clone https://huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/build-wheels-win64-cuda.yaml b/.github/workflows/build-wheels-win64-cuda.yaml index f0a17da8c..5224f20f0 100644 --- a/.github/workflows/build-wheels-win64-cuda.yaml +++ b/.github/workflows/build-wheels-win64-cuda.yaml @@ -75,7 +75,7 @@ jobs: d=cuda/$SHERPA_ONNX_VERSION - git clone https://huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/build-wheels-win64.yaml b/.github/workflows/build-wheels-win64.yaml index 14e3e2ac4..0bae16c4a 100644 --- a/.github/workflows/build-wheels-win64.yaml +++ b/.github/workflows/build-wheels-win64.yaml @@ -73,7 +73,7 @@ jobs: d=cpu/$SHERPA_ONNX_VERSION - git clone https://huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-wheels huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/build-xcframework.yaml b/.github/workflows/build-xcframework.yaml index 2afd95cab..90953010e 100644 --- a/.github/workflows/build-xcframework.yaml +++ b/.github/workflows/build-xcframework.yaml @@ -135,7 +135,7 @@ jobs: rm -rf huggingface export GIT_CLONE_PROTECTION_ACTIVE=false - GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/csukuangfj/sherpa-onnx-libs huggingface + GIT_LFS_SKIP_SMUDGE=1 git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-libs huggingface cd huggingface diff --git a/.github/workflows/c-api.yaml b/.github/workflows/c-api.yaml index 589bda71f..94a52a4bc 100644 --- a/.github/workflows/c-api.yaml +++ b/.github/workflows/c-api.yaml @@ -4,8 +4,6 @@ on: push: branches: - master - tags: - - 'v[0-9]+.[0-9]+.[0-9]+*' paths: - '.github/workflows/c-api.yaml' - 'CMakeLists.txt' diff --git a/.github/workflows/cxx-api.yaml b/.github/workflows/cxx-api.yaml new file mode 100644 index 000000000..1c882f2ff --- /dev/null +++ b/.github/workflows/cxx-api.yaml @@ -0,0 +1,117 @@ +name: cxx-api + +on: + push: + branches: + - master + paths: + - '.github/workflows/cxx-api.yaml' + - 'CMakeLists.txt' + - 'cmake/**' + - 'sherpa-onnx/csrc/*' + - 'sherpa-onnx/c-api/*' + - 'cxx-api-examples/**' + pull_request: + branches: + - master + paths: + - '.github/workflows/cxx-api.yaml' + - 'CMakeLists.txt' + - 'cmake/**' + - 'sherpa-onnx/csrc/*' + - 'sherpa-onnx/c-api/*' + - 'cxx-api-examples/**' + + workflow_dispatch: + +concurrency: + group: cxx-api-${{ github.ref }} + cancel-in-progress: true + +jobs: + cxx_api: + name: ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: ccache + uses: hendrikmuhs/ccache-action@v1.2 + with: + key: ${{ matrix.os }}-cxx-api-shared + + - name: Build sherpa-onnx + shell: bash + run: | + export CMAKE_CXX_COMPILER_LAUNCHER=ccache + export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" + cmake --version + + mkdir build + cd build + + cmake \ + -D CMAKE_BUILD_TYPE=Release \ + -D BUILD_SHARED_LIBS=ON \ + -D CMAKE_INSTALL_PREFIX=./install \ + -D SHERPA_ONNX_ENABLE_BINARY=OFF \ + .. + + make -j2 install + + ls -lh install/lib + ls -lh install/include + + if [[ ${{ matrix.os }} == ubuntu-latest ]]; then + ldd ./install/lib/libsherpa-onnx-c-api.so + ldd ./install/lib/libsherpa-onnx-cxx-api.so + echo "---" + readelf -d ./install/lib/libsherpa-onnx-c-api.so + readelf -d ./install/lib/libsherpa-onnx-cxx-api.so + fi + + if [[ ${{ matrix.os }} == macos-latest ]]; then + otool -L ./install/lib/libsherpa-onnx-c-api.dylib + otool -L ./install/lib/libsherpa-onnx-cxx-api.dylib + fi + + - name: Test streaming zipformer + shell: bash + run: | + g++ -std=c++17 -o streaming-zipformer-cxx-api ./cxx-api-examples/streaming-zipformer-cxx-api.cc \ + -I ./build/install/include \ + -L ./build/install/lib/ \ + -l sherpa-onnx-cxx-api \ + -l sherpa-onnx-c-api \ + -l onnxruntime + + ls -lh streaming-zipformer-cxx-api + + if [[ ${{ matrix.os }} == ubuntu-latest ]]; then + ldd ./streaming-zipformer-cxx-api + echo "----" + readelf -d ./streaming-zipformer-cxx-api + fi + + curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 + tar xvf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 + rm sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 + + ls -lh sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20 + echo "---" + ls -lh sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/test_wavs + + export LD_LIBRARY_PATH=$PWD/build/install/lib:$LD_LIBRARY_PATH + export DYLD_LIBRARY_PATH=$PWD/build/install/lib:$DYLD_LIBRARY_PATH + + ./streaming-zipformer-cxx-api + + rm -rf sherpa-onnx-streaming-zipformer-* + rm ./streaming-zipformer-cxx-api diff --git a/.github/workflows/dot-net.yaml b/.github/workflows/dot-net.yaml index 36637a9e2..4c6e44133 100644 --- a/.github/workflows/dot-net.yaml +++ b/.github/workflows/dot-net.yaml @@ -90,7 +90,7 @@ jobs: export GIT_CLONE_PROTECTION_ACTIVE=false export GIT_LFS_SKIP_SMUDGE=1 - git clone https://huggingface.co/csukuangfj/sherpa-onnx-libs huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-libs huggingface cd huggingface git fetch diff --git a/.github/workflows/export-3dspeaker-to-onnx.yaml b/.github/workflows/export-3dspeaker-to-onnx.yaml index a3fa98760..e62d42784 100644 --- a/.github/workflows/export-3dspeaker-to-onnx.yaml +++ b/.github/workflows/export-3dspeaker-to-onnx.yaml @@ -59,7 +59,7 @@ jobs: d=speaker-embedding-models export GIT_LFS_SKIP_SMUDGE=1 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/csukuangfj/$d huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/$d huggingface mv -v ./*.onnx ./huggingface cd huggingface git lfs track "*.onnx" diff --git a/.github/workflows/export-ced-to-onnx.yaml b/.github/workflows/export-ced-to-onnx.yaml index 70c4cc5fb..2f714bb80 100644 --- a/.github/workflows/export-ced-to-onnx.yaml +++ b/.github/workflows/export-ced-to-onnx.yaml @@ -66,7 +66,7 @@ jobs: export GIT_LFS_SKIP_SMUDGE=1 d=sherpa-onnx-ced-$m-audio-tagging-2024-04-19 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/k2-fsa/$d huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/k2-fsa/$d huggingface mv -v $d/* huggingface cd huggingface git lfs track "*.onnx" diff --git a/.github/workflows/export-libriheavy.yaml b/.github/workflows/export-libriheavy.yaml index cfe0a28d2..69c22ef24 100644 --- a/.github/workflows/export-libriheavy.yaml +++ b/.github/workflows/export-libriheavy.yaml @@ -56,7 +56,7 @@ jobs: src=sherpa-onnx-zipformer-en-libriheavy-20230926-$m echo "Process $src" - git clone https://huggingface.co/csukuangfj/$src huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/$src huggingface cd huggingface git fetch git pull @@ -100,7 +100,7 @@ jobs: src=sherpa-onnx-zipformer-en-libriheavy-20230830-$m-punct-case echo "Process $src" - git clone https://huggingface.co/csukuangfj/$src huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/$src huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/export-melo-tts-to-onnx.yaml b/.github/workflows/export-melo-tts-to-onnx.yaml index 0dc9bfe9d..42cc28d6a 100644 --- a/.github/workflows/export-melo-tts-to-onnx.yaml +++ b/.github/workflows/export-melo-tts-to-onnx.yaml @@ -56,7 +56,7 @@ jobs: export GIT_LFS_SKIP_SMUDGE=1 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/csukuangfj/vits-melo-tts-zh_en huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/vits-melo-tts-zh_en huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/export-nemo-fast-conformer-hybrid-transducer-ctc-non-streaming.yaml b/.github/workflows/export-nemo-fast-conformer-hybrid-transducer-ctc-non-streaming.yaml index 138c708ad..bbabfb60c 100644 --- a/.github/workflows/export-nemo-fast-conformer-hybrid-transducer-ctc-non-streaming.yaml +++ b/.github/workflows/export-nemo-fast-conformer-hybrid-transducer-ctc-non-streaming.yaml @@ -67,7 +67,7 @@ jobs: rm -rf huggingface export GIT_LFS_SKIP_SMUDGE=1 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/csukuangfj/$m huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/$m huggingface cp -av $m/* huggingface cd huggingface git lfs track "*.onnx" diff --git a/.github/workflows/export-nemo-fast-conformer-hybrid-transducer-transducer-non-streaming.yaml b/.github/workflows/export-nemo-fast-conformer-hybrid-transducer-transducer-non-streaming.yaml index 7a7b7fc4e..4a7e2339e 100644 --- a/.github/workflows/export-nemo-fast-conformer-hybrid-transducer-transducer-non-streaming.yaml +++ b/.github/workflows/export-nemo-fast-conformer-hybrid-transducer-transducer-non-streaming.yaml @@ -67,7 +67,7 @@ jobs: rm -rf huggingface export GIT_LFS_SKIP_SMUDGE=1 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/csukuangfj/$m huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/$m huggingface cp -av $m/* huggingface cd huggingface git lfs track "*.onnx" diff --git a/.github/workflows/export-nemo-speaker-verification-to-onnx.yaml b/.github/workflows/export-nemo-speaker-verification-to-onnx.yaml index a9bd5a788..505966413 100644 --- a/.github/workflows/export-nemo-speaker-verification-to-onnx.yaml +++ b/.github/workflows/export-nemo-speaker-verification-to-onnx.yaml @@ -59,7 +59,7 @@ jobs: d=speaker-embedding-models export GIT_LFS_SKIP_SMUDGE=1 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/csukuangfj/$d huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/$d huggingface mv -v ./*.onnx ./huggingface cd huggingface git lfs track "*.onnx" diff --git a/.github/workflows/export-pyannote-segmentation-to-onnx.yaml b/.github/workflows/export-pyannote-segmentation-to-onnx.yaml index ece0ffa28..53f8dac7d 100644 --- a/.github/workflows/export-pyannote-segmentation-to-onnx.yaml +++ b/.github/workflows/export-pyannote-segmentation-to-onnx.yaml @@ -75,7 +75,7 @@ jobs: d=sherpa-onnx-pyannote-segmentation-3-0 export GIT_LFS_SKIP_SMUDGE=1 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/csukuangfj/$d huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/$d huggingface cp -v $d/* ./huggingface cd huggingface git lfs track "*.onnx" diff --git a/.github/workflows/export-revai-segmentation-to-onnx.yaml b/.github/workflows/export-revai-segmentation-to-onnx.yaml index f0f1594c6..d82f7c4e0 100644 --- a/.github/workflows/export-revai-segmentation-to-onnx.yaml +++ b/.github/workflows/export-revai-segmentation-to-onnx.yaml @@ -75,7 +75,7 @@ jobs: d=sherpa-onnx-reverb-diarization-v1 export GIT_LFS_SKIP_SMUDGE=1 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/csukuangfj/$d huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/$d huggingface cp -v $d/* ./huggingface cd huggingface git lfs track "*.onnx" diff --git a/.github/workflows/export-sense-voice-to-onnx.yaml b/.github/workflows/export-sense-voice-to-onnx.yaml index 41a9a31a6..1c3e91729 100644 --- a/.github/workflows/export-sense-voice-to-onnx.yaml +++ b/.github/workflows/export-sense-voice-to-onnx.yaml @@ -66,7 +66,7 @@ jobs: export GIT_LFS_SKIP_SMUDGE=1 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/csukuangfj/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17 huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17 huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/export-telespeech-ctc.yaml b/.github/workflows/export-telespeech-ctc.yaml index 102c3884e..4f66d7ca4 100644 --- a/.github/workflows/export-telespeech-ctc.yaml +++ b/.github/workflows/export-telespeech-ctc.yaml @@ -60,7 +60,7 @@ jobs: export GIT_CLONE_PROTECTION_ACTIVE=false - GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/csukuangfj/sherpa-onnx-telespeech-ctc-zh-2024-06-04 hf + 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" @@ -84,7 +84,7 @@ jobs: export GIT_CLONE_PROTECTION_ACTIVE=false rm -rf hf - GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/csukuangfj/sherpa-onnx-telespeech-ctc-int8-zh-2024-06-04 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" diff --git a/.github/workflows/export-wenet-to-onnx.yaml b/.github/workflows/export-wenet-to-onnx.yaml index 626f477e6..7ef3a54b6 100644 --- a/.github/workflows/export-wenet-to-onnx.yaml +++ b/.github/workflows/export-wenet-to-onnx.yaml @@ -49,7 +49,7 @@ jobs: export GIT_LFS_SKIP_SMUDGE=1 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/csukuangfj/sherpa-onnx-zh-wenet-aishell huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-zh-wenet-aishell huggingface cd huggingface git fetch git pull @@ -98,7 +98,7 @@ jobs: export GIT_LFS_SKIP_SMUDGE=1 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/csukuangfj/sherpa-onnx-zh-wenet-aishell2 huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-zh-wenet-aishell2 huggingface cd huggingface git fetch git pull @@ -147,7 +147,7 @@ jobs: export GIT_LFS_SKIP_SMUDGE=1 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/csukuangfj/sherpa-onnx-zh-wenet-multi-cn huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-zh-wenet-multi-cn huggingface cd huggingface git fetch git pull @@ -196,7 +196,7 @@ jobs: export GIT_LFS_SKIP_SMUDGE=1 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/csukuangfj/sherpa-onnx-zh-wenet-wenetspeech huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-zh-wenet-wenetspeech huggingface cd huggingface git fetch git pull @@ -245,7 +245,7 @@ jobs: export GIT_LFS_SKIP_SMUDGE=1 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/csukuangfj/sherpa-onnx-en-wenet-librispeech huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-en-wenet-librispeech huggingface cd huggingface git fetch git pull @@ -295,7 +295,7 @@ jobs: export GIT_LFS_SKIP_SMUDGE=1 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/csukuangfj/sherpa-onnx-en-wenet-gigaspeech huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-en-wenet-gigaspeech huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/export-wespeaker-to-onnx.yaml b/.github/workflows/export-wespeaker-to-onnx.yaml index 764f77ca7..05694f693 100644 --- a/.github/workflows/export-wespeaker-to-onnx.yaml +++ b/.github/workflows/export-wespeaker-to-onnx.yaml @@ -64,7 +64,7 @@ jobs: d=speaker-embedding-models export GIT_LFS_SKIP_SMUDGE=1 export GIT_CLONE_PROTECTION_ACTIVE=false - git clone https://huggingface.co/csukuangfj/$d huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/$d huggingface mv -v ./*.onnx ./huggingface cd huggingface git lfs track "*.onnx" diff --git a/.github/workflows/export-whisper-to-onnx.yaml b/.github/workflows/export-whisper-to-onnx.yaml index a50aa99d7..53aebdd3b 100644 --- a/.github/workflows/export-whisper-to-onnx.yaml +++ b/.github/workflows/export-whisper-to-onnx.yaml @@ -145,7 +145,7 @@ jobs: export GIT_LFS_SKIP_SMUDGE=1 - git clone https://huggingface.co/csukuangfj/sherpa-onnx-whisper-${{ matrix.model }} huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-whisper-${{ matrix.model }} huggingface rm -rf huggingface/* diff --git a/.github/workflows/flutter-android.yaml b/.github/workflows/flutter-android.yaml index 9752a82c6..c2b1d01db 100644 --- a/.github/workflows/flutter-android.yaml +++ b/.github/workflows/flutter-android.yaml @@ -214,7 +214,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-flutter huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-flutter huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/flutter-linux.yaml b/.github/workflows/flutter-linux.yaml index b6b1fb9c8..f1fdd5ec7 100644 --- a/.github/workflows/flutter-linux.yaml +++ b/.github/workflows/flutter-linux.yaml @@ -261,7 +261,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-flutter huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-flutter huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/flutter-macos.yaml b/.github/workflows/flutter-macos.yaml index 7c8a38e4c..e85ff1644 100644 --- a/.github/workflows/flutter-macos.yaml +++ b/.github/workflows/flutter-macos.yaml @@ -101,7 +101,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-flutter huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-flutter huggingface cd huggingface git fetch git pull @@ -207,7 +207,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-flutter huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-flutter huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/flutter-windows-x64.yaml b/.github/workflows/flutter-windows-x64.yaml index f4d296b70..59f6a6af9 100644 --- a/.github/workflows/flutter-windows-x64.yaml +++ b/.github/workflows/flutter-windows-x64.yaml @@ -94,7 +94,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-flutter huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-flutter huggingface cd huggingface git fetch git pull @@ -192,7 +192,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-flutter huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-flutter huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/lazarus.yaml b/.github/workflows/lazarus.yaml index 11df53644..e7cef16ef 100644 --- a/.github/workflows/lazarus.yaml +++ b/.github/workflows/lazarus.yaml @@ -355,7 +355,7 @@ jobs: SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" - git clone https://huggingface.co/csukuangfj/sherpa-onnx-bin huggingface + git clone https://csukuangfj:$HF_TOKEN@huggingface.co/csukuangfj/sherpa-onnx-bin huggingface cd huggingface git fetch git pull diff --git a/.github/workflows/linux.yaml b/.github/workflows/linux.yaml index 1d3e8dc7b..e21c452c0 100644 --- a/.github/workflows/linux.yaml +++ b/.github/workflows/linux.yaml @@ -19,6 +19,8 @@ on: - '.github/scripts/test-offline-punctuation.sh' - '.github/scripts/test-online-punctuation.sh' - '.github/scripts/test-speaker-diarization.sh' + - '.github/scripts/test-c-api.sh' + - '.github/scripts/test-cxx-api.sh' - 'CMakeLists.txt' - 'cmake/**' - 'sherpa-onnx/csrc/*' @@ -40,6 +42,8 @@ on: - '.github/scripts/test-offline-punctuation.sh' - '.github/scripts/test-online-punctuation.sh' - '.github/scripts/test-speaker-diarization.sh' + - '.github/scripts/test-c-api.sh' + - '.github/scripts/test-cxx-api.sh' - 'CMakeLists.txt' - 'cmake/**' - 'sherpa-onnx/csrc/*' @@ -145,6 +149,16 @@ jobs: name: release-${{ matrix.build_type }}-with-shared-lib-${{ matrix.shared_lib }}-with-tts-${{ matrix.with_tts }} path: install/* + - name: Test C++ API + shell: bash + run: | + du -h -d1 . + export PATH=$PWD/build/bin:$PATH + export CXX_STREAMING_ZIPFORMER_EXE=streaming-zipformer-cxx-api + + .github/scripts/test-cxx-api.sh + du -h -d1 . + - name: Test offline speaker diarization shell: bash run: | diff --git a/.github/workflows/macos.yaml b/.github/workflows/macos.yaml index f3d70f583..7b01846a7 100644 --- a/.github/workflows/macos.yaml +++ b/.github/workflows/macos.yaml @@ -19,6 +19,8 @@ on: - '.github/scripts/test-offline-punctuation.sh' - '.github/scripts/test-online-punctuation.sh' - '.github/scripts/test-speaker-diarization.sh' + - '.github/scripts/test-c-api.sh' + - '.github/scripts/test-cxx-api.sh' - 'CMakeLists.txt' - 'cmake/**' - 'sherpa-onnx/csrc/*' @@ -39,6 +41,8 @@ on: - '.github/scripts/test-offline-punctuation.sh' - '.github/scripts/test-online-punctuation.sh' - '.github/scripts/test-speaker-diarization.sh' + - '.github/scripts/test-c-api.sh' + - '.github/scripts/test-cxx-api.sh' - 'CMakeLists.txt' - 'cmake/**' - 'sherpa-onnx/csrc/*' @@ -117,6 +121,16 @@ jobs: otool -L build/bin/sherpa-onnx otool -l build/bin/sherpa-onnx + - name: Test C++ API + shell: bash + run: | + du -h -d1 . + export PATH=$PWD/build/bin:$PATH + export CXX_STREAMING_ZIPFORMER_EXE=streaming-zipformer-cxx-api + + .github/scripts/test-cxx-api.sh + du -h -d1 . + - name: Test offline speaker diarization shell: bash run: | diff --git a/.github/workflows/sanitizer.yaml b/.github/workflows/sanitizer.yaml index 7fce3834a..f12bafa9a 100644 --- a/.github/workflows/sanitizer.yaml +++ b/.github/workflows/sanitizer.yaml @@ -76,6 +76,14 @@ jobs: otool -L build/bin/sherpa-onnx otool -l build/bin/sherpa-onnx + - name: Test C++ API + shell: bash + run: | + export PATH=$PWD/build/bin:$PATH + export CXX_STREAMING_ZIPFORMER_EXE=streaming-zipformer-cxx-api + + .github/scripts/test-cxx-api.sh + - name: Test online punctuation shell: bash run: | @@ -109,7 +117,6 @@ jobs: .github/scripts/test-online-ctc.sh - - name: Test C API shell: bash run: | diff --git a/.github/workflows/windows-x64.yaml b/.github/workflows/windows-x64.yaml index 758935918..f07d6c78c 100644 --- a/.github/workflows/windows-x64.yaml +++ b/.github/workflows/windows-x64.yaml @@ -18,6 +18,8 @@ on: - '.github/scripts/test-offline-punctuation.sh' - '.github/scripts/test-online-punctuation.sh' - '.github/scripts/test-speaker-diarization.sh' + - '.github/scripts/test-c-api.sh' + - '.github/scripts/test-cxx-api.sh' - 'CMakeLists.txt' - 'cmake/**' - 'sherpa-onnx/csrc/*' @@ -36,6 +38,8 @@ on: - '.github/scripts/test-offline-punctuation.sh' - '.github/scripts/test-online-punctuation.sh' - '.github/scripts/test-speaker-diarization.sh' + - '.github/scripts/test-c-api.sh' + - '.github/scripts/test-cxx-api.sh' - 'CMakeLists.txt' - 'cmake/**' - 'sherpa-onnx/csrc/*' @@ -89,10 +93,17 @@ jobs: name: release-windows-x64-${{ matrix.shared_lib }}-${{ matrix.with_tts }} path: build/install/* + - name: Test C++ API + shell: bash + run: | + export PATH=$PWD/build/bin/Release:$PATH + export CXX_STREAMING_ZIPFORMER_EXE=streaming-zipformer-cxx-api.exe + + .github/scripts/test-cxx-api.sh + - name: Test offline speaker diarization shell: bash run: | - du -h -d1 . export PATH=$PWD/build/bin/Release:$PATH export EXE=sherpa-onnx-offline-speaker-diarization.exe diff --git a/.github/workflows/windows-x86.yaml b/.github/workflows/windows-x86.yaml index b9e473184..bacbdff01 100644 --- a/.github/workflows/windows-x86.yaml +++ b/.github/workflows/windows-x86.yaml @@ -18,6 +18,8 @@ on: - '.github/scripts/test-offline-punctuation.sh' - '.github/scripts/test-online-punctuation.sh' - '.github/scripts/test-speaker-diarization.sh' + - '.github/scripts/test-c-api.sh' + - '.github/scripts/test-cxx-api.sh' - 'CMakeLists.txt' - 'cmake/**' - 'sherpa-onnx/csrc/*' @@ -36,6 +38,8 @@ on: - '.github/scripts/test-offline-punctuation.sh' - '.github/scripts/test-online-punctuation.sh' - '.github/scripts/test-speaker-diarization.sh' + - '.github/scripts/test-c-api.sh' + - '.github/scripts/test-cxx-api.sh' - 'CMakeLists.txt' - 'cmake/**' - 'sherpa-onnx/csrc/*' @@ -89,10 +93,17 @@ jobs: name: release-windows-x86-${{ matrix.shared_lib }}-${{ matrix.with_tts }} path: build/install/* + - name: Test C++ API + shell: bash + run: | + export PATH=$PWD/build/bin/Release:$PATH + export CXX_STREAMING_ZIPFORMER_EXE=streaming-zipformer-cxx-api.exe + + .github/scripts/test-cxx-api.sh + - name: Test offline speaker diarization shell: bash run: | - du -h -d1 . export PATH=$PWD/build/bin/Release:$PATH export EXE=sherpa-onnx-offline-speaker-diarization.exe diff --git a/CMakeLists.txt b/CMakeLists.txt index 578dc78d8..4e1850c44 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,9 +51,11 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib") set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") -set(CMAKE_SKIP_BUILD_RPATH FALSE) -set(BUILD_RPATH_USE_ORIGIN TRUE) -set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) +if(NOT WIN32) + set(CMAKE_SKIP_BUILD_RPATH FALSE) + set(BUILD_RPATH_USE_ORIGIN TRUE) + set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) +endif() if(NOT APPLE) set(SHERPA_ONNX_RPATH_ORIGIN "$ORIGIN") @@ -399,6 +401,7 @@ add_subdirectory(sherpa-onnx) if(SHERPA_ONNX_ENABLE_C_API AND SHERPA_ONNX_ENABLE_BINARY AND SHERPA_ONNX_BUILD_C_API_EXAMPLES) set(SHERPA_ONNX_PKG_WITH_CARGS "-lcargs") add_subdirectory(c-api-examples) + add_subdirectory(cxx-api-examples) endif() if(SHERPA_ONNX_ENABLE_WASM) diff --git a/c-api-examples/streaming-ctc-buffered-tokens-c-api.c b/c-api-examples/streaming-ctc-buffered-tokens-c-api.c index 33690e008..eb834fb70 100644 --- a/c-api-examples/streaming-ctc-buffered-tokens-c-api.c +++ b/c-api-examples/streaming-ctc-buffered-tokens-c-api.c @@ -95,7 +95,7 @@ int32_t main() { recognizer_config.decoding_method = "greedy_search"; recognizer_config.model_config = online_model_config; - SherpaOnnxOnlineRecognizer *recognizer = + const SherpaOnnxOnlineRecognizer *recognizer = SherpaOnnxCreateOnlineRecognizer(&recognizer_config); free((void *)tokens_buf); diff --git a/c-api-examples/streaming-paraformer-buffered-tokens-c-api.c b/c-api-examples/streaming-paraformer-buffered-tokens-c-api.c index a597374df..be08f4149 100644 --- a/c-api-examples/streaming-paraformer-buffered-tokens-c-api.c +++ b/c-api-examples/streaming-paraformer-buffered-tokens-c-api.c @@ -96,7 +96,7 @@ int32_t main() { recognizer_config.decoding_method = "greedy_search"; recognizer_config.model_config = online_model_config; - SherpaOnnxOnlineRecognizer *recognizer = + const SherpaOnnxOnlineRecognizer *recognizer = SherpaOnnxCreateOnlineRecognizer(&recognizer_config); free((void *)tokens_buf); diff --git a/c-api-examples/streaming-paraformer-c-api.c b/c-api-examples/streaming-paraformer-c-api.c index b54116f08..11748e084 100644 --- a/c-api-examples/streaming-paraformer-c-api.c +++ b/c-api-examples/streaming-paraformer-c-api.c @@ -57,7 +57,7 @@ int32_t main() { recognizer_config.decoding_method = "greedy_search"; recognizer_config.model_config = online_model_config; - SherpaOnnxOnlineRecognizer *recognizer = + const SherpaOnnxOnlineRecognizer *recognizer = SherpaOnnxCreateOnlineRecognizer(&recognizer_config); if (recognizer == NULL) { diff --git a/c-api-examples/streaming-zipformer-buffered-tokens-hotwords-c-api.c b/c-api-examples/streaming-zipformer-buffered-tokens-hotwords-c-api.c index c991d4999..c8afca15a 100644 --- a/c-api-examples/streaming-zipformer-buffered-tokens-hotwords-c-api.c +++ b/c-api-examples/streaming-zipformer-buffered-tokens-hotwords-c-api.c @@ -116,7 +116,7 @@ int32_t main() { recognizer_config.hotwords_buf = hotwords_buf; recognizer_config.hotwords_buf_size = hotwords_buf_size; - SherpaOnnxOnlineRecognizer *recognizer = + const SherpaOnnxOnlineRecognizer *recognizer = SherpaOnnxCreateOnlineRecognizer(&recognizer_config); free((void *)tokens_buf); diff --git a/c-api-examples/streaming-zipformer-c-api.c b/c-api-examples/streaming-zipformer-c-api.c index e1417639d..a38d22f02 100644 --- a/c-api-examples/streaming-zipformer-c-api.c +++ b/c-api-examples/streaming-zipformer-c-api.c @@ -63,7 +63,7 @@ int32_t main() { recognizer_config.decoding_method = "greedy_search"; recognizer_config.model_config = online_model_config; - SherpaOnnxOnlineRecognizer *recognizer = + const SherpaOnnxOnlineRecognizer *recognizer = SherpaOnnxCreateOnlineRecognizer(&recognizer_config); if (recognizer == NULL) { diff --git a/cmake/cmake_extension.py b/cmake/cmake_extension.py index c49c32555..3d0dbef8e 100644 --- a/cmake/cmake_extension.py +++ b/cmake/cmake_extension.py @@ -80,6 +80,7 @@ def get_binaries(): binaries += [ "onnxruntime.dll", "sherpa-onnx-c-api.dll", + "sherpa-onnx-cxx-api.dll", ] return binaries diff --git a/cxx-api-examples/CMakeLists.txt b/cxx-api-examples/CMakeLists.txt new file mode 100644 index 000000000..b51d2e50d --- /dev/null +++ b/cxx-api-examples/CMakeLists.txt @@ -0,0 +1,4 @@ +include_directories(${CMAKE_SOURCE_DIR}) + +add_executable(streaming-zipformer-cxx-api ./streaming-zipformer-cxx-api.cc) +target_link_libraries(streaming-zipformer-cxx-api sherpa-onnx-cxx-api) diff --git a/cxx-api-examples/streaming-zipformer-cxx-api.cc b/cxx-api-examples/streaming-zipformer-cxx-api.cc new file mode 100644 index 000000000..4f38647f4 --- /dev/null +++ b/cxx-api-examples/streaming-zipformer-cxx-api.cc @@ -0,0 +1,91 @@ +// cxx-api-examples/streaming-zipformer-cxx-api.cc +// Copyright (c) 2024 Xiaomi Corporation + +// +// This file demonstrates how to use streaming Zipformer +// with sherpa-onnx's C++ API. +// +// clang-format off +// +// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 +// tar xvf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 +// rm sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 +// +// clang-format on + +#include // NOLINT +#include +#include + +#include "sherpa-onnx/c-api/cxx-api.h" + +int32_t main() { + using namespace sherpa_onnx::cxx; + OnlineRecognizerConfig config; + + // please see + // https://k2-fsa.github.io/sherpa/onnx/pretrained_models/online-transducer/zipformer-transducer-models.html#csukuangfj-sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20-bilingual-chinese-english + config.model_config.transducer.encoder = + "./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/" + "encoder-epoch-99-avg-1.int8.onnx"; + + // Note: We recommend not using int8.onnx for the decoder. + config.model_config.transducer.decoder = + "./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/" + "decoder-epoch-99-avg-1.onnx"; + + config.model_config.transducer.joiner = + "./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/" + "joiner-epoch-99-avg-1.int8.onnx"; + + config.model_config.tokens = + "./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/tokens.txt"; + + config.model_config.num_threads = 1; + + std::cout << "Loading model\n"; + OnlineRecognizer recongizer = OnlineRecognizer::Create(config); + if (!recongizer.Get()) { + std::cerr << "Please check your config\n"; + return -1; + } + std::cout << "Loading model done\n"; + + std::string wave_filename = + "./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/test_wavs/" + "0.wav"; + Wave wave = ReadWave(wave_filename); + if (wave.samples.empty()) { + std::cerr << "Failed to read: '" << wave_filename << "'\n"; + return -1; + } + + std::cout << "Start recognition\n"; + const auto begin = std::chrono::steady_clock::now(); + + OnlineStream stream = recongizer.CreateStream(); + stream.AcceptWaveform(wave.sample_rate, wave.samples.data(), + wave.samples.size()); + while (recongizer.IsReady(&stream)) { + recongizer.Decode(&stream); + } + + OnlineRecognizerResult result = recongizer.GetResult(&stream); + + const auto end = std::chrono::steady_clock::now(); + const float elapsed_seconds = + std::chrono::duration_cast(end - begin) + .count() / + 1000.; + float duration = wave.samples.size() / static_cast(wave.sample_rate); + float rtf = elapsed_seconds / duration; + + std::cout << "text: " << result.text << "\n"; + printf("Number of threads: %d\n", config.model_config.num_threads); + printf("Duration: %.3fs\n", duration); + printf("Elapsed seconds: %.3fs\n", elapsed_seconds); + printf("(Real time factor) RTF = %.3f / %.3f = %.3f\n", elapsed_seconds, + duration, rtf); + + return 0; +} diff --git a/ffmpeg-examples/sherpa-onnx-ffmpeg.c b/ffmpeg-examples/sherpa-onnx-ffmpeg.c index f99ac0bdc..31e7491f0 100644 --- a/ffmpeg-examples/sherpa-onnx-ffmpeg.c +++ b/ffmpeg-examples/sherpa-onnx-ffmpeg.c @@ -290,7 +290,7 @@ int main(int argc, char **argv) { } SherpaOnnxOnlineRecognizerConfig config; - memset(&config, 0, sizeof(config)); + memset(&config, 0, sizeof(config)); config.model_config.tokens = argv[1]; config.model_config.transducer.encoder = argv[2]; config.model_config.transducer.decoder = argv[3]; @@ -318,7 +318,7 @@ int main(int argc, char **argv) { config.rule2_min_trailing_silence = 1.2; config.rule3_min_utterance_length = 300; - SherpaOnnxOnlineRecognizer *recognizer = + const SherpaOnnxOnlineRecognizer *recognizer = SherpaOnnxCreateOnlineRecognizer(&config); SherpaOnnxOnlineStream *stream = SherpaOnnxCreateOnlineStream(recognizer); const SherpaOnnxDisplay *display = SherpaOnnxCreateDisplay(50); diff --git a/mfc-examples/StreamingSpeechRecognition/StreamingSpeechRecognitionDlg.h b/mfc-examples/StreamingSpeechRecognition/StreamingSpeechRecognitionDlg.h index 36b3d9cad..39acbcfe4 100644 --- a/mfc-examples/StreamingSpeechRecognition/StreamingSpeechRecognitionDlg.h +++ b/mfc-examples/StreamingSpeechRecognition/StreamingSpeechRecognitionDlg.h @@ -45,7 +45,7 @@ class CStreamingSpeechRecognitionDlg : public CDialogEx { private: Microphone mic_; - SherpaOnnxOnlineRecognizer *recognizer_ = nullptr; + const SherpaOnnxOnlineRecognizer *recognizer_ = nullptr; PaStream *pa_stream_ = nullptr; RecognizerThread *thread_ = nullptr; @@ -54,7 +54,7 @@ class CStreamingSpeechRecognitionDlg : public CDialogEx { public: bool started_ = false; - SherpaOnnxOnlineStream *stream_ = nullptr; + const SherpaOnnxOnlineStream *stream_ = nullptr; public: int RunThread(); @@ -79,4 +79,4 @@ class RecognizerThread : public CWinThread { private: CStreamingSpeechRecognitionDlg *dlg_; -}; \ No newline at end of file +}; diff --git a/scripts/node-addon-api/src/streaming-asr.cc b/scripts/node-addon-api/src/streaming-asr.cc index 6057cade3..cb3aaac86 100644 --- a/scripts/node-addon-api/src/streaming-asr.cc +++ b/scripts/node-addon-api/src/streaming-asr.cc @@ -199,7 +199,8 @@ static Napi::External CreateOnlineRecognizerWrapper( c.ctc_fst_decoder_config = GetCtcFstDecoderConfig(o); - SherpaOnnxOnlineRecognizer *recognizer = SherpaOnnxCreateOnlineRecognizer(&c); + const SherpaOnnxOnlineRecognizer *recognizer = + SherpaOnnxCreateOnlineRecognizer(&c); if (c.model_config.transducer.encoder) { delete[] c.model_config.transducer.encoder; @@ -281,7 +282,7 @@ static Napi::External CreateOnlineRecognizerWrapper( } return Napi::External::New( - env, recognizer, + env, const_cast(recognizer), [](Napi::Env env, SherpaOnnxOnlineRecognizer *recognizer) { SherpaOnnxDestroyOnlineRecognizer(recognizer); }); diff --git a/sherpa-onnx/c-api/CMakeLists.txt b/sherpa-onnx/c-api/CMakeLists.txt index d87f849aa..9c8a59771 100644 --- a/sherpa-onnx/c-api/CMakeLists.txt +++ b/sherpa-onnx/c-api/CMakeLists.txt @@ -3,12 +3,25 @@ add_library(sherpa-onnx-c-api c-api.cc) target_link_libraries(sherpa-onnx-c-api sherpa-onnx-core) if(BUILD_SHARED_LIBS) - target_compile_definitions(sherpa-onnx-c-api PRIVATE SHERPA_ONNX_BUILD_SHARED_LIBS=1) - target_compile_definitions(sherpa-onnx-c-api PRIVATE SHERPA_ONNX_BUILD_MAIN_LIB=1) + target_compile_definitions(sherpa-onnx-c-api PUBLIC SHERPA_ONNX_BUILD_SHARED_LIBS=1) + target_compile_definitions(sherpa-onnx-c-api PUBLIC SHERPA_ONNX_BUILD_MAIN_LIB=1) endif() -install(TARGETS sherpa-onnx-c-api DESTINATION lib) +add_library(sherpa-onnx-cxx-api cxx-api.cc) +target_link_libraries(sherpa-onnx-cxx-api sherpa-onnx-c-api) -install(FILES c-api.h - DESTINATION include/sherpa-onnx/c-api +install( + TARGETS + sherpa-onnx-c-api + sherpa-onnx-cxx-api + DESTINATION + lib +) + +install( + FILES + c-api.h + cxx-api.h + DESTINATION + include/sherpa-onnx/c-api ) diff --git a/sherpa-onnx/c-api/c-api.cc b/sherpa-onnx/c-api/c-api.cc index 4ba0a4a60..653355a66 100644 --- a/sherpa-onnx/c-api/c-api.cc +++ b/sherpa-onnx/c-api/c-api.cc @@ -5,6 +5,7 @@ #include "sherpa-onnx/c-api/c-api.h" #include +#include #include #include #include @@ -51,7 +52,7 @@ struct SherpaOnnxDisplay { #define SHERPA_ONNX_OR(x, y) (x ? x : y) -SherpaOnnxOnlineRecognizer *SherpaOnnxCreateOnlineRecognizer( +const SherpaOnnxOnlineRecognizer *SherpaOnnxCreateOnlineRecognizer( const SherpaOnnxOnlineRecognizerConfig *config) { sherpa_onnx::OnlineRecognizerConfig recognizer_config; diff --git a/sherpa-onnx/c-api/c-api.h b/sherpa-onnx/c-api/c-api.h index 4b41a81a9..0a01379d4 100644 --- a/sherpa-onnx/c-api/c-api.h +++ b/sherpa-onnx/c-api/c-api.h @@ -205,7 +205,8 @@ SHERPA_ONNX_API typedef struct SherpaOnnxOnlineStream SherpaOnnxOnlineStream; /// @param config Config for the recognizer. /// @return Return a pointer to the recognizer. The user has to invoke // SherpaOnnxDestroyOnlineRecognizer() to free it to avoid memory leak. -SHERPA_ONNX_API SherpaOnnxOnlineRecognizer *SherpaOnnxCreateOnlineRecognizer( +SHERPA_ONNX_API const SherpaOnnxOnlineRecognizer * +SherpaOnnxCreateOnlineRecognizer( const SherpaOnnxOnlineRecognizerConfig *config); /// Free a pointer returned by SherpaOnnxCreateOnlineRecognizer() diff --git a/sherpa-onnx/c-api/cxx-api.cc b/sherpa-onnx/c-api/cxx-api.cc new file mode 100644 index 000000000..243e99738 --- /dev/null +++ b/sherpa-onnx/c-api/cxx-api.cc @@ -0,0 +1,159 @@ +// sherpa-onnx/c-api/cxx-api.cc +// +// Copyright (c) 2024 Xiaomi Corporation +#include "sherpa-onnx/c-api/cxx-api.h" + +#include +#include + +namespace sherpa_onnx::cxx { + +Wave ReadWave(const std::string &filename) { + auto p = SherpaOnnxReadWave(filename.c_str()); + + Wave ans; + if (p) { + ans.samples.resize(p->num_samples); + + std::copy(p->samples, p->samples + p->num_samples, ans.samples.data()); + + ans.sample_rate = p->sample_rate; + SherpaOnnxFreeWave(p); + } + + return ans; +} + +OnlineStream::OnlineStream(const SherpaOnnxOnlineStream *p) + : MoveOnly(p) {} + +void OnlineStream::Destroy(const SherpaOnnxOnlineStream *p) const { + SherpaOnnxDestroyOnlineStream(p); +} + +void OnlineStream::AcceptWaveform(int32_t sample_rate, const float *samples, + int32_t n) const { + SherpaOnnxOnlineStreamAcceptWaveform(p_, sample_rate, samples, n); +} + +OnlineRecognizer OnlineRecognizer::Create( + const OnlineRecognizerConfig &config) { + struct SherpaOnnxOnlineRecognizerConfig c; + memset(&c, 0, sizeof(c)); + + c.feat_config.sample_rate = config.feat_config.sample_rate; + c.feat_config.feature_dim = config.feat_config.feature_dim; + + c.model_config.transducer.encoder = + config.model_config.transducer.encoder.c_str(); + c.model_config.transducer.decoder = + config.model_config.transducer.decoder.c_str(); + c.model_config.transducer.joiner = + config.model_config.transducer.joiner.c_str(); + + c.model_config.paraformer.encoder = + config.model_config.paraformer.encoder.c_str(); + c.model_config.paraformer.decoder = + config.model_config.paraformer.decoder.c_str(); + + c.model_config.zipformer2_ctc.model = + config.model_config.zipformer2_ctc.model.c_str(); + + c.model_config.tokens = config.model_config.tokens.c_str(); + c.model_config.num_threads = config.model_config.num_threads; + c.model_config.provider = config.model_config.provider.c_str(); + c.model_config.debug = config.model_config.debug; + c.model_config.model_type = config.model_config.model_type.c_str(); + c.model_config.modeling_unit = config.model_config.modeling_unit.c_str(); + c.model_config.bpe_vocab = config.model_config.bpe_vocab.c_str(); + c.model_config.tokens_buf = config.model_config.tokens_buf.c_str(); + c.model_config.tokens_buf_size = config.model_config.tokens_buf.size(); + + c.decoding_method = config.decoding_method.c_str(); + c.max_active_paths = config.max_active_paths; + c.enable_endpoint = config.enable_endpoint; + c.rule1_min_trailing_silence = config.rule1_min_trailing_silence; + c.rule2_min_trailing_silence = config.rule2_min_trailing_silence; + c.rule3_min_utterance_length = config.rule3_min_utterance_length; + c.hotwords_file = config.hotwords_file.c_str(); + c.hotwords_score = config.hotwords_score; + + c.ctc_fst_decoder_config.graph = config.ctc_fst_decoder_config.graph.c_str(); + c.ctc_fst_decoder_config.max_active = + config.ctc_fst_decoder_config.max_active; + + c.rule_fsts = config.rule_fsts.c_str(); + c.rule_fars = config.rule_fars.c_str(); + + c.blank_penalty = config.blank_penalty; + + c.hotwords_buf = config.hotwords_buf.c_str(); + c.hotwords_buf_size = config.hotwords_buf.size(); + + auto p = SherpaOnnxCreateOnlineRecognizer(&c); + return OnlineRecognizer(p); +} + +OnlineRecognizer::OnlineRecognizer(const SherpaOnnxOnlineRecognizer *p) + : MoveOnly(p) {} + +void OnlineRecognizer::Destroy(const SherpaOnnxOnlineRecognizer *p) const { + SherpaOnnxDestroyOnlineRecognizer(p); +} + +OnlineStream OnlineRecognizer::CreateStream() const { + auto s = SherpaOnnxCreateOnlineStream(p_); + return OnlineStream{s}; +} + +OnlineStream OnlineRecognizer::CreateStream(const std::string &hotwords) const { + auto s = SherpaOnnxCreateOnlineStreamWithHotwords(p_, hotwords.c_str()); + return OnlineStream{s}; +} + +bool OnlineRecognizer::IsReady(const OnlineStream *s) const { + return SherpaOnnxIsOnlineStreamReady(p_, s->Get()); +} + +void OnlineRecognizer::Decode(const OnlineStream *s) const { + SherpaOnnxDecodeOnlineStream(p_, s->Get()); +} + +void OnlineRecognizer::Decode(const OnlineStream *ss, int32_t n) const { + if (n <= 0) { + return; + } + + std::vector streams(n); + for (int32_t i = 0; i != n; ++n) { + streams[i] = ss[i].Get(); + } + + SherpaOnnxDecodeMultipleOnlineStreams(p_, streams.data(), n); +} + +OnlineRecognizerResult OnlineRecognizer::GetResult( + const OnlineStream *s) const { + auto r = SherpaOnnxGetOnlineStreamResult(p_, s->Get()); + + OnlineRecognizerResult ans; + ans.text = r->text; + + ans.tokens.resize(r->count); + for (int32_t i = 0; i != r->count; ++i) { + ans.tokens[i] = r->tokens_arr[i]; + } + + if (r->timestamps) { + ans.timestamps.resize(r->count); + std::copy(r->timestamps, r->timestamps + r->count, ans.timestamps.data()); + } + + ans.json = r->json; + + SherpaOnnxDestroyOnlineRecognizerResult(r); + + return ans; +} + +} // namespace sherpa_onnx::cxx diff --git a/sherpa-onnx/c-api/cxx-api.h b/sherpa-onnx/c-api/cxx-api.h new file mode 100644 index 000000000..416333b7f --- /dev/null +++ b/sherpa-onnx/c-api/cxx-api.h @@ -0,0 +1,179 @@ +// sherpa-onnx/c-api/cxx-api.h +// +// Copyright (c) 2024 Xiaomi Corporation + +// C++ Wrapper of the C API for sherpa-onnx +#ifndef SHERPA_ONNX_C_API_CXX_API_H_ +#define SHERPA_ONNX_C_API_CXX_API_H_ + +#include +#include + +#include "sherpa-onnx/c-api/c-api.h" + +namespace sherpa_onnx::cxx { + +struct SHERPA_ONNX_API OnlineTransducerModelConfig { + std::string encoder; + std::string decoder; + std::string joiner; +}; + +struct SHERPA_ONNX_API OnlineParaformerModelConfig { + std::string encoder; + std::string decoder; +}; + +struct SHERPA_ONNX_API OnlineZipformer2CtcModelConfig { + std::string model; +}; + +struct SHERPA_ONNX_API OnlineModelConfig { + OnlineTransducerModelConfig transducer; + OnlineParaformerModelConfig paraformer; + OnlineZipformer2CtcModelConfig zipformer2_ctc; + std::string tokens; + int32_t num_threads = 1; + std::string provider = "cpu"; + bool debug = false; + std::string model_type; + std::string modeling_unit = "cjkchar"; + std::string bpe_vocab; + std::string tokens_buf; +}; + +struct SHERPA_ONNX_API FeatureConfig { + int32_t sample_rate = 16000; + int32_t feature_dim = 80; +}; + +struct SHERPA_ONNX_API OnlineCtcFstDecoderConfig { + std::string graph; + int32_t max_active = 3000; +}; + +struct SHERPA_ONNX_API OnlineRecognizerConfig { + FeatureConfig feat_config; + OnlineModelConfig model_config; + + std::string decoding_method = "greedy_search"; + + int32_t max_active_paths = 4; + + bool enable_endpoint = false; + + float rule1_min_trailing_silence = 2.4; + + float rule2_min_trailing_silence = 1.2; + + float rule3_min_utterance_length = 20; + + std::string hotwords_file; + + float hotwords_score = 1.5; + + OnlineCtcFstDecoderConfig ctc_fst_decoder_config; + std::string rule_fsts; + std::string rule_fars; + float blank_penalty = 0; + + std::string hotwords_buf; +}; + +struct SHERPA_ONNX_API OnlineRecognizerResult { + std::string text; + std::vector tokens; + std::vector timestamps; + std::string json; +}; + +struct SHERPA_ONNX_API Wave { + std::vector samples; + int32_t sample_rate; +}; + +SHERPA_ONNX_API Wave ReadWave(const std::string &filename); + +template +class SHERPA_ONNX_API MoveOnly { + public: + explicit MoveOnly(const T *p) : p_(p) {} + + ~MoveOnly() { Destroy(); } + + MoveOnly(const MoveOnly &) = delete; + + MoveOnly &operator=(const MoveOnly &) = delete; + + MoveOnly(MoveOnly &&other) : p_(other.Release()) {} + + MoveOnly &operator=(MoveOnly &&other) { + if (&other == this) { + return *this; + } + + Destroy(); + + p_ = other.Release(); + } + + const T *Get() const { return p_; } + + const T *Release() { + const T *p = p_; + p_ = nullptr; + return p; + } + + private: + void Destroy() { + if (p_ == nullptr) { + return; + } + + static_cast(this)->Destroy(p_); + + p_ = nullptr; + } + + protected: + const T *p_ = nullptr; +}; + +class SHERPA_ONNX_API OnlineStream + : public MoveOnly { + public: + explicit OnlineStream(const SherpaOnnxOnlineStream *p); + + void AcceptWaveform(int32_t sample_rate, const float *samples, + int32_t n) const; + + void Destroy(const SherpaOnnxOnlineStream *p) const; +}; + +class SHERPA_ONNX_API OnlineRecognizer + : public MoveOnly { + public: + static OnlineRecognizer Create(const OnlineRecognizerConfig &config); + + void Destroy(const SherpaOnnxOnlineRecognizer *p) const; + + OnlineStream CreateStream() const; + + OnlineStream CreateStream(const std::string &hotwords) const; + + bool IsReady(const OnlineStream *s) const; + + void Decode(const OnlineStream *s) const; + + void Decode(const OnlineStream *ss, int32_t n) const; + + OnlineRecognizerResult GetResult(const OnlineStream *s) const; + + private: + explicit OnlineRecognizer(const SherpaOnnxOnlineRecognizer *p); +}; + +} // namespace sherpa_onnx::cxx + +#endif // SHERPA_ONNX_C_API_CXX_API_H_