Fangjun Kuang
Committed by GitHub

Fix CI errors introduced by supporting loading keywords from buffers (#1366)

@@ -19,11 +19,13 @@ class KeywordSpotterConfig { @@ -19,11 +19,13 @@ class KeywordSpotterConfig {
19 this.keywordsScore = 1.0, 19 this.keywordsScore = 1.0,
20 this.keywordsThreshold = 0.25, 20 this.keywordsThreshold = 0.25,
21 this.keywordsFile = '', 21 this.keywordsFile = '',
  22 + this.keywordsBuf = '',
  23 + this.keywordsBufSize = 0,
22 }); 24 });
23 25
24 @override 26 @override
25 String toString() { 27 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)';
27 } 29 }
28 30
29 final FeatureConfig feat; 31 final FeatureConfig feat;
@@ -35,6 +37,8 @@ class KeywordSpotterConfig { @@ -35,6 +37,8 @@ class KeywordSpotterConfig {
35 final double keywordsScore; 37 final double keywordsScore;
36 final double keywordsThreshold; 38 final double keywordsThreshold;
37 final String keywordsFile; 39 final String keywordsFile;
  40 + final String keywordsBuf;
  41 + final int keywordsBufSize;
38 } 42 }
39 43
40 class KeywordResult { 44 class KeywordResult {
@@ -89,9 +93,12 @@ class KeywordSpotter { @@ -89,9 +93,12 @@ class KeywordSpotter {
89 c.ref.keywordsScore = config.keywordsScore; 93 c.ref.keywordsScore = config.keywordsScore;
90 c.ref.keywordsThreshold = config.keywordsThreshold; 94 c.ref.keywordsThreshold = config.keywordsThreshold;
91 c.ref.keywordsFile = config.keywordsFile.toNativeUtf8(); 95 c.ref.keywordsFile = config.keywordsFile.toNativeUtf8();
  96 + c.ref.keywordsBuf = config.keywordsBuf.toNativeUtf8();
  97 + c.ref.keywordsBufSize = config.keywordsBufSize;
92 98
93 final ptr = SherpaOnnxBindings.createKeywordSpotter?.call(c) ?? nullptr; 99 final ptr = SherpaOnnxBindings.createKeywordSpotter?.call(c) ?? nullptr;
94 100
  101 + calloc.free(c.ref.keywordsBuf);
95 calloc.free(c.ref.keywordsFile); 102 calloc.free(c.ref.keywordsFile);
96 calloc.free(c.ref.model.bpeVocab); 103 calloc.free(c.ref.model.bpeVocab);
97 calloc.free(c.ref.model.modelingUnit); 104 calloc.free(c.ref.model.modelingUnit);
@@ -371,6 +371,11 @@ final class SherpaOnnxKeywordSpotterConfig extends Struct { @@ -371,6 +371,11 @@ final class SherpaOnnxKeywordSpotterConfig extends Struct {
371 external double keywordsThreshold; 371 external double keywordsThreshold;
372 372
373 external Pointer<Utf8> keywordsFile; 373 external Pointer<Utf8> keywordsFile;
  374 +
  375 + external Pointer<Utf8> keywordsBuf;
  376 +
  377 + @Int32()
  378 + external int keywordsBufSize;
374 } 379 }
375 380
376 final class SherpaOnnxOfflinePunctuation extends Opaque {} 381 final class SherpaOnnxOfflinePunctuation extends Opaque {}
@@ -17,6 +17,8 @@ namespace SherpaOnnx @@ -17,6 +17,8 @@ namespace SherpaOnnx
17 KeywordsScore = 1.0F; 17 KeywordsScore = 1.0F;
18 KeywordsThreshold = 0.25F; 18 KeywordsThreshold = 0.25F;
19 KeywordsFile = ""; 19 KeywordsFile = "";
  20 + KeywordsBuf= "";
  21 + KeywordsBufSize= 0;
20 } 22 }
21 public FeatureConfig FeatConfig; 23 public FeatureConfig FeatConfig;
22 public OnlineModelConfig ModelConfig; 24 public OnlineModelConfig ModelConfig;
@@ -28,5 +30,10 @@ namespace SherpaOnnx @@ -28,5 +30,10 @@ namespace SherpaOnnx
28 30
29 [MarshalAs(UnmanagedType.LPStr)] 31 [MarshalAs(UnmanagedType.LPStr)]
30 public string KeywordsFile; 32 public string KeywordsFile;
  33 +
  34 + [MarshalAs(UnmanagedType.LPStr)]
  35 + public string KeywordsBuf;
  36 +
  37 + public int KeywordsBufSize;
31 } 38 }
32 } 39 }
@@ -2,5 +2,5 @@ @@ -2,5 +2,5 @@
2 2
3 package sherpa_onnx 3 package sherpa_onnx
4 4
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
6 import "C" 6 import "C"
@@ -43,6 +43,8 @@ static Napi::External<SherpaOnnxKeywordSpotter> CreateKeywordSpotterWrapper( @@ -43,6 +43,8 @@ static Napi::External<SherpaOnnxKeywordSpotter> CreateKeywordSpotterWrapper(
43 SHERPA_ONNX_ASSIGN_ATTR_FLOAT(keywords_score, keywordsScore); 43 SHERPA_ONNX_ASSIGN_ATTR_FLOAT(keywords_score, keywordsScore);
44 SHERPA_ONNX_ASSIGN_ATTR_FLOAT(keywords_threshold, keywordsThreshold); 44 SHERPA_ONNX_ASSIGN_ATTR_FLOAT(keywords_threshold, keywordsThreshold);
45 SHERPA_ONNX_ASSIGN_ATTR_STR(keywords_file, keywordsFile); 45 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);
46 48
47 SherpaOnnxKeywordSpotter *kws = SherpaOnnxCreateKeywordSpotter(&c); 49 SherpaOnnxKeywordSpotter *kws = SherpaOnnxCreateKeywordSpotter(&c);
48 50
@@ -86,6 +88,10 @@ static Napi::External<SherpaOnnxKeywordSpotter> CreateKeywordSpotterWrapper( @@ -86,6 +88,10 @@ static Napi::External<SherpaOnnxKeywordSpotter> CreateKeywordSpotterWrapper(
86 delete[] c.keywords_file; 88 delete[] c.keywords_file;
87 } 89 }
88 90
  91 + if (c.keywords_buf) {
  92 + delete[] c.keywords_buf;
  93 + }
  94 +
89 if (!kws) { 95 if (!kws) {
90 Napi::TypeError::New(env, "Please check your config!") 96 Napi::TypeError::New(env, "Please check your config!")
91 .ThrowAsJavaScriptException(); 97 .ThrowAsJavaScriptException();
@@ -966,7 +966,9 @@ func sherpaOnnxKeywordSpotterConfig( @@ -966,7 +966,9 @@ func sherpaOnnxKeywordSpotterConfig(
966 maxActivePaths: Int = 4, 966 maxActivePaths: Int = 4,
967 numTrailingBlanks: Int = 1, 967 numTrailingBlanks: Int = 1,
968 keywordsScore: Float = 1.0, 968 keywordsScore: Float = 1.0,
969 - keywordsThreshold: Float = 0.25 969 + keywordsThreshold: Float = 0.25,
  970 + keywordsBuf: String = "",
  971 + keywordsBufSize: Int = 0
970 ) -> SherpaOnnxKeywordSpotterConfig { 972 ) -> SherpaOnnxKeywordSpotterConfig {
971 return SherpaOnnxKeywordSpotterConfig( 973 return SherpaOnnxKeywordSpotterConfig(
972 feat_config: featConfig, 974 feat_config: featConfig,
@@ -975,7 +977,9 @@ func sherpaOnnxKeywordSpotterConfig( @@ -975,7 +977,9 @@ func sherpaOnnxKeywordSpotterConfig(
975 num_trailing_blanks: Int32(numTrailingBlanks), 977 num_trailing_blanks: Int32(numTrailingBlanks),
976 keywords_score: keywordsScore, 978 keywords_score: keywordsScore,
977 keywords_threshold: keywordsThreshold, 979 keywords_threshold: keywordsThreshold,
978 - keywords_file: toCPointer(keywordsFile) 980 + keywords_file: toCPointer(keywordsFile),
  981 + keywords_buf: toCPointer(keywordsBuf),
  982 + keywords_buf_size: Int32(keywordsBufSize)
979 ) 983 )
980 } 984 }
981 985
@@ -172,10 +172,18 @@ function initKwsConfig(config, Module) { @@ -172,10 +172,18 @@ function initKwsConfig(config, Module) {
172 }; 172 };
173 } 173 }
174 174
  175 + if (!('keywordsBuf' in config)) {
  176 + config.keywordsBuf = '';
  177 + }
  178 +
  179 + if (!('keywordsBufSize' in config)) {
  180 + config.keywordsBufSize = 0;
  181 + }
  182 +
175 let featConfig = initFeatureExtractorConfig(config.featConfig, Module); 183 let featConfig = initFeatureExtractorConfig(config.featConfig, Module);
176 184
177 let modelConfig = initModelConfig(config.modelConfig, Module); 185 let modelConfig = initModelConfig(config.modelConfig, Module);
178 - let numBytes = featConfig.len + modelConfig.len + 4 * 5; 186 + let numBytes = featConfig.len + modelConfig.len + 4 * 7;
179 187
180 let ptr = Module._malloc(numBytes); 188 let ptr = Module._malloc(numBytes);
181 let offset = 0; 189 let offset = 0;
@@ -198,11 +206,22 @@ function initKwsConfig(config, Module) { @@ -198,11 +206,22 @@ function initKwsConfig(config, Module) {
198 offset += 4; 206 offset += 4;
199 207
200 let keywordsLen = Module.lengthBytesUTF8(config.keywords) + 1; 208 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);
202 Module.stringToUTF8(config.keywords, keywordsBuffer, keywordsLen); 212 Module.stringToUTF8(config.keywords, keywordsBuffer, keywordsLen);
  213 + Module.stringToUTF8(
  214 + config.keywordsBuf, keywordsBuffer + keywordsLen, keywordsBufLen);
  215 +
203 Module.setValue(ptr + offset, keywordsBuffer, 'i8*'); 216 Module.setValue(ptr + offset, keywordsBuffer, 'i8*');
204 offset += 4; 217 offset += 4;
205 218
  219 + Module.setValue(ptr + offset, keywordsBuffer + keywordsLen, 'i8*');
  220 + offset += 4;
  221 +
  222 + Module.setValue(ptr + offset, config.keywordsBufLen, 'i32');
  223 + offset += 4;
  224 +
206 return { 225 return {
207 ptr: ptr, len: numBytes, featConfig: featConfig, modelConfig: modelConfig, 226 ptr: ptr, len: numBytes, featConfig: featConfig, modelConfig: modelConfig,
208 keywordsBuffer: keywordsBuffer 227 keywordsBuffer: keywordsBuffer
@@ -24,7 +24,7 @@ static_assert(sizeof(SherpaOnnxOnlineModelConfig) == @@ -24,7 +24,7 @@ static_assert(sizeof(SherpaOnnxOnlineModelConfig) ==
24 static_assert(sizeof(SherpaOnnxFeatureConfig) == 2 * 4, ""); 24 static_assert(sizeof(SherpaOnnxFeatureConfig) == 2 * 4, "");
25 static_assert(sizeof(SherpaOnnxKeywordSpotterConfig) == 25 static_assert(sizeof(SherpaOnnxKeywordSpotterConfig) ==
26 sizeof(SherpaOnnxFeatureConfig) + 26 sizeof(SherpaOnnxFeatureConfig) +
27 - sizeof(SherpaOnnxOnlineModelConfig) + 5 * 4, 27 + sizeof(SherpaOnnxOnlineModelConfig) + 7 * 4,
28 ""); 28 "");
29 29
30 void CopyHeap(const char *src, int32_t num_bytes, char *dst) { 30 void CopyHeap(const char *src, int32_t num_bytes, char *dst) {