Sync api.py with clean PyPI version
Browse files
api.py
CHANGED
|
@@ -1,51 +1,40 @@
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
-
|
| 4 |
from dataclasses import asdict
|
| 5 |
|
| 6 |
-
from utils.audio import LogMelSpectrogram
|
| 7 |
-
from config import ModelConfig, MelConfig
|
| 8 |
-
from models.model import StableTTS
|
| 9 |
-
|
| 10 |
-
from text import symbols
|
| 11 |
-
from text import cleaned_text_to_sequence
|
| 12 |
-
from text.
|
| 13 |
-
from
|
| 14 |
-
from
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# training or changing ffgan config is not supported in this repo
|
| 22 |
-
# you can train your own model at https://github.com/fishaudio/vocoder
|
| 23 |
-
from vocoders.ffgan.model import FireflyGANBaseWrapper
|
| 24 |
-
vocoder = FireflyGANBaseWrapper(model_path)
|
| 25 |
-
|
| 26 |
-
elif model_name == 'vocos':
|
| 27 |
-
from vocoders.vocos.models.model import Vocos
|
| 28 |
-
from config import VocosConfig, MelConfig
|
| 29 |
vocoder = Vocos(VocosConfig(), MelConfig())
|
| 30 |
vocoder.load_state_dict(torch.load(model_path, weights_only=True, map_location='cpu'))
|
| 31 |
vocoder.eval()
|
| 32 |
-
|
| 33 |
else:
|
| 34 |
-
raise NotImplementedError(f"Unsupported
|
| 35 |
-
|
| 36 |
return vocoder
|
| 37 |
|
|
|
|
| 38 |
class StableTTSAPI(nn.Module):
|
| 39 |
-
def __init__(self, tts_model_path, vocoder_model_path, vocoder_name='
|
| 40 |
super().__init__()
|
| 41 |
-
|
| 42 |
self.mel_config = MelConfig()
|
| 43 |
self.tts_model_config = ModelConfig()
|
| 44 |
-
|
| 45 |
self.mel_extractor = LogMelSpectrogram(**asdict(self.mel_config))
|
| 46 |
-
|
| 47 |
-
# text to mel spectrogram
|
| 48 |
self.tts_model = StableTTS(len(symbols), self.mel_config.n_mels, **asdict(self.tts_model_config))
|
|
|
|
| 49 |
if tts_model_path.endswith(".safetensors"):
|
| 50 |
from safetensors.torch import load_file
|
| 51 |
state = load_file(tts_model_path)
|
|
@@ -53,55 +42,41 @@ class StableTTSAPI(nn.Module):
|
|
| 53 |
state = torch.load(tts_model_path, map_location='cpu', weights_only=True)
|
| 54 |
self.tts_model.load_state_dict(state)
|
| 55 |
self.tts_model.eval()
|
| 56 |
-
|
| 57 |
-
# mel spectrogram to waveform
|
| 58 |
self.vocoder_model = get_vocoder(vocoder_model_path, vocoder_name)
|
| 59 |
self.vocoder_model.eval()
|
| 60 |
-
|
| 61 |
self.g2p_mapping = {
|
| 62 |
-
'
|
| 63 |
-
'japanese': japanese_to_ipa2,
|
| 64 |
-
'english': english_to_ipa2,
|
| 65 |
}
|
| 66 |
self.supported_languages = self.g2p_mapping.keys()
|
| 67 |
-
|
| 68 |
-
@
|
| 69 |
-
def inference(self, text, ref_audio, language, step, temperature=1.0,
|
|
|
|
| 70 |
device = next(self.parameters()).device
|
| 71 |
phonemizer = self.g2p_mapping.get(language)
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
text = phonemizer(text)
|
| 74 |
-
text = torch.tensor(
|
|
|
|
|
|
|
|
|
|
| 75 |
text_length = torch.tensor([text.size(-1)], dtype=torch.long, device=device)
|
| 76 |
-
|
| 77 |
ref_audio = load_and_resample_audio(ref_audio, self.mel_config.sample_rate).to(device)
|
| 78 |
ref_audio = self.mel_extractor(ref_audio)
|
| 79 |
-
|
| 80 |
-
mel_output = self.tts_model.synthesise(
|
|
|
|
|
|
|
|
|
|
| 81 |
audio_output = self.vocoder_model(mel_output)
|
| 82 |
return audio_output.cpu(), mel_output.cpu()
|
| 83 |
-
|
| 84 |
def get_params(self):
|
| 85 |
tts_param = sum(p.numel() for p in self.tts_model.parameters()) / 1e6
|
| 86 |
vocoder_param = sum(p.numel() for p in self.vocoder_model.parameters()) / 1e6
|
| 87 |
return tts_param, vocoder_param
|
| 88 |
-
|
| 89 |
-
if __name__ == '__main__':
|
| 90 |
-
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 91 |
-
tts_model_path = './checkpoints/checkpoint_0.pt'
|
| 92 |
-
vocoder_model_path = './vocoders/pretrained/vocos.pt'
|
| 93 |
-
|
| 94 |
-
model = StableTTSAPI(tts_model_path, vocoder_model_path, 'vocos')
|
| 95 |
-
model.to(device)
|
| 96 |
-
|
| 97 |
-
text = '樱落满殇祈念集……殇歌花落集思祈……樱花满地集于我心……揲舞纷飞祈愿相随……'
|
| 98 |
-
audio = './audio_1.wav'
|
| 99 |
-
|
| 100 |
-
audio_output, mel_output = model.inference(text, audio, 'chinese', 10, solver='dopri5', cfg=3)
|
| 101 |
-
print(audio_output.shape)
|
| 102 |
-
print(mel_output.shape)
|
| 103 |
-
|
| 104 |
-
import torchaudio
|
| 105 |
-
torchaudio.save('output.wav', audio_output, MelConfig().sample_rate)
|
| 106 |
-
|
| 107 |
-
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
|
|
|
| 3 |
from dataclasses import asdict
|
| 4 |
|
| 5 |
+
from .utils.audio import LogMelSpectrogram
|
| 6 |
+
from .config import ModelConfig, MelConfig
|
| 7 |
+
from .models.model import StableTTS
|
| 8 |
+
|
| 9 |
+
from .text import symbols
|
| 10 |
+
from .text import cleaned_text_to_sequence
|
| 11 |
+
from .text.burmese import burmese_to_ipa2
|
| 12 |
+
from .datas.dataset import intersperse
|
| 13 |
+
from .utils.audio import load_and_resample_audio
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def get_vocoder(model_path, model_name='vocos'):
|
| 17 |
+
if model_name == 'vocos':
|
| 18 |
+
from .vocoders.vocos.models.model import Vocos
|
| 19 |
+
from .config import VocosConfig, MelConfig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
vocoder = Vocos(VocosConfig(), MelConfig())
|
| 21 |
vocoder.load_state_dict(torch.load(model_path, weights_only=True, map_location='cpu'))
|
| 22 |
vocoder.eval()
|
|
|
|
| 23 |
else:
|
| 24 |
+
raise NotImplementedError(f"Unsupported vocoder: {model_name}")
|
|
|
|
| 25 |
return vocoder
|
| 26 |
|
| 27 |
+
|
| 28 |
class StableTTSAPI(nn.Module):
|
| 29 |
+
def __init__(self, tts_model_path, vocoder_model_path, vocoder_name='vocos'):
|
| 30 |
super().__init__()
|
|
|
|
| 31 |
self.mel_config = MelConfig()
|
| 32 |
self.tts_model_config = ModelConfig()
|
| 33 |
+
|
| 34 |
self.mel_extractor = LogMelSpectrogram(**asdict(self.mel_config))
|
| 35 |
+
|
|
|
|
| 36 |
self.tts_model = StableTTS(len(symbols), self.mel_config.n_mels, **asdict(self.tts_model_config))
|
| 37 |
+
|
| 38 |
if tts_model_path.endswith(".safetensors"):
|
| 39 |
from safetensors.torch import load_file
|
| 40 |
state = load_file(tts_model_path)
|
|
|
|
| 42 |
state = torch.load(tts_model_path, map_location='cpu', weights_only=True)
|
| 43 |
self.tts_model.load_state_dict(state)
|
| 44 |
self.tts_model.eval()
|
| 45 |
+
|
|
|
|
| 46 |
self.vocoder_model = get_vocoder(vocoder_model_path, vocoder_name)
|
| 47 |
self.vocoder_model.eval()
|
| 48 |
+
|
| 49 |
self.g2p_mapping = {
|
| 50 |
+
'burmese': burmese_to_ipa2,
|
|
|
|
|
|
|
| 51 |
}
|
| 52 |
self.supported_languages = self.g2p_mapping.keys()
|
| 53 |
+
|
| 54 |
+
@torch.inference_mode()
|
| 55 |
+
def inference(self, text, ref_audio, language, step, temperature=1.0,
|
| 56 |
+
length_scale=1.0, solver=None, cfg=3.0):
|
| 57 |
device = next(self.parameters()).device
|
| 58 |
phonemizer = self.g2p_mapping.get(language)
|
| 59 |
+
if phonemizer is None:
|
| 60 |
+
raise ValueError(f"Unsupported language: {language}")
|
| 61 |
+
|
| 62 |
text = phonemizer(text)
|
| 63 |
+
text = torch.tensor(
|
| 64 |
+
intersperse(cleaned_text_to_sequence(text), item=0),
|
| 65 |
+
dtype=torch.long, device=device
|
| 66 |
+
).unsqueeze(0)
|
| 67 |
text_length = torch.tensor([text.size(-1)], dtype=torch.long, device=device)
|
| 68 |
+
|
| 69 |
ref_audio = load_and_resample_audio(ref_audio, self.mel_config.sample_rate).to(device)
|
| 70 |
ref_audio = self.mel_extractor(ref_audio)
|
| 71 |
+
|
| 72 |
+
mel_output = self.tts_model.synthesise(
|
| 73 |
+
text, text_length, step, temperature, ref_audio,
|
| 74 |
+
length_scale, solver, cfg
|
| 75 |
+
)['decoder_outputs']
|
| 76 |
audio_output = self.vocoder_model(mel_output)
|
| 77 |
return audio_output.cpu(), mel_output.cpu()
|
| 78 |
+
|
| 79 |
def get_params(self):
|
| 80 |
tts_param = sum(p.numel() for p in self.tts_model.parameters()) / 1e6
|
| 81 |
vocoder_param = sum(p.numel() for p in self.vocoder_model.parameters()) / 1e6
|
| 82 |
return tts_param, vocoder_param
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|