Add Python API and Python examples for audio tagging (#753)

This commit is contained in:
Fangjun Kuang
2024-04-11 11:12:48 +08:00
committed by GitHub
parent 904a3cc8a9
commit 34d70a259f
12 changed files with 245 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
include_directories(${CMAKE_SOURCE_DIR})
set(srcs
audio-tagging.cc
circular-buffer.cc
display.cc
endpoint.cc

View File

@@ -0,0 +1,87 @@
// sherpa-onnx/python/csrc/audio-tagging.cc
//
// Copyright (c) 2024 Xiaomi Corporation
#include "sherpa-onnx/python/csrc/audio-tagging.h"
#include <string>
#include "sherpa-onnx/csrc/audio-tagging.h"
namespace sherpa_onnx {
static void PybindOfflineZipformerAudioTaggingModelConfig(py::module *m) {
using PyClass = OfflineZipformerAudioTaggingModelConfig;
py::class_<PyClass>(*m, "OfflineZipformerAudioTaggingModelConfig")
.def(py::init<>())
.def(py::init<const std::string &>(), py::arg("model"))
.def_readwrite("model", &PyClass::model)
.def("validate", &PyClass::Validate)
.def("__str__", &PyClass::ToString);
}
static void PybindAudioTaggingModelConfig(py::module *m) {
PybindOfflineZipformerAudioTaggingModelConfig(m);
using PyClass = AudioTaggingModelConfig;
py::class_<PyClass>(*m, "AudioTaggingModelConfig")
.def(py::init<>())
.def(py::init<const OfflineZipformerAudioTaggingModelConfig &, int32_t,
bool, const std::string &>(),
py::arg("zipformer"), py::arg("num_threads") = 1,
py::arg("debug") = false, py::arg("provider") = "cpu")
.def_readwrite("zipformer", &PyClass::zipformer)
.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 PybindAudioTaggingConfig(py::module *m) {
PybindAudioTaggingModelConfig(m);
using PyClass = AudioTaggingConfig;
py::class_<PyClass>(*m, "AudioTaggingConfig")
.def(py::init<>())
.def(py::init<const AudioTaggingModelConfig &, const std::string &,
int32_t>(),
py::arg("model"), py::arg("labels"), py::arg("top_k") = 5)
.def_readwrite("model", &PyClass::model)
.def_readwrite("labels", &PyClass::labels)
.def_readwrite("top_k", &PyClass::top_k)
.def("validate", &PyClass::Validate)
.def("__str__", &PyClass::ToString);
}
static void PybindAudioEvent(py::module *m) {
using PyClass = AudioEvent;
py::class_<PyClass>(*m, "AudioEvent")
.def_property_readonly(
"name", [](const PyClass &self) -> std::string { return self.name; })
.def_property_readonly(
"index", [](const PyClass &self) -> int32_t { return self.index; })
.def_property_readonly(
"prob", [](const PyClass &self) -> float { return self.prob; })
.def("__str__", &PyClass::ToString);
}
void PybindAudioTagging(py::module *m) {
PybindAudioTaggingConfig(m);
PybindAudioEvent(m);
using PyClass = AudioTagging;
py::class_<PyClass>(*m, "AudioTagging")
.def(py::init<const AudioTaggingConfig &>(), py::arg("config"),
py::call_guard<py::gil_scoped_release>())
.def("create_stream", &PyClass::CreateStream,
py::call_guard<py::gil_scoped_release>())
.def("compute", &PyClass::Compute, py::arg("s"), py::arg("top_k") = -1,
py::call_guard<py::gil_scoped_release>());
}
} // namespace sherpa_onnx

View File

@@ -0,0 +1,16 @@
// sherpa-onnx/python/csrc/audio-tagging.h
//
// Copyright (c) 2024 Xiaomi Corporation
#ifndef SHERPA_ONNX_PYTHON_CSRC_AUDIO_TAGGING_H_
#define SHERPA_ONNX_PYTHON_CSRC_AUDIO_TAGGING_H_
#include "sherpa-onnx/python/csrc/sherpa-onnx.h"
namespace sherpa_onnx {
void PybindAudioTagging(py::module *m);
}
#endif // SHERPA_ONNX_PYTHON_CSRC_AUDIO_TAGGING_H_

View File

@@ -16,7 +16,7 @@ void PybindOfflineTtsVitsModelConfig(py::module *m) {
py::class_<PyClass>(*m, "OfflineTtsVitsModelConfig")
.def(py::init<>())
.def(py::init<const std::string &, const std::string &,
const std::string &, const std::string, float, float,
const std::string &, const std::string &, float, float,
float>(),
py::arg("model"), py::arg("lexicon"), py::arg("tokens"),
py::arg("data_dir") = "", py::arg("noise_scale") = 0.667,

View File

@@ -5,6 +5,7 @@
#include "sherpa-onnx/python/csrc/sherpa-onnx.h"
#include "sherpa-onnx/python/csrc/alsa.h"
#include "sherpa-onnx/python/csrc/audio-tagging.h"
#include "sherpa-onnx/python/csrc/circular-buffer.h"
#include "sherpa-onnx/python/csrc/display.h"
#include "sherpa-onnx/python/csrc/endpoint.h"
@@ -38,6 +39,7 @@ PYBIND11_MODULE(_sherpa_onnx, m) {
m.doc() = "pybind11 binding of sherpa-onnx";
PybindWaveWriter(&m);
PybindAudioTagging(&m);
PybindFeatures(&m);
PybindOnlineCtcFstDecoderConfig(&m);

View File

@@ -14,7 +14,7 @@ static void PybindSpeakerEmbeddingExtractorConfig(py::module *m) {
using PyClass = SpeakerEmbeddingExtractorConfig;
py::class_<PyClass>(*m, "SpeakerEmbeddingExtractorConfig")
.def(py::init<>())
.def(py::init<const std::string &, int32_t, bool, const std::string>(),
.def(py::init<const std::string &, int32_t, bool, const std::string &>(),
py::arg("model"), py::arg("num_threads") = 1,
py::arg("debug") = false, py::arg("provider") = "cpu")
.def_readwrite("model", &PyClass::model)

View File

@@ -33,7 +33,7 @@ static void PybindSpokenLanguageIdentificationConfig(py::module *m) {
py::class_<PyClass>(*m, "SpokenLanguageIdentificationConfig")
.def(py::init<>())
.def(py::init<const SpokenLanguageIdentificationWhisperConfig &, int32_t,
bool, const std::string>(),
bool, const std::string &>(),
py::arg("whisper"), py::arg("num_threads") = 1,
py::arg("debug") = false, py::arg("provider") = "cpu")
.def_readwrite("whisper", &PyClass::whisper)
@@ -53,7 +53,7 @@ void PybindSpokenLanguageIdentification(py::module *m) {
py::arg("config"), py::call_guard<py::gil_scoped_release>())
.def("create_stream", &PyClass::CreateStream,
py::call_guard<py::gil_scoped_release>())
.def("compute", &PyClass::Compute,
.def("compute", &PyClass::Compute, py::arg("s"),
py::call_guard<py::gil_scoped_release>());
}