Skip to content

Commit d8809b5

Browse files
authored
Fix CI errors introduced by supporting loading keywords from buffers (#1366)
1 parent 06b61cc commit d8809b5

File tree

8 files changed

+55
-7
lines changed

8 files changed

+55
-7
lines changed

flutter/sherpa_onnx/lib/src/keyword_spotter.dart

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ class KeywordSpotterConfig {
1919
this.keywordsScore = 1.0,
2020
this.keywordsThreshold = 0.25,
2121
this.keywordsFile = '',
22+
this.keywordsBuf = '',
23+
this.keywordsBufSize = 0,
2224
});
2325

2426
@override
2527
String toString() {
26-
return 'KeywordSpotterConfig(feat: $feat, model: $model, maxActivePaths: $maxActivePaths, numTrailingBlanks: $numTrailingBlanks, keywordsScore: $keywordsScore, keywordsThreshold: $keywordsThreshold, keywordsFile: $keywordsFile)';
28+
return 'KeywordSpotterConfig(feat: $feat, model: $model, maxActivePaths: $maxActivePaths, numTrailingBlanks: $numTrailingBlanks, keywordsScore: $keywordsScore, keywordsThreshold: $keywordsThreshold, keywordsFile: $keywordsFile, keywordsBuf: $keywordsBuf, keywordsBufSize: $keywordsBufSize)';
2729
}
2830

2931
final FeatureConfig feat;
@@ -35,6 +37,8 @@ class KeywordSpotterConfig {
3537
final double keywordsScore;
3638
final double keywordsThreshold;
3739
final String keywordsFile;
40+
final String keywordsBuf;
41+
final int keywordsBufSize;
3842
}
3943

4044
class KeywordResult {
@@ -89,9 +93,12 @@ class KeywordSpotter {
8993
c.ref.keywordsScore = config.keywordsScore;
9094
c.ref.keywordsThreshold = config.keywordsThreshold;
9195
c.ref.keywordsFile = config.keywordsFile.toNativeUtf8();
96+
c.ref.keywordsBuf = config.keywordsBuf.toNativeUtf8();
97+
c.ref.keywordsBufSize = config.keywordsBufSize;
9298

9399
final ptr = SherpaOnnxBindings.createKeywordSpotter?.call(c) ?? nullptr;
94100

101+
calloc.free(c.ref.keywordsBuf);
95102
calloc.free(c.ref.keywordsFile);
96103
calloc.free(c.ref.model.bpeVocab);
97104
calloc.free(c.ref.model.modelingUnit);

flutter/sherpa_onnx/lib/src/sherpa_onnx_bindings.dart

+5
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ final class SherpaOnnxKeywordSpotterConfig extends Struct {
371371
external double keywordsThreshold;
372372

373373
external Pointer<Utf8> keywordsFile;
374+
375+
external Pointer<Utf8> keywordsBuf;
376+
377+
@Int32()
378+
external int keywordsBufSize;
374379
}
375380

376381
final class SherpaOnnxOfflinePunctuation extends Opaque {}

scripts/dotnet/KeywordSpotterConfig.cs

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public KeywordSpotterConfig()
1717
KeywordsScore = 1.0F;
1818
KeywordsThreshold = 0.25F;
1919
KeywordsFile = "";
20+
KeywordsBuf= "";
21+
KeywordsBufSize= 0;
2022
}
2123
public FeatureConfig FeatConfig;
2224
public OnlineModelConfig ModelConfig;
@@ -28,5 +30,10 @@ public KeywordSpotterConfig()
2830

2931
[MarshalAs(UnmanagedType.LPStr)]
3032
public string KeywordsFile;
33+
34+
[MarshalAs(UnmanagedType.LPStr)]
35+
public string KeywordsBuf;
36+
37+
public int KeywordsBufSize;
3138
}
3239
}

scripts/go/_internal/build_linux_arm64.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
package sherpa_onnx
44

5-
// #cgo LDFLAGS: -L ${SRCDIR}/lib/aarch64-unknown-linux-gnu -lsherpa-onnx-c-api -lsherpa-onnx-core -lkaldi-native-fbank-core -lkaldi-decoder-core -lsherpa-onnx-kaldifst-core -lsherpa-onnx-fstfar -lsherpa-onnx-fst -lpiper_phonemize -lespeak-ng -lucd -lonnxruntime -lssentencepiece_core -Wl,-rpath,${SRCDIR}/lib/aarch64-unknown-linux-gnu
5+
// #cgo LDFLAGS: -L ${SRCDIR}/lib/aarch64-unknown-linux-gnu -lsherpa-onnx-c-api -lonnxruntime -Wl,-rpath,${SRCDIR}/lib/aarch64-unknown-linux-gnu
66
import "C"

scripts/node-addon-api/src/keyword-spotting.cc

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ static Napi::External<SherpaOnnxKeywordSpotter> CreateKeywordSpotterWrapper(
4343
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(keywords_score, keywordsScore);
4444
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(keywords_threshold, keywordsThreshold);
4545
SHERPA_ONNX_ASSIGN_ATTR_STR(keywords_file, keywordsFile);
46+
SHERPA_ONNX_ASSIGN_ATTR_STR(keywords_buf, keywordsBuf);
47+
SHERPA_ONNX_ASSIGN_ATTR_INT32(keywords_buf_size, keywordsBufSize);
4648

4749
SherpaOnnxKeywordSpotter *kws = SherpaOnnxCreateKeywordSpotter(&c);
4850

@@ -86,6 +88,10 @@ static Napi::External<SherpaOnnxKeywordSpotter> CreateKeywordSpotterWrapper(
8688
delete[] c.keywords_file;
8789
}
8890

91+
if (c.keywords_buf) {
92+
delete[] c.keywords_buf;
93+
}
94+
8995
if (!kws) {
9096
Napi::TypeError::New(env, "Please check your config!")
9197
.ThrowAsJavaScriptException();

swift-api-examples/SherpaOnnx.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,9 @@ func sherpaOnnxKeywordSpotterConfig(
966966
maxActivePaths: Int = 4,
967967
numTrailingBlanks: Int = 1,
968968
keywordsScore: Float = 1.0,
969-
keywordsThreshold: Float = 0.25
969+
keywordsThreshold: Float = 0.25,
970+
keywordsBuf: String = "",
971+
keywordsBufSize: Int = 0
970972
) -> SherpaOnnxKeywordSpotterConfig {
971973
return SherpaOnnxKeywordSpotterConfig(
972974
feat_config: featConfig,
@@ -975,7 +977,9 @@ func sherpaOnnxKeywordSpotterConfig(
975977
num_trailing_blanks: Int32(numTrailingBlanks),
976978
keywords_score: keywordsScore,
977979
keywords_threshold: keywordsThreshold,
978-
keywords_file: toCPointer(keywordsFile)
980+
keywords_file: toCPointer(keywordsFile),
981+
keywords_buf: toCPointer(keywordsBuf),
982+
keywords_buf_size: Int32(keywordsBufSize)
979983
)
980984
}
981985

wasm/kws/sherpa-onnx-kws.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,18 @@ function initKwsConfig(config, Module) {
172172
};
173173
}
174174

175+
if (!('keywordsBuf' in config)) {
176+
config.keywordsBuf = '';
177+
}
178+
179+
if (!('keywordsBufSize' in config)) {
180+
config.keywordsBufSize = 0;
181+
}
182+
175183
let featConfig = initFeatureExtractorConfig(config.featConfig, Module);
176184

177185
let modelConfig = initModelConfig(config.modelConfig, Module);
178-
let numBytes = featConfig.len + modelConfig.len + 4 * 5;
186+
let numBytes = featConfig.len + modelConfig.len + 4 * 7;
179187

180188
let ptr = Module._malloc(numBytes);
181189
let offset = 0;
@@ -198,11 +206,22 @@ function initKwsConfig(config, Module) {
198206
offset += 4;
199207

200208
let keywordsLen = Module.lengthBytesUTF8(config.keywords) + 1;
201-
let keywordsBuffer = Module._malloc(keywordsLen);
209+
let keywordsBufLen = Module.lengthBytesUTF8(config.keywordsBuf) + 1;
210+
211+
let keywordsBuffer = Module._malloc(keywordsLen + keywordsBufLen);
202212
Module.stringToUTF8(config.keywords, keywordsBuffer, keywordsLen);
213+
Module.stringToUTF8(
214+
config.keywordsBuf, keywordsBuffer + keywordsLen, keywordsBufLen);
215+
203216
Module.setValue(ptr + offset, keywordsBuffer, 'i8*');
204217
offset += 4;
205218

219+
Module.setValue(ptr + offset, keywordsBuffer + keywordsLen, 'i8*');
220+
offset += 4;
221+
222+
Module.setValue(ptr + offset, config.keywordsBufLen, 'i32');
223+
offset += 4;
224+
206225
return {
207226
ptr: ptr, len: numBytes, featConfig: featConfig, modelConfig: modelConfig,
208227
keywordsBuffer: keywordsBuffer

wasm/kws/sherpa-onnx-wasm-main-kws.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static_assert(sizeof(SherpaOnnxOnlineModelConfig) ==
2424
static_assert(sizeof(SherpaOnnxFeatureConfig) == 2 * 4, "");
2525
static_assert(sizeof(SherpaOnnxKeywordSpotterConfig) ==
2626
sizeof(SherpaOnnxFeatureConfig) +
27-
sizeof(SherpaOnnxOnlineModelConfig) + 5 * 4,
27+
sizeof(SherpaOnnxOnlineModelConfig) + 7 * 4,
2828
"");
2929

3030
void CopyHeap(const char *src, int32_t num_bytes, char *dst) {

0 commit comments

Comments
 (0)