Skip to content

Commit

Permalink
Add Lazarus example for Moonshine models.
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj committed Nov 12, 2024
1 parent f97daed commit 659ea7f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build-ios-shared.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ fi

if [ ! -z CMAKE_VERBOSE_MAKEFILE ]; then
CMAKE_VERBOSE_MAKEFILE=ON
else
CMAKE_VERBOSE_MAKEFILE=OFF
fi

if [ ! -f $onnxruntime_dir/onnxruntime.xcframework/ios-arm64/onnxruntime.a ]; then
Expand Down
54 changes: 54 additions & 0 deletions lazarus-examples/generate_subtitles/my_init.pas
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,30 @@ function CreateOfflineRecognizerWhisper(
Result := TSherpaOnnxOfflineRecognizer.Create(Config);
end;

function CreateOfflineRecognizerMoonshine(
Tokens: AnsiString;
Preprocessor: AnsiString;
Encoder: AnsiString;
UncachedDecoder: AnsiString;
CachedDecoder: AnsiString): TSherpaOnnxOfflineRecognizer;
var
Config: TSherpaOnnxOfflineRecognizerConfig;
begin
Initialize(Config);

Config.ModelConfig.Moonshine.Preprocessor := Preprocessor;
Config.ModelConfig.Moonshine.Encoder := Encoder;
Config.ModelConfig.Moonshine.UncachedDecoder := UncachedDecoder;
Config.ModelConfig.Moonshine.CachedDecoder := CachedDecoder;

Config.ModelConfig.Tokens := Tokens;
Config.ModelConfig.Provider := 'cpu';
Config.ModelConfig.NumThreads := 2;
Config.ModelConfig.Debug := False;

Result := TSherpaOnnxOfflineRecognizer.Create(Config);
end;

constructor TMyInitThread.Create(CreateSuspended : boolean; ModelDirectory: AnsiString);
begin
inherited Create(CreateSuspended);
Expand Down Expand Up @@ -193,6 +217,11 @@ procedure TMyInitThread.Execute;
NeMoTransducerEncoder: AnsiString;
NeMoTransducerDecoder: AnsiString;
NeMoTransducerJoiner: AnsiString;

MoonshinePreprocessor: AnsiString;
MoonshineEncoder: AnsiString;
MoonshineUncachedDecoder: AnsiString;
MoonshineCachedDecoder: AnsiString;
begin
VadFilename := ModelDir + 'silero_vad.onnx';
Tokens := ModelDir + 'tokens.txt';
Expand Down Expand Up @@ -292,6 +321,24 @@ procedure TMyInitThread.Execute;
NeMoTransducerDecoder := ModelDir + 'nemo-transducer-decoder.onnx';
NeMoTransducerJoiner := ModelDir + 'nemo-transducer-joiner.onnx';

{
Please Visit
https://k2-fsa.github.io/sherpa/onnx/moonshine/models.html
to download a Moonshine model.
Note that you have to rename model files after downloading. The following
is an example.
mv preprocess.onnx moonshine-preprocessor.onnx
mv encode.int8.onnx moonshine-encoder.onnx
mv uncached_decode.int8.onnx moonshine-uncached-decoder.onnx
mv cached_decode.int8.onnx moonshine-cached-decoder.onnx
}
MoonshinePreprocessor := ModelDir + 'moonshine-preprocessor.onnx';
MoonshineEncoder := ModelDir + 'moonshine-encoder.onnx';
MoonshineUncachedDecoder := ModelDir + 'moonshine-uncached-decoder.onnx';
MoonshineCachedDecoder := ModelDir + 'moonshine-cached-decoder.onnx';

if not FileExists(VadFilename) then
begin
Status := VadFilename + ' does not exist! Please download it from' +
Expand Down Expand Up @@ -344,6 +391,13 @@ procedure TMyInitThread.Execute;
NeMoTransducerEncoder, NeMoTransducerDecoder, NeMoTransducerJoiner, 'nemo_transducer');
Msg := 'NeMo transducer';
end
else if FileExists(MoonshinePreprocessor) and FileExists(MoonshineEncoder) and FileExists(MoonshineUncachedDecoder) and FileExists(MoonshineCachedDecoder) then
begin
Form1.OfflineRecognizer := CreateOfflineRecognizerMoonshine(Tokens,
MoonshinePreprocessor, MoonshineEncoder, MoonshineUncachedDecoder,
MoonshineCachedDecoder);
Msg := 'Moonshine';
end
else
begin
Status := 'Please download at least one non-streaming speech recognition model first.';
Expand Down
14 changes: 14 additions & 0 deletions scripts/lazarus/generate-subtitles.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ def get_models():
popd
""",
),
Model(
model_name="sherpa-onnx-moonshine-tiny-en-int8",
lang="en",
short_name="moonshine_tiny",
cmd="""
pushd $model_name
mv -v preprocess.onnx moonshine-preprocessor.onnx
mv -v encode.int8.onnx moonshine-encoder.onnx
mv -v uncached_decode.int8.onnx moonshine-uncached-decoder.onnx
mv -v cached_decode.int8.onnx moonshine-cached-decoder.onnx
popd
""",
),
Model(
model_name="sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17",
lang="zh_en_ko_ja_yue",
Expand Down

0 comments on commit 659ea7f

Please sign in to comment.