Add Python API and Python examples for audio tagging (#753)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
include_directories(${CMAKE_SOURCE_DIR})
|
||||
|
||||
set(srcs
|
||||
audio-tagging.cc
|
||||
circular-buffer.cc
|
||||
display.cc
|
||||
endpoint.cc
|
||||
|
||||
87
sherpa-onnx/python/csrc/audio-tagging.cc
Normal file
87
sherpa-onnx/python/csrc/audio-tagging.cc
Normal 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
|
||||
16
sherpa-onnx/python/csrc/audio-tagging.h
Normal file
16
sherpa-onnx/python/csrc/audio-tagging.h
Normal 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_
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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>());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
from _sherpa_onnx import (
|
||||
Alsa,
|
||||
AudioEvent,
|
||||
AudioTagging,
|
||||
AudioTaggingConfig,
|
||||
AudioTaggingModelConfig,
|
||||
CircularBuffer,
|
||||
Display,
|
||||
OfflineStream,
|
||||
@@ -7,6 +11,7 @@ from _sherpa_onnx import (
|
||||
OfflineTtsConfig,
|
||||
OfflineTtsModelConfig,
|
||||
OfflineTtsVitsModelConfig,
|
||||
OfflineZipformerAudioTaggingModelConfig,
|
||||
OnlineStream,
|
||||
SileroVadModelConfig,
|
||||
SpeakerEmbeddingExtractor,
|
||||
|
||||
Reference in New Issue
Block a user