From c052ba786b3858f2dbed3e28314740a422e979ab Mon Sep 17 00:00:00 2001 From: Aruxxxi Date: Tue, 19 Aug 2025 07:16:33 +0000 Subject: [PATCH 1/6] feat: add punctuation C++ API --- sherpa-onnx/c-api/cxx-api.cc | 29 +++++++++++++++++++++++++++++ sherpa-onnx/c-api/cxx-api.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/sherpa-onnx/c-api/cxx-api.cc b/sherpa-onnx/c-api/cxx-api.cc index f73433ba02..58e603e90b 100644 --- a/sherpa-onnx/c-api/cxx-api.cc +++ b/sherpa-onnx/c-api/cxx-api.cc @@ -821,4 +821,33 @@ bool FileExists(const std::string &filename) { return SherpaOnnxFileExists(filename.c_str()); } +// ============================================================ +// For Offline Punctuation +// ============================================================ +OfflinePunctuation OfflinePunctuation::Create(const OfflinePunctuationConfig &config) { + struct SherpaOnnxOfflinePunctuationConfig c; + memset(&c, 0, sizeof(c)); + c.model.ct_transformer = config.model.ct_transformer.c_str(); + c.model.num_threads = config.model.num_threads; + c.model.debug = config.model.debug; + c.model.provider = config.model.provider.c_str(); + + const SherpaOnnxOfflinePunctuation *punct = SherpaOnnxCreateOfflinePunctuation(&c); + return OfflinePunctuation(punct); +} + +OfflinePunctuation::OfflinePunctuation(const SherpaOnnxOfflinePunctuation *p) + : MoveOnly(p) {} + +void OfflinePunctuation::Destroy(const SherpaOnnxOfflinePunctuation *p) const { + SherpaOnnxDestroyOfflinePunctuation(p_); +} + +std::string OfflinePunctuation::AddPunctuation(const std::string &text) const { + const char *result = SherpaOfflinePunctuationAddPunct(p_, text.c_str()); + std::string ans(result); + SherpaOfflinePunctuationFreeText(result); + return ans; +} + } // namespace sherpa_onnx::cxx diff --git a/sherpa-onnx/c-api/cxx-api.h b/sherpa-onnx/c-api/cxx-api.h index 2fc2c89f88..5b328bbe1d 100644 --- a/sherpa-onnx/c-api/cxx-api.h +++ b/sherpa-onnx/c-api/cxx-api.h @@ -673,6 +673,34 @@ SHERPA_ONNX_API std::string GetGitSha1(); SHERPA_ONNX_API std::string GetGitDate(); SHERPA_ONNX_API bool FileExists(const std::string &filename); +// ============================================================================ +// Offline Punctuation +// ============================================================================ +struct OfflinePunctuationModelConfig { + std::string ct_transformer; + int32_t num_threads = 1; + bool debug = false; + std::string provider = "cpu"; +}; + +struct OfflinePunctuationConfig { + OfflinePunctuationModelConfig model; +}; + +class SHERPA_ONNX_API OfflinePunctuation + : public MoveOnly { + public: + static OfflinePunctuation Create(const OfflinePunctuationConfig &config); + + void Destroy(const SherpaOnnxOfflinePunctuation *p) const; + + // Add punctuations to the input text and return it. + std::string AddPunctuation(const std::string &text) const; + + private: + explicit OfflinePunctuation(const SherpaOnnxOfflinePunctuation *p); +}; + } // namespace sherpa_onnx::cxx #endif // SHERPA_ONNX_C_API_CXX_API_H_ From 4134ab2261b4e89520a9225d19a7faa14c6d5dc7 Mon Sep 17 00:00:00 2001 From: Aruxxxi Date: Tue, 19 Aug 2025 08:00:21 +0000 Subject: [PATCH 2/6] fix: use parameter p instead of p_ in Destroy method --- sherpa-onnx/c-api/cxx-api.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sherpa-onnx/c-api/cxx-api.cc b/sherpa-onnx/c-api/cxx-api.cc index 58e603e90b..bab0144b98 100644 --- a/sherpa-onnx/c-api/cxx-api.cc +++ b/sherpa-onnx/c-api/cxx-api.cc @@ -840,7 +840,7 @@ OfflinePunctuation::OfflinePunctuation(const SherpaOnnxOfflinePunctuation *p) : MoveOnly(p) {} void OfflinePunctuation::Destroy(const SherpaOnnxOfflinePunctuation *p) const { - SherpaOnnxDestroyOfflinePunctuation(p_); + SherpaOnnxDestroyOfflinePunctuation(p); } std::string OfflinePunctuation::AddPunctuation(const std::string &text) const { From 0de319278e1c6e45c53f8fe837436c02081792d4 Mon Sep 17 00:00:00 2001 From: Aruxxxi Date: Tue, 19 Aug 2025 08:20:34 +0000 Subject: [PATCH 3/6] how to use punctuation cpp api --- cxx-api-examples/punctuation-cxx-api.cc | 50 +++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 cxx-api-examples/punctuation-cxx-api.cc diff --git a/cxx-api-examples/punctuation-cxx-api.cc b/cxx-api-examples/punctuation-cxx-api.cc new file mode 100644 index 0000000000..c377cc4f73 --- /dev/null +++ b/cxx-api-examples/punctuation-cxx-api.cc @@ -0,0 +1,50 @@ +// cxx-api-examples/fire-red-asr-cxx-api.cc +// Copyright (c) 2025 Xiaomi Corporation + +// +// This file demonstrates how to use FireRedAsr AED with sherpa-onnx's C++ API. +// +// clang-format off +// +// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16.tar.bz2 +// tar xvf sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16.tar.bz2 +// rm sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16.tar.bz2 +// +// clang-format on + +#include // NOLINT +#include +#include + +#include "sherpa-onnx/c-api/cxx-api.h" + +int32_t main() { + const char *kUsageMessage = R"usage( +Add punctuations to the input text. + +The input text can contain both Chinese and English words. + +Usage: + +wget https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 +tar xvf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 +rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 + +./bin/sherpa-onnx-offline-punctuation \ + --ct-transformer=./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx + "你好吗how are you Fantasitic 谢谢我很好你怎么样呢" + +The output text should look like below: +)usage"; + using namespace sherpa_onnx::cxx; // NOLINT + OfflinePunctuationConfig punctuation_config; + punctuation_config.model.ct_transformer = "./models/punctuation.onnx"; + punctuation_config.model.num_threads = 1; + punctuation_config.model.debug = false; + punctuation_config.model.provider = "cpu"; + OfflinePunctuation punct = OfflinePunctuation::Create(punctuation_config); + std::string text = "你好吗how are you Fantasitic 谢谢我很好你怎么样呢"; + std::string text_with_punct = punct.AddPunctuation(text); + std::cout << "Original text: " << text << std::endl; + std::cout << "With punctuation: " << text_with_punct << std::endl; +} From 44255d9dd63bddbdc5f876eef031e0532b7ed43b Mon Sep 17 00:00:00 2001 From: Aruxxxi Date: Tue, 19 Aug 2025 08:31:52 +0000 Subject: [PATCH 4/6] remove unused headers --- cxx-api-examples/punctuation-cxx-api.cc | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/cxx-api-examples/punctuation-cxx-api.cc b/cxx-api-examples/punctuation-cxx-api.cc index c377cc4f73..823385cf40 100644 --- a/cxx-api-examples/punctuation-cxx-api.cc +++ b/cxx-api-examples/punctuation-cxx-api.cc @@ -1,17 +1,6 @@ -// cxx-api-examples/fire-red-asr-cxx-api.cc +// cxx-api-examples/punctuation-cxx-api.cc // Copyright (c) 2025 Xiaomi Corporation -// -// This file demonstrates how to use FireRedAsr AED with sherpa-onnx's C++ API. -// -// clang-format off -// -// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16.tar.bz2 -// tar xvf sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16.tar.bz2 -// rm sherpa-onnx-fire-red-asr-large-zh_en-2025-02-16.tar.bz2 -// -// clang-format on - #include // NOLINT #include #include From abb746f26761ef772e1201ad83aea776d11b870f Mon Sep 17 00:00:00 2001 From: Aruxxxi Date: Tue, 19 Aug 2025 09:11:24 +0000 Subject: [PATCH 5/6] add to CMakeLists.txt and update punctuation example --- cxx-api-examples/CMakeLists.txt | 3 +++ cxx-api-examples/punctuation-cxx-api.cc | 34 +++++++++++-------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/cxx-api-examples/CMakeLists.txt b/cxx-api-examples/CMakeLists.txt index e2a75fb3a7..6ab57e78de 100644 --- a/cxx-api-examples/CMakeLists.txt +++ b/cxx-api-examples/CMakeLists.txt @@ -30,6 +30,9 @@ target_link_libraries(sense-voice-cxx-api sherpa-onnx-cxx-api) add_executable(nemo-canary-cxx-api ./nemo-canary-cxx-api.cc) target_link_libraries(nemo-canary-cxx-api sherpa-onnx-cxx-api) +add_executable(punctuation-cxx-api.cc ./punctuation-cxx-api.cc) +target_link_libraries(punctuation-cxx-api sherpa-onnx-cxx-api) + if(SHERPA_ONNX_ENABLE_PORTAUDIO) add_executable(sense-voice-simulate-streaming-microphone-cxx-api ./sense-voice-simulate-streaming-microphone-cxx-api.cc diff --git a/cxx-api-examples/punctuation-cxx-api.cc b/cxx-api-examples/punctuation-cxx-api.cc index 823385cf40..8ce4d52e52 100644 --- a/cxx-api-examples/punctuation-cxx-api.cc +++ b/cxx-api-examples/punctuation-cxx-api.cc @@ -1,39 +1,35 @@ // cxx-api-examples/punctuation-cxx-api.cc // Copyright (c) 2025 Xiaomi Corporation -#include // NOLINT +// To use punctuation model: +// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 +// tar xvf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 +// rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 + #include #include #include "sherpa-onnx/c-api/cxx-api.h" int32_t main() { - const char *kUsageMessage = R"usage( -Add punctuations to the input text. - -The input text can contain both Chinese and English words. - -Usage: - -wget https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 -tar xvf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 -rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2 - -./bin/sherpa-onnx-offline-punctuation \ - --ct-transformer=./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx - "你好吗how are you Fantasitic 谢谢我很好你怎么样呢" - -The output text should look like below: -)usage"; using namespace sherpa_onnx::cxx; // NOLINT + OfflinePunctuationConfig punctuation_config; - punctuation_config.model.ct_transformer = "./models/punctuation.onnx"; + punctuation_config.model.ct_transformer = "./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx"; punctuation_config.model.num_threads = 1; punctuation_config.model.debug = false; punctuation_config.model.provider = "cpu"; + OfflinePunctuation punct = OfflinePunctuation::Create(punctuation_config); + if (!punct.Get()) { + std::cerr << "Failed to create punctuation model. Please check your config\n"; + return -1; + } + std::string text = "你好吗how are you Fantasitic 谢谢我很好你怎么样呢"; std::string text_with_punct = punct.AddPunctuation(text); std::cout << "Original text: " << text << std::endl; std::cout << "With punctuation: " << text_with_punct << std::endl; + + return 0; } From 9281e4805232f8d6a679c9cb246f3ad21a7fc9d7 Mon Sep 17 00:00:00 2001 From: Aruxxxi Date: Tue, 19 Aug 2025 09:26:44 +0000 Subject: [PATCH 6/6] correct CMakeLists typo --- cxx-api-examples/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cxx-api-examples/CMakeLists.txt b/cxx-api-examples/CMakeLists.txt index 6ab57e78de..88465ec3a7 100644 --- a/cxx-api-examples/CMakeLists.txt +++ b/cxx-api-examples/CMakeLists.txt @@ -30,7 +30,7 @@ target_link_libraries(sense-voice-cxx-api sherpa-onnx-cxx-api) add_executable(nemo-canary-cxx-api ./nemo-canary-cxx-api.cc) target_link_libraries(nemo-canary-cxx-api sherpa-onnx-cxx-api) -add_executable(punctuation-cxx-api.cc ./punctuation-cxx-api.cc) +add_executable(punctuation-cxx-api ./punctuation-cxx-api.cc) target_link_libraries(punctuation-cxx-api sherpa-onnx-cxx-api) if(SHERPA_ONNX_ENABLE_PORTAUDIO)