Refactor synthesis.py for TTSTokenizer

This commit is contained in:
Eren Gölge 2021-11-16 13:34:45 +01:00
parent e5785b34b0
commit 5a9653978a
1 changed files with 12 additions and 41 deletions
TTS/tts/utils

View File

@ -4,34 +4,6 @@ import numpy as np
import torch import torch
from torch import nn from torch import nn
from .text import phoneme_to_sequence, text_to_sequence
def text_to_seq(text, CONFIG, custom_symbols=None, language=None):
text_cleaner = [CONFIG.text_cleaner]
# text ot phonemes to sequence vector
if CONFIG.use_phonemes:
seq = np.asarray(
phoneme_to_sequence(
text,
text_cleaner,
language if language else CONFIG.phoneme_language,
CONFIG.enable_eos_bos_chars,
tp=CONFIG.characters,
add_blank=CONFIG.add_blank,
use_espeak_phonemes=CONFIG.use_espeak_phonemes,
custom_symbols=custom_symbols,
),
dtype=np.int32,
)
else:
seq = np.asarray(
text_to_sequence(
text, text_cleaner, tp=CONFIG.characters, add_blank=CONFIG.add_blank, custom_symbols=custom_symbols
),
dtype=np.int32,
)
return seq
def numpy_to_torch(np_array, dtype, cuda=False): def numpy_to_torch(np_array, dtype, cuda=False):
@ -143,9 +115,9 @@ def synthesis(
CONFIG, CONFIG,
use_cuda, use_cuda,
ap, ap,
tokenizer,
speaker_id=None, speaker_id=None,
style_wav=None, style_wav=None,
enable_eos_bos_chars=False, # pylint: disable=unused-argument
use_griffin_lim=False, use_griffin_lim=False,
do_trim_silence=False, do_trim_silence=False,
d_vector=None, d_vector=None,
@ -194,17 +166,17 @@ def synthesis(
""" """
# GST processing # GST processing
style_mel = None style_mel = None
custom_symbols = None if CONFIG.has("gst") and CONFIG.gst and style_wav is not None:
if style_wav: if isinstance(style_wav, dict):
style_mel = style_wav
else:
style_mel = compute_style_mel(style_wav, ap, cuda=use_cuda) style_mel = compute_style_mel(style_wav, ap, cuda=use_cuda)
elif CONFIG.has("gst") and CONFIG.gst and not style_wav: # convert text to sequence of token IDs
if CONFIG.gst.gst_style_input_weights: text_inputs = np.asarray(
style_mel = CONFIG.gst.gst_style_input_weights tokenizer.text_to_ids(text),
if hasattr(model, "make_symbols"): dtype=np.int32,
custom_symbols = model.make_symbols(CONFIG) )
# preprocess the given text # pass tensors to backend
text_inputs = text_to_seq(text, CONFIG, custom_symbols=custom_symbols, language=language_name)
if speaker_id is not None: if speaker_id is not None:
speaker_id = id_to_torch(speaker_id, cuda=use_cuda) speaker_id = id_to_torch(speaker_id, cuda=use_cuda)
@ -218,7 +190,6 @@ def synthesis(
style_mel = numpy_to_torch(style_mel, torch.float, cuda=use_cuda) style_mel = numpy_to_torch(style_mel, torch.float, cuda=use_cuda)
text_inputs = numpy_to_torch(text_inputs, torch.long, cuda=use_cuda) text_inputs = numpy_to_torch(text_inputs, torch.long, cuda=use_cuda)
text_inputs = text_inputs.unsqueeze(0) text_inputs = text_inputs.unsqueeze(0)
# synthesize voice # synthesize voice
outputs = run_model_torch(model, text_inputs, speaker_id, style_mel, d_vector=d_vector, language_id=language_id) outputs = run_model_torch(model, text_inputs, speaker_id, style_mel, d_vector=d_vector, language_id=language_id)
model_outputs = outputs["model_outputs"] model_outputs = outputs["model_outputs"]