Add Pascal/Go/C#/Dart API for NeMo Canary ASR models (#2367)
Add support for the new NeMo Canary ASR model across multiple language bindings by introducing a Canary model configuration and setter method on the offline recognizer. - Define Canary model config in Pascal, Go, C#, Dart and update converter functions - Add SetConfig API for offline recognizer (Pascal, Go, C#, Dart) - Extend CI/workflows and example scripts to test non-streaming Canary decoding
This commit is contained in:
84
dart-api-examples/non-streaming-asr/bin/nemo-canary.dart
Normal file
84
dart-api-examples/non-streaming-asr/bin/nemo-canary.dart
Normal file
@@ -0,0 +1,84 @@
|
||||
// Copyright (c) 2025 Xiaomi Corporation
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:args/args.dart';
|
||||
import 'package:sherpa_onnx/sherpa_onnx.dart' as sherpa_onnx;
|
||||
|
||||
import './init.dart';
|
||||
|
||||
void main(List<String> arguments) async {
|
||||
await initSherpaOnnx();
|
||||
|
||||
final parser = ArgParser()
|
||||
..addOption('encoder', help: 'Path to the NeMo Canary encoder model')
|
||||
..addOption('decoder', help: 'Path to the NeMo Canary decoder model')
|
||||
..addOption('src-lang', help: 'Language of the input audio')
|
||||
..addOption('tgt-lang', help: 'Language of the recognition result')
|
||||
..addOption('tokens', help: 'Path to tokens.txt')
|
||||
..addOption('input-wav', help: 'Path to input.wav to transcribe');
|
||||
|
||||
final res = parser.parse(arguments);
|
||||
if (res['encoder'] == null ||
|
||||
res['decoder'] == null ||
|
||||
res['src-lang'] == null ||
|
||||
res['tgt-lang'] == null ||
|
||||
res['tokens'] == null ||
|
||||
res['input-wav'] == null) {
|
||||
print(parser.usage);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
final encoder = res['encoder'] as String;
|
||||
final decoder = res['decoder'] as String;
|
||||
final srcLang = res['src-lang'] as String;
|
||||
final tgtLang = res['tgt-lang'] as String;
|
||||
final tokens = res['tokens'] as String;
|
||||
final inputWav = res['input-wav'] as String;
|
||||
|
||||
final canary = sherpa_onnx.OfflineCanaryModelConfig(
|
||||
encoder: encoder, decoder: decoder, srcLang: srcLang, tgtLang: tgtLang);
|
||||
|
||||
final modelConfig = sherpa_onnx.OfflineModelConfig(
|
||||
canary: canary,
|
||||
tokens: tokens,
|
||||
debug: false,
|
||||
numThreads: 1,
|
||||
);
|
||||
var config = sherpa_onnx.OfflineRecognizerConfig(model: modelConfig);
|
||||
final recognizer = sherpa_onnx.OfflineRecognizer(config);
|
||||
|
||||
final waveData = sherpa_onnx.readWave(inputWav);
|
||||
final stream = recognizer.createStream();
|
||||
|
||||
stream.acceptWaveform(
|
||||
samples: waveData.samples, sampleRate: waveData.sampleRate);
|
||||
recognizer.decode(stream);
|
||||
|
||||
final result = recognizer.getResult(stream);
|
||||
print('Result in $tgtLang: ${result.text}');
|
||||
|
||||
stream.free();
|
||||
|
||||
// Example to change the target language to de
|
||||
if (tgtLang != 'en') {
|
||||
var json = config.toJson();
|
||||
|
||||
((json['model'] as Map<String, dynamic>)!['canary']
|
||||
as Map<String, dynamic>)!['tgtLang'] = 'en';
|
||||
|
||||
config = sherpa_onnx.OfflineRecognizerConfig.fromJson(json);
|
||||
recognizer.setConfig(config);
|
||||
|
||||
final stream = recognizer.createStream();
|
||||
|
||||
stream.acceptWaveform(
|
||||
samples: waveData.samples, sampleRate: waveData.sampleRate);
|
||||
recognizer.decode(stream);
|
||||
|
||||
final result = recognizer.getResult(stream);
|
||||
print('Result in English: ${result.text}');
|
||||
stream.free();
|
||||
}
|
||||
|
||||
recognizer.free();
|
||||
}
|
||||
33
dart-api-examples/non-streaming-asr/run-nemo-canary.sh
Executable file
33
dart-api-examples/non-streaming-asr/run-nemo-canary.sh
Executable file
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -ex
|
||||
|
||||
dart pub get
|
||||
|
||||
if [ ! -f sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8/encoder.int8.onnx ]; then
|
||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8.tar.bz2
|
||||
tar xvf sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8.tar.bz2
|
||||
rm sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8.tar.bz2
|
||||
fi
|
||||
|
||||
for tgt_lang in en de es fr; do
|
||||
dart run \
|
||||
./bin/nemo-canary.dart \
|
||||
--encoder ./sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8/encoder.int8.onnx \
|
||||
--decoder ./sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8/decoder.int8.onnx \
|
||||
--tokens ./sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8/tokens.txt \
|
||||
--src-lang en \
|
||||
--tgt-lang $tgt_lang \
|
||||
--input-wav ./sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8/test_wavs/en.wav
|
||||
done
|
||||
|
||||
for tgt_lang in en de; do
|
||||
dart run \
|
||||
./bin/nemo-canary.dart \
|
||||
--encoder ./sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8/encoder.int8.onnx \
|
||||
--decoder ./sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8/decoder.int8.onnx \
|
||||
--tokens ./sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8/tokens.txt \
|
||||
--src-lang de \
|
||||
--tgt-lang $tgt_lang \
|
||||
--input-wav ./sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8/test_wavs/de.wav
|
||||
done
|
||||
Reference in New Issue
Block a user