Skip to content

Commit

Permalink
Add python wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
pkufool committed Feb 7, 2024
1 parent a790b60 commit 0f1ca54
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions sherpa-onnx/python/csrc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pybind11_add_module(_sherpa_onnx
display.cc
endpoint.cc
features.cc
keyword-spotter.cc
offline-ctc-fst-decoder-config.cc
offline-lm-config.cc
offline-model-config.cc
Expand Down
82 changes: 82 additions & 0 deletions sherpa-onnx/python/csrc/keyword-spotter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// sherpa-onnx/python/csrc/keyword-spotter.cc
//
// Copyright (c) 2024 Xiaomi Corporation

#include "sherpa-onnx/python/csrc/keyword-spotter.h"

#include <string>
#include <vector>

#include "sherpa-onnx/csrc/keyword-spotter.h"

namespace sherpa_onnx {

static void PybindKeywordResult(py::module *m) {
using PyClass = KeywordResult;
py::class_<PyClass>(*m, "KeywordResult")
.def_property_readonly(
"keyword",
[](PyClass &self) -> py::str {
return py::str(PyUnicode_DecodeUTF8(self.keyword.c_str(),
self.keyword.size(), "ignore"));
})
.def_property_readonly(
"tokens",
[](PyClass &self) -> std::vector<std::string> { return self.tokens; })
.def_property_readonly(
"timestamps",
[](PyClass &self) -> std::vector<float> { return self.timestamps; });
}

static void PybindKeywordSpotterConfig(py::module *m) {
using PyClass = KeywordSpotterConfig;
py::class_<PyClass>(*m, "KeywordSpotterConfig")
.def(py::init<const FeatureExtractorConfig &, const OnlineModelConfig &,
int32_t, int32_t, float, float, const std::string &>(),
py::arg("feat_config"), py::arg("model_config"),
py::arg("max_active_paths") = 4, py::arg("num_trailing_blanks") = 1,
py::arg("keywords_score") = 1.0,
py::arg("keywords_threshold") = 0.25, py::arg("keywords_file") = "")
.def_readwrite("feat_config", &PyClass::feat_config)
.def_readwrite("model_config", &PyClass::model_config)
.def_readwrite("max_active_paths", &PyClass::max_active_paths)
.def_readwrite("num_trailing_blanks", &PyClass::num_trailing_blanks)
.def_readwrite("keywords_score", &PyClass::keywords_score)
.def_readwrite("keywords_threshold", &PyClass::keywords_threshold)
.def_readwrite("keywords_file", &PyClass::keywords_file)
.def("__str__", &PyClass::ToString);
}

void PybindKeywordSpotter(py::module *m) {
PybindKeywordResult(m);
PybindKeywordSpotterConfig(m);

using PyClass = KeywordSpotter;
py::class_<PyClass>(*m, "KeywordSpotter")
.def(py::init<const KeywordSpotterConfig &>(), py::arg("config"),
py::call_guard<py::gil_scoped_release>())
.def(
"create_stream",
[](const PyClass &self) { return self.CreateStream(); },
py::call_guard<py::gil_scoped_release>())
.def(
"create_stream",
[](PyClass &self, const std::string &keywords) {
return self.CreateStream(keywords);
},
py::arg("keywords"), py::call_guard<py::gil_scoped_release>())
.def("is_ready", &PyClass::IsReady,
py::call_guard<py::gil_scoped_release>())
.def("decode_stream", &PyClass::DecodeStream,
py::call_guard<py::gil_scoped_release>())
.def(
"decode_streams",
[](PyClass &self, std::vector<OnlineStream *> ss) {
self.DecodeStreams(ss.data(), ss.size());
},
py::call_guard<py::gil_scoped_release>())
.def("get_result", &PyClass::GetResult,
py::call_guard<py::gil_scoped_release>());
}

} // namespace sherpa_onnx
16 changes: 16 additions & 0 deletions sherpa-onnx/python/csrc/keyword-spotter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// sherpa-onnx/python/csrc/keyword-spotter.h
//
// Copyright (c) 2024 Xiaomi Corporation

#ifndef SHERPA_ONNX_PYTHON_CSRC_KEYWORD_SPOTTER_H_
#define SHERPA_ONNX_PYTHON_CSRC_KEYWORD_SPOTTER_H_

#include "sherpa-onnx/python/csrc/sherpa-onnx.h"

namespace sherpa_onnx {

void PybindKeywordSpotter(py::module *m);

}

#endif // SHERPA_ONNX_PYTHON_CSRC_KEYWORD_SPOTTER_H_
2 changes: 2 additions & 0 deletions sherpa-onnx/python/csrc/sherpa-onnx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "sherpa-onnx/python/csrc/display.h"
#include "sherpa-onnx/python/csrc/endpoint.h"
#include "sherpa-onnx/python/csrc/features.h"
#include "sherpa-onnx/python/csrc/keyword-spotter.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-model-config.h"
Expand Down Expand Up @@ -35,6 +36,7 @@ PYBIND11_MODULE(_sherpa_onnx, m) {
PybindOnlineStream(&m);
PybindEndpoint(&m);
PybindOnlineRecognizer(&m);
PybindKeywordSpotter(&m);

PybindDisplay(&m);

Expand Down

0 comments on commit 0f1ca54

Please sign in to comment.