Add Python API for punctuation models. (#762)
This commit is contained in:
2
.github/scripts/test-offline-punctuation.sh
vendored
2
.github/scripts/test-offline-punctuation.sh
vendored
@@ -14,7 +14,7 @@ echo "PATH: $PATH"
|
|||||||
which $EXE
|
which $EXE
|
||||||
|
|
||||||
log "------------------------------------------------------------"
|
log "------------------------------------------------------------"
|
||||||
log "Download model "
|
log "Download the punctuation model "
|
||||||
log "------------------------------------------------------------"
|
log "------------------------------------------------------------"
|
||||||
|
|
||||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
|
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
|
||||||
|
|||||||
12
.github/scripts/test-python.sh
vendored
12
.github/scripts/test-python.sh
vendored
@@ -8,6 +8,18 @@ log() {
|
|||||||
echo -e "$(date '+%Y-%m-%d %H:%M:%S') (${fname}:${BASH_LINENO[0]}:${FUNCNAME[1]}) $*"
|
echo -e "$(date '+%Y-%m-%d %H:%M:%S') (${fname}:${BASH_LINENO[0]}:${FUNCNAME[1]}) $*"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log "test offline punctuation"
|
||||||
|
|
||||||
|
curl -SL -O 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
|
||||||
|
repo=sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12
|
||||||
|
ls -lh $repo
|
||||||
|
|
||||||
|
python3 ./python-api-examples/add-punctuation.py
|
||||||
|
|
||||||
|
rm -rf $repo
|
||||||
|
|
||||||
log "test audio tagging"
|
log "test audio tagging"
|
||||||
|
|
||||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/audio-tagging-models/sherpa-onnx-zipformer-audio-tagging-2024-04-09.tar.bz2
|
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/audio-tagging-models/sherpa-onnx-zipformer-audio-tagging-2024-04-09.tar.bz2
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -91,3 +91,4 @@ sr-data
|
|||||||
*xcworkspace/xcuserdata/*
|
*xcworkspace/xcuserdata/*
|
||||||
|
|
||||||
vits-icefall-*
|
vits-icefall-*
|
||||||
|
sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
|
|
||||||
if [ ! -f ./silero_vad.onnx ]; then
|
if [ ! -f ./silero_vad.onnx ]; then
|
||||||
curl -SL -O https://github.com/snakers4/silero-vad/blob/master/files/silero_vad.onnx
|
curl -SL -O https://github.com/snakers4/silero-vad/raw/master/files/silero_vad.onnx
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -f ./sherpa-onnx-paraformer-trilingual-zh-cantonese-en/model.int8.onnx ]; then
|
if [ ! -f ./sherpa-onnx-paraformer-trilingual-zh-cantonese-en/model.int8.onnx ]; then
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
|
|
||||||
if [ ! -f ./silero_vad.onnx ]; then
|
if [ ! -f ./silero_vad.onnx ]; then
|
||||||
curl -SL -O https://github.com/snakers4/silero-vad/blob/master/files/silero_vad.onnx
|
curl -SL -O https://github.com/snakers4/silero-vad/raw/master/files/silero_vad.onnx
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -f ./sherpa-onnx-whisper-tiny.en/tiny.en-encoder.int8.onnx ]; then
|
if [ ! -f ./sherpa-onnx-whisper-tiny.en/tiny.en-encoder.int8.onnx ]; then
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ if [ ! -f ./sr-data/enroll/fangjun-sr-1.wav ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -f ./silero_vad.onnx ]; then
|
if [ ! -f ./silero_vad.onnx ]; then
|
||||||
curl -SL -O https://github.com/snakers4/silero-vad/blob/master/files/silero_vad.onnx
|
curl -SL -O https://github.com/snakers4/silero-vad/raw/master/files/silero_vad.onnx
|
||||||
fi
|
fi
|
||||||
|
|
||||||
go mod tidy
|
go mod tidy
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
|
|
||||||
if [ ! -f ./silero_vad.onnx ]; then
|
if [ ! -f ./silero_vad.onnx ]; then
|
||||||
curl -SL -O https://github.com/snakers4/silero-vad/blob/master/files/silero_vad.onnx
|
curl -SL -O https://github.com/snakers4/silero-vad/raw/master/files/silero_vad.onnx
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -f ./sherpa-onnx-whisper-tiny/tiny-encoder.int8.onnx ]; then
|
if [ ! -f ./sherpa-onnx-whisper-tiny/tiny-encoder.int8.onnx ]; then
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
|
|
||||||
if [ ! -f ./silero_vad.onnx ]; then
|
if [ ! -f ./silero_vad.onnx ]; then
|
||||||
curl -SL -O https://github.com/snakers4/silero-vad/blob/master/files/silero_vad.onnx
|
curl -SL -O https://github.com/snakers4/silero-vad/raw/master/files/silero_vad.onnx
|
||||||
fi
|
fi
|
||||||
|
|
||||||
go mod tidy
|
go mod tidy
|
||||||
|
|||||||
46
python-api-examples/add-punctuation.py
Executable file
46
python-api-examples/add-punctuation.py
Executable file
@@ -0,0 +1,46 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
"""
|
||||||
|
This script shows how to add punctuations to text using sherpa-onnx Python API.
|
||||||
|
|
||||||
|
Please download the model from
|
||||||
|
https://github.com/k2-fsa/sherpa-onnx/releases/tag/punctuation-models
|
||||||
|
|
||||||
|
The following is an example
|
||||||
|
|
||||||
|
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
|
||||||
|
"""
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import sherpa_onnx
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
model = "./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx"
|
||||||
|
if not Path(model).is_file():
|
||||||
|
raise ValueError(f"{model} does not exist")
|
||||||
|
config = sherpa_onnx.OfflinePunctuationConfig(
|
||||||
|
model=sherpa_onnx.OfflinePunctuationModelConfig(ct_transformer=model),
|
||||||
|
)
|
||||||
|
|
||||||
|
punct = sherpa_onnx.OfflinePunctuation(config)
|
||||||
|
|
||||||
|
text_list = [
|
||||||
|
"这是一个测试你好吗How are you我很好thank you are you ok谢谢你",
|
||||||
|
"我们都是木头人不会说话不会动",
|
||||||
|
"The African blogosphere is rapidly expanding bringing more voices online in the form of commentaries opinions analyses rants and poetry",
|
||||||
|
]
|
||||||
|
for text in text_list:
|
||||||
|
text_with_punct = punct.add_punctuation(text)
|
||||||
|
print("----------")
|
||||||
|
print(f"input: {text}")
|
||||||
|
print(f"output: {text_with_punct}")
|
||||||
|
|
||||||
|
print("----------")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -12,6 +12,7 @@ set(srcs
|
|||||||
offline-model-config.cc
|
offline-model-config.cc
|
||||||
offline-nemo-enc-dec-ctc-model-config.cc
|
offline-nemo-enc-dec-ctc-model-config.cc
|
||||||
offline-paraformer-model-config.cc
|
offline-paraformer-model-config.cc
|
||||||
|
offline-punctuation.cc
|
||||||
offline-recognizer.cc
|
offline-recognizer.cc
|
||||||
offline-stream.cc
|
offline-stream.cc
|
||||||
offline-tdnn-model-config.cc
|
offline-tdnn-model-config.cc
|
||||||
|
|||||||
49
sherpa-onnx/python/csrc/offline-punctuation.cc
Normal file
49
sherpa-onnx/python/csrc/offline-punctuation.cc
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
// sherpa-onnx/python/csrc/offline-punctuation.cc
|
||||||
|
//
|
||||||
|
// Copyright (c) 2024 Xiaomi Corporation
|
||||||
|
|
||||||
|
#include "sherpa-onnx/python/csrc/offline-punctuation.h"
|
||||||
|
|
||||||
|
#include "sherpa-onnx/csrc/offline-punctuation.h"
|
||||||
|
|
||||||
|
namespace sherpa_onnx {
|
||||||
|
|
||||||
|
static void PybindOfflinePunctuationModelConfig(py::module *m) {
|
||||||
|
using PyClass = OfflinePunctuationModelConfig;
|
||||||
|
py::class_<PyClass>(*m, "OfflinePunctuationModelConfig")
|
||||||
|
.def(py::init<>())
|
||||||
|
.def(py::init<const std::string &, int32_t, bool, const std::string &>(),
|
||||||
|
py::arg("ct_transformer"), py::arg("num_threads") = 1,
|
||||||
|
py::arg("debug") = false, py::arg("provider") = "cpu")
|
||||||
|
.def_readwrite("ct_transformer", &PyClass::ct_transformer)
|
||||||
|
.def_readwrite("num_threads", &PyClass::num_threads)
|
||||||
|
.def_readwrite("debug", &PyClass::debug)
|
||||||
|
.def_readwrite("provider", &PyClass::provider)
|
||||||
|
.def("validate", &PyClass::Validate)
|
||||||
|
.def("__str__", &PyClass::ToString);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void PybindOfflinePunctuationConfig(py::module *m) {
|
||||||
|
PybindOfflinePunctuationModelConfig(m);
|
||||||
|
using PyClass = OfflinePunctuationConfig;
|
||||||
|
|
||||||
|
py::class_<PyClass>(*m, "OfflinePunctuationConfig")
|
||||||
|
.def(py::init<>())
|
||||||
|
.def(py::init<const OfflinePunctuationModelConfig &>(), py::arg("model"))
|
||||||
|
.def_readwrite("model", &PyClass::model)
|
||||||
|
.def("validate", &PyClass::Validate)
|
||||||
|
.def("__str__", &PyClass::ToString);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PybindOfflinePunctuation(py::module *m) {
|
||||||
|
PybindOfflinePunctuationConfig(m);
|
||||||
|
using PyClass = OfflinePunctuation;
|
||||||
|
|
||||||
|
py::class_<PyClass>(*m, "OfflinePunctuation")
|
||||||
|
.def(py::init<const OfflinePunctuationConfig &>(), py::arg("config"),
|
||||||
|
py::call_guard<py::gil_scoped_release>())
|
||||||
|
.def("add_punctuation", &PyClass::AddPunctuation, py::arg("text"),
|
||||||
|
py::call_guard<py::gil_scoped_release>());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace sherpa_onnx
|
||||||
16
sherpa-onnx/python/csrc/offline-punctuation.h
Normal file
16
sherpa-onnx/python/csrc/offline-punctuation.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
// sherpa-onnx/python/csrc/offline-punctuation.h
|
||||||
|
//
|
||||||
|
// Copyright (c) 2024 Xiaomi Corporation
|
||||||
|
|
||||||
|
#ifndef SHERPA_ONNX_PYTHON_CSRC_OFFLINE_PUNCTUATION_H_
|
||||||
|
#define SHERPA_ONNX_PYTHON_CSRC_OFFLINE_PUNCTUATION_H_
|
||||||
|
|
||||||
|
#include "sherpa-onnx/python/csrc/sherpa-onnx.h"
|
||||||
|
|
||||||
|
namespace sherpa_onnx {
|
||||||
|
|
||||||
|
void PybindOfflinePunctuation(py::module *m);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // SHERPA_ONNX_PYTHON_CSRC_OFFLINE_PUNCTUATION_H_
|
||||||
@@ -14,6 +14,7 @@
|
|||||||
#include "sherpa-onnx/python/csrc/offline-ctc-fst-decoder-config.h"
|
#include "sherpa-onnx/python/csrc/offline-ctc-fst-decoder-config.h"
|
||||||
#include "sherpa-onnx/python/csrc/offline-lm-config.h"
|
#include "sherpa-onnx/python/csrc/offline-lm-config.h"
|
||||||
#include "sherpa-onnx/python/csrc/offline-model-config.h"
|
#include "sherpa-onnx/python/csrc/offline-model-config.h"
|
||||||
|
#include "sherpa-onnx/python/csrc/offline-punctuation.h"
|
||||||
#include "sherpa-onnx/python/csrc/offline-recognizer.h"
|
#include "sherpa-onnx/python/csrc/offline-recognizer.h"
|
||||||
#include "sherpa-onnx/python/csrc/offline-stream.h"
|
#include "sherpa-onnx/python/csrc/offline-stream.h"
|
||||||
#include "sherpa-onnx/python/csrc/online-ctc-fst-decoder-config.h"
|
#include "sherpa-onnx/python/csrc/online-ctc-fst-decoder-config.h"
|
||||||
@@ -40,6 +41,7 @@ PYBIND11_MODULE(_sherpa_onnx, m) {
|
|||||||
|
|
||||||
PybindWaveWriter(&m);
|
PybindWaveWriter(&m);
|
||||||
PybindAudioTagging(&m);
|
PybindAudioTagging(&m);
|
||||||
|
PybindOfflinePunctuation(&m);
|
||||||
|
|
||||||
PybindFeatures(&m);
|
PybindFeatures(&m);
|
||||||
PybindOnlineCtcFstDecoderConfig(&m);
|
PybindOnlineCtcFstDecoderConfig(&m);
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ from _sherpa_onnx import (
|
|||||||
AudioTaggingModelConfig,
|
AudioTaggingModelConfig,
|
||||||
CircularBuffer,
|
CircularBuffer,
|
||||||
Display,
|
Display,
|
||||||
|
OfflinePunctuation,
|
||||||
|
OfflinePunctuationConfig,
|
||||||
|
OfflinePunctuationModelConfig,
|
||||||
OfflineStream,
|
OfflineStream,
|
||||||
OfflineTts,
|
OfflineTts,
|
||||||
OfflineTtsConfig,
|
OfflineTtsConfig,
|
||||||
|
|||||||
Reference in New Issue
Block a user