From a157d04e05eca9f606bc1fa072bb1280d6fcdeb8 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Mon, 29 Jul 2024 12:11:00 +0800 Subject: [PATCH 1/9] Dart API for adding punctuations --- flutter/sherpa_onnx/lib/sherpa_onnx.dart | 1 + flutter/sherpa_onnx/lib/src/punctuation.dart | 92 +++++++++++++++++++ .../lib/src/sherpa_onnx_bindings.dart | 67 ++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 flutter/sherpa_onnx/lib/src/punctuation.dart diff --git a/flutter/sherpa_onnx/lib/sherpa_onnx.dart b/flutter/sherpa_onnx/lib/sherpa_onnx.dart index 119e43217..b15e67532 100644 --- a/flutter/sherpa_onnx/lib/sherpa_onnx.dart +++ b/flutter/sherpa_onnx/lib/sherpa_onnx.dart @@ -9,6 +9,7 @@ export 'src/offline_recognizer.dart'; export 'src/offline_stream.dart'; export 'src/online_recognizer.dart'; export 'src/online_stream.dart'; +export 'src/punctuation.dart'; export 'src/speaker_identification.dart'; export 'src/tts.dart'; export 'src/vad.dart'; diff --git a/flutter/sherpa_onnx/lib/src/punctuation.dart b/flutter/sherpa_onnx/lib/src/punctuation.dart new file mode 100644 index 000000000..3c75afe4d --- /dev/null +++ b/flutter/sherpa_onnx/lib/src/punctuation.dart @@ -0,0 +1,92 @@ +// Copyright (c) 2024 Xiaomi Corporation +import 'dart:ffi'; +import 'package:ffi/ffi.dart'; + +import './sherpa_onnx_bindings.dart'; + +class OfflinePunctuationModelConfig { + OfflinePunctuationModelConfig( + {required this.ctTransformer, + this.numThreads = 1, + this.provider = 'cpu', + this.debug = true}); + + @override + String toString() { + return 'OfflinePunctuationModelConfig(ctTransformer: $ctTransformer, numThreads: $numThreads, provider: $provider, debug: $debug)'; + } + + final String ctTransformer; + final int numThreads; + final String provider; + final bool debug; +} + +class OfflinePunctuationConfig { + OfflinePunctuationConfig({ + required this.model, + }); + + @override + String toString() { + return 'OfflinePunctuationConfig(model: $model)'; + } + + final OfflinePunctuationModelConfig model; +} + +class OfflinePunctuation { + OfflinePunctuation._({required this.ptr, required this.config}); + + // The user has to invoke OfflinePunctuation.free() to avoid memory leak. + factory OfflinePunctuation({required OfflinePunctuationConfig config}) { + final c = calloc(); + + final ctTransformerPtr = config.model.ctTransformer.toNativeUtf8(); + c.ref.model.ctTransformer = ctTransformerPtr; + c.ref.model.numThreads = config.model.numThreads; + c.ref.model.debug = config.model.debug ? 1 : 0; + + final providerPtr = config.model.provider.toNativeUtf8(); + c.ref.model.provider = providerPtr; + + final ptr = + SherpaOnnxBindings.sherpaOnnxCreateOfflinePunctuation?.call(c) ?? + nullptr; + + calloc.free(providerPtr); + calloc.free(ctTransformerPtr); + calloc.free(c); + + return OfflinePunctuation._(ptr: ptr, config: config); + } + + void free() { + SherpaOnnxBindings.sherpaOnnxDestroyOfflinePunctuation?.call(ptr); + ptr = nullptr; + } + + String addPunct(String text) { + final textPtr = text.toNativeUtf8(); + + final p = SherpaOnnxBindings.sherpaOfflinePunctuationAddPunct + ?.call(ptr, textPtr) ?? + nullptr; + + if (p == nullptr) { + calloc.free(textPtr); + return ''; + } + + final ans = p.toDartString(); + + SherpaOnnxBindings.sherpaOfflinePunctuationFreeText?.call(p); + + calloc.free(textPtr); + + return ans; + } + + Pointer ptr; + final OfflinePunctuationConfig config; +} diff --git a/flutter/sherpa_onnx/lib/src/sherpa_onnx_bindings.dart b/flutter/sherpa_onnx/lib/src/sherpa_onnx_bindings.dart index 7ed3461ad..31dff4e7d 100644 --- a/flutter/sherpa_onnx/lib/src/sherpa_onnx_bindings.dart +++ b/flutter/sherpa_onnx/lib/src/sherpa_onnx_bindings.dart @@ -2,6 +2,22 @@ import 'dart:ffi'; import 'package:ffi/ffi.dart'; +final class SherpaOnnxOfflinePunctuationModelConfig extends Struct { + external Pointer ctTransformer; + + @Int32() + external int numThreads; + + @Int32() + external int debug; + + external Pointer provider; +} + +final class SherpaOnnxOfflinePunctuationConfig extends Struct { + external SherpaOnnxOfflinePunctuationModelConfig model; +} + final class SherpaOnnxOfflineZipformerAudioTaggingModelConfig extends Struct { external Pointer model; } @@ -338,6 +354,8 @@ final class SherpaOnnxKeywordSpotterConfig extends Struct { external Pointer keywordsFile; } +final class SherpaOnnxOfflinePunctuation extends Opaque {} + final class SherpaOnnxAudioTagging extends Opaque {} final class SherpaOnnxKeywordSpotter extends Opaque {} @@ -360,6 +378,29 @@ final class SherpaOnnxSpeakerEmbeddingExtractor extends Opaque {} final class SherpaOnnxSpeakerEmbeddingManager extends Opaque {} +typedef SherpaOnnxCreateOfflinePunctuationNative + = Pointer Function( + Pointer); + +typedef SherpaOnnxCreateOfflinePunctuation + = SherpaOnnxCreateOfflinePunctuationNative; + +typedef SherpaOnnxDestroyOfflinePunctuationNative = Void Function( + Pointer); + +typedef SherpaOnnxDestroyOfflinePunctuation = void Function( + Pointer); + +typedef SherpaOfflinePunctuationAddPunctNative = Pointer Function( + Pointer, Pointer); + +typedef SherpaOfflinePunctuationAddPunct + = SherpaOfflinePunctuationAddPunctNative; + +typedef SherpaOfflinePunctuationFreeTextNative = Void Function(Pointer); + +typedef SherpaOfflinePunctuationFreeText = void Function(Pointer); + typedef SherpaOnnxCreateAudioTaggingNative = Pointer Function(Pointer); @@ -875,6 +916,12 @@ typedef SherpaOnnxFreeWaveNative = Void Function(Pointer); typedef SherpaOnnxFreeWave = void Function(Pointer); class SherpaOnnxBindings { + static SherpaOnnxCreateOfflinePunctuation? sherpaOnnxCreateOfflinePunctuation; + static SherpaOnnxDestroyOfflinePunctuation? + sherpaOnnxDestroyOfflinePunctuation; + static SherpaOfflinePunctuationAddPunct? sherpaOfflinePunctuationAddPunct; + static SherpaOfflinePunctuationFreeText? sherpaOfflinePunctuationFreeText; + static SherpaOnnxCreateAudioTagging? sherpaOnnxCreateAudioTagging; static SherpaOnnxDestroyAudioTagging? sherpaOnnxDestroyAudioTagging; static SherpaOnnxAudioTaggingCreateOfflineStream? @@ -1036,6 +1083,26 @@ class SherpaOnnxBindings { static SherpaOnnxFreeWave? freeWave; static void init(DynamicLibrary dynamicLibrary) { + sherpaOnnxCreateOfflinePunctuation ??= dynamicLibrary + .lookup>( + 'SherpaOnnxCreateOfflinePunctuation') + .asFunction(); + + sherpaOnnxDestroyOfflinePunctuation ??= dynamicLibrary + .lookup>( + 'SherpaOnnxDestroyOfflinePunctuation') + .asFunction(); + + sherpaOfflinePunctuationAddPunct ??= dynamicLibrary + .lookup>( + 'SherpaOfflinePunctuationAddPunct') + .asFunction(); + + sherpaOfflinePunctuationFreeText ??= dynamicLibrary + .lookup>( + 'SherpaOfflinePunctuationFreeText') + .asFunction(); + sherpaOnnxCreateAudioTagging ??= dynamicLibrary .lookup>( 'SherpaOnnxCreateAudioTagging') From ad4f0216437dc5c078de2148a3c18e7ada63f796 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Mon, 29 Jul 2024 12:12:02 +0800 Subject: [PATCH 2/9] begin to example about adding puncutations with Dart API --- dart-api-examples/add-punctuations/.gitignore | 3 ++ .../add-punctuations/CHANGELOG.md | 3 ++ dart-api-examples/add-punctuations/README.md | 2 ++ .../add-punctuations/analysis_options.yaml | 30 +++++++++++++++++++ .../bin/add_punctuations.dart | 5 ++++ .../lib/add_punctuations.dart | 3 ++ .../add-punctuations/pubspec.yaml | 15 ++++++++++ .../test/add_punctuations_test.dart | 8 +++++ 8 files changed, 69 insertions(+) create mode 100644 dart-api-examples/add-punctuations/.gitignore create mode 100644 dart-api-examples/add-punctuations/CHANGELOG.md create mode 100644 dart-api-examples/add-punctuations/README.md create mode 100644 dart-api-examples/add-punctuations/analysis_options.yaml create mode 100644 dart-api-examples/add-punctuations/bin/add_punctuations.dart create mode 100644 dart-api-examples/add-punctuations/lib/add_punctuations.dart create mode 100644 dart-api-examples/add-punctuations/pubspec.yaml create mode 100644 dart-api-examples/add-punctuations/test/add_punctuations_test.dart diff --git a/dart-api-examples/add-punctuations/.gitignore b/dart-api-examples/add-punctuations/.gitignore new file mode 100644 index 000000000..3a8579040 --- /dev/null +++ b/dart-api-examples/add-punctuations/.gitignore @@ -0,0 +1,3 @@ +# https://dart.dev/guides/libraries/private-files +# Created by `dart pub` +.dart_tool/ diff --git a/dart-api-examples/add-punctuations/CHANGELOG.md b/dart-api-examples/add-punctuations/CHANGELOG.md new file mode 100644 index 000000000..effe43c82 --- /dev/null +++ b/dart-api-examples/add-punctuations/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +- Initial version. diff --git a/dart-api-examples/add-punctuations/README.md b/dart-api-examples/add-punctuations/README.md new file mode 100644 index 000000000..3816eca3a --- /dev/null +++ b/dart-api-examples/add-punctuations/README.md @@ -0,0 +1,2 @@ +A sample command-line application with an entrypoint in `bin/`, library code +in `lib/`, and example unit test in `test/`. diff --git a/dart-api-examples/add-punctuations/analysis_options.yaml b/dart-api-examples/add-punctuations/analysis_options.yaml new file mode 100644 index 000000000..dee8927aa --- /dev/null +++ b/dart-api-examples/add-punctuations/analysis_options.yaml @@ -0,0 +1,30 @@ +# This file configures the static analysis results for your project (errors, +# warnings, and lints). +# +# This enables the 'recommended' set of lints from `package:lints`. +# This set helps identify many issues that may lead to problems when running +# or consuming Dart code, and enforces writing Dart using a single, idiomatic +# style and format. +# +# If you want a smaller set of lints you can change this to specify +# 'package:lints/core.yaml'. These are just the most critical lints +# (the recommended set includes the core lints). +# The core lints are also what is used by pub.dev for scoring packages. + +include: package:lints/recommended.yaml + +# Uncomment the following section to specify additional rules. + +# linter: +# rules: +# - camel_case_types + +# analyzer: +# exclude: +# - path/to/excluded/files/** + +# For more information about the core and recommended set of lints, see +# https://dart.dev/go/core-lints + +# For additional information about configuring this file, see +# https://dart.dev/guides/language/analysis-options diff --git a/dart-api-examples/add-punctuations/bin/add_punctuations.dart b/dart-api-examples/add-punctuations/bin/add_punctuations.dart new file mode 100644 index 000000000..c4ab1ba40 --- /dev/null +++ b/dart-api-examples/add-punctuations/bin/add_punctuations.dart @@ -0,0 +1,5 @@ +import 'package:add_punctuations/add_punctuations.dart' as add_punctuations; + +void main(List arguments) { + print('Hello world: ${add_punctuations.calculate()}!'); +} diff --git a/dart-api-examples/add-punctuations/lib/add_punctuations.dart b/dart-api-examples/add-punctuations/lib/add_punctuations.dart new file mode 100644 index 000000000..f64ad7265 --- /dev/null +++ b/dart-api-examples/add-punctuations/lib/add_punctuations.dart @@ -0,0 +1,3 @@ +int calculate() { + return 6 * 7; +} diff --git a/dart-api-examples/add-punctuations/pubspec.yaml b/dart-api-examples/add-punctuations/pubspec.yaml new file mode 100644 index 000000000..db2fc081d --- /dev/null +++ b/dart-api-examples/add-punctuations/pubspec.yaml @@ -0,0 +1,15 @@ +name: add_punctuations +description: A sample command-line application. +version: 1.0.0 +# repository: https://github.com/my_org/my_repo + +environment: + sdk: ^3.4.0 + +# Add regular dependencies here. +dependencies: + # path: ^1.8.0 + +dev_dependencies: + lints: ^3.0.0 + test: ^1.24.0 diff --git a/dart-api-examples/add-punctuations/test/add_punctuations_test.dart b/dart-api-examples/add-punctuations/test/add_punctuations_test.dart new file mode 100644 index 000000000..accd7e588 --- /dev/null +++ b/dart-api-examples/add-punctuations/test/add_punctuations_test.dart @@ -0,0 +1,8 @@ +import 'package:add_punctuations/add_punctuations.dart'; +import 'package:test/test.dart'; + +void main() { + test('calculate', () { + expect(calculate(), 42); + }); +} From f04f26cada46b549a8323a83a79f517c7ddfdaf3 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Mon, 29 Jul 2024 12:12:17 +0800 Subject: [PATCH 3/9] remove unused files --- dart-api-examples/add-punctuations/CHANGELOG.md | 3 --- .../add-punctuations/lib/add_punctuations.dart | 3 --- .../add-punctuations/test/add_punctuations_test.dart | 8 -------- 3 files changed, 14 deletions(-) delete mode 100644 dart-api-examples/add-punctuations/CHANGELOG.md delete mode 100644 dart-api-examples/add-punctuations/lib/add_punctuations.dart delete mode 100644 dart-api-examples/add-punctuations/test/add_punctuations_test.dart diff --git a/dart-api-examples/add-punctuations/CHANGELOG.md b/dart-api-examples/add-punctuations/CHANGELOG.md deleted file mode 100644 index effe43c82..000000000 --- a/dart-api-examples/add-punctuations/CHANGELOG.md +++ /dev/null @@ -1,3 +0,0 @@ -## 1.0.0 - -- Initial version. diff --git a/dart-api-examples/add-punctuations/lib/add_punctuations.dart b/dart-api-examples/add-punctuations/lib/add_punctuations.dart deleted file mode 100644 index f64ad7265..000000000 --- a/dart-api-examples/add-punctuations/lib/add_punctuations.dart +++ /dev/null @@ -1,3 +0,0 @@ -int calculate() { - return 6 * 7; -} diff --git a/dart-api-examples/add-punctuations/test/add_punctuations_test.dart b/dart-api-examples/add-punctuations/test/add_punctuations_test.dart deleted file mode 100644 index accd7e588..000000000 --- a/dart-api-examples/add-punctuations/test/add_punctuations_test.dart +++ /dev/null @@ -1,8 +0,0 @@ -import 'package:add_punctuations/add_punctuations.dart'; -import 'package:test/test.dart'; - -void main() { - test('calculate', () { - expect(calculate(), 42); - }); -} From 6627f311ba0edc76e703092c0c59c1ddb5216070 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Mon, 29 Jul 2024 12:13:59 +0800 Subject: [PATCH 4/9] update pubspec --- dart-api-examples/add-punctuations/README.md | 6 ++++-- .../add-punctuations/pubspec.yaml | 14 +++++++++----- scripts/dart/add-punctuations-pubspec.yaml | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 scripts/dart/add-punctuations-pubspec.yaml diff --git a/dart-api-examples/add-punctuations/README.md b/dart-api-examples/add-punctuations/README.md index 3816eca3a..f5a5c58a0 100644 --- a/dart-api-examples/add-punctuations/README.md +++ b/dart-api-examples/add-punctuations/README.md @@ -1,2 +1,4 @@ -A sample command-line application with an entrypoint in `bin/`, library code -in `lib/`, and example unit test in `test/`. +# Introduction + +This example shows how to use the Dart API from sherpa-onnx to add punctuations to text. + diff --git a/dart-api-examples/add-punctuations/pubspec.yaml b/dart-api-examples/add-punctuations/pubspec.yaml index db2fc081d..783fe2752 100644 --- a/dart-api-examples/add-punctuations/pubspec.yaml +++ b/dart-api-examples/add-punctuations/pubspec.yaml @@ -1,15 +1,19 @@ name: add_punctuations -description: A sample command-line application. + +description: > + This example demonstrates how to use the Dart API to add punctuations to text. + version: 1.0.0 -# repository: https://github.com/my_org/my_repo environment: sdk: ^3.4.0 -# Add regular dependencies here. dependencies: - # path: ^1.8.0 + # sherpa_onnx: ^1.10.19 + sherpa_onnx: + path: ../../flutter/sherpa_onnx + path: ^1.9.0 + args: ^2.5.0 dev_dependencies: lints: ^3.0.0 - test: ^1.24.0 diff --git a/scripts/dart/add-punctuations-pubspec.yaml b/scripts/dart/add-punctuations-pubspec.yaml new file mode 100644 index 000000000..886695ac1 --- /dev/null +++ b/scripts/dart/add-punctuations-pubspec.yaml @@ -0,0 +1,18 @@ +name: add_punctuations + +description: > + This example demonstrates how to use the Dart API to add punctuations to text. + +version: 1.0.0 + +environment: + sdk: ^3.4.0 + +dependencies: + sherpa_onnx: + path: ../../flutter/sherpa_onnx + path: ^1.9.0 + args: ^2.5.0 + +dev_dependencies: + lints: ^3.0.0 From ed88d4092e56d719728c15ce88e8c10b57e83245 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Mon, 29 Jul 2024 12:23:43 +0800 Subject: [PATCH 5/9] Finish adding Dart API example about adding punctuations --- .github/scripts/test-dart.sh | 5 +++ .github/workflows/test-dart.yaml | 1 + .../bin/add_punctuations.dart | 5 --- .../add-punctuations/bin/init.dart | 1 + .../add-punctuations/bin/punctuations.dart | 44 +++++++++++++++++++ .../add-punctuations/run-ct-transformer.sh | 15 +++++++ 6 files changed, 66 insertions(+), 5 deletions(-) delete mode 100644 dart-api-examples/add-punctuations/bin/add_punctuations.dart create mode 120000 dart-api-examples/add-punctuations/bin/init.dart create mode 100644 dart-api-examples/add-punctuations/bin/punctuations.dart create mode 100755 dart-api-examples/add-punctuations/run-ct-transformer.sh diff --git a/.github/scripts/test-dart.sh b/.github/scripts/test-dart.sh index a35233602..bcdb86b67 100755 --- a/.github/scripts/test-dart.sh +++ b/.github/scripts/test-dart.sh @@ -4,6 +4,11 @@ set -ex cd dart-api-examples +pushd add-punctuations +echo '----------CT Transformer----------' +./run-ct-transformer.sh +popd + pushd audio-tagging echo '----------zipformer----------' ./run-zipformer.sh diff --git a/.github/workflows/test-dart.yaml b/.github/workflows/test-dart.yaml index e90176d09..f280bfc84 100644 --- a/.github/workflows/test-dart.yaml +++ b/.github/workflows/test-dart.yaml @@ -111,6 +111,7 @@ jobs: cp scripts/dart/kws-pubspec.yaml dart-api-examples/keyword-spotter/pubspec.yaml cp scripts/dart/vad-non-streaming-asr-pubspec.yaml dart-api-examples/vad-with-non-streaming-asr/pubspec.yaml cp scripts/dart/audio-tagging-pubspec.yaml dart-api-examples/audio-tagging/pubspec.yaml + cp scripts/dart/add-punctuations-pubspec.yaml dart-api-examples/add-punctuations/pubspec.yaml cp scripts/dart/sherpa-onnx-pubspec.yaml flutter/sherpa_onnx/pubspec.yaml diff --git a/dart-api-examples/add-punctuations/bin/add_punctuations.dart b/dart-api-examples/add-punctuations/bin/add_punctuations.dart deleted file mode 100644 index c4ab1ba40..000000000 --- a/dart-api-examples/add-punctuations/bin/add_punctuations.dart +++ /dev/null @@ -1,5 +0,0 @@ -import 'package:add_punctuations/add_punctuations.dart' as add_punctuations; - -void main(List arguments) { - print('Hello world: ${add_punctuations.calculate()}!'); -} diff --git a/dart-api-examples/add-punctuations/bin/init.dart b/dart-api-examples/add-punctuations/bin/init.dart new file mode 120000 index 000000000..48508cfd3 --- /dev/null +++ b/dart-api-examples/add-punctuations/bin/init.dart @@ -0,0 +1 @@ +../../vad/bin/init.dart \ No newline at end of file diff --git a/dart-api-examples/add-punctuations/bin/punctuations.dart b/dart-api-examples/add-punctuations/bin/punctuations.dart new file mode 100644 index 000000000..9f1690354 --- /dev/null +++ b/dart-api-examples/add-punctuations/bin/punctuations.dart @@ -0,0 +1,44 @@ +// Copyright (c) 2024 Xiaomi Corporation +import 'dart:io'; + +import 'package:args/args.dart'; +import 'package:sherpa_onnx/sherpa_onnx.dart' as sherpa_onnx; +import './init.dart'; + +void main(List arguments) async { + await initSherpaOnnx(); + + final parser = ArgParser()..addOption('model', help: 'Path to model.onnx'); + + final res = parser.parse(arguments); + if (res['model'] == null) { + print(parser.usage); + exit(1); + } + + final modelFile = res['model'] as String; + final modelConfig = sherpa_onnx.OfflinePunctuationModelConfig( + ctTransformer: modelFile, + numThreads: 1, + provider: 'cpu', + debug: false, + ); + + final config = sherpa_onnx.OfflinePunctuationConfig(model: modelConfig); + + final punct = sherpa_onnx.OfflinePunctuation(config: config); + + final texts = [ + '这是一个测试你好吗How are you我很好thank you are you ok谢谢你', + '我们都是木头人不会说话不会动', + 'The African blogosphere is rapidly expanding bringing more voices online in the form of commentaries opinions analyses rants and poetry', + ]; + + for (final t in texts) { + final textWithPunct = punct.addPunct(t); + print('----------'); + print('Before: $t'); + print('After: $textWithPunct'); + } + print('----------'); +} diff --git a/dart-api-examples/add-punctuations/run-ct-transformer.sh b/dart-api-examples/add-punctuations/run-ct-transformer.sh new file mode 100755 index 000000000..597c2077d --- /dev/null +++ b/dart-api-examples/add-punctuations/run-ct-transformer.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +set -ex + +dart pub get + +if [[ ! -f ./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx ]]; then + curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 + tar xvf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 + rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 +fi + +dart run \ + ./bin/punctuations.dart \ + --model ./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx From bce18de84b278c1c99e20e887eef6d159bc69c56 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Mon, 29 Jul 2024 12:27:09 +0800 Subject: [PATCH 6/9] update readme --- dart-api-examples/README.md | 1 + dart-api-examples/add-punctuations/README.md | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/dart-api-examples/README.md b/dart-api-examples/README.md index 239313eb3..da49310e1 100644 --- a/dart-api-examples/README.md +++ b/dart-api-examples/README.md @@ -16,6 +16,7 @@ https://pub.dev/packages/sherpa_onnx | [./vad](./vad)| Example for voice activity detection| | [./vad-with-non-streaming-asr](./vad-with-non-streaming-asr)| Example for voice activity detection with non-streaming speech recognition. You can use it to generate subtitles.| | [./audio-tagging](./audio-tagging)| Example for audio tagging.| +| [./add-punctuations](./add-punctuations)| Example for adding punctuations to text.| ## How to create an example in this folder diff --git a/dart-api-examples/add-punctuations/README.md b/dart-api-examples/add-punctuations/README.md index f5a5c58a0..95bb3d82f 100644 --- a/dart-api-examples/add-punctuations/README.md +++ b/dart-api-examples/add-punctuations/README.md @@ -2,3 +2,7 @@ This example shows how to use the Dart API from sherpa-onnx to add punctuations to text. +| File | Description| +|------|------------| +|[./bin/punctuations.dart](./bin/punctuations.dart)| Use a [CT Transformer model](https://modelscope.cn/models/iic/punc_ct-transformer_zh-cn-common-vocab272727-pytorch/summary) to add punctuations to text. See [./run-ct-transformer.sh](./run-ct-transformer.sh)| + From e1493e4084f784cb9f9b4fb243b88944aeeceb04 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Mon, 29 Jul 2024 12:30:51 +0800 Subject: [PATCH 7/9] release v1.10.20 --- CHANGELOG.md | 1 + CMakeLists.txt | 2 +- dart-api-examples/add-punctuations/pubspec.yaml | 4 +--- dart-api-examples/audio-tagging/pubspec.yaml | 2 +- dart-api-examples/keyword-spotter/pubspec.yaml | 2 +- dart-api-examples/non-streaming-asr/pubspec.yaml | 2 +- dart-api-examples/streaming-asr/pubspec.yaml | 2 +- dart-api-examples/tts/pubspec.yaml | 2 +- .../vad-with-non-streaming-asr/pubspec.yaml | 2 +- dart-api-examples/vad/pubspec.yaml | 2 +- flutter-examples/streaming_asr/pubspec.yaml | 4 ++-- flutter-examples/tts/pubspec.yaml | 4 ++-- flutter/sherpa_onnx/pubspec.yaml | 12 ++++++------ flutter/sherpa_onnx_ios/ios/sherpa_onnx_ios.podspec | 2 +- .../macos/sherpa_onnx_macos.podspec | 2 +- nodejs-addon-examples/package.json | 2 +- scripts/dart/sherpa-onnx-pubspec.yaml | 2 +- 17 files changed, 24 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1120192b8..47c0ff020 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 1.10.20 * Add Dart API for audio tagging +* Add Dart API for adding punctuations to text ## 1.10.19 diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ae1e3740..c9cbb568f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ project(sherpa-onnx) # ./nodejs-addon-examples # ./dart-api-examples/ # ./CHANGELOG.md -set(SHERPA_ONNX_VERSION "1.10.19") +set(SHERPA_ONNX_VERSION "1.10.20") # Disable warning about # diff --git a/dart-api-examples/add-punctuations/pubspec.yaml b/dart-api-examples/add-punctuations/pubspec.yaml index 783fe2752..dd15296ae 100644 --- a/dart-api-examples/add-punctuations/pubspec.yaml +++ b/dart-api-examples/add-punctuations/pubspec.yaml @@ -9,9 +9,7 @@ environment: sdk: ^3.4.0 dependencies: - # sherpa_onnx: ^1.10.19 - sherpa_onnx: - path: ../../flutter/sherpa_onnx + sherpa_onnx: ^1.10.20 path: ^1.9.0 args: ^2.5.0 diff --git a/dart-api-examples/audio-tagging/pubspec.yaml b/dart-api-examples/audio-tagging/pubspec.yaml index 15845b8c4..c5f6ec1cf 100644 --- a/dart-api-examples/audio-tagging/pubspec.yaml +++ b/dart-api-examples/audio-tagging/pubspec.yaml @@ -9,7 +9,7 @@ environment: sdk: ^3.4.0 dependencies: - sherpa_onnx: ^1.10.19 + sherpa_onnx: ^1.10.20 path: ^1.9.0 args: ^2.5.0 diff --git a/dart-api-examples/keyword-spotter/pubspec.yaml b/dart-api-examples/keyword-spotter/pubspec.yaml index 205aa1f89..fa52703e4 100644 --- a/dart-api-examples/keyword-spotter/pubspec.yaml +++ b/dart-api-examples/keyword-spotter/pubspec.yaml @@ -9,7 +9,7 @@ environment: sdk: ^3.4.0 dependencies: - sherpa_onnx: ^1.10.19 + sherpa_onnx: ^1.10.20 # sherpa_onnx: # path: ../../flutter/sherpa_onnx path: ^1.9.0 diff --git a/dart-api-examples/non-streaming-asr/pubspec.yaml b/dart-api-examples/non-streaming-asr/pubspec.yaml index 9f287e25e..5b9697bd2 100644 --- a/dart-api-examples/non-streaming-asr/pubspec.yaml +++ b/dart-api-examples/non-streaming-asr/pubspec.yaml @@ -10,7 +10,7 @@ environment: # Add regular dependencies here. dependencies: - sherpa_onnx: ^1.10.19 + sherpa_onnx: ^1.10.20 path: ^1.9.0 args: ^2.5.0 diff --git a/dart-api-examples/streaming-asr/pubspec.yaml b/dart-api-examples/streaming-asr/pubspec.yaml index 7c471f608..89f58df53 100644 --- a/dart-api-examples/streaming-asr/pubspec.yaml +++ b/dart-api-examples/streaming-asr/pubspec.yaml @@ -11,7 +11,7 @@ environment: # Add regular dependencies here. dependencies: - sherpa_onnx: ^1.10.19 + sherpa_onnx: ^1.10.20 path: ^1.9.0 args: ^2.5.0 diff --git a/dart-api-examples/tts/pubspec.yaml b/dart-api-examples/tts/pubspec.yaml index 0d8fd5c14..5bcf28f18 100644 --- a/dart-api-examples/tts/pubspec.yaml +++ b/dart-api-examples/tts/pubspec.yaml @@ -8,7 +8,7 @@ environment: # Add regular dependencies here. dependencies: - sherpa_onnx: ^1.10.19 + sherpa_onnx: ^1.10.20 path: ^1.9.0 args: ^2.5.0 diff --git a/dart-api-examples/vad-with-non-streaming-asr/pubspec.yaml b/dart-api-examples/vad-with-non-streaming-asr/pubspec.yaml index 955796b6a..794908fa5 100644 --- a/dart-api-examples/vad-with-non-streaming-asr/pubspec.yaml +++ b/dart-api-examples/vad-with-non-streaming-asr/pubspec.yaml @@ -10,7 +10,7 @@ environment: sdk: ^3.4.0 dependencies: - sherpa_onnx: ^1.10.19 + sherpa_onnx: ^1.10.20 path: ^1.9.0 args: ^2.5.0 diff --git a/dart-api-examples/vad/pubspec.yaml b/dart-api-examples/vad/pubspec.yaml index da9b42ef7..0b05f4758 100644 --- a/dart-api-examples/vad/pubspec.yaml +++ b/dart-api-examples/vad/pubspec.yaml @@ -9,7 +9,7 @@ environment: sdk: ^3.4.0 dependencies: - sherpa_onnx: ^1.10.19 + sherpa_onnx: ^1.10.20 path: ^1.9.0 args: ^2.5.0 diff --git a/flutter-examples/streaming_asr/pubspec.yaml b/flutter-examples/streaming_asr/pubspec.yaml index 2867dedb9..c6ca09780 100644 --- a/flutter-examples/streaming_asr/pubspec.yaml +++ b/flutter-examples/streaming_asr/pubspec.yaml @@ -5,7 +5,7 @@ description: > publish_to: 'none' -version: 1.10.19 +version: 1.10.20 topics: - speech-recognition @@ -30,7 +30,7 @@ dependencies: record: ^5.1.0 url_launcher: ^6.2.6 - sherpa_onnx: ^1.10.19 + sherpa_onnx: ^1.10.20 # sherpa_onnx: # path: ../../flutter/sherpa_onnx diff --git a/flutter-examples/tts/pubspec.yaml b/flutter-examples/tts/pubspec.yaml index 14b9164bf..468538242 100644 --- a/flutter-examples/tts/pubspec.yaml +++ b/flutter-examples/tts/pubspec.yaml @@ -5,7 +5,7 @@ description: > publish_to: 'none' # Remove this line if you wish to publish to pub.dev -version: 1.10.19 +version: 1.10.20 environment: sdk: '>=3.4.0 <4.0.0' @@ -17,7 +17,7 @@ dependencies: cupertino_icons: ^1.0.6 path_provider: ^2.1.3 path: ^1.9.0 - sherpa_onnx: ^1.10.19 + sherpa_onnx: ^1.10.20 url_launcher: ^6.2.6 audioplayers: ^5.0.0 diff --git a/flutter/sherpa_onnx/pubspec.yaml b/flutter/sherpa_onnx/pubspec.yaml index c00b9868c..30df70a7a 100644 --- a/flutter/sherpa_onnx/pubspec.yaml +++ b/flutter/sherpa_onnx/pubspec.yaml @@ -17,7 +17,7 @@ topics: - voice-activity-detection # remember to change the version in ../sherpa_onnx_macos/macos/sherpa_onnx_macos.podspec -version: 1.10.19 +version: 1.10.20 homepage: https://github.com/k2-fsa/sherpa-onnx @@ -30,23 +30,23 @@ dependencies: flutter: sdk: flutter - sherpa_onnx_android: ^1.10.19 + sherpa_onnx_android: ^1.10.20 # sherpa_onnx_android: # path: ../sherpa_onnx_android - sherpa_onnx_macos: ^1.10.19 + sherpa_onnx_macos: ^1.10.20 # sherpa_onnx_macos: # path: ../sherpa_onnx_macos - sherpa_onnx_linux: ^1.10.19 + sherpa_onnx_linux: ^1.10.20 # sherpa_onnx_linux: # path: ../sherpa_onnx_linux # - sherpa_onnx_windows: ^1.10.19 + sherpa_onnx_windows: ^1.10.20 # sherpa_onnx_windows: # path: ../sherpa_onnx_windows - sherpa_onnx_ios: ^1.10.19 + sherpa_onnx_ios: ^1.10.20 # sherpa_onnx_ios: # path: ../sherpa_onnx_ios diff --git a/flutter/sherpa_onnx_ios/ios/sherpa_onnx_ios.podspec b/flutter/sherpa_onnx_ios/ios/sherpa_onnx_ios.podspec index a2c9241c8..06eab244c 100644 --- a/flutter/sherpa_onnx_ios/ios/sherpa_onnx_ios.podspec +++ b/flutter/sherpa_onnx_ios/ios/sherpa_onnx_ios.podspec @@ -7,7 +7,7 @@ # https://groups.google.com/g/dart-ffi/c/nUATMBy7r0c Pod::Spec.new do |s| s.name = 'sherpa_onnx_ios' - s.version = '1.10.19' + s.version = '1.10.20' s.summary = 'A new Flutter FFI plugin project.' s.description = <<-DESC A new Flutter FFI plugin project. diff --git a/flutter/sherpa_onnx_macos/macos/sherpa_onnx_macos.podspec b/flutter/sherpa_onnx_macos/macos/sherpa_onnx_macos.podspec index a15f6a896..dc3b31026 100644 --- a/flutter/sherpa_onnx_macos/macos/sherpa_onnx_macos.podspec +++ b/flutter/sherpa_onnx_macos/macos/sherpa_onnx_macos.podspec @@ -4,7 +4,7 @@ # Pod::Spec.new do |s| s.name = 'sherpa_onnx_macos' - s.version = '1.10.19' + s.version = '1.10.20' s.summary = 'sherpa-onnx Flutter FFI plugin project.' s.description = <<-DESC sherpa-onnx Flutter FFI plugin project. diff --git a/nodejs-addon-examples/package.json b/nodejs-addon-examples/package.json index 9a76720f9..defbf30b0 100644 --- a/nodejs-addon-examples/package.json +++ b/nodejs-addon-examples/package.json @@ -1,5 +1,5 @@ { "dependencies": { - "sherpa-onnx-node": "^1.10.19" + "sherpa-onnx-node": "^1.10.20" } } diff --git a/scripts/dart/sherpa-onnx-pubspec.yaml b/scripts/dart/sherpa-onnx-pubspec.yaml index 11e7baf0d..c315f1cf9 100644 --- a/scripts/dart/sherpa-onnx-pubspec.yaml +++ b/scripts/dart/sherpa-onnx-pubspec.yaml @@ -17,7 +17,7 @@ topics: - voice-activity-detection # remember to change the version in ../sherpa_onnx_macos/macos/sherpa_onnx.podspec -version: 1.10.19 +version: 1.10.20 homepage: https://github.com/k2-fsa/sherpa-onnx From e79f96bb464b0320edb06db71cdf57a1213994e7 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Mon, 29 Jul 2024 12:35:09 +0800 Subject: [PATCH 8/9] free punctuation to avoid memory leak --- dart-api-examples/add-punctuations/bin/punctuations.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dart-api-examples/add-punctuations/bin/punctuations.dart b/dart-api-examples/add-punctuations/bin/punctuations.dart index 9f1690354..47505cca5 100644 --- a/dart-api-examples/add-punctuations/bin/punctuations.dart +++ b/dart-api-examples/add-punctuations/bin/punctuations.dart @@ -41,4 +41,6 @@ void main(List arguments) async { print('After: $textWithPunct'); } print('----------'); + + punct.free(); } From c1ae680257eafce00cea7645593b0f021f2ff276 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Mon, 29 Jul 2024 12:38:12 +0800 Subject: [PATCH 9/9] minor fixes --- flutter/sherpa_onnx/lib/src/punctuation.dart | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/flutter/sherpa_onnx/lib/src/punctuation.dart b/flutter/sherpa_onnx/lib/src/punctuation.dart index 3c75afe4d..b4197fa46 100644 --- a/flutter/sherpa_onnx/lib/src/punctuation.dart +++ b/flutter/sherpa_onnx/lib/src/punctuation.dart @@ -73,8 +73,9 @@ class OfflinePunctuation { ?.call(ptr, textPtr) ?? nullptr; + calloc.free(textPtr); + if (p == nullptr) { - calloc.free(textPtr); return ''; } @@ -82,8 +83,6 @@ class OfflinePunctuation { SherpaOnnxBindings.sherpaOfflinePunctuationFreeText?.call(p); - calloc.free(textPtr); - return ans; }