Dart API for adding punctuations to text (#1182)
This commit is contained in:
@@ -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';
|
||||
|
||||
91
flutter/sherpa_onnx/lib/src/punctuation.dart
Normal file
91
flutter/sherpa_onnx/lib/src/punctuation.dart
Normal file
@@ -0,0 +1,91 @@
|
||||
// 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<SherpaOnnxOfflinePunctuationConfig>();
|
||||
|
||||
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;
|
||||
|
||||
calloc.free(textPtr);
|
||||
|
||||
if (p == nullptr) {
|
||||
return '';
|
||||
}
|
||||
|
||||
final ans = p.toDartString();
|
||||
|
||||
SherpaOnnxBindings.sherpaOfflinePunctuationFreeText?.call(p);
|
||||
|
||||
return ans;
|
||||
}
|
||||
|
||||
Pointer<SherpaOnnxOfflinePunctuation> ptr;
|
||||
final OfflinePunctuationConfig config;
|
||||
}
|
||||
@@ -2,6 +2,22 @@
|
||||
import 'dart:ffi';
|
||||
import 'package:ffi/ffi.dart';
|
||||
|
||||
final class SherpaOnnxOfflinePunctuationModelConfig extends Struct {
|
||||
external Pointer<Utf8> ctTransformer;
|
||||
|
||||
@Int32()
|
||||
external int numThreads;
|
||||
|
||||
@Int32()
|
||||
external int debug;
|
||||
|
||||
external Pointer<Utf8> provider;
|
||||
}
|
||||
|
||||
final class SherpaOnnxOfflinePunctuationConfig extends Struct {
|
||||
external SherpaOnnxOfflinePunctuationModelConfig model;
|
||||
}
|
||||
|
||||
final class SherpaOnnxOfflineZipformerAudioTaggingModelConfig extends Struct {
|
||||
external Pointer<Utf8> model;
|
||||
}
|
||||
@@ -338,6 +354,8 @@ final class SherpaOnnxKeywordSpotterConfig extends Struct {
|
||||
external Pointer<Utf8> 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<SherpaOnnxOfflinePunctuation> Function(
|
||||
Pointer<SherpaOnnxOfflinePunctuationConfig>);
|
||||
|
||||
typedef SherpaOnnxCreateOfflinePunctuation
|
||||
= SherpaOnnxCreateOfflinePunctuationNative;
|
||||
|
||||
typedef SherpaOnnxDestroyOfflinePunctuationNative = Void Function(
|
||||
Pointer<SherpaOnnxOfflinePunctuation>);
|
||||
|
||||
typedef SherpaOnnxDestroyOfflinePunctuation = void Function(
|
||||
Pointer<SherpaOnnxOfflinePunctuation>);
|
||||
|
||||
typedef SherpaOfflinePunctuationAddPunctNative = Pointer<Utf8> Function(
|
||||
Pointer<SherpaOnnxOfflinePunctuation>, Pointer<Utf8>);
|
||||
|
||||
typedef SherpaOfflinePunctuationAddPunct
|
||||
= SherpaOfflinePunctuationAddPunctNative;
|
||||
|
||||
typedef SherpaOfflinePunctuationFreeTextNative = Void Function(Pointer<Utf8>);
|
||||
|
||||
typedef SherpaOfflinePunctuationFreeText = void Function(Pointer<Utf8>);
|
||||
|
||||
typedef SherpaOnnxCreateAudioTaggingNative = Pointer<SherpaOnnxAudioTagging>
|
||||
Function(Pointer<SherpaOnnxAudioTaggingConfig>);
|
||||
|
||||
@@ -875,6 +916,12 @@ typedef SherpaOnnxFreeWaveNative = Void Function(Pointer<SherpaOnnxWave>);
|
||||
typedef SherpaOnnxFreeWave = void Function(Pointer<SherpaOnnxWave>);
|
||||
|
||||
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<NativeFunction<SherpaOnnxCreateOfflinePunctuationNative>>(
|
||||
'SherpaOnnxCreateOfflinePunctuation')
|
||||
.asFunction();
|
||||
|
||||
sherpaOnnxDestroyOfflinePunctuation ??= dynamicLibrary
|
||||
.lookup<NativeFunction<SherpaOnnxDestroyOfflinePunctuationNative>>(
|
||||
'SherpaOnnxDestroyOfflinePunctuation')
|
||||
.asFunction();
|
||||
|
||||
sherpaOfflinePunctuationAddPunct ??= dynamicLibrary
|
||||
.lookup<NativeFunction<SherpaOfflinePunctuationAddPunctNative>>(
|
||||
'SherpaOfflinePunctuationAddPunct')
|
||||
.asFunction();
|
||||
|
||||
sherpaOfflinePunctuationFreeText ??= dynamicLibrary
|
||||
.lookup<NativeFunction<SherpaOfflinePunctuationFreeTextNative>>(
|
||||
'SherpaOfflinePunctuationFreeText')
|
||||
.asFunction();
|
||||
|
||||
sherpaOnnxCreateAudioTagging ??= dynamicLibrary
|
||||
.lookup<NativeFunction<SherpaOnnxCreateAudioTaggingNative>>(
|
||||
'SherpaOnnxCreateAudioTagging')
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user