Add Pascal API for Google MedASR model (#2950)

This commit is contained in:
Fangjun Kuang 2025-12-29 12:45:09 +08:00 committed by GitHub
parent 29c920d3d4
commit 654b9fd502
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 143 additions and 2 deletions

View File

@ -139,6 +139,10 @@ jobs:
rm -rf sherpa-onnx-*
echo "---"
./run-medasr-ctc.sh
rm -rf sherpa-onnx-*
echo "---"
./run-omnilingual-asr-ctc.sh
rm -rf sherpa-onnx-*
echo "---"

View File

@ -13,3 +13,4 @@ zipformer_ctc
wenet_ctc
nemo_canary
omnilingual_asr_ctc
medasr_ctc

View File

@ -0,0 +1,76 @@
{ Copyright (c) 2025 Xiaomi Corporation }
{
This file shows how to use a non-streaming Google MedASR CTC model
to decode files.
You can download the model files from
https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models
}
program medasr_ctc;
{$mode objfpc}
uses
sherpa_onnx,
DateUtils,
SysUtils;
var
Wave: TSherpaOnnxWave;
WaveFilename: AnsiString;
Config: TSherpaOnnxOfflineRecognizerConfig;
Recognizer: TSherpaOnnxOfflineRecognizer;
Stream: TSherpaOnnxOfflineStream;
RecognitionResult: TSherpaOnnxOfflineRecognizerResult;
Start: TDateTime;
Stop: TDateTime;
Elapsed: Single;
Duration: Single;
RealTimeFactor: Single;
begin
Initialize(Config);
Config.ModelConfig.MedAsr.Model := './sherpa-onnx-medasr-ctc-en-int8-2025-12-25/model.int8.onnx';
Config.ModelConfig.Tokens := './sherpa-onnx-medasr-ctc-en-int8-2025-12-25/tokens.txt';
Config.ModelConfig.Provider := 'cpu';
Config.ModelConfig.NumThreads := 1;
Config.ModelConfig.Debug := True;
WaveFilename := './sherpa-onnx-medasr-ctc-en-int8-2025-12-25/test_wavs/0.wav';
Wave := SherpaOnnxReadWave(WaveFilename);
Recognizer := TSherpaOnnxOfflineRecognizer.Create(Config);
Stream := Recognizer.CreateStream();
Start := Now;
Stream.AcceptWaveform(Wave.Samples, Wave.SampleRate);
Recognizer.Decode(Stream);
RecognitionResult := Recognizer.GetResult(Stream);
Stop := Now;
Elapsed := MilliSecondsBetween(Stop, Start) / 1000;
Duration := Length(Wave.Samples) / Wave.SampleRate;
RealTimeFactor := Elapsed / Duration;
WriteLn(RecognitionResult.ToString);
WriteLn(Format('NumThreads %d', [Config.ModelConfig.NumThreads]));
WriteLn(Format('Elapsed %.3f s', [Elapsed]));
WriteLn(Format('Wave duration %.3f s', [Duration]));
WriteLn(Format('RTF = %.3f/%.3f = %.3f', [Elapsed, Duration, RealTimeFactor]));
{Free resources to avoid memory leak.
Note: You don't need to invoke them for this simple script.
However, you have to invoke them in your own large/complex project.
}
FreeAndNil(Stream);
FreeAndNil(Recognizer);
end.

View File

@ -0,0 +1,42 @@
#!/usr/bin/env bash
set -ex
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SHERPA_ONNX_DIR=$(cd $SCRIPT_DIR/../.. && pwd)
echo "SHERPA_ONNX_DIR: $SHERPA_ONNX_DIR"
if [[ ! -f ../../build/install/lib/libsherpa-onnx-c-api.dylib && ! -f ../../build/install/lib/libsherpa-onnx-c-api.so && ! -f ../../build/install/lib/sherpa-onnx-c-api.dll ]]; then
mkdir -p ../../build
pushd ../../build
cmake \
-DCMAKE_INSTALL_PREFIX=./install \
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
-DBUILD_SHARED_LIBS=ON \
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
..
cmake --build . --target install --config Release
ls -lh lib
popd
fi
if [ ! -f ./sherpa-onnx-medasr-ctc-en-int8-2025-12-25/tokens.txt ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-medasr-ctc-en-int8-2025-12-25.tar.bz2
tar xvf sherpa-onnx-medasr-ctc-en-int8-2025-12-25.tar.bz2
rm sherpa-onnx-medasr-ctc-en-int8-2025-12-25.tar.bz2
fi
fpc \
-dSHERPA_ONNX_USE_SHARED_LIBS \
-Fu$SHERPA_ONNX_DIR/sherpa-onnx/pascal-api \
-Fl$SHERPA_ONNX_DIR/build/install/lib \
./medasr_ctc.pas
export LD_LIBRARY_PATH=$SHERPA_ONNX_DIR/build/install/lib:$LD_LIBRARY_PATH
export DYLD_LIBRARY_PATH=$SHERPA_ONNX_DIR/build/install/lib:$DYLD_LIBRARY_PATH
./medasr_ctc

View File

@ -340,6 +340,11 @@ type
function ToString: AnsiString;
end;
TSherpaOnnxOfflineMedAsrCtcModelConfig = record
Model: AnsiString;
function ToString: AnsiString;
end;
TSherpaOnnxOfflineWhisperModelConfig = record
Encoder: AnsiString;
Decoder: AnsiString;
@ -416,6 +421,7 @@ type
Canary: TSherpaOnnxOfflineCanaryModelConfig;
WenetCtc: TSherpaOnnxOfflineWenetCtcModelConfig;
Omnilingual: TSherpaOnnxOfflineOmnilingualAsrCtcModelConfig;
MedAsr: TSherpaOnnxOfflineMedAsrCtcModelConfig;
class operator Initialize({$IFDEF FPC}var{$ELSE}out{$ENDIF} Dest: TSherpaOnnxOfflineModelConfig);
function ToString: AnsiString;
end;
@ -828,6 +834,9 @@ type
SherpaOnnxOfflineOmnilingualAsrCtcModelConfig = record
Model: PAnsiChar;
end;
SherpaOnnxOfflineMedAsrCtcModelConfig = record
Model: PAnsiChar;
end;
SherpaOnnxOfflineWhisperModelConfig = record
Encoder: PAnsiChar;
Decoder: PAnsiChar;
@ -886,6 +895,7 @@ type
Canary: SherpaOnnxOfflineCanaryModelConfig;
WenetCtc: SherpaOnnxOfflineWenetCtcModelConfig;
Omnilingual: SherpaOnnxOfflineOmnilingualAsrCtcModelConfig;
MedAsr: SherpaOnnxOfflineMedAsrCtcModelConfig;
end;
SherpaOnnxOfflineRecognizerConfig = record
@ -1719,6 +1729,12 @@ begin
[Self.Model]);
end;
function TSherpaOnnxOfflineMedAsrCtcModelConfig.ToString: AnsiString;
begin
Result := Format('TSherpaOnnxOfflineMedAsrCtcModelConfig(Model := %s)',
[Self.Model]);
end;
function TSherpaOnnxOfflineWhisperModelConfig.ToString: AnsiString;
begin
Result := Format('TSherpaOnnxOfflineWhisperModelConfig(' +
@ -1810,7 +1826,8 @@ begin
'ZipformerCtc := %s, ' +
'Canary := %s, ' +
'WenetCtc := %s, ' +
'Omnilingual := %s' +
'Omnilingual := %s, ' +
'MedAsr := %s' +
')',
[Self.Transducer.ToString, Self.Paraformer.ToString,
Self.NeMoCtc.ToString, Self.Whisper.ToString, Self.Tdnn.ToString,
@ -1819,7 +1836,7 @@ begin
Self.TeleSpeechCtc, Self.SenseVoice.ToString, Self.Moonshine.ToString,
Self.FireRedAsr.ToString, Self.Dolphin.ToString,
Self.ZipformerCtc.ToString, Self.Canary.ToString, Self.WenetCtc.ToString,
Self.Omnilingual.ToString
Self.Omnilingual.ToString, Self.MedAsr.ToString
]);
end;
@ -1900,6 +1917,7 @@ begin
C.ModelConfig.WenetCtc.Model := PAnsiChar(Config.ModelConfig.WenetCtc.Model);
C.ModelConfig.Omnilingual.Model := PAnsiChar(Config.ModelConfig.Omnilingual.Model);
C.ModelConfig.MedAsr.Model := PAnsiChar(Config.ModelConfig.MedAsr.Model);
C.LMConfig.Model := PAnsiChar(Config.LMConfig.Model);
C.LMConfig.Scale := Config.LMConfig.Scale;