-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add python API and examples for TTS (#364)
- Loading branch information
1 parent
1ac2232
commit 655e0fa
Showing
16 changed files
with
320 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Copyright (c) 2023 Xiaomi Corporation | ||
|
||
""" | ||
This file demonstrates how to use sherpa-onnx Python API to generate audio | ||
from text, i.e., text-to-speech. | ||
Usage: | ||
1. Download a model | ||
wget https://huggingface.co/csukuangfj/vits-ljs/resolve/main/vits-ljs.onnx | ||
wget https://huggingface.co/csukuangfj/vits-ljs/resolve/main/lexicon.txt | ||
wget https://huggingface.co/csukuangfj/vits-ljs/resolve/main/tokens.txt | ||
python3 ./python-api-examples/offline-tts.py \ | ||
--vits-model=./vits-ljs.onnx \ | ||
--vits-lexicon=./lexicon.txt \ | ||
--vits-tokens=./tokens.txt \ | ||
--output-filename=./generated.wav \ | ||
'liliana, the most beautiful and lovely assistant of our team!' | ||
""" | ||
|
||
import argparse | ||
|
||
import sherpa_onnx | ||
import soundfile as sf | ||
|
||
|
||
def get_args(): | ||
parser = argparse.ArgumentParser( | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter | ||
) | ||
|
||
parser.add_argument( | ||
"--vits-model", | ||
type=str, | ||
help="Path to vits model.onnx", | ||
) | ||
|
||
parser.add_argument( | ||
"--vits-lexicon", | ||
type=str, | ||
help="Path to lexicon.txt", | ||
) | ||
|
||
parser.add_argument( | ||
"--vits-tokens", | ||
type=str, | ||
help="Path to tokens.txt", | ||
) | ||
|
||
parser.add_argument( | ||
"--output-filename", | ||
type=str, | ||
default="./generated.wav", | ||
help="Path to save generated wave", | ||
) | ||
|
||
parser.add_argument( | ||
"--debug", | ||
type=bool, | ||
default=False, | ||
help="True to show debug messages", | ||
) | ||
|
||
parser.add_argument( | ||
"--provider", | ||
type=str, | ||
default="cpu", | ||
help="valid values: cpu, cuda, coreml", | ||
) | ||
|
||
parser.add_argument( | ||
"--num-threads", | ||
type=int, | ||
default=1, | ||
help="Number of threads for neural network computation", | ||
) | ||
|
||
parser.add_argument( | ||
"text", | ||
type=str, | ||
help="The input text to generate audio for", | ||
) | ||
|
||
return parser.parse_args() | ||
|
||
|
||
def main(): | ||
args = get_args() | ||
print(args) | ||
|
||
tts_config = sherpa_onnx.OfflineTtsConfig( | ||
model=sherpa_onnx.OfflineTtsModelConfig( | ||
vits=sherpa_onnx.OfflineTtsVitsModelConfig( | ||
model=args.vits_model, | ||
lexicon=args.vits_lexicon, | ||
tokens=args.vits_tokens, | ||
), | ||
provider=args.provider, | ||
debug=args.debug, | ||
num_threads=args.num_threads, | ||
) | ||
) | ||
tts = sherpa_onnx.OfflineTts(tts_config) | ||
audio = tts.generate(args.text) | ||
sf.write( | ||
args.output_filename, | ||
audio.samples, | ||
samplerate=audio.sample_rate, | ||
subtype="PCM_16", | ||
) | ||
print(f"Saved to {args.output_filename}") | ||
print(f"The text is '{args.text}'") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// sherpa-onnx/python/csrc/offline-tts-model-config.cc | ||
// | ||
// Copyright (c) 2023 Xiaomi Corporation | ||
|
||
#include "sherpa-onnx/python/csrc/offline-tts-model-config.h" | ||
|
||
#include <string> | ||
|
||
#include "sherpa-onnx/csrc/offline-tts-model-config.h" | ||
#include "sherpa-onnx/python/csrc/offline-tts-vits-model-config.h" | ||
|
||
namespace sherpa_onnx { | ||
|
||
void PybindOfflineTtsModelConfig(py::module *m) { | ||
PybindOfflineTtsVitsModelConfig(m); | ||
|
||
using PyClass = OfflineTtsModelConfig; | ||
|
||
py::class_<PyClass>(*m, "OfflineTtsModelConfig") | ||
.def(py::init<>()) | ||
.def(py::init<const OfflineTtsVitsModelConfig &, int32_t, bool, | ||
const std::string &>(), | ||
py::arg("vits"), py::arg("num_threads") = 1, | ||
py::arg("debug") = false, py::arg("provider") = "cpu") | ||
.def_readwrite("vits", &PyClass::vits) | ||
.def_readwrite("num_threads", &PyClass::num_threads) | ||
.def_readwrite("debug", &PyClass::debug) | ||
.def_readwrite("provider", &PyClass::provider) | ||
.def("__str__", &PyClass::ToString); | ||
} | ||
|
||
} // namespace sherpa_onnx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// sherpa-onnx/python/csrc/offline-tts-model-config.h | ||
// | ||
// Copyright (c) 2023 Xiaomi Corporation | ||
|
||
#ifndef SHERPA_ONNX_PYTHON_CSRC_OFFLINE_TTS_MODEL_CONFIG_H_ | ||
#define SHERPA_ONNX_PYTHON_CSRC_OFFLINE_TTS_MODEL_CONFIG_H_ | ||
|
||
#include "sherpa-onnx/python/csrc/sherpa-onnx.h" | ||
|
||
namespace sherpa_onnx { | ||
|
||
void PybindOfflineTtsModelConfig(py::module *m); | ||
|
||
} | ||
|
||
#endif // SHERPA_ONNX_PYTHON_CSRC_OFFLINE_TTS_MODEL_CONFIG_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// sherpa-onnx/python/csrc/offline-tts-vits-model-config.cc | ||
// | ||
// Copyright (c) 2023 Xiaomi Corporation | ||
|
||
#include "sherpa-onnx/python/csrc/offline-tts-vits-model-config.h" | ||
|
||
#include <string> | ||
|
||
#include "sherpa-onnx/csrc/offline-tts-vits-model-config.h" | ||
|
||
namespace sherpa_onnx { | ||
|
||
void PybindOfflineTtsVitsModelConfig(py::module *m) { | ||
using PyClass = OfflineTtsVitsModelConfig; | ||
|
||
py::class_<PyClass>(*m, "OfflineTtsVitsModelConfig") | ||
.def(py::init<>()) | ||
.def(py::init<const std::string &, const std::string &, | ||
const std::string &>(), | ||
py::arg("model"), py::arg("lexicon"), py::arg("tokens")) | ||
.def_readwrite("model", &PyClass::model) | ||
.def_readwrite("lexicon", &PyClass::lexicon) | ||
.def_readwrite("tokens", &PyClass::tokens) | ||
.def("__str__", &PyClass::ToString); | ||
} | ||
|
||
} // namespace sherpa_onnx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// sherpa-onnx/python/csrc/offline-tts-vits-model-config.h | ||
// | ||
// Copyright (c) 2023 Xiaomi Corporation | ||
|
||
#ifndef SHERPA_ONNX_PYTHON_CSRC_OFFLINE_TTS_VITS_MODEL_CONFIG_H_ | ||
#define SHERPA_ONNX_PYTHON_CSRC_OFFLINE_TTS_VITS_MODEL_CONFIG_H_ | ||
|
||
#include "sherpa-onnx/python/csrc/sherpa-onnx.h" | ||
|
||
namespace sherpa_onnx { | ||
|
||
void PybindOfflineTtsVitsModelConfig(py::module *m); | ||
|
||
} | ||
|
||
#endif // SHERPA_ONNX_PYTHON_CSRC_OFFLINE_TTS_VITS_MODEL_CONFIG_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// sherpa-onnx/python/csrc/offline-tts.cc | ||
// | ||
// Copyright (c) 2023 Xiaomi Corporation | ||
#include "sherpa-onnx/python/csrc/offline-tts.h" | ||
|
||
#include "sherpa-onnx/csrc/offline-tts.h" | ||
#include "sherpa-onnx/python/csrc/offline-tts-model-config.h" | ||
|
||
namespace sherpa_onnx { | ||
|
||
static void PybindGeneratedAudio(py::module *m) { | ||
using PyClass = GeneratedAudio; | ||
py::class_<PyClass>(*m, "GeneratedAudio") | ||
.def(py::init<>()) | ||
.def_readwrite("samples", &PyClass::samples) | ||
.def_readwrite("sample_rate", &PyClass::sample_rate) | ||
.def("__str__", [](PyClass &self) { | ||
std::ostringstream os; | ||
os << "GeneratedAudio(sample_rate=" << self.sample_rate << ", "; | ||
os << "num_samples=" << self.samples.size() << ")"; | ||
return os.str(); | ||
}); | ||
} | ||
|
||
static void PybindOfflineTtsConfig(py::module *m) { | ||
PybindOfflineTtsModelConfig(m); | ||
|
||
using PyClass = OfflineTtsConfig; | ||
py::class_<PyClass>(*m, "OfflineTtsConfig") | ||
.def(py::init<>()) | ||
.def(py::init<const OfflineTtsModelConfig &>(), py::arg("model")) | ||
.def_readwrite("model", &PyClass::model) | ||
.def("__str__", &PyClass::ToString); | ||
} | ||
|
||
void PybindOfflineTts(py::module *m) { | ||
PybindOfflineTtsConfig(m); | ||
PybindGeneratedAudio(m); | ||
|
||
using PyClass = OfflineTts; | ||
py::class_<PyClass>(*m, "OfflineTts") | ||
.def(py::init<const OfflineTtsConfig &>(), py::arg("config")) | ||
.def("generate", &PyClass::Generate); | ||
} | ||
|
||
} // namespace sherpa_onnx |
Oops, something went wrong.