Skip to content

Commit 544857b

Browse files
authored
Fix building (#1343)
1 parent 65cfa75 commit 544857b

17 files changed

+190
-44
lines changed

.github/workflows/c-api-test-loading-tokens-hotwords-from-memory.yaml .github/workflows/c-api-from-buffer.yaml

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: c-api-test-loading-tokens-hotwords-from-memory
1+
name: c-api-from-memory
22

33
on:
44
push:
@@ -7,7 +7,7 @@ on:
77
tags:
88
- 'v[0-9]+.[0-9]+.[0-9]+*'
99
paths:
10-
- '.github/workflows/c-api.yaml'
10+
- '.github/workflows/c-api-from-buffer.yaml'
1111
- 'CMakeLists.txt'
1212
- 'cmake/**'
1313
- 'sherpa-onnx/csrc/*'
@@ -18,7 +18,7 @@ on:
1818
branches:
1919
- master
2020
paths:
21-
- '.github/workflows/c-api.yaml'
21+
- '.github/workflows/c-api-from-buffer.yaml'
2222
- 'CMakeLists.txt'
2323
- 'cmake/**'
2424
- 'sherpa-onnx/csrc/*'
@@ -29,11 +29,11 @@ on:
2929
workflow_dispatch:
3030

3131
concurrency:
32-
group: c-api-${{ github.ref }}
32+
group: c-api-from-buffer-${{ github.ref }}
3333
cancel-in-progress: true
3434

3535
jobs:
36-
c_api:
36+
c_api_from_buffer:
3737
name: ${{ matrix.os }}
3838
runs-on: ${{ matrix.os }}
3939
strategy:
@@ -106,16 +106,17 @@ jobs:
106106
curl -SL -O https://huggingface.co/desh2608/icefall-asr-librispeech-pruned-transducer-stateless7-streaming-small/blob/main/data/lang_bpe_500/bpe.model
107107
cp bpe.model sherpa-onnx-streaming-zipformer-en-20M-2023-02-17/
108108
rm bpe.model
109-
109+
110110
printf "▁A ▁T ▁P :1.5\n▁A ▁B ▁C :3.0" > hotwords.txt
111+
mv hotwords.txt ./sherpa-onnx-streaming-zipformer-en-20M-2023-02-17
111112
112113
ls -lh sherpa-onnx-streaming-zipformer-en-20M-2023-02-17
113114
echo "---"
114115
ls -lh sherpa-onnx-streaming-zipformer-en-20M-2023-02-17/test_wavs
115116
116117
export LD_LIBRARY_PATH=$PWD/build/install/lib:$LD_LIBRARY_PATH
117118
export DYLD_LIBRARY_PATH=$PWD/build/install/lib:$DYLD_LIBRARY_PATH
118-
119+
119120
./streaming-zipformer-buffered-tokens-hotwords-c-api
120-
121+
121122
rm -rf sherpa-onnx-streaming-zipformer-*

c-api-examples/streaming-zipformer-buffered-tokens-hotwords-c-api.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
//
77
// This file demonstrates how to use streaming Zipformer with sherpa-onnx's C
8-
// and with tokens and hotwords loaded from buffered strings instead of from external
9-
// files API.
8+
// and with tokens and hotwords loaded from buffered strings instead of from
9+
// external files API.
1010
// clang-format off
1111
//
1212
// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-en-20M-2023-02-17.tar.bz2
@@ -22,7 +22,7 @@
2222
#include "sherpa-onnx/c-api/c-api.h"
2323

2424
static size_t ReadFile(const char *filename, const char **buffer_out) {
25-
FILE *file = fopen(filename, "rb");
25+
FILE *file = fopen(filename, "r");
2626
if (file == NULL) {
2727
fprintf(stderr, "Failed to open %s\n", filename);
2828
return -1;
@@ -39,7 +39,7 @@ static size_t ReadFile(const char *filename, const char **buffer_out) {
3939
size_t read_bytes = fread(*buffer_out, 1, size, file);
4040
if (read_bytes != size) {
4141
printf("Errors occured in reading the file %s\n", filename);
42-
free(*buffer_out);
42+
free((void *)*buffer_out);
4343
*buffer_out = NULL;
4444
fclose(file);
4545
return -1;
@@ -80,14 +80,14 @@ int32_t main() {
8080
size_t token_buf_size = ReadFile(tokens_filename, &tokens_buf);
8181
if (token_buf_size < 1) {
8282
fprintf(stderr, "Please check your tokens.txt!\n");
83-
free(tokens_buf);
83+
free((void *)tokens_buf);
8484
return -1;
8585
}
8686
const char *hotwords_buf;
8787
size_t hotwords_buf_size = ReadFile(hotwords_filename, &hotwords_buf);
8888
if (hotwords_buf_size < 1) {
8989
fprintf(stderr, "Please check your hotwords.txt!\n");
90-
free(hotwords_buf);
90+
free((void *)hotwords_buf);
9191
return -1;
9292
}
9393

@@ -119,9 +119,9 @@ int32_t main() {
119119
SherpaOnnxOnlineRecognizer *recognizer =
120120
SherpaOnnxCreateOnlineRecognizer(&recognizer_config);
121121

122-
free(tokens_buf);
122+
free((void *)tokens_buf);
123123
tokens_buf = NULL;
124-
free(hotwords_buf);
124+
free((void *)hotwords_buf);
125125
hotwords_buf = NULL;
126126

127127
if (recognizer == NULL) {
@@ -199,4 +199,4 @@ int32_t main() {
199199
fprintf(stderr, "\n");
200200

201201
return 0;
202-
}
202+
}

flutter/sherpa_onnx/lib/src/sherpa_onnx_bindings.dart

+10
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ final class SherpaOnnxOnlineModelConfig extends Struct {
234234
external Pointer<Utf8> modelingUnit;
235235

236236
external Pointer<Utf8> bpeVocab;
237+
238+
external Pointer<Utf8> tokensBuf;
239+
240+
@Int32()
241+
external int tokensBufSize;
237242
}
238243

239244
final class SherpaOnnxOnlineCtcFstDecoderConfig extends Struct {
@@ -275,6 +280,11 @@ final class SherpaOnnxOnlineRecognizerConfig extends Struct {
275280

276281
@Float()
277282
external double blankPenalty;
283+
284+
external Pointer<Utf8> hotwordsBuf;
285+
286+
@Int32()
287+
external int hotwordsBufSize;
278288
}
279289

280290
final class SherpaOnnxSileroVadModelConfig extends Struct {

scripts/dotnet/OnlineModelConfig.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public OnlineModelConfig()
2222
ModelType = "";
2323
ModelingUnit = "cjkchar";
2424
BpeVocab = "";
25+
TokensBuf = "";
26+
TokensBufSize = 0;
2527
}
2628

2729
public OnlineTransducerModelConfig Transducer;
@@ -48,6 +50,11 @@ public OnlineModelConfig()
4850

4951
[MarshalAs(UnmanagedType.LPStr)]
5052
public string BpeVocab;
53+
54+
[MarshalAs(UnmanagedType.LPStr)]
55+
public string TokensBuf;
56+
57+
public int TokensBufSize;
5158
}
5259

53-
}
60+
}

scripts/dotnet/OnlineRecognizerConfig.cs

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public OnlineRecognizerConfig()
2626
RuleFsts = "";
2727
RuleFars = "";
2828
BlankPenalty = 0.0F;
29+
HotwordsBuf = "";
30+
HotwordsBufSize = 0;
2931
}
3032
public FeatureConfig FeatConfig;
3133
public OnlineModelConfig ModelConfig;
@@ -72,5 +74,10 @@ public OnlineRecognizerConfig()
7274
public string RuleFars;
7375

7476
public float BlankPenalty;
77+
78+
[MarshalAs(UnmanagedType.LPStr)]
79+
public string HotwordsBuf;
80+
81+
public int HotwordsBufSize;
7582
}
7683
}

scripts/go/sherpa_onnx.go

+14
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ type OnlineModelConfig struct {
8989
ModelType string // Optional. You can specify it for faster model initialization
9090
ModelingUnit string // Optional. cjkchar, bpe, cjkchar+bpe
9191
BpeVocab string // Optional.
92+
TokensBuf string // Optional.
93+
TokensBufSize int // Optional.
9294
}
9395

9496
// Configuration for the feature extractor
@@ -133,6 +135,8 @@ type OnlineRecognizerConfig struct {
133135
CtcFstDecoderConfig OnlineCtcFstDecoderConfig
134136
RuleFsts string
135137
RuleFars string
138+
HotwordsBuf string
139+
HotwordsBufSize int
136140
}
137141

138142
// It contains the recognition result for a online stream.
@@ -184,6 +188,11 @@ func NewOnlineRecognizer(config *OnlineRecognizerConfig) *OnlineRecognizer {
184188
c.model_config.tokens = C.CString(config.ModelConfig.Tokens)
185189
defer C.free(unsafe.Pointer(c.model_config.tokens))
186190

191+
c.model_config.tokens_buf = C.CString(config.ModelConfig.TokensBuf)
192+
defer C.free(unsafe.Pointer(c.model_config.tokens_buf))
193+
194+
c.model_config.tokens_buf_size = C.int(config.ModelConfig.TokensBufSize)
195+
187196
c.model_config.num_threads = C.int(config.ModelConfig.NumThreads)
188197

189198
c.model_config.provider = C.CString(config.ModelConfig.Provider)
@@ -212,6 +221,11 @@ func NewOnlineRecognizer(config *OnlineRecognizerConfig) *OnlineRecognizer {
212221
c.hotwords_file = C.CString(config.HotwordsFile)
213222
defer C.free(unsafe.Pointer(c.hotwords_file))
214223

224+
c.hotwords_buf = C.CString(config.HotwordsBuf)
225+
defer C.free(unsafe.Pointer(c.hotwords_buf))
226+
227+
c.hotwords_buf_size = C.int(config.HotwordsBufSize)
228+
215229
c.hotwords_score = C.float(config.HotwordsScore)
216230
c.blank_penalty = C.float(config.BlankPenalty)
217231

scripts/node-addon-api/src/streaming-asr.cc

+12
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ SherpaOnnxOnlineModelConfig GetOnlineModelConfig(Napi::Object obj) {
120120
SHERPA_ONNX_ASSIGN_ATTR_STR(model_type, modelType);
121121
SHERPA_ONNX_ASSIGN_ATTR_STR(modeling_unit, modelingUnit);
122122
SHERPA_ONNX_ASSIGN_ATTR_STR(bpe_vocab, bpeVocab);
123+
SHERPA_ONNX_ASSIGN_ATTR_STR(tokens_buf, tokensBuf);
124+
SHERPA_ONNX_ASSIGN_ATTR_INT32(tokens_buf_size, tokensBufSize);
123125

124126
return c;
125127
}
@@ -192,6 +194,8 @@ static Napi::External<SherpaOnnxOnlineRecognizer> CreateOnlineRecognizerWrapper(
192194
SHERPA_ONNX_ASSIGN_ATTR_STR(rule_fsts, ruleFsts);
193195
SHERPA_ONNX_ASSIGN_ATTR_STR(rule_fars, ruleFars);
194196
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(blank_penalty, blankPenalty);
197+
SHERPA_ONNX_ASSIGN_ATTR_STR(hotwords_buf, hotwordsBuf);
198+
SHERPA_ONNX_ASSIGN_ATTR_INT32(hotwords_buf_size, hotwordsBufSize);
195199

196200
c.ctc_fst_decoder_config = GetCtcFstDecoderConfig(o);
197201

@@ -241,6 +245,10 @@ static Napi::External<SherpaOnnxOnlineRecognizer> CreateOnlineRecognizerWrapper(
241245
delete[] c.model_config.bpe_vocab;
242246
}
243247

248+
if (c.model_config.tokens_buf) {
249+
delete[] c.model_config.tokens_buf;
250+
}
251+
244252
if (c.decoding_method) {
245253
delete[] c.decoding_method;
246254
}
@@ -257,6 +265,10 @@ static Napi::External<SherpaOnnxOnlineRecognizer> CreateOnlineRecognizerWrapper(
257265
delete[] c.rule_fars;
258266
}
259267

268+
if (c.hotwords_buf) {
269+
delete[] c.hotwords_buf;
270+
}
271+
260272
if (c.ctc_fst_decoder_config.graph) {
261273
delete[] c.ctc_fst_decoder_config.graph;
262274
}

sherpa-onnx/c-api/c-api.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ SHERPA_ONNX_API typedef struct SherpaOnnxOnlineModelConfig {
9191
/// if non-null, loading the tokens from the buffered string directly in
9292
/// prioriy
9393
const char *tokens_buf;
94-
/// byte size excluding the tailing '\0'
94+
/// byte size excluding the trailing '\0'
9595
int32_t tokens_buf_size;
9696
} SherpaOnnxOnlineModelConfig;
9797

sherpa-onnx/csrc/offline-stream.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "sherpa-onnx/csrc/offline-stream.h"
66

7+
#include <math.h>
8+
79
#include <algorithm>
810
#include <cassert>
911
#include <cmath>
@@ -245,7 +247,7 @@ class OfflineStream::Impl {
245247
for (int32_t i = 0; i != n; ++i) {
246248
float x = p[i];
247249
x = (x > amin) ? x : amin;
248-
x = std::log10f(x) * multiplier;
250+
x = log10f(x) * multiplier;
249251

250252
max_x = (x > max_x) ? x : max_x;
251253
p[i] = x;

sherpa-onnx/csrc/online-recognizer-transducer-impl.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,8 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl {
372372
// segment is incremented only when the last
373373
// result is not empty, contains non-blanks and longer than context_size)
374374
const auto &r = s->GetResult();
375-
if (!r.tokens.empty() && r.tokens.back() != 0 && r.tokens.size() > context_size) {
375+
if (!r.tokens.empty() && r.tokens.back() != 0 &&
376+
r.tokens.size() > context_size) {
376377
s->GetCurrentSegment() += 1;
377378
}
378379
}
@@ -392,7 +393,8 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl {
392393
// if last result is not empty, then
393394
// preserve last tokens as the context for next result
394395
if (static_cast<int32_t>(last_result.tokens.size()) > context_size) {
395-
std::vector<int64_t> context(last_result.tokens.end() - context_size, last_result.tokens.end());
396+
std::vector<int64_t> context(last_result.tokens.end() - context_size,
397+
last_result.tokens.end());
396398

397399
Hypotheses context_hyp({{context, 0}});
398400
r.hyps = std::move(context_hyp);

sherpa-onnx/pascal-api/sherpa_onnx.pas

+8
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ TSherpaOnnxOnlineModelConfig = record
145145
ModelType: AnsiString;
146146
ModelingUnit: AnsiString;
147147
BpeVocab: AnsiString;
148+
TokensBuf: AnsiString;
149+
TokensBufSize: Integer;
148150
function ToString: AnsiString;
149151
class operator Initialize({$IFDEF FPC}var{$ELSE}out{$ENDIF} Dest: TSherpaOnnxOnlineModelConfig);
150152
end;
@@ -178,6 +180,8 @@ TSherpaOnnxOnlineRecognizerConfig = record
178180
RuleFsts: AnsiString;
179181
RuleFars: AnsiString;
180182
BlankPenalty: Single;
183+
HotwordsBuf: AnsiString;
184+
HotwordsBufSize: Integer;
181185
function ToString: AnsiString;
182186
class operator Initialize({$IFDEF FPC}var{$ELSE}out{$ENDIF} Dest: TSherpaOnnxOnlineRecognizerConfig);
183187
end;
@@ -490,6 +494,8 @@ SherpaOnnxOnlineModelConfig= record
490494
ModelType: PAnsiChar;
491495
ModelingUnit: PAnsiChar;
492496
BpeVocab: PAnsiChar;
497+
TokensBuf: PAnsiChar;
498+
TokensBufSize: cint32;
493499
end;
494500
SherpaOnnxFeatureConfig = record
495501
SampleRate: cint32;
@@ -514,6 +520,8 @@ SherpaOnnxOnlineRecognizerConfig = record
514520
RuleFsts: PAnsiChar;
515521
RuleFars: PAnsiChar;
516522
BlankPenalty: cfloat;
523+
HotwordsBuf: PAnsiChar;
524+
HotwordsBufSize: cint32;
517525
end;
518526

519527
PSherpaOnnxOnlineRecognizerConfig = ^SherpaOnnxOnlineRecognizerConfig;

0 commit comments

Comments
 (0)