Spaces:
Configuration error
Configuration error
void
commited on
Commit
•
3c3eb10
1
Parent(s):
163d304
make vits a tts module
Browse files- vits_tts/__init__.py +0 -0
- vits_tts/tts.py +123 -0
vits_tts/__init__.py
ADDED
File without changes
|
vits_tts/tts.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
import sys
|
3 |
+
import os
|
4 |
+
run_dir = os.path.dirname(os.path.realpath(__file__))
|
5 |
+
sys.path.append(run_dir)
|
6 |
+
import re
|
7 |
+
import argparse
|
8 |
+
import utils
|
9 |
+
import commons
|
10 |
+
import json
|
11 |
+
import torch
|
12 |
+
from models import SynthesizerTrn
|
13 |
+
from text import text_to_sequence, _clean_text
|
14 |
+
from torch import no_grad, LongTensor
|
15 |
+
import logging
|
16 |
+
logging.getLogger('numba').setLevel(logging.WARNING)
|
17 |
+
limitation = os.getenv("SYSTEM") == "spaces" # limit text and audio length in huggingface spaces
|
18 |
+
|
19 |
+
import scipy
|
20 |
+
|
21 |
+
hps_ms = utils.get_hparams_from_file(f'{run_dir}/config/config.json')
|
22 |
+
|
23 |
+
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
24 |
+
|
25 |
+
tts_fn = None
|
26 |
+
voice_opt = (0.6, 0.668, 1)
|
27 |
+
|
28 |
+
def get_text(text, hps, is_symbol):
|
29 |
+
text_norm, clean_text = text_to_sequence(text, hps.symbols, [] if is_symbol else hps.data.text_cleaners)
|
30 |
+
if hps.data.add_blank:
|
31 |
+
text_norm = commons.intersperse(text_norm, 0)
|
32 |
+
text_norm = LongTensor(text_norm)
|
33 |
+
return text_norm, clean_text
|
34 |
+
|
35 |
+
def create_tts_fn(net_g_ms, speaker_id):
|
36 |
+
def tts_fn(text, language, noise_scale, noise_scale_w, length_scale, is_symbol):
|
37 |
+
text = text.replace('\n', ' ').replace('\r', '').replace(" ", "")
|
38 |
+
if limitation:
|
39 |
+
text_len = len(re.sub("\[([A-Z]{2})\]", "", text))
|
40 |
+
max_len = 100
|
41 |
+
if is_symbol:
|
42 |
+
max_len *= 3
|
43 |
+
if text_len > max_len:
|
44 |
+
return "Error: Text is too long", None
|
45 |
+
if not is_symbol:
|
46 |
+
if language == 0:
|
47 |
+
text = f"[ZH]{text}[ZH]"
|
48 |
+
elif language == 1:
|
49 |
+
text = f"[JA]{text}[JA]"
|
50 |
+
else:
|
51 |
+
text = f"{text}"
|
52 |
+
stn_tst, clean_text = get_text(text, hps_ms, is_symbol)
|
53 |
+
with no_grad():
|
54 |
+
x_tst = stn_tst.unsqueeze(0).to(device)
|
55 |
+
x_tst_lengths = LongTensor([stn_tst.size(0)]).to(device)
|
56 |
+
sid = LongTensor([speaker_id]).to(device)
|
57 |
+
audio = net_g_ms.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=noise_scale, noise_scale_w=noise_scale_w,
|
58 |
+
length_scale=length_scale)[0][0, 0].data.cpu().float().numpy()
|
59 |
+
|
60 |
+
return "Success", (22050, audio)
|
61 |
+
return tts_fn
|
62 |
+
|
63 |
+
def create_to_symbol_fn(hps):
|
64 |
+
def to_symbol_fn(is_symbol_input, input_text, temp_lang):
|
65 |
+
if temp_lang == 0:
|
66 |
+
clean_text = f'[ZH]{input_text}[ZH]'
|
67 |
+
elif temp_lang == 1:
|
68 |
+
clean_text = f'[JA]{input_text}[JA]'
|
69 |
+
else:
|
70 |
+
clean_text = input_text
|
71 |
+
return _clean_text(clean_text, hps.data.text_cleaners) if is_symbol_input else ''
|
72 |
+
|
73 |
+
return to_symbol_fn
|
74 |
+
|
75 |
+
def _LoadCharacter(name):
|
76 |
+
with open(f"{run_dir}/pretrained_models/info.json", "r", encoding="utf-8") as f:
|
77 |
+
models_info = json.load(f)
|
78 |
+
for i, info in models_info.items():
|
79 |
+
sid = info['sid']
|
80 |
+
name_en = info['name_en']
|
81 |
+
name_zh = info['name_zh']
|
82 |
+
title = info['title']
|
83 |
+
cover = f"{run_dir}/pretrained_models/{i}/{info['cover']}"
|
84 |
+
example = info['example']
|
85 |
+
language = info['language']
|
86 |
+
if name == 'Any' or name == name_zh or name == name_en:
|
87 |
+
net_g_ms = SynthesizerTrn(
|
88 |
+
len(hps_ms.symbols),
|
89 |
+
hps_ms.data.filter_length // 2 + 1,
|
90 |
+
hps_ms.train.segment_size // hps_ms.data.hop_length,
|
91 |
+
n_speakers=hps_ms.data.n_speakers if info['type'] == "multi" else 0,
|
92 |
+
**hps_ms.model)
|
93 |
+
utils.load_checkpoint(f'{run_dir}/pretrained_models/{i}/{i}.pth', net_g_ms, None)
|
94 |
+
_ = net_g_ms.eval().to(device)
|
95 |
+
tts_fn = create_tts_fn(net_g_ms, sid)
|
96 |
+
to_symbol_fn = create_to_symbol_fn(hps_ms)
|
97 |
+
return True, tts_fn
|
98 |
+
return False, None
|
99 |
+
|
100 |
+
def LoadCharacter(name):
|
101 |
+
global tts_fn
|
102 |
+
_, tts_fn = _LoadCharacter(name)
|
103 |
+
|
104 |
+
def SetVoiceOption(ns, nsw, ls):
|
105 |
+
global voice_opt
|
106 |
+
voice_opt = (ns, nsw, ls)
|
107 |
+
|
108 |
+
LoadCharacter("Any")
|
109 |
+
|
110 |
+
def GenerateTTS(text):
|
111 |
+
if tts_fn != None and voice_opt != None:
|
112 |
+
(ns, nsw, ls) = voice_opt
|
113 |
+
symbol_input = False
|
114 |
+
result, (sampling_rate, output) = tts_fn(text, 0, ns, nsw, ls, symbol_input)
|
115 |
+
if result == "Success":
|
116 |
+
save_path = f"{run_dir}/output.wav"
|
117 |
+
scipy.io.wavfile.write(save_path, rate=sampling_rate, data=output.T)
|
118 |
+
return True, save_path
|
119 |
+
else:
|
120 |
+
print(f'TTS: {result}')
|
121 |
+
return False, None
|
122 |
+
|
123 |
+
__all__ = ['LoadCharacter', 'SetVoiceOption', 'GenerateTTS']
|