From bf06b268d07c1ed9c24234f2508bedca44a47449 Mon Sep 17 00:00:00 2001 From: lllwan <32893806+lllwan@users.noreply.github.com> Date: Tue, 17 Sep 2024 13:39:56 +0800 Subject: [PATCH 1/4] Fix sherpa_onnx.go (#1353) --- scripts/go/sherpa_onnx.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scripts/go/sherpa_onnx.go b/scripts/go/sherpa_onnx.go index ad5060c2c..85ab8e0b1 100644 --- a/scripts/go/sherpa_onnx.go +++ b/scripts/go/sherpa_onnx.go @@ -610,12 +610,15 @@ func (recognizer *OfflineRecognizer) DecodeStreams(s []*OfflineStream) { func (s *OfflineStream) GetResult() *OfflineRecognizerResult { p := C.SherpaOnnxGetOfflineStreamResult(s.impl) defer C.SherpaOnnxDestroyOfflineRecognizerResult(p) + n := int(p.count) + if n == 0 { + return nil + } result := &OfflineRecognizerResult{} result.Text = C.GoString(p.text) result.Lang = C.GoString(p.lang) result.Emotion = C.GoString(p.emotion) result.Event = C.GoString(p.event) - n := int(p.count) result.Tokens = make([]string, n) tokens := (*[1 << 28]*C.char)(unsafe.Pointer(p.tokens_arr))[:n:n] for i := 0; i < n; i++ { From cddac52780d587444d70b72d7ae4d2d7dfdf1312 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Wed, 18 Sep 2024 11:03:42 +0800 Subject: [PATCH 2/4] Support passing utf-8 strings from JavaScript to C++. (#1355) We first convert utf-16 strings to Uint8Array and then we pass the array to C++. --- nodejs-addon-examples/package.json | 2 +- .../test_asr_non_streaming_sense_voice.js | 15 ++++++++++ scripts/node-addon-api/src/macros.h | 29 ++++++++++++------- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/nodejs-addon-examples/package.json b/nodejs-addon-examples/package.json index 121a8103e..41bcfb8fc 100644 --- a/nodejs-addon-examples/package.json +++ b/nodejs-addon-examples/package.json @@ -1,5 +1,5 @@ { "dependencies": { - "sherpa-onnx-node": "^1.10.26" + "sherpa-onnx-node": "^1.10.27" } } diff --git a/nodejs-addon-examples/test_asr_non_streaming_sense_voice.js b/nodejs-addon-examples/test_asr_non_streaming_sense_voice.js index 99371e8f3..116024ebb 100644 --- a/nodejs-addon-examples/test_asr_non_streaming_sense_voice.js +++ b/nodejs-addon-examples/test_asr_non_streaming_sense_voice.js @@ -3,6 +3,19 @@ const sherpa_onnx = require('sherpa-onnx-node'); // Please download test files from // https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models + + +// If your path contains non-ascii characters, e.g., Chinese, you can use +// the following code +// + +// let encoder = new TextEncoder(); +// let tokens = encoder.encode( +// './sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/测试.txt'); +// let model = encoder.encode( +// './sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/测试.int8.onnx'); + + const config = { 'featConfig': { 'sampleRate': 16000, @@ -12,9 +25,11 @@ const config = { 'senseVoice': { 'model': './sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/model.int8.onnx', + // 'model': model, 'useInverseTextNormalization': 1, }, 'tokens': './sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/tokens.txt', + // 'tokens': tokens, 'numThreads': 2, 'provider': 'cpu', 'debug': 1, diff --git a/scripts/node-addon-api/src/macros.h b/scripts/node-addon-api/src/macros.h index bed930620..ac0dbd567 100644 --- a/scripts/node-addon-api/src/macros.h +++ b/scripts/node-addon-api/src/macros.h @@ -7,17 +7,24 @@ #include #include -#define SHERPA_ONNX_ASSIGN_ATTR_STR(c_name, js_name) \ - do { \ - if (o.Has(#js_name) && o.Get(#js_name).IsString()) { \ - Napi::String _str = o.Get(#js_name).As(); \ - std::string s = _str.Utf8Value(); \ - char *p = new char[s.size() + 1]; \ - std::copy(s.begin(), s.end(), p); \ - p[s.size()] = 0; \ - \ - c.c_name = p; \ - } \ +#define SHERPA_ONNX_ASSIGN_ATTR_STR(c_name, js_name) \ + do { \ + if (o.Has(#js_name) && o.Get(#js_name).IsString()) { \ + Napi::String _str = o.Get(#js_name).As(); \ + std::string s = _str.Utf8Value(); \ + char *p = new char[s.size() + 1]; \ + std::copy(s.begin(), s.end(), p); \ + p[s.size()] = 0; \ + \ + c.c_name = p; \ + } else if (o.Has(#js_name) && o.Get(#js_name).IsTypedArray()) { \ + Napi::Uint8Array _array = o.Get(#js_name).As(); \ + char *p = new char[_array.ElementLength() + 1]; \ + std::copy(_array.Data(), _array.Data() + _array.ElementLength(), p); \ + p[_array.ElementLength()] = '\0'; \ + \ + c.c_name = p; \ + } \ } while (0) #define SHERPA_ONNX_ASSIGN_ATTR_INT32(c_name, js_name) \ From 7e642325f32aae097d552ec545294d90b0ef8a5b Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Wed, 18 Sep 2024 12:04:02 +0800 Subject: [PATCH 3/4] Fix building Flutter TTS examples for Linux (#1356) --- flutter-examples/tts/pubspec.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/flutter-examples/tts/pubspec.yaml b/flutter-examples/tts/pubspec.yaml index 5693c6054..722b33cac 100644 --- a/flutter-examples/tts/pubspec.yaml +++ b/flutter-examples/tts/pubspec.yaml @@ -21,7 +21,8 @@ dependencies: sherpa_onnx: ^1.10.26 # sherpa_onnx: # path: ../../flutter/sherpa_onnx - url_launcher: ^6.2.6 + url_launcher: 6.2.6 + url_launcher_linux: 3.1.0 audioplayers: ^5.0.0 flutter: From 576a3aa90d663db6d5d74051482897d0538c97d4 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Wed, 18 Sep 2024 13:43:49 +0800 Subject: [PATCH 4/4] Add non-streaming ONNX models for Russian ASR (#1358) --- .github/scripts/test-offline-transducer.sh | 40 +++++++ .../workflows/export-russian-onnx-models.yaml | 108 ++++++++++++++++++ .github/workflows/linux.yaml | 16 +-- .github/workflows/macos.yaml | 13 +-- scripts/apk/generate-vad-asr-apk-script.py | 40 +++++++ sherpa-onnx/kotlin-api/OfflineRecognizer.kt | 26 +++++ 6 files changed, 228 insertions(+), 15 deletions(-) create mode 100644 .github/workflows/export-russian-onnx-models.yaml diff --git a/.github/scripts/test-offline-transducer.sh b/.github/scripts/test-offline-transducer.sh index 1bec7ec9b..7ac729860 100755 --- a/.github/scripts/test-offline-transducer.sh +++ b/.github/scripts/test-offline-transducer.sh @@ -15,6 +15,46 @@ echo "PATH: $PATH" which $EXE +log "------------------------------------------------------------------------" +log "Run zipformer transducer models (Russian) " +log "------------------------------------------------------------------------" +for type in small-zipformer zipformer; do + url=https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-$type-ru-2024-09-18.tar.bz2 + name=$(basename $url) + curl -SL -O $url + tar xvf $name + rm $name + repo=$(basename -s .tar.bz2 $name) + ls -lh $repo + + log "test $repo" + test_wavs=( + 0.wav + 1.wav + ) + + for w in ${test_wavs[@]}; do + time $EXE \ + --tokens=$repo/tokens.txt \ + --encoder=$repo/encoder.onnx \ + --decoder=$repo/decoder.onnx \ + --joiner=$repo/joiner.onnx \ + --debug=1 \ + $repo/test_wavs/$w + done + + for w in ${test_wavs[@]}; do + time $EXE \ + --tokens=$repo/tokens.txt \ + --encoder=$repo/encoder.int8.onnx \ + --decoder=$repo/decoder.onnx \ + --joiner=$repo/joiner.int8.onnx \ + --debug=1 \ + $repo/test_wavs/$w + done + rm -rf $repo +done + log "------------------------------------------------------------------------" log "Run zipformer transducer models (Japanese from ReazonSpeech) " log "------------------------------------------------------------------------" diff --git a/.github/workflows/export-russian-onnx-models.yaml b/.github/workflows/export-russian-onnx-models.yaml new file mode 100644 index 000000000..16ee4df44 --- /dev/null +++ b/.github/workflows/export-russian-onnx-models.yaml @@ -0,0 +1,108 @@ +name: export-russian-onnx-models + +on: + workflow_dispatch: + +concurrency: + group: export-russian-onnx-models-${{ github.ref }} + cancel-in-progress: true + +jobs: + export-russian-onnx-models: + if: github.repository_owner == 'k2-fsa' || github.repository_owner == 'csukuangfj' + name: export Russian onnx models + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [macos-latest] + python-version: ["3.8"] + + steps: + - uses: actions/checkout@v4 + + - name: vosk-model-ru (zipformer v1) + shell: bash + run: | + cat >README.md <README.md < { + val modelDir = "sherpa-onnx-zipformer-ru-2024-09-18" + return OfflineModelConfig( + transducer = OfflineTransducerModelConfig( + encoder = "$modelDir/encoder.int8.onnx", + decoder = "$modelDir/decoder.onnx", + joiner = "$modelDir/joiner.int8.onnx", + ), + tokens = "$modelDir/tokens.txt", + modelType = "transducer", + ) + } + + 18 -> { + val modelDir = "sherpa-onnx-small-zipformer-ru-2024-09-18" + return OfflineModelConfig( + transducer = OfflineTransducerModelConfig( + encoder = "$modelDir/encoder.int8.onnx", + decoder = "$modelDir/decoder.onnx", + joiner = "$modelDir/joiner.int8.onnx", + ), + tokens = "$modelDir/tokens.txt", + modelType = "transducer", + ) + } } return null }