Add Dart API for homophone replacer (#2167)

This commit is contained in:
Fangjun Kuang
2025-04-30 23:15:28 +08:00
committed by GitHub
parent e537094b07
commit 85df96d528
8 changed files with 226 additions and 53 deletions

View File

@@ -0,0 +1,78 @@
// 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('model', help: 'Path to the SenseVoice model')
..addOption('tokens', help: 'Path to tokens.txt')
..addOption('language',
help: 'auto, zh, en, ja, ko, yue, or leave it empty to use auto',
defaultsTo: '')
..addOption('use-itn',
help: 'true to use inverse text normalization', defaultsTo: 'false')
..addOption('input-wav', help: 'Path to input.wav to transcribe')
..addOption('hr-dict-dir',
help: 'Path to jieba dict for homophone replacer')
..addOption('hr-lexicon',
help: 'Path to lexicon.txt for homophone replacer')
..addOption('hr-rule-fsts',
help: 'Path to replace.fst for homophone replacer');
final res = parser.parse(arguments);
if (res['model'] == null ||
res['tokens'] == null ||
res['hr-dict-dir'] == null ||
res['hr-lexicon'] == null ||
res['hr-rule-fsts'] == null ||
res['input-wav'] == null) {
print(parser.usage);
exit(1);
}
final model = res['model'] as String;
final tokens = res['tokens'] as String;
final inputWav = res['input-wav'] as String;
final language = res['language'] as String;
final useItn = (res['use-itn'] as String).toLowerCase() == 'true';
final hrDictDir = res['hr-dict-dir'] as String;
final hrLexicon = res['hr-lexicon'] as String;
final hrRuleFsts = res['hr-rule-fsts'] as String;
final senseVoice = sherpa_onnx.OfflineSenseVoiceModelConfig(
model: model, language: language, useInverseTextNormalization: useItn);
final modelConfig = sherpa_onnx.OfflineModelConfig(
senseVoice: senseVoice,
tokens: tokens,
debug: true,
numThreads: 1,
);
final hr = sherpa_onnx.HomophoneReplacerConfig(
dictDir: hrDictDir, lexicon: hrLexicon, ruleFsts: hrRuleFsts);
final config =
sherpa_onnx.OfflineRecognizerConfig(model: modelConfig, hr: hr);
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.text);
stream.free();
recognizer.free();
}

View File

@@ -0,0 +1,31 @@
#!/usr/bin/env bash
set -ex
dart pub get
if [ ! -f ./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/tokens.txt ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17.tar.bz2
tar xvf sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17.tar.bz2
rm sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17.tar.bz2
fi
if [ ! -d dict ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/hr-files/dict.tar.bz2
tar xf dict.tar.bz2
rm dict.tar.bz2
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/hr-files/replace.fst
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/hr-files/test-hr.wav
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/hr-files/lexicon.txt
fi
dart run \
./bin/sense-voice-with-hr.dart \
--model ./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/model.int8.onnx \
--tokens ./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/tokens.txt \
--use-itn true \
--hr-dict-dir ./dict \
--hr-lexicon ./lexicon.txt \
--hr-rule-fsts ./replace.fst \
--input-wav ./test-hr.wav