Add C++ runtime and Python API for NeMo Canary models (#2352)

This commit is contained in:
Fangjun Kuang
2025-07-07 17:03:49 +08:00
committed by GitHub
parent f8d957a24b
commit 0e738c356c
24 changed files with 1091 additions and 8 deletions

View File

@@ -9,6 +9,7 @@ set(srcs
features.cc
homophone-replacer.cc
keyword-spotter.cc
offline-canary-model-config.cc
offline-ctc-fst-decoder-config.cc
offline-dolphin-model-config.cc
offline-fire-red-asr-model-config.cc

View File

@@ -0,0 +1,30 @@
// sherpa-onnx/python/csrc/offline-canary-model-config.cc
//
// Copyright (c) 2025 Xiaomi Corporation
#include "sherpa-onnx/csrc/offline-canary-model-config.h"
#include <string>
#include <vector>
#include "sherpa-onnx/python/csrc/offline-canary-model-config.h"
namespace sherpa_onnx {
void PybindOfflineCanaryModelConfig(py::module *m) {
using PyClass = OfflineCanaryModelConfig;
py::class_<PyClass>(*m, "OfflineCanaryModelConfig")
.def(py::init<const std::string &, const std::string &,
const std::string &, const std::string &, bool>(),
py::arg("encoder") = "", py::arg("decoder") = "",
py::arg("src_lang") = "", py::arg("tgt_lang") = "",
py::arg("use_pnc") = true)
.def_readwrite("encoder", &PyClass::encoder)
.def_readwrite("decoder", &PyClass::decoder)
.def_readwrite("src_lang", &PyClass::src_lang)
.def_readwrite("tgt_lang", &PyClass::tgt_lang)
.def_readwrite("use_pnc", &PyClass::use_pnc)
.def("__str__", &PyClass::ToString);
}
} // namespace sherpa_onnx

View File

@@ -0,0 +1,16 @@
// sherpa-onnx/python/csrc/offline-canary-model-config.h
//
// Copyright (c) 2025 Xiaomi Corporation
#ifndef SHERPA_ONNX_PYTHON_CSRC_OFFLINE_CANARY_MODEL_CONFIG_H_
#define SHERPA_ONNX_PYTHON_CSRC_OFFLINE_CANARY_MODEL_CONFIG_H_
#include "sherpa-onnx/python/csrc/sherpa-onnx.h"
namespace sherpa_onnx {
void PybindOfflineCanaryModelConfig(py::module *m);
}
#endif // SHERPA_ONNX_PYTHON_CSRC_OFFLINE_CANARY_MODEL_CONFIG_H_

View File

@@ -8,6 +8,7 @@
#include <vector>
#include "sherpa-onnx/csrc/offline-model-config.h"
#include "sherpa-onnx/python/csrc/offline-canary-model-config.h"
#include "sherpa-onnx/python/csrc/offline-dolphin-model-config.h"
#include "sherpa-onnx/python/csrc/offline-fire-red-asr-model-config.h"
#include "sherpa-onnx/python/csrc/offline-moonshine-model-config.h"
@@ -34,6 +35,7 @@ void PybindOfflineModelConfig(py::module *m) {
PybindOfflineSenseVoiceModelConfig(m);
PybindOfflineMoonshineModelConfig(m);
PybindOfflineDolphinModelConfig(m);
PybindOfflineCanaryModelConfig(m);
using PyClass = OfflineModelConfig;
py::class_<PyClass>(*m, "OfflineModelConfig")
@@ -47,7 +49,8 @@ void PybindOfflineModelConfig(py::module *m) {
const OfflineWenetCtcModelConfig &,
const OfflineSenseVoiceModelConfig &,
const OfflineMoonshineModelConfig &,
const OfflineDolphinModelConfig &, const std::string &,
const OfflineDolphinModelConfig &,
const OfflineCanaryModelConfig &, const std::string &,
const std::string &, int32_t, bool, const std::string &,
const std::string &, const std::string &,
const std::string &>(),
@@ -62,8 +65,9 @@ void PybindOfflineModelConfig(py::module *m) {
py::arg("sense_voice") = OfflineSenseVoiceModelConfig(),
py::arg("moonshine") = OfflineMoonshineModelConfig(),
py::arg("dolphin") = OfflineDolphinModelConfig(),
py::arg("telespeech_ctc") = "", py::arg("tokens"),
py::arg("num_threads"), py::arg("debug") = false,
py::arg("canary") = OfflineCanaryModelConfig(),
py::arg("telespeech_ctc") = "", py::arg("tokens") = "",
py::arg("num_threads") = 1, py::arg("debug") = false,
py::arg("provider") = "cpu", py::arg("model_type") = "",
py::arg("modeling_unit") = "cjkchar", py::arg("bpe_vocab") = "")
.def_readwrite("transducer", &PyClass::transducer)
@@ -77,6 +81,7 @@ void PybindOfflineModelConfig(py::module *m) {
.def_readwrite("sense_voice", &PyClass::sense_voice)
.def_readwrite("moonshine", &PyClass::moonshine)
.def_readwrite("dolphin", &PyClass::dolphin)
.def_readwrite("canary", &PyClass::canary)
.def_readwrite("telespeech_ctc", &PyClass::telespeech_ctc)
.def_readwrite("tokens", &PyClass::tokens)
.def_readwrite("num_threads", &PyClass::num_threads)

View File

@@ -19,7 +19,8 @@ static void PybindOfflineRecognizerConfig(py::module *m) {
const std::string &, int32_t, const std::string &, float,
float, const std::string &, const std::string &,
const HomophoneReplacerConfig &>(),
py::arg("feat_config"), py::arg("model_config"),
py::arg("feat_config") = FeatureExtractorConfig(),
py::arg("model_config") = OfflineModelConfig(),
py::arg("lm_config") = OfflineLMConfig(),
py::arg("ctc_fst_decoder_config") = OfflineCtcFstDecoderConfig(),
py::arg("decoding_method") = "greedy_search",
@@ -61,6 +62,8 @@ void PybindOfflineRecognizer(py::module *m) {
py::arg("hotwords"), py::call_guard<py::gil_scoped_release>())
.def("decode_stream", &PyClass::DecodeStream, py::arg("s"),
py::call_guard<py::gil_scoped_release>())
.def("set_config", &PyClass::SetConfig, py::arg("config"),
py::call_guard<py::gil_scoped_release>())
.def(
"decode_streams",
[](const PyClass &self, std::vector<OfflineStream *> ss) {