Skip to content

Commit

Permalink
fix wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj committed Sep 13, 2024
1 parent f08f783 commit 67b3d63
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 30 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/c-api-from-buffer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches:
- master
- small-fixes
tags:
- 'v[0-9]+.[0-9]+.[0-9]+*'
paths:
Expand Down Expand Up @@ -107,16 +106,17 @@ jobs:
curl -SL -O https://huggingface.co/desh2608/icefall-asr-librispeech-pruned-transducer-stateless7-streaming-small/blob/main/data/lang_bpe_500/bpe.model
cp bpe.model sherpa-onnx-streaming-zipformer-en-20M-2023-02-17/
rm bpe.model
printf "▁A ▁T ▁P :1.5\n▁A ▁B ▁C :3.0" > hotwords.txt
mv hotwords.txt ./sherpa-onnx-streaming-zipformer-en-20M-2023-02-17
ls -lh sherpa-onnx-streaming-zipformer-en-20M-2023-02-17
echo "---"
ls -lh sherpa-onnx-streaming-zipformer-en-20M-2023-02-17/test_wavs
export LD_LIBRARY_PATH=$PWD/build/install/lib:$LD_LIBRARY_PATH
export DYLD_LIBRARY_PATH=$PWD/build/install/lib:$DYLD_LIBRARY_PATH
./streaming-zipformer-buffered-tokens-hotwords-c-api
rm -rf sherpa-onnx-streaming-zipformer-*
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

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

static size_t ReadFile(const char *filename, const char **buffer_out) {
FILE *file = fopen(filename, "rb");
FILE *file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr, "Failed to open %s\n", filename);
return -1;
Expand All @@ -39,7 +39,7 @@ static size_t ReadFile(const char *filename, const char **buffer_out) {
size_t read_bytes = fread(*buffer_out, 1, size, file);
if (read_bytes != size) {
printf("Errors occured in reading the file %s\n", filename);
free(*buffer_out);
free((void *)*buffer_out);
*buffer_out = NULL;
fclose(file);
return -1;
Expand Down Expand Up @@ -80,14 +80,14 @@ int32_t main() {
size_t token_buf_size = ReadFile(tokens_filename, &tokens_buf);
if (token_buf_size < 1) {
fprintf(stderr, "Please check your tokens.txt!\n");
free(tokens_buf);
free((void *)tokens_buf);
return -1;
}
const char *hotwords_buf;
size_t hotwords_buf_size = ReadFile(hotwords_filename, &hotwords_buf);
if (hotwords_buf_size < 1) {
fprintf(stderr, "Please check your hotwords.txt!\n");
free(hotwords_buf);
free((void *)hotwords_buf);
return -1;
}

Expand Down Expand Up @@ -119,9 +119,9 @@ int32_t main() {
SherpaOnnxOnlineRecognizer *recognizer =
SherpaOnnxCreateOnlineRecognizer(&recognizer_config);

free(tokens_buf);
free((void *)tokens_buf);
tokens_buf = NULL;
free(hotwords_buf);
free((void *)hotwords_buf);
hotwords_buf = NULL;

if (recognizer == NULL) {
Expand Down Expand Up @@ -199,4 +199,4 @@ int32_t main() {
fprintf(stderr, "\n");

return 0;
}
}
10 changes: 10 additions & 0 deletions flutter/sherpa_onnx/lib/src/sherpa_onnx_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ final class SherpaOnnxOnlineModelConfig extends Struct {
external Pointer<Utf8> modelingUnit;

external Pointer<Utf8> bpeVocab;

external Pointer<Utf8> tokensBuf;

@Int32()
external int tokensBufSize;
}

final class SherpaOnnxOnlineCtcFstDecoderConfig extends Struct {
Expand Down Expand Up @@ -275,6 +280,11 @@ final class SherpaOnnxOnlineRecognizerConfig extends Struct {

@Float()
external double blankPenalty;

external Pointer<Utf8> hotwordsBuf;

@Int32()
external int hotwordsBufSize;
}

final class SherpaOnnxSileroVadModelConfig extends Struct {
Expand Down
9 changes: 8 additions & 1 deletion scripts/dotnet/OnlineModelConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public OnlineModelConfig()
ModelType = "";
ModelingUnit = "cjkchar";
BpeVocab = "";
TokensBuf = "";
TokensBufSize = 0;
}

public OnlineTransducerModelConfig Transducer;
Expand All @@ -48,6 +50,11 @@ public OnlineModelConfig()

[MarshalAs(UnmanagedType.LPStr)]
public string BpeVocab;

[MarshalAs(UnmanagedType.LPStr)]
public string TokensBuf;

public int TokensBufSize;
}

}
}
7 changes: 7 additions & 0 deletions scripts/dotnet/OnlineRecognizerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public OnlineRecognizerConfig()
RuleFsts = "";
RuleFars = "";
BlankPenalty = 0.0F;
HotwordsBuf = "";
HotwordsBufSize = 0;
}
public FeatureConfig FeatConfig;
public OnlineModelConfig ModelConfig;
Expand Down Expand Up @@ -72,5 +74,10 @@ public OnlineRecognizerConfig()
public string RuleFars;

public float BlankPenalty;

[MarshalAs(UnmanagedType.LPStr)]
public string HotwordsBuf;

public int HotwordsBufSize;
}
}
14 changes: 14 additions & 0 deletions scripts/go/sherpa_onnx.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ type OnlineModelConfig struct {
ModelType string // Optional. You can specify it for faster model initialization
ModelingUnit string // Optional. cjkchar, bpe, cjkchar+bpe
BpeVocab string // Optional.
TokensBuf string // Optional.
TokensBufSize int // Optional.
}

// Configuration for the feature extractor
Expand Down Expand Up @@ -133,6 +135,8 @@ type OnlineRecognizerConfig struct {
CtcFstDecoderConfig OnlineCtcFstDecoderConfig
RuleFsts string
RuleFars string
HotwordsBuf string
HotwordsBufSize int
}

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

c.model_config.tokens_buf = C.CString(config.ModelConfig.TokensBuf)
defer C.free(unsafe.Pointer(c.model_config.tokens_buf))

c.model_config.tokens_buf_size = C.int(config.ModelConfig.TokensBufSize)

c.model_config.num_threads = C.int(config.ModelConfig.NumThreads)

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

c.hotwords_buf = C.CString(config.HotwordsBuf)
defer C.free(unsafe.Pointer(c.hotwords_buf))

c.hotwords_buf_size = C.int(config.HotwordsBufSize)

c.hotwords_score = C.float(config.HotwordsScore)
c.blank_penalty = C.float(config.BlankPenalty)

Expand Down
12 changes: 12 additions & 0 deletions scripts/node-addon-api/src/streaming-asr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ SherpaOnnxOnlineModelConfig GetOnlineModelConfig(Napi::Object obj) {
SHERPA_ONNX_ASSIGN_ATTR_STR(model_type, modelType);
SHERPA_ONNX_ASSIGN_ATTR_STR(modeling_unit, modelingUnit);
SHERPA_ONNX_ASSIGN_ATTR_STR(bpe_vocab, bpeVocab);
SHERPA_ONNX_ASSIGN_ATTR_STR(tokens_buf, tokensBuf);
SHERPA_ONNX_ASSIGN_ATTR_INT32(tokens_buf_size, tokensBufSize);

return c;
}
Expand Down Expand Up @@ -192,6 +194,8 @@ static Napi::External<SherpaOnnxOnlineRecognizer> CreateOnlineRecognizerWrapper(
SHERPA_ONNX_ASSIGN_ATTR_STR(rule_fsts, ruleFsts);
SHERPA_ONNX_ASSIGN_ATTR_STR(rule_fars, ruleFars);
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(blank_penalty, blankPenalty);
SHERPA_ONNX_ASSIGN_ATTR_STR(hotwords_buf, hotwordsBuf);
SHERPA_ONNX_ASSIGN_ATTR_INT32(hotwords_buf_size, hotwordsBufSize);

c.ctc_fst_decoder_config = GetCtcFstDecoderConfig(o);

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

if (c.model_config.tokens_buf) {
delete[] c.model_config.tokens_buf;
}

if (c.decoding_method) {
delete[] c.decoding_method;
}
Expand All @@ -257,6 +265,10 @@ static Napi::External<SherpaOnnxOnlineRecognizer> CreateOnlineRecognizerWrapper(
delete[] c.rule_fars;
}

if (c.hotwords_buf) {
delete[] c.hotwords_buf;
}

if (c.ctc_fst_decoder_config.graph) {
delete[] c.ctc_fst_decoder_config.graph;
}
Expand Down
8 changes: 8 additions & 0 deletions sherpa-onnx/pascal-api/sherpa_onnx.pas
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ TSherpaOnnxOnlineModelConfig = record
ModelType: AnsiString;
ModelingUnit: AnsiString;
BpeVocab: AnsiString;
TokensBuf: AnsiString;
TokensBufSize: Integer;
function ToString: AnsiString;
class operator Initialize({$IFDEF FPC}var{$ELSE}out{$ENDIF} Dest: TSherpaOnnxOnlineModelConfig);
end;
Expand Down Expand Up @@ -178,6 +180,8 @@ TSherpaOnnxOnlineRecognizerConfig = record
RuleFsts: AnsiString;
RuleFars: AnsiString;
BlankPenalty: Single;
HotwordsBuf: AnsiString;
HotwordsBufSize: Integer;
function ToString: AnsiString;
class operator Initialize({$IFDEF FPC}var{$ELSE}out{$ENDIF} Dest: TSherpaOnnxOnlineRecognizerConfig);
end;
Expand Down Expand Up @@ -490,6 +494,8 @@ SherpaOnnxOnlineModelConfig= record
ModelType: PAnsiChar;
ModelingUnit: PAnsiChar;
BpeVocab: PAnsiChar;
TokensBuf: PAnsiChar;
TokensBufSize: cint32;
end;
SherpaOnnxFeatureConfig = record
SampleRate: cint32;
Expand All @@ -514,6 +520,8 @@ SherpaOnnxOnlineRecognizerConfig = record
RuleFsts: PAnsiChar;
RuleFars: PAnsiChar;
BlankPenalty: cfloat;
HotwordsBuf: PAnsiChar;
HotwordsBufSize: cint32;
end;

PSherpaOnnxOnlineRecognizerConfig = ^SherpaOnnxOnlineRecognizerConfig;
Expand Down
16 changes: 12 additions & 4 deletions swift-api-examples/SherpaOnnx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ func sherpaOnnxOnlineModelConfig(
debug: Int = 0,
modelType: String = "",
modelingUnit: String = "cjkchar",
bpeVocab: String = ""
bpeVocab: String = "",
tokensBuf: String = "",
tokensBufSize: Int = 0
) -> SherpaOnnxOnlineModelConfig {
return SherpaOnnxOnlineModelConfig(
transducer: transducer,
Expand All @@ -102,7 +104,9 @@ func sherpaOnnxOnlineModelConfig(
debug: Int32(debug),
model_type: toCPointer(modelType),
modeling_unit: toCPointer(modelingUnit),
bpe_vocab: toCPointer(bpeVocab)
bpe_vocab: toCPointer(bpeVocab),
tokens_buf: toCPointer(tokensBuf),
tokens_buf_size: Int32(tokensBufSize)
)
}

Expand Down Expand Up @@ -138,7 +142,9 @@ func sherpaOnnxOnlineRecognizerConfig(
ctcFstDecoderConfig: SherpaOnnxOnlineCtcFstDecoderConfig = sherpaOnnxOnlineCtcFstDecoderConfig(),
ruleFsts: String = "",
ruleFars: String = "",
blankPenalty: Float = 0.0
blankPenalty: Float = 0.0,
hotwordsBuf: String = "",
hotwordsBufSize: Int = 0
) -> SherpaOnnxOnlineRecognizerConfig {
return SherpaOnnxOnlineRecognizerConfig(
feat_config: featConfig,
Expand All @@ -154,7 +160,9 @@ func sherpaOnnxOnlineRecognizerConfig(
ctc_fst_decoder_config: ctcFstDecoderConfig,
rule_fsts: toCPointer(ruleFsts),
rule_fars: toCPointer(ruleFars),
blank_penalty: blankPenalty
blank_penalty: blankPenalty,
hotwords_buf: toCPointer(hotwordsBuf),
hotwords_buf_size: Int32(hotwordsBufSize)
)
}

Expand Down
Loading

0 comments on commit 67b3d63

Please sign in to comment.