Add C API for punctuation (#768)

This commit is contained in:
Fangjun Kuang
2024-04-14 19:02:34 +08:00
committed by GitHub
parent b0265b258d
commit 13730ecbd8
13 changed files with 302 additions and 12 deletions

View File

@@ -15,6 +15,7 @@
#include "sherpa-onnx/csrc/display.h"
#include "sherpa-onnx/csrc/keyword-spotter.h"
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/offline-punctuation.h"
#include "sherpa-onnx/csrc/offline-recognizer.h"
#include "sherpa-onnx/csrc/online-recognizer.h"
#include "sherpa-onnx/csrc/speaker-embedding-extractor.h"
@@ -1299,3 +1300,48 @@ void SherpaOnnxAudioTaggingFreeResults(
delete[] events;
}
struct SherpaOnnxOfflinePunctuation {
std::unique_ptr<sherpa_onnx::OfflinePunctuation> impl;
};
const SherpaOnnxOfflinePunctuation *SherpaOnnxCreateOfflinePunctuation(
const SherpaOnnxOfflinePunctuationConfig *config) {
sherpa_onnx::OfflinePunctuationConfig c;
c.model.ct_transformer = SHERPA_ONNX_OR(config->model.ct_transformer, "");
c.model.num_threads = SHERPA_ONNX_OR(config->model.num_threads, 1);
c.model.debug = config->model.debug;
c.model.provider = SHERPA_ONNX_OR(config->model.provider, "cpu");
if (c.model.debug) {
SHERPA_ONNX_LOGE("%s\n", c.ToString().c_str());
}
if (!c.Validate()) {
SHERPA_ONNX_LOGE("Errors in config");
return nullptr;
}
SherpaOnnxOfflinePunctuation *punct = new SherpaOnnxOfflinePunctuation;
punct->impl = std::make_unique<sherpa_onnx::OfflinePunctuation>(c);
return punct;
}
void SherpaOnnxDestroyOfflinePunctuation(
const SherpaOnnxOfflinePunctuation *punct) {
delete punct;
}
const char *SherpaOfflinePunctuationAddPunct(
const SherpaOnnxOfflinePunctuation *punct, const char *text) {
std::string text_with_punct = punct->impl->AddPunctuation(text);
char *ans = new char[text_with_punct.size() + 1];
std::copy(text_with_punct.begin(), text_with_punct.end(), ans);
ans[text_with_punct.size()] = 0;
return ans;
}
void SherpaOfflinePunctuationFreeText(const char *text) { delete[] text; }

View File

@@ -1149,6 +1149,41 @@ SherpaOnnxAudioTaggingCompute(const SherpaOnnxAudioTagging *tagger,
SHERPA_ONNX_API void SherpaOnnxAudioTaggingFreeResults(
const SherpaOnnxAudioEvent *const *p);
// ============================================================
// For punctuation
// ============================================================
SHERPA_ONNX_API typedef struct SherpaOnnxOfflinePunctuationModelConfig {
const char *ct_transformer;
int32_t num_threads;
int32_t debug; // true to print debug information of the model
const char *provider;
} SherpaOnnxOfflinePunctuationModelConfig;
SHERPA_ONNX_API typedef struct SherpaOnnxOfflinePunctuationConfig {
SherpaOnnxOfflinePunctuationModelConfig model;
} SherpaOnnxOfflinePunctuationConfig;
SHERPA_ONNX_API typedef struct SherpaOnnxOfflinePunctuation
SherpaOnnxOfflinePunctuation;
// The user has to invoke SherpaOnnxDestroyOfflinePunctuation()
// to free the returned pointer to avoid memory leak
SHERPA_ONNX_API const SherpaOnnxOfflinePunctuation *
SherpaOnnxCreateOfflinePunctuation(
const SherpaOnnxOfflinePunctuationConfig *config);
SHERPA_ONNX_API void SherpaOnnxDestroyOfflinePunctuation(
const SherpaOnnxOfflinePunctuation *punct);
// Add punctuations to the input text.
// The user has to invoke SherpaOfflinePunctuationFreeText()
// to free the returned pointer to avoid memory leak
SHERPA_ONNX_API const char *SherpaOfflinePunctuationAddPunct(
const SherpaOnnxOfflinePunctuation *punct, const char *text);
SHERPA_ONNX_API void SherpaOfflinePunctuationFreeText(const char *text);
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif