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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
import os
|
| 3 |
+
import re
|
| 4 |
+
import argparse
|
| 5 |
+
import utils
|
| 6 |
+
import commons
|
| 7 |
+
import json
|
| 8 |
+
import torch
|
| 9 |
+
import gradio as gr
|
| 10 |
+
from models import SynthesizerTrn
|
| 11 |
+
from text import text_to_sequence, _clean_text
|
| 12 |
+
from torch import no_grad, LongTensor
|
| 13 |
+
from gradio_client import utils as client_utils
|
| 14 |
+
import logging
|
| 15 |
+
logging.getLogger('numba').setLevel(logging.WARNING)
|
| 16 |
+
limitation = os.getenv("SYSTEM") == "spaces" # limit text and audio length in huggingface spaces
|
| 17 |
+
|
| 18 |
+
hps_ms = utils.get_hparams_from_file(r'config/config.json')
|
| 19 |
+
|
| 20 |
+
audio_postprocess_ori = gr.Audio.postprocess
|
| 21 |
+
|
| 22 |
+
def audio_postprocess(self, y):
|
| 23 |
+
data = audio_postprocess_ori(self, y)
|
| 24 |
+
if data is None:
|
| 25 |
+
return None
|
| 26 |
+
return client_utils.encode_url_or_file_to_base64(data["name"])
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
gr.Audio.postprocess = audio_postprocess
|
| 30 |
+
|
| 31 |
+
def get_text(text, hps, is_symbol):
|
| 32 |
+
text_norm, clean_text = text_to_sequence(text, hps.symbols, [] if is_symbol else hps.data.text_cleaners)
|
| 33 |
+
if hps.data.add_blank:
|
| 34 |
+
text_norm = commons.intersperse(text_norm, 0)
|
| 35 |
+
text_norm = LongTensor(text_norm)
|
| 36 |
+
return text_norm, clean_text
|
| 37 |
+
|
| 38 |
+
def create_tts_fn(net_g_ms, speaker_id):
|
| 39 |
+
def tts_fn(text, language, noise_scale, noise_scale_w, length_scale, is_symbol):
|
| 40 |
+
text = text.replace('\n', ' ').replace('\r', '').replace(" ", "")
|
| 41 |
+
if limitation:
|
| 42 |
+
text_len = len(re.sub("\[([A-Z]{2})\]", "", text))
|
| 43 |
+
max_len = 100
|
| 44 |
+
if is_symbol:
|
| 45 |
+
max_len *= 3
|
| 46 |
+
if text_len > max_len:
|
| 47 |
+
return "Error: Text is too long", None
|
| 48 |
+
if not is_symbol:
|
| 49 |
+
if language == 0:
|
| 50 |
+
text = f"[ZH]{text}[ZH]"
|
| 51 |
+
elif language == 1:
|
| 52 |
+
text = f"[JA]{text}[JA]"
|
| 53 |
+
else:
|
| 54 |
+
text = f"{text}"
|
| 55 |
+
stn_tst, clean_text = get_text(text, hps_ms, is_symbol)
|
| 56 |
+
with no_grad():
|
| 57 |
+
x_tst = stn_tst.unsqueeze(0).to(device)
|
| 58 |
+
x_tst_lengths = LongTensor([stn_tst.size(0)]).to(device)
|
| 59 |
+
sid = LongTensor([speaker_id]).to(device)
|
| 60 |
+
audio = net_g_ms.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=noise_scale, noise_scale_w=noise_scale_w,
|
| 61 |
+
length_scale=length_scale)[0][0, 0].data.cpu().float().numpy()
|
| 62 |
+
|
| 63 |
+
return "Success", (22050, audio)
|
| 64 |
+
return tts_fn
|
| 65 |
+
|
| 66 |
+
def create_to_symbol_fn(hps):
|
| 67 |
+
def to_symbol_fn(is_symbol_input, input_text, temp_lang):
|
| 68 |
+
if temp_lang == 0:
|
| 69 |
+
clean_text = f'[ZH]{input_text}[ZH]'
|
| 70 |
+
elif temp_lang == 1:
|
| 71 |
+
clean_text = f'[JA]{input_text}[JA]'
|
| 72 |
+
else:
|
| 73 |
+
clean_text = input_text
|
| 74 |
+
return _clean_text(clean_text, hps.data.text_cleaners) if is_symbol_input else ''
|
| 75 |
+
|
| 76 |
+
return to_symbol_fn
|
| 77 |
+
def change_lang(language):
|
| 78 |
+
if language == 0:
|
| 79 |
+
return 0.6, 0.668, 1.2
|
| 80 |
+
elif language == 1:
|
| 81 |
+
return 0.6, 0.668, 1
|
| 82 |
+
else:
|
| 83 |
+
return 0.6, 0.668, 1
|
| 84 |
+
|
| 85 |
+
download_audio_js = """
|
| 86 |
+
() =>{{
|
| 87 |
+
let root = document.querySelector("body > gradio-app");
|
| 88 |
+
if (root.shadowRoot != null)
|
| 89 |
+
root = root.shadowRoot;
|
| 90 |
+
let audio = root.querySelector("#tts-audio-{audio_id}").querySelector("audio");
|
| 91 |
+
let text = root.querySelector("#input-text-{audio_id}").querySelector("textarea");
|
| 92 |
+
if (audio == undefined)
|
| 93 |
+
return;
|
| 94 |
+
text = text.value;
|
| 95 |
+
if (text == undefined)
|
| 96 |
+
text = Math.floor(Math.random()*100000000);
|
| 97 |
+
audio = audio.src;
|
| 98 |
+
let oA = document.createElement("a");
|
| 99 |
+
oA.download = text.substr(0, 20)+'.wav';
|
| 100 |
+
oA.href = audio;
|
| 101 |
+
document.body.appendChild(oA);
|
| 102 |
+
oA.click();
|
| 103 |
+
oA.remove();
|
| 104 |
+
}}
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
+
if __name__ == '__main__':
|
| 108 |
+
parser = argparse.ArgumentParser()
|
| 109 |
+
parser.add_argument('--device', type=str, default='cpu')
|
| 110 |
+
parser.add_argument('--api', action="store_true", default=False)
|
| 111 |
+
parser.add_argument("--share", action="store_true", default=False, help="share gradio app")
|
| 112 |
+
parser.add_argument("--all", action="store_true", default=False, help="enable all models")
|
| 113 |
+
args = parser.parse_args()
|
| 114 |
+
device = torch.device(args.device)
|
| 115 |
+
categories = ["Honkai: Star Rail", "Blue Archive", "Lycoris Recoil"]
|
| 116 |
+
others = {
|
| 117 |
+
"Princess Connect! Re:Dive": "https://huggingface.co/spaces/sayashi/vits-models-pcr",
|
| 118 |
+
"Genshin Impact": "https://huggingface.co/spaces/sayashi/vits-models-genshin-bh3",
|
| 119 |
+
"Honkai Impact 3rd": "https://huggingface.co/spaces/sayashi/vits-models-genshin-bh3",
|
| 120 |
+
"Overwatch 2": "https://huggingface.co/spaces/sayashi/vits-models-ow2",
|
| 121 |
+
}
|
| 122 |
+
if args.all:
|
| 123 |
+
categories = ["Honkai: Star Rail", "Blue Archive", "Lycoris Recoil", "Princess Connect! Re:Dive", "Genshin Impact", "Honkai Impact 3rd", "Overwatch 2"]
|
| 124 |
+
others = {}
|
| 125 |
+
models = []
|
| 126 |
+
with open("pretrained_models/info.json", "r", encoding="utf-8") as f:
|
| 127 |
+
models_info = json.load(f)
|
| 128 |
+
for i, info in models_info.items():
|
| 129 |
+
if info['title'].split("-")[0] not in categories or not info['enable']:
|
| 130 |
+
continue
|
| 131 |
+
sid = info['sid']
|
| 132 |
+
name_en = info['name_en']
|
| 133 |
+
name_zh = info['name_zh']
|
| 134 |
+
title = info['title']
|
| 135 |
+
cover = f"pretrained_models/{i}/{info['cover']}"
|
| 136 |
+
example = info['example']
|
| 137 |
+
language = info['language']
|
| 138 |
+
net_g_ms = SynthesizerTrn(
|
| 139 |
+
len(hps_ms.symbols),
|
| 140 |
+
hps_ms.data.filter_length // 2 + 1,
|
| 141 |
+
hps_ms.train.segment_size // hps_ms.data.hop_length,
|
| 142 |
+
n_speakers=hps_ms.data.n_speakers if info['type'] == "multi" else 0,
|
| 143 |
+
**hps_ms.model)
|
| 144 |
+
utils.load_checkpoint(f'pretrained_models/{i}/{i}.pth', net_g_ms, None)
|
| 145 |
+
_ = net_g_ms.eval().to(device)
|
| 146 |
+
models.append((sid, name_en, name_zh, title, cover, example, language, net_g_ms, create_tts_fn(net_g_ms, sid), create_to_symbol_fn(hps_ms)))
|
| 147 |
+
with gr.Blocks() as app:
|
| 148 |
+
gr.Markdown(
|
| 149 |
+
"# <center> vits-models\n"
|
| 150 |
+
"## <center> Please do not generate content that could infringe upon the rights or cause harm to individuals or organizations.\n"
|
| 151 |
+
"## <center> 请不要生成会对个人以及组织造成侵害的内容\n\n"
|
| 152 |
+
"[](https://colab.research.google.com/drive/10QOk9NPgoKZUXkIhhuVaZ7SYra1MPMKH?usp=share_link)\n\n"
|
| 153 |
+
"[](https://huggingface.co/spaces/sayashi/vits-models?duplicate=true)\n\n"
|
| 154 |
+
"[](https://github.com/SayaSS/vits-finetuning)"
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
with gr.Tabs():
|
| 158 |
+
for category in categories:
|
| 159 |
+
with gr.TabItem(category):
|
| 160 |
+
with gr.TabItem("EN"):
|
| 161 |
+
for (sid, name_en, name_zh, title, cover, example, language, net_g_ms, tts_fn, to_symbol_fn) in models:
|
| 162 |
+
if title.split("-")[0] != category:
|
| 163 |
+
continue
|
| 164 |
+
with gr.TabItem(name_en):
|
| 165 |
+
with gr.Row():
|
| 166 |
+
gr.Markdown(
|
| 167 |
+
'<div align="center">'
|
| 168 |
+
f'<a><strong>{title}</strong></a>'
|
| 169 |
+
f'<img style="width:auto;height:300px;" src="file/{cover}">' if cover else ""
|
| 170 |
+
'</div>'
|
| 171 |
+
)
|
| 172 |
+
with gr.Row():
|
| 173 |
+
with gr.Column():
|
| 174 |
+
input_text = gr.Textbox(label="Text (100 words limitation)" if limitation else "Text", lines=5, value=example, elem_id=f"input-text-en-{name_en.replace(' ','')}")
|
| 175 |
+
lang = gr.Dropdown(label="Language", choices=["Chinese", "Japanese", "Mix(wrap the Chinese text with [ZH][ZH], wrap the Japanese text with [JA][JA])"],
|
| 176 |
+
type="index", value=language)
|
| 177 |
+
with gr.Accordion(label="Advanced Options", open=False):
|
| 178 |
+
symbol_input = gr.Checkbox(value=False, label="Symbol input")
|
| 179 |
+
symbol_list = gr.Dataset(label="Symbol list", components=[input_text],
|
| 180 |
+
samples=[[x] for x in hps_ms.symbols])
|
| 181 |
+
symbol_list_json = gr.Json(value=hps_ms.symbols, visible=False)
|
| 182 |
+
btn = gr.Button(value="Generate", variant="primary")
|
| 183 |
+
with gr.Row():
|
| 184 |
+
ns = gr.Slider(label="noise_scale", minimum=0.1, maximum=1.0, step=0.1, value=0.6, interactive=True)
|
| 185 |
+
nsw = gr.Slider(label="noise_scale_w", minimum=0.1, maximum=1.0, step=0.1, value=0.668, interactive=True)
|
| 186 |
+
ls = gr.Slider(label="length_scale", minimum=0.1, maximum=2.0, step=0.1, value=1.2 if language=="Chinese" else 1, interactive=True)
|
| 187 |
+
with gr.Column():
|
| 188 |
+
o1 = gr.Textbox(label="Output Message")
|
| 189 |
+
o2 = gr.Audio(label="Output Audio", elem_id=f"tts-audio-en-{name_en.replace(' ','')}")
|
| 190 |
+
download = gr.Button("Download Audio")
|
| 191 |
+
btn.click(tts_fn, inputs=[input_text, lang, ns, nsw, ls, symbol_input], outputs=[o1, o2], api_name=f"tts-{name_en}")
|
| 192 |
+
download.click(None, [], [], _js=download_audio_js.format(audio_id=f"en-{name_en.replace(' ', '')}"))
|
| 193 |
+
lang.change(change_lang, inputs=[lang], outputs=[ns, nsw, ls])
|
| 194 |
+
symbol_input.change(
|
| 195 |
+
to_symbol_fn,
|
| 196 |
+
[symbol_input, input_text, lang],
|
| 197 |
+
[input_text]
|
| 198 |
+
)
|
| 199 |
+
symbol_list.click(None, [symbol_list, symbol_list_json], [input_text],
|
| 200 |
+
_js=f"""
|
| 201 |
+
(i,symbols) => {{
|
| 202 |
+
let root = document.querySelector("body > gradio-app");
|
| 203 |
+
if (root.shadowRoot != null)
|
| 204 |
+
root = root.shadowRoot;
|
| 205 |
+
let text_input = root.querySelector("#input-text-en-{name_en.replace(' ', '')}").querySelector("textarea");
|
| 206 |
+
let startPos = text_input.selectionStart;
|
| 207 |
+
let endPos = text_input.selectionEnd;
|
| 208 |
+
let oldTxt = text_input.value;
|
| 209 |
+
let result = oldTxt.substring(0, startPos) + symbols[i] + oldTxt.substring(endPos);
|
| 210 |
+
text_input.value = result;
|
| 211 |
+
let x = window.scrollX, y = window.scrollY;
|
| 212 |
+
text_input.focus();
|
| 213 |
+
text_input.selectionStart = startPos + symbols[i].length;
|
| 214 |
+
text_input.selectionEnd = startPos + symbols[i].length;
|
| 215 |
+
text_input.blur();
|
| 216 |
+
window.scrollTo(x, y);
|
| 217 |
+
return text_input.value;
|
| 218 |
+
}}""")
|
| 219 |
+
with gr.TabItem("中文"):
|
| 220 |
+
for (sid, name_en, name_zh, title, cover, example, language, net_g_ms, tts_fn, to_symbol_fn) in models:
|
| 221 |
+
if title.split("-")[0] != category:
|
| 222 |
+
continue
|
| 223 |
+
with gr.TabItem(name_zh):
|
| 224 |
+
with gr.Row():
|
| 225 |
+
gr.Markdown(
|
| 226 |
+
'<div align="center">'
|
| 227 |
+
f'<a><strong>{title}</strong></a>'
|
| 228 |
+
f'<img style="width:auto;height:300px;" src="file/{cover}">' if cover else ""
|
| 229 |
+
'</div>'
|
| 230 |
+
)
|
| 231 |
+
with gr.Row():
|
| 232 |
+
with gr.Column():
|
| 233 |
+
input_text = gr.Textbox(label="文本 (100字上限)" if limitation else "文本", lines=5, value=example, elem_id=f"input-text-zh-{name_zh}")
|
| 234 |
+
lang = gr.Dropdown(label="语言", choices=["中文", "日语", "中日混合(中文用[ZH][ZH]包裹起来,日文用[JA][JA]包裹起来)"],
|
| 235 |
+
type="index", value="中文"if language == "Chinese" else "日语")
|
| 236 |
+
with gr.Accordion(label="高级选项", open=False):
|
| 237 |
+
symbol_input = gr.Checkbox(value=False, label="符号输入")
|
| 238 |
+
symbol_list = gr.Dataset(label="符号列表", components=[input_text],
|
| 239 |
+
samples=[[x] for x in hps_ms.symbols])
|
| 240 |
+
symbol_list_json = gr.Json(value=hps_ms.symbols, visible=False)
|
| 241 |
+
btn = gr.Button(value="生成", variant="primary")
|
| 242 |
+
with gr.Row():
|
| 243 |
+
ns = gr.Slider(label="控制感情变化程度", minimum=0.1, maximum=1.0, step=0.1, value=0.6, interactive=True)
|
| 244 |
+
nsw = gr.Slider(label="控制音素发音长度", minimum=0.1, maximum=1.0, step=0.1, value=0.668, interactive=True)
|
| 245 |
+
ls = gr.Slider(label="控制整体语速", minimum=0.1, maximum=2.0, step=0.1, value=1.2 if language=="Chinese" else 1, interactive=True)
|
| 246 |
+
with gr.Column():
|
| 247 |
+
o1 = gr.Textbox(label="输出信息")
|
| 248 |
+
o2 = gr.Audio(label="输出音频", elem_id=f"tts-audio-zh-{name_zh}")
|
| 249 |
+
download = gr.Button("下载音频")
|
| 250 |
+
btn.click(tts_fn, inputs=[input_text, lang, ns, nsw, ls, symbol_input], outputs=[o1, o2])
|
| 251 |
+
download.click(None, [], [], _js=download_audio_js.format(audio_id=f"zh-{name_zh}"))
|
| 252 |
+
lang.change(change_lang, inputs=[lang], outputs=[ns, nsw, ls])
|
| 253 |
+
symbol_input.change(
|
| 254 |
+
to_symbol_fn,
|
| 255 |
+
[symbol_input, input_text, lang],
|
| 256 |
+
[input_text]
|
| 257 |
+
)
|
| 258 |
+
symbol_list.click(None, [symbol_list, symbol_list_json], [input_text],
|
| 259 |
+
_js=f"""
|
| 260 |
+
(i,symbols) => {{
|
| 261 |
+
let root = document.querySelector("body > gradio-app");
|
| 262 |
+
if (root.shadowRoot != null)
|
| 263 |
+
root = root.shadowRoot;
|
| 264 |
+
let text_input = root.querySelector("#input-text-zh-{name_zh}").querySelector("textarea");
|
| 265 |
+
let startPos = text_input.selectionStart;
|
| 266 |
+
let endPos = text_input.selectionEnd;
|
| 267 |
+
let oldTxt = text_input.value;
|
| 268 |
+
let result = oldTxt.substring(0, startPos) + symbols[i] + oldTxt.substring(endPos);
|
| 269 |
+
text_input.value = result;
|
| 270 |
+
let x = window.scrollX, y = window.scrollY;
|
| 271 |
+
text_input.focus();
|
| 272 |
+
text_input.selectionStart = startPos + symbols[i].length;
|
| 273 |
+
text_input.selectionEnd = startPos + symbols[i].length;
|
| 274 |
+
text_input.blur();
|
| 275 |
+
window.scrollTo(x, y);
|
| 276 |
+
return text_input.value;
|
| 277 |
+
}}""")
|
| 278 |
+
for category, link in others.items():
|
| 279 |
+
with gr.TabItem(category):
|
| 280 |
+
gr.Markdown(
|
| 281 |
+
f'''
|
| 282 |
+
<center>
|
| 283 |
+
<h2>Click to Go</h2>
|
| 284 |
+
<a href="{link}">
|
| 285 |
+
<img src="https://huggingface.co/datasets/huggingface/badges/raw/main/open-in-hf-spaces-xl-dark.svg"
|
| 286 |
+
</a>
|
| 287 |
+
</center>
|
| 288 |
+
'''
|
| 289 |
+
)
|
| 290 |
+
app.queue(concurrency_count=1, api_open=args.api).launch(share=args.share)
|