Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from pypinyin import lazy_pinyin, Style
|
3 |
+
import torch
|
4 |
+
from ttts.utils.infer_utils import load_model
|
5 |
+
from ttts.vocoder.feature_extractors import MelSpectrogramFeatures
|
6 |
+
import torchaudio
|
7 |
+
MODELS = {
|
8 |
+
'vqvae.pth':'./TTTS/vae-30.pt',
|
9 |
+
'gpt.pth': './TTTS/gpt-70.pt',
|
10 |
+
'clvp2.pth': '',
|
11 |
+
'diffusion.pth': './TTTS/diffusion-855.pt',
|
12 |
+
'vocoder.pth': './ttts/pretrained_models/pytorch_model.bin',
|
13 |
+
'rlg_auto.pth': '',
|
14 |
+
'rlg_diffuser.pth': '',
|
15 |
+
}
|
16 |
+
from ttts.gpt.voice_tokenizer import VoiceBpeTokenizer
|
17 |
+
import torch.nn.functional as F
|
18 |
+
auto_conditioning = cond_mel
|
19 |
+
settings = {'temperature': .8, 'length_penalty': 1.0, 'repetition_penalty': 2.0,
|
20 |
+
'top_p': .8,
|
21 |
+
'cond_free_k': 2.0, 'diffusion_temperature': 1.0}
|
22 |
+
top_p = .8
|
23 |
+
temperature = .8
|
24 |
+
autoregressive_batch_size = 1
|
25 |
+
length_penalty = 1.0
|
26 |
+
repetition_penalty = 2.0
|
27 |
+
max_mel_tokens = 600
|
28 |
+
from vocos import Vocos
|
29 |
+
from ttts.diffusion.train import do_spectrogram_diffusion
|
30 |
+
from ttts.utils.diffusion import SpacedDiffusion, space_timesteps, get_named_beta_schedule
|
31 |
+
from ttts.diffusion.aa_model import denormalize_tacotron_mel, normalize_tacotron_mel
|
32 |
+
# print(device)
|
33 |
+
|
34 |
+
vocos = Vocos.from_pretrained("charactr/vocos-mel-24khz")
|
35 |
+
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
|
36 |
+
|
37 |
+
def speak(text):
|
38 |
+
pinyin = ' '.join(lazy_pinyin(text, style=Style.TONE3, neutral_tone_with_five=True))
|
39 |
+
tokenizer = VoiceBpeTokenizer('ttts/gpt/gpt_tts_tokenizer.json')
|
40 |
+
text_tokens = torch.IntTensor(tokenizer.encode(pinyin)).unsqueeze(0).to(device)
|
41 |
+
text_tokens = F.pad(text_tokens, (0, 1)) # This may not be necessary.
|
42 |
+
text_tokens = text_tokens.to(device)
|
43 |
+
print(pinyin)
|
44 |
+
print(text_tokens)
|
45 |
+
gpt = load_model('gpt',MODELS['gpt.pth'], './ttts/gpt/config.json',device)
|
46 |
+
gpt.post_init_gpt2_config(use_deepspeed=False, kv_cache=False, half=False)
|
47 |
+
cond_audio = 'ttts/3.wav'
|
48 |
+
audio,sr = torchaudio.load(cond_audio)
|
49 |
+
if audio.shape[0]>1:
|
50 |
+
audio = audio[0].unsqueeze(0)
|
51 |
+
audio = torchaudio.transforms.Resample(sr, 24000)(audio)
|
52 |
+
cond_mel = MelSpectrogramFeatures()(audio).to(device)
|
53 |
+
print(cond_mel.shape)
|
54 |
+
codes = gpt.inference_speech(auto_conditioning, text_tokens,
|
55 |
+
do_sample=True,
|
56 |
+
top_p=top_p,
|
57 |
+
temperature=temperature,
|
58 |
+
num_return_sequences=autoregressive_batch_size,
|
59 |
+
length_penalty=length_penalty,
|
60 |
+
repetition_penalty=repetition_penalty,
|
61 |
+
max_generate_length=max_mel_tokens)
|
62 |
+
latent = gpt(auto_conditioning, text_tokens,
|
63 |
+
torch.tensor([text_tokens.shape[-1]], device=text_tokens.device), codes,
|
64 |
+
torch.tensor([codes.shape[-1]*gpt.mel_length_compression], device=text_tokens.device),
|
65 |
+
return_latent=True, clip_inputs=False).transpose(1,2)
|
66 |
+
diffusion = load_model('diffusion',MODELS['diffusion.pth'],'./ttts/diffusion/config.yaml',device)
|
67 |
+
diffuser = SpacedDiffusion(use_timesteps=space_timesteps(1000, [50]), model_mean_type='epsilon',
|
68 |
+
model_var_type='learned_range', loss_type='mse', betas=get_named_beta_schedule('linear', 1000),
|
69 |
+
conditioning_free=True, conditioning_free_k=2., sampler='dpm++2m')
|
70 |
+
diffusion_conditioning = normalize_tacotron_mel(cond_mel)
|
71 |
+
mel = do_spectrogram_diffusion(diffusion, diffuser, latent, diffusion_conditioning, temperature=1.0).detach().cpu()
|
72 |
+
wav = vocos.decode(mel)
|
73 |
+
return (24000, wav.detach().cpu())
|
74 |
+
|
75 |
+
with gr.Blocks() as demo:
|
76 |
+
gr.Markdown('# TTTS\n\nA demo of [TTTS](https://github.com/adelacvg/ttts) based on XTTS. TTTS only supports Chinese.')
|
77 |
+
gr.Textbox(label="Text to say", interactive=True, value="大家好,今天来点大家想看的东西。")
|