Skip to content

Commit

Permalink
Fix building (#1343)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj committed Sep 13, 2024
1 parent 65cfa75 commit 544857b
Show file tree
Hide file tree
Showing 17 changed files with 190 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: c-api-test-loading-tokens-hotwords-from-memory
name: c-api-from-memory

on:
push:
Expand All @@ -7,7 +7,7 @@ on:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+*'
paths:
- '.github/workflows/c-api.yaml'
- '.github/workflows/c-api-from-buffer.yaml'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-onnx/csrc/*'
Expand All @@ -18,7 +18,7 @@ on:
branches:
- master
paths:
- '.github/workflows/c-api.yaml'
- '.github/workflows/c-api-from-buffer.yaml'
- 'CMakeLists.txt'
- 'cmake/**'
- 'sherpa-onnx/csrc/*'
Expand All @@ -29,11 +29,11 @@ on:
workflow_dispatch:

concurrency:
group: c-api-${{ github.ref }}
group: c-api-from-buffer-${{ github.ref }}
cancel-in-progress: true

jobs:
c_api:
c_api_from_buffer:
name: ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
Expand Down Expand Up @@ -106,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
2 changes: 1 addition & 1 deletion sherpa-onnx/c-api/c-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ SHERPA_ONNX_API typedef struct SherpaOnnxOnlineModelConfig {
/// if non-null, loading the tokens from the buffered string directly in
/// prioriy
const char *tokens_buf;
/// byte size excluding the tailing '\0'
/// byte size excluding the trailing '\0'
int32_t tokens_buf_size;
} SherpaOnnxOnlineModelConfig;

Expand Down
4 changes: 3 additions & 1 deletion sherpa-onnx/csrc/offline-stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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

#include <math.h>

#include <algorithm>
#include <cassert>
#include <cmath>
Expand Down Expand Up @@ -245,7 +247,7 @@ class OfflineStream::Impl {
for (int32_t i = 0; i != n; ++i) {
float x = p[i];
x = (x > amin) ? x : amin;
x = std::log10f(x) * multiplier;
x = log10f(x) * multiplier;

max_x = (x > max_x) ? x : max_x;
p[i] = x;
Expand Down
6 changes: 4 additions & 2 deletions sherpa-onnx/csrc/online-recognizer-transducer-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,8 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl {
// segment is incremented only when the last
// result is not empty, contains non-blanks and longer than context_size)
const auto &r = s->GetResult();
if (!r.tokens.empty() && r.tokens.back() != 0 && r.tokens.size() > context_size) {
if (!r.tokens.empty() && r.tokens.back() != 0 &&
r.tokens.size() > context_size) {
s->GetCurrentSegment() += 1;
}
}
Expand All @@ -392,7 +393,8 @@ class OnlineRecognizerTransducerImpl : public OnlineRecognizerImpl {
// if last result is not empty, then
// preserve last tokens as the context for next result
if (static_cast<int32_t>(last_result.tokens.size()) > context_size) {
std::vector<int64_t> context(last_result.tokens.end() - context_size, last_result.tokens.end());
std::vector<int64_t> context(last_result.tokens.end() - context_size,
last_result.tokens.end());

Hypotheses context_hyp({{context, 0}});
r.hyps = std::move(context_hyp);
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
Loading

0 comments on commit 544857b

Please sign in to comment.