adding a python api for offline decode (#110)

This commit is contained in:
manyeyes
2023-04-02 13:17:43 +08:00
committed by GitHub
parent 94d77fa52e
commit 3f7e0c23ac
17 changed files with 712 additions and 15 deletions

View File

@@ -4,6 +4,11 @@ pybind11_add_module(_sherpa_onnx
display.cc
endpoint.cc
features.cc
offline-model-config.cc
offline-paraformer-model-config.cc
offline-recognizer.cc
offline-stream.cc
offline-transducer-model-config.cc
online-recognizer.cc
online-stream.cc
online-transducer-model-config.cc

View File

@@ -0,0 +1,36 @@
// sherpa-onnx/python/csrc/offline-model-config.cc
//
// Copyright (c) 2023 by manyeyes
#include "sherpa-onnx/python/csrc/offline-model-config.h"
#include <string>
#include <vector>
#include "sherpa-onnx/python/csrc/offline-transducer-model-config.h"
#include "sherpa-onnx/python/csrc/offline-paraformer-model-config.h"
#include "sherpa-onnx/csrc/offline-model-config.h"
namespace sherpa_onnx {
void PybindOfflineModelConfig(py::module *m) {
PybindOfflineTransducerModelConfig(m);
PybindOfflineParaformerModelConfig(m);
using PyClass = OfflineModelConfig;
py::class_<PyClass>(*m, "OfflineModelConfig")
.def(py::init<OfflineTransducerModelConfig &,
OfflineParaformerModelConfig &,
const std::string &, int32_t, bool>(),
py::arg("transducer"), py::arg("paraformer"), py::arg("tokens"),
py::arg("num_threads"), py::arg("debug") = false)
.def_readwrite("transducer", &PyClass::transducer)
.def_readwrite("paraformer", &PyClass::paraformer)
.def_readwrite("tokens", &PyClass::tokens)
.def_readwrite("num_threads", &PyClass::num_threads)
.def_readwrite("debug", &PyClass::debug)
.def("__str__", &PyClass::ToString);
}
} // namespace sherpa_onnx

View File

@@ -0,0 +1,16 @@
// sherpa-onnx/python/csrc/offline-model-config.h
//
// Copyright (c) 2023 by manyeyes
#ifndef SHERPA_ONNX_PYTHON_CSRC_OFFLINE_MODEL_CONFIG_H_
#define SHERPA_ONNX_PYTHON_CSRC_OFFLINE_MODEL_CONFIG_H_
#include "sherpa-onnx/python/csrc/sherpa-onnx.h"
namespace sherpa_onnx {
void PybindOfflineModelConfig(py::module *m);
}
#endif // SHERPA_ONNX_PYTHON_CSRC_OFFLINE_MODEL_CONFIG_H_

View File

@@ -0,0 +1,24 @@
// sherpa-onnx/python/csrc/offline-paraformer-model-config.cc
//
// Copyright (c) 2023 by manyeyes
#include "sherpa-onnx/python/csrc/offline-paraformer-model-config.h"
#include <string>
#include <vector>
#include "sherpa-onnx/csrc/offline-paraformer-model-config.h"
namespace sherpa_onnx {
void PybindOfflineParaformerModelConfig(py::module *m) {
using PyClass = OfflineParaformerModelConfig;
py::class_<PyClass>(*m, "OfflineParaformerModelConfig")
.def(py::init<const std::string &>(),
py::arg("model"))
.def_readwrite("model", &PyClass::model)
.def("__str__", &PyClass::ToString);
}
} // namespace sherpa_onnx

View File

@@ -0,0 +1,16 @@
// sherpa-onnx/python/csrc/offline-paraformer-model-config.h
//
// Copyright (c) 2023 by manyeyes
#ifndef SHERPA_ONNX_PYTHON_CSRC_OFFLINE_PARAFORMER_MODEL_CONFIG_H_
#define SHERPA_ONNX_PYTHON_CSRC_OFFLINE_PARAFORMER_MODEL_CONFIG_H_
#include "sherpa-onnx/python/csrc/sherpa-onnx.h"
namespace sherpa_onnx {
void PybindOfflineParaformerModelConfig(py::module *m);
}
#endif // SHERPA_ONNX_PYTHON_CSRC_OFFLINE_PARAFORMER_MODEL_CONFIG_H_

View File

@@ -0,0 +1,43 @@
// sherpa-onnx/python/csrc/offline-recognizer.cc
//
// Copyright (c) 2023 by manyeyes
#include "sherpa-onnx/python/csrc/offline-recognizer.h"
#include <string>
#include <vector>
#include "sherpa-onnx/csrc/offline-recognizer.h"
namespace sherpa_onnx {
static void PybindOfflineRecognizerConfig(py::module *m) {
using PyClass = OfflineRecognizerConfig;
py::class_<PyClass>(*m, "OfflineRecognizerConfig")
.def(py::init<const OfflineFeatureExtractorConfig &,
const OfflineModelConfig &, const std::string &>(),
py::arg("feat_config"), py::arg("model_config"),
py::arg("decoding_method"))
.def_readwrite("feat_config", &PyClass::feat_config)
.def_readwrite("model_config", &PyClass::model_config)
.def_readwrite("decoding_method", &PyClass::decoding_method)
.def("__str__", &PyClass::ToString);
}
void PybindOfflineRecognizer(py::module *m) {
PybindOfflineRecognizerConfig(m);
using PyClass = OfflineRecognizer;
py::class_<PyClass>(*m, "OfflineRecognizer")
.def(py::init<const OfflineRecognizerConfig &>(), py::arg("config"))
.def("create_stream", &PyClass::CreateStream)
.def("decode_stream", &PyClass::DecodeStream)
.def("decode_streams",
[](PyClass &self, std::vector<OfflineStream *> ss) {
self.DecodeStreams(ss.data(), ss.size());
});
}
} // namespace sherpa_onnx

View File

@@ -0,0 +1,16 @@
// sherpa-onnx/python/csrc/offline-recognizer.h
//
// Copyright (c) 2023 by manyeyes
#ifndef SHERPA_ONNX_PYTHON_CSRC_OFFLINE_RECOGNIZER_H_
#define SHERPA_ONNX_PYTHON_CSRC_OFFLINE_RECOGNIZER_H_
#include "sherpa-onnx/python/csrc/sherpa-onnx.h"
namespace sherpa_onnx {
void PybindOfflineRecognizer(py::module *m);
}
#endif // SHERPA_ONNX_PYTHON_CSRC_OFFLINE_RECOGNIZER_H_

View File

@@ -0,0 +1,61 @@
// sherpa-onnx/python/csrc/offline-stream.cc
//
// Copyright (c) 2023 by manyeyes
#include "sherpa-onnx/python/csrc/offline-stream.h"
#include "sherpa-onnx/csrc/offline-stream.h"
namespace sherpa_onnx {
constexpr const char *kAcceptWaveformUsage = R"(
Process audio samples.
Args:
sample_rate:
Sample rate of the input samples. If it is different from the one
expected by the model, we will do resampling inside.
waveform:
A 1-D float32 tensor containing audio samples. It must be normalized
to the range [-1, 1].
)";
static void PybindOfflineRecognitionResult(py::module *m) { // NOLINT
using PyClass = OfflineRecognitionResult;
py::class_<PyClass>(*m, "OfflineRecognitionResult")
.def_property_readonly("text",
[](const PyClass &self) { return self.text; })
.def_property_readonly("tokens",
[](const PyClass &self) { return self.tokens; })
.def_property_readonly(
"timestamps", [](const PyClass &self) { return self.timestamps; });
}
static void PybindOfflineFeatureExtractorConfig(py::module *m) {
using PyClass = OfflineFeatureExtractorConfig;
py::class_<PyClass>(*m, "OfflineFeatureExtractorConfig")
.def(py::init<int32_t, int32_t>(), py::arg("sampling_rate") = 16000,
py::arg("feature_dim") = 80)
.def_readwrite("sampling_rate", &PyClass::sampling_rate)
.def_readwrite("feature_dim", &PyClass::feature_dim)
.def("__str__", &PyClass::ToString);
}
void PybindOfflineStream(py::module *m) {
PybindOfflineFeatureExtractorConfig(m);
PybindOfflineRecognitionResult(m);
using PyClass = OfflineStream;
py::class_<PyClass>(*m, "OfflineStream")
.def(
"accept_waveform",
[](PyClass &self, float sample_rate, py::array_t<float> waveform) {
self.AcceptWaveform(sample_rate, waveform.data(), waveform.size());
},
py::arg("sample_rate"), py::arg("waveform"), kAcceptWaveformUsage)
.def_property_readonly("result", &PyClass::GetResult);
}
} // namespace sherpa_onnx

View File

@@ -0,0 +1,16 @@
// sherpa-onnx/python/csrc/offline-stream.h
//
// Copyright (c) 2023 by manyeyes
#ifndef SHERPA_ONNX_PYTHON_CSRC_OFFLINE_STREAM_H_
#define SHERPA_ONNX_PYTHON_CSRC_OFFLINE_STREAM_H_
#include "sherpa-onnx/python/csrc/sherpa-onnx.h"
namespace sherpa_onnx {
void PybindOfflineStream(py::module *m);
}
#endif // SHERPA_ONNX_PYTHON_CSRC_OFFLINE_STREAM_H_

View File

@@ -0,0 +1,28 @@
// sherpa-onnx/python/csrc/offline-transducer-model-config.cc
//
// Copyright (c) 2023 by manyeyes
#include "sherpa-onnx/python/csrc/offline-transducer-model-config.h"
#include <string>
#include <vector>
#include "sherpa-onnx/csrc/offline-transducer-model-config.h"
namespace sherpa_onnx {
void PybindOfflineTransducerModelConfig(py::module *m) {
using PyClass = OfflineTransducerModelConfig;
py::class_<PyClass>(*m, "OfflineTransducerModelConfig")
.def(py::init<const std::string &, const std::string &,
const std::string &>(),
py::arg("encoder_filename"), py::arg("decoder_filename"),
py::arg("joiner_filename"))
.def_readwrite("encoder_filename", &PyClass::encoder_filename)
.def_readwrite("decoder_filename", &PyClass::decoder_filename)
.def_readwrite("joiner_filename", &PyClass::joiner_filename)
.def("__str__", &PyClass::ToString);
}
} // namespace sherpa_onnx

View File

@@ -0,0 +1,16 @@
// sherpa-onnx/python/csrc/offline-transducer-model-config.h
//
// Copyright (c) 2023 by manyeyes
#ifndef SHERPA_ONNX_PYTHON_CSRC_OFFLINE_TRANSDUCER_MODEL_CONFIG_H_
#define SHERPA_ONNX_PYTHON_CSRC_OFFLINE_TRANSDUCER_MODEL_CONFIG_H_
#include "sherpa-onnx/python/csrc/sherpa-onnx.h"
namespace sherpa_onnx {
void PybindOfflineTransducerModelConfig(py::module *m);
}
#endif // SHERPA_ONNX_PYTHON_CSRC_OFFLINE_TRANSDUCER_MODEL_CONFIG_H_

View File

@@ -11,10 +11,17 @@
#include "sherpa-onnx/python/csrc/online-stream.h"
#include "sherpa-onnx/python/csrc/online-transducer-model-config.h"
#include "sherpa-onnx/python/csrc/offline-model-config.h"
#include "sherpa-onnx/python/csrc/offline-paraformer-model-config.h"
#include "sherpa-onnx/python/csrc/offline-recognizer.h"
#include "sherpa-onnx/python/csrc/offline-stream.h"
#include "sherpa-onnx/python/csrc/offline-transducer-model-config.h"
namespace sherpa_onnx {
PYBIND11_MODULE(_sherpa_onnx, m) {
m.doc() = "pybind11 binding of sherpa-onnx";
PybindFeatures(&m);
PybindOnlineTransducerModelConfig(&m);
PybindOnlineStream(&m);
@@ -22,6 +29,10 @@ PYBIND11_MODULE(_sherpa_onnx, m) {
PybindOnlineRecognizer(&m);
PybindDisplay(&m);
PybindOfflineStream(&m);
PybindOfflineModelConfig(&m);
PybindOfflineRecognizer(&m);
}
} // namespace sherpa_onnx