Spaces:
Running
Running
File size: 13,044 Bytes
570c8ab e85d807 570c8ab 49ef91b e7fbb2d 570c8ab f9d0d4d 570c8ab f9d0d4d 570c8ab 11efcdb 570c8ab e85d807 570c8ab e85d807 570c8ab e85d807 570c8ab be81ba7 570c8ab be81ba7 11efcdb be81ba7 11efcdb e85d807 570c8ab f9d0d4d 570c8ab 11efcdb 570c8ab 49b5b9a 570c8ab f7d072b 570c8ab 49b5b9a 3bfb981 f7d072b 0bbf048 f7d072b 570c8ab 49b5b9a 570c8ab f9d0d4d 570c8ab f9d0d4d 570c8ab fb043b2 570c8ab fb043b2 570c8ab 49b5b9a 570c8ab 49b5b9a f9d0d4d 570c8ab f9d0d4d 570c8ab 49b5b9a f7d072b 570c8ab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
import os
import sys
import importlib
from subprocess import call
from pathlib import Path
import json
import math
import matplotlib.pyplot as plt
import torch
from torch import nn
from torch.nn import functional as F
from torch.utils.data import DataLoader
from scipy.io.wavfile import write
import gradio as gr
import scipy.io.wavfile
import numpy as np
from libs.audio import wav2ogg, float2pcm
def run_cmd(command):
try:
# print(command)
call(command, shell=True)
except KeyboardInterrupt:
print("Process interrupted")
sys.exit(1)
current = os.getcwd()
full = current + "/vits/monotonic_align"
os.chdir(full)
run_cmd("python3 setup.py build_ext --inplace")
run_cmd("mv vits/monotonic_align/* ./")
run_cmd("rm -rf vits")
# run_cmd(f"mv {current}/lfs/*.pth {current}/pretrained/")
# run_cmd("apt-get install espeak -y")
# run_cmd("gdown 'https://drive.google.com/uc?id=1q86w74Ygw2hNzYP9cWkeClGT5X25PvBT'")
os.chdir("../..")
from lojban.lojban import lojban2ipa
sys.path.insert(0, './vits')
import vits.commons as commons
import vits.utils as utils
from vits.data_utils import TextAudioLoader, TextAudioCollate, TextAudioSpeakerLoader, TextAudioSpeakerCollate
from vits.models import SynthesizerTrn
from vits.text.symbols import symbols
from vits.text import _clean_text
from vits.text import cleaners
from vits.text.symbols import symbols
sys.path.insert(0, './nix_tts_simple')
from nix_tts_simple.tts import generate_voice
language_id_lookup = {
"Lojban": "jbo",
"Transcription": "ipa",
"English": "en",
"German": "de",
"Greek": "el",
"Spanish": "es",
"Finnish": "fi",
"Russian": "ru",
"Hungarian": "hu",
"Dutch": "nl",
"French": "fr",
'Polish': "pl",
'Portuguese': "pt",
'Italian': "it",
}
# def download_pretrained():
# if not all(Path(file).exists() for file in ["pretrained_ljs.pth", "pretrained_vctk.pth"]):
# url = "https://drive.google.com/uc?id=1q86w74Ygw2hNzYP9cWkeClGT5X25PvBT"
# gdown.download_folder(url, quiet=True, use_cookies=False)
# Mappings from symbol to numeric ID and vice versa:
_symbol_to_id = {s: i for i, s in enumerate(symbols)}
_id_to_symbol = {i: s for i, s in enumerate(symbols)}
def text_to_sequence(text, language, cleaner_names, mode):
'''Converts a string of text to a sequence of IDs corresponding to the symbols in the text.
Args:
text: string to convert to a sequence
cleaner_names: names of the cleaner functions to run the text through
Returns:
List of integers corresponding to the symbols in the text
'''
sequence = []
if language == 'jbo':
clean_text = lojban2ipa(text, mode)
elif language == 'ipa':
clean_text = text
else:
clean_text = _clean_text(text, cleaner_names)
for symbol in clean_text:
symbol_id = _symbol_to_id[symbol]
sequence += [symbol_id]
return clean_text, sequence
def get_text(text, language, hps, mode):
ipa_text, text_norm = text_to_sequence(
text, language, hps.data.text_cleaners, mode)
if hps.data.add_blank:
text_norm = commons.intersperse(text_norm, 0)
text_norm_tensor = torch.LongTensor(text_norm)
return ipa_text, text_norm_tensor
def load_checkpoints():
hps = utils.get_hparams_from_file(current + "/vits/configs/ljs_base.json")
model = SynthesizerTrn(
len(symbols),
hps.data.filter_length // 2 + 1,
hps.train.segment_size // hps.data.hop_length,
**hps.model)
_ = model.eval()
_ = utils.load_checkpoint(current + "/pretrained/vits/pretrained_ljs.pth", model, None)
hps_vctk = utils.get_hparams_from_file(current + "/vits/configs/vctk_base.json")
net_g_vctk = SynthesizerTrn(
len(symbols),
hps_vctk.data.filter_length // 2 + 1,
hps_vctk.train.segment_size // hps_vctk.data.hop_length,
n_speakers=hps_vctk.data.n_speakers,
**hps_vctk.model)
_ = model.eval()
_ = utils.load_checkpoint(current + "/pretrained/vits/pretrained_vctk.pth", net_g_vctk, None)
return model, hps, net_g_vctk, hps_vctk
def inference(text, language, noise_scale, noise_scale_w, length_scale, voice, file_format):
if len(text.strip())==0:
return []
language = language.split()[0]
language = language_id_lookup[language] if bool(
language_id_lookup[language]) else "jbo"
result = []
if voice == 'Nix-Deterministic' and language == 'jbo':
result = generate_voice(lojban2ipa(text,'nix'), current+"/pretrained/nix-tts/nix-ljspeech-v0.1")
elif voice == 'Nix-Stochastic' and language == 'jbo':
result = generate_voice(lojban2ipa(text,'nix'), current+"/pretrained/nix-tts/nix-ljspeech-sdp-v0.1")
elif voice == 'LJS':
ipa_text, stn_tst = get_text(text, language, hps, mode="VITS")
with torch.no_grad():
x_tst = stn_tst.unsqueeze(0)
x_tst_lengths = torch.LongTensor([stn_tst.size(0)])
audio = model.infer(x_tst, x_tst_lengths, noise_scale=noise_scale,
noise_scale_w=noise_scale_w, length_scale=length_scale)[0][0, 0].data.float().numpy()
result = [ipa_text, (hps_vctk.data.sampling_rate, audio)]
else:
ipa_text, stn_tst = get_text(text, language, hps_vctk, mode="VITS")
with torch.no_grad():
x_tst = stn_tst.unsqueeze(0)
x_tst_lengths = torch.LongTensor([stn_tst.size(0)])
sid = torch.LongTensor([voice])
audio = model_vctk.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=noise_scale,
noise_scale_w=noise_scale_w, length_scale=length_scale)[0][0, 0].data.cpu().float().numpy()
result = [ipa_text, (hps_vctk.data.sampling_rate, audio)]
if file_format == 'ogg':
result = [result[0], wav2ogg(result[1][1], result[1][0], text, language)]
else:
result = [result[0], (result[1][0], float2pcm(result[1][1]))]
return result
# download_pretrained()
model, hps, model_vctk, hps_vctk = load_checkpoints()
defaults = {
"text": "coi munje",
"language": "Lojban",
"noise_scale": .667,
"noise_scale_w": .8,
"speed": 1.8,
"voice": "LJS",
"example": ["", "Lojban", 0.667, 0.8, 1.8,"LJS","wav"]
}
inputs = []
outputs = []
css = """
h1 {font-size:200%;}
h2 {font-size:120%;}
h2 a {color: #0020c5;text-decoration: underline;}
p a {text-decoration: underline;}
img {display: inline-block;height:32px;}
#velsku {
text-align: left;
margin: 0;
/* display: none; */
/* justify-content: baseline; */
/* align-items: flex-start; */
padding: 0;
/* height: 28px; */
width: 100%;
bottom: 0px;
left: 0px;
position: fixed;
background: white;
z-index: 10;
}
#velsku_sebenji {
padding: 0.1rem;
display: flex;
white-space: nowrap;
text-overflow: ellipsis;
box-shadow: 0 0 0 1px rgb(56 136 233), 0 0 0 2px rgb(34 87 213),
0 0 0 3px rgb(38 99 224), 0 0 0 4px rgb(25 65 165);
margin-top: 3px;
}
.velsku_pamei {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.velsku_pixra {
max-height: 20px;
margin-right: 0.2rem;
}
#velsku a {
color: #2b79e0;
text-decoration: none;
}
"""
def conditionally_hide_widgets(voice):
if str(voice).startswith("Nix"):
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
else:
return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
title = "<h1>la vitci voksa - <i><img src='/file/assets/jbolanci.png'/>Lojban text-to-speech</i></h1>"
description = "<h2>VITS & Nix-TTS text-to-speech adapted to Lojban. Join <a href='https://discord.gg/BVm4EYR'>Lojban Discord live chat</a> to discuss further.</h2>"
article = "<p style='text-align: center'><a href='https://github.com/jaywalnut310/vits'>VITS: Conditional Variational Autoencoder with Adversarial Learning for End-to-End Text-to-Speech</a> | <a href='https://github.com/rendchevi/nix-tts'>Nix-TTS: Lightweight and End-to-end Text-to-Speech via Module-wise Distillation</a></p>"
chat = """
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.5.1/socket.io.js"></script>"
<script>
document.addEventListener("DOMContentLoaded", () => {
var socket1Chat_connected;
var socket1Chat = io("wss://jbotcan.org:9091", {
transports: ["polling", "websocket"],
});
// if (socket1Chat) {
socket1Chat.on("connect", function () {
console.log(socket1Chat);
socket1Chat_connected = true;
});
socket1Chat.on("connect_error", function () {
console.log("1chat connection error");
});
function trimSocketChunk(text) {
return text
.replace(/[\n\r]+$/gims, " ")
.replace(/<br *\/?>/gims, " ");
// .split('<')[0]
}
socket1Chat.on("sentFrom", function (data) {
if (!socket1Chat_connected) return;
const i = data.data;
const msg = {
d: trimSocketChunk(i.chunk),
s: i.channelId,
w: i.author,
};
const velsku = document.getElementById("velsku_sebenji");
velsku.innerHTML = `<img src="https://la-lojban.github.io/sutysisku/pixra/nunsku.svg" class="velsku_pixra"/> <span class="velsku_pamei">[${msg.s}] ${msg.w}: ${msg.d}</span>`;
});
socket1Chat.on("history", function (data) {
if (!socket1Chat_connected) return;
const i = data.slice(-1)[0];
if (!i) return;
const msg = {
d: trimSocketChunk(i.chunk),
s: i.channelId,
w: i.author,
};
const velsku = document.getElementById("velsku_sebenji");
velsku.innerHTML = `<img src="https://la-lojban.github.io/sutysisku/pixra/nunsku.svg" class="velsku_pixra"/> <span class="velsku_pamei">[${msg.s}] ${msg.w}: ${msg.d}</span>`;
});
// }
});
</script>
<div id="velsku" class="noselect">
<a id="velsku_sebenji" href="https://discord.gg/4KhzRzpmVr" target="_blank">
<img src="https://la-lojban.github.io/sutysisku/pixra/nunsku.svg" class="velsku_pixra"/>
Live chat on Discord
</a>
</div>
"""
with gr.Blocks(css=css) as demo:
gr.HTML(title)
gr.HTML(description)
with gr.Row():
with gr.Column():
input_text = gr.Textbox(lines=4, value=defaults["text"], label="Input text", placeholder="add your text, or click one of the examples to load them")
langs = gr.Radio([
'Lojban',
'English',
'Transcription',
], value=defaults["language"], label="Language")
voices = gr.Radio(["LJS", 0, 1, 2, 3, 4, "Nix-Deterministic", "Nix-Stochastic"], value=defaults["voice"], label="Voice")
noise_scale = gr.Slider(label="Noise scale", minimum=0, maximum=2,
step=0.1, value=defaults["noise_scale"])
noise_scale_w = gr.Slider(label="Noise scale W", minimum=0, maximum=2,
step=0.1, value=defaults["noise_scale_w"])
slowness = gr.Slider(label="Slowness", minimum=0.1, maximum=3,
step=0.1, value=defaults["speed"])
file_format = gr.Radio(["wav", "ogg"], value="wav", label="File format")
inputs = [input_text, langs, noise_scale, noise_scale_w, slowness, voices, file_format]
# events
vits_inputs = [noise_scale, noise_scale_w, slowness]
voices.change(fn=conditionally_hide_widgets, inputs=voices,outputs=vits_inputs)
with gr.Column():
ipa_block = gr.Textbox(label="International Phonetic Alphabet")
audio = gr.Audio(type="numpy", label="Output audio")
outputs = [ ipa_block, audio ]
btn = gr.Button("Vocalize")
btn.click(fn=inference, inputs=inputs, outputs=outputs, api_name="cupra")
examples = list(map(lambda el: el[0:len(el)] + defaults["example"][len(el):], [
["coi ro do ma nuzba", "Lojban"],
["mi djica lo nu do zvati ti", "Lojban", 0.667, 0.8, 1.8,4],
["mu xagji sofybakni cu zvati le purdi", "Lojban", 0.667, 0.8, 1.8, "Nix-Deterministic"],
["ni'o le pa tirxu be me'e zo .teris. pu ki kansa le za'u pendo be le nei le ka xabju le foldi be loi spati", "Lojban"],
[", miː dʒˈiːʃaː loːnʊuː doː zvˈaːtiː tiː.", "Transcription"],
["We propose VITS, Conditional Variational Autoencoder with Adversarial Learning for End-to-End Text-to-Speech.", "English"],
]))
gr.Examples(examples, inputs, fn=inference, outputs=outputs, cache_examples=True, run_on_click=True)
gr.HTML(article)
gr.HTML(chat)
demo.launch(server_name="0.0.0.0")
|