Skip to content

Commit

Permalink
Add Lazarus example for Moonshine models. (#1532)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Nov 12, 2024
1 parent 4fab3f2 commit a16c9af
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 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
16 changes: 8 additions & 8 deletions cmake/piper-phonemize.cmake
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
function(download_piper_phonemize)
include(FetchContent)

set(piper_phonemize_URL "https://github.com/csukuangfj/piper-phonemize/archive/38ee199dcc49c7b6de89f7ebfb32ed682763fa1b.zip")
set(piper_phonemize_URL2 "https://hf-mirror.com/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/piper-phonemize-38ee199dcc49c7b6de89f7ebfb32ed682763fa1b.zip")
set(piper_phonemize_HASH "SHA256=ab4d06ca76047e1585c63c482f39ffead5315785345055360703cc9382c5e74b")
set(piper_phonemize_URL "https://github.com/csukuangfj/piper-phonemize/archive/78a788e0b719013401572d70fef372e77bff8e43.zip")
set(piper_phonemize_URL2 "https://hf-mirror.com/csukuangfj/sherpa-onnx-cmake-deps/resolve/main/piper-phonemize-78a788e0b719013401572d70fef372e77bff8e43.zip")
set(piper_phonemize_HASH "SHA256=89641a46489a4898754643ce57bda9c9b54b4ca46485fdc02bf0dc84b866645d")

# If you don't have access to the Internet,
# please pre-download kaldi-decoder
set(possible_file_locations
$ENV{HOME}/Downloads/piper-phonemize-38ee199dcc49c7b6de89f7ebfb32ed682763fa1b.zip
${CMAKE_SOURCE_DIR}/piper-phonemize-38ee199dcc49c7b6de89f7ebfb32ed682763fa1b.zip
${CMAKE_BINARY_DIR}/piper-phonemize-38ee199dcc49c7b6de89f7ebfb32ed682763fa1b.zip
/tmp/piper-phonemize-38ee199dcc49c7b6de89f7ebfb32ed682763fa1b.zip
/star-fj/fangjun/download/github/piper-phonemize-38ee199dcc49c7b6de89f7ebfb32ed682763fa1b.zip
$ENV{HOME}/Downloads/piper-phonemize-78a788e0b719013401572d70fef372e77bff8e43.zip
${CMAKE_SOURCE_DIR}/piper-phonemize-78a788e0b719013401572d70fef372e77bff8e43.zip
${CMAKE_BINARY_DIR}/piper-phonemize-78a788e0b719013401572d70fef372e77bff8e43.zip
/tmp/piper-phonemize-78a788e0b719013401572d70fef372e77bff8e43.zip
/star-fj/fangjun/download/github/piper-phonemize-78a788e0b719013401572d70fef372e77bff8e43.zip
)

foreach(f IN LISTS possible_file_locations)
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 a16c9af

Please sign in to comment.