Spaces:
Running
Running
File size: 12,018 Bytes
ea6a7ed 202457e ea6a7ed |
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 352 353 354 355 356 357 358 359 360 361 |
import os
import sys
import json
import time
from importlib.metadata import version
from enum import Enum
from huggingface_hub import hf_hub_download
use_zerogpu = False
try:
import spaces # it's for ZeroGPU
use_zerogpu = True
print("ZeroGPU is available, changing inference call.")
except ImportError:
print("ZeroGPU is not available, skipping...")
import gradio as gr
import torch
import torchaudio
# BigVGAN
import bigvgan
# RAD-TTS code
from radtts import RADTTS
from data import Data
from common import update_params
use_cuda = torch.cuda.is_available()
if use_cuda:
print("CUDA is available, setting correct inference_device variable.")
device = "cuda"
else:
device = "cpu"
def download_file_from_repo(
repo_id: str,
filename: str,
local_dir: str = ".",
repo_type: str = "model",
) -> str:
try:
os.makedirs(local_dir, exist_ok=True)
file_path = hf_hub_download(
repo_id=repo_id,
filename=filename,
local_dir=local_dir,
cache_dir=None,
force_download=False,
repo_type=repo_type,
)
return file_path
except Exception as e:
raise Exception(f"An error occurred during download: {e}") from e
download_file_from_repo(
"Yehor/radtts-uk",
"radtts-pp-dap-model/model_dap_84000.pt",
"./models/",
)
# Init the model
seed = 1234
config = "configs/radtts-pp-dap-model.json"
radtts_path = "models/radtts-pp-dap-model/model_dap_84000.pt"
params = []
# Load the config
with open(config) as f:
data = f.read()
config = json.loads(data)
update_params(config, params)
data_config = config["data_config"]
model_config = config["model_config"]
# Seed
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
# Load vocoder
vocoder_model = bigvgan.BigVGAN.from_pretrained(
"nvidia/bigvgan_v2_22khz_80band_fmax8k_256x", use_cuda_kernel=False,
)
vocoder_model.remove_weight_norm()
vocoder_model = vocoder_model.eval().to(device)
# Load RAD-TTS
if use_cuda:
radtts = RADTTS(**model_config).cuda()
else:
radtts = RADTTS(**model_config)
radtts.enable_inverse_cache() # cache inverse matrix for 1x1 invertible convs
checkpoint_dict = torch.load(radtts_path, map_location="cpu") # todo: CPU?
radtts.load_state_dict(checkpoint_dict["state_dict"], strict=False)
radtts.eval()
print(f"Loaded checkpoint '{radtts_path}')")
ignore_keys = ["training_files", "validation_files"]
trainset = Data(
data_config["training_files"],
**dict((k, v) for k, v in data_config.items() if k not in ignore_keys),
)
# Config
concurrency_limit = 5
title = "RAD-TTS++ Ukrainian"
# https://www.tablesgenerator.com/markdown_tables
authors_table = """
## Authors
Follow them on social networks and **contact** if you need any help or have any questions:
| <img src="https://avatars.githubusercontent.com/u/7875085?v=4" width="100"> **Yehor Smoliakov** |
|-------------------------------------------------------------------------------------------------|
| https://t.me/smlkw in Telegram |
| https://x.com/yehor_smoliakov at X |
| https://github.com/egorsmkv at GitHub |
| https://huggingface.co/Yehor at Hugging Face |
| or use egorsmkv@gmail.com |
""".strip()
description_head = f"""
# {title}
## Overview
Type your text in Ukrainian and select a voice to synthesize speech using [the RAD-TTS++ model](https://huggingface.co/Yehor/radtts-uk) and [BigVGAN v2](https://huggingface.co/nvidia/bigvgan_v2_22khz_80band_fmax8k_256x) with 22050 Hz.
""".strip()
description_foot = f"""
{authors_table}
""".strip()
tech_env = f"""
#### Environment
- Python: {sys.version}
""".strip()
tech_libraries = f"""
#### Libraries
- gradio: {version("gradio")}
- torch: {version("torch")}
- scipy: {version("scipy")}
- numba: {version("numba")}
- librosa: {version("librosa")}
- unidecode: {version("unidecode")}
- inflect: {version("inflect")}
""".strip()
class VoiceOption(Enum):
Tetiana = "Tetiana (female) 👩"
Mykyta = "Mykyta (male) 👨"
Lada = "Lada (female) 👩"
voice_mapping = {
VoiceOption.Tetiana.value: "tetiana",
VoiceOption.Mykyta.value: "mykyta",
VoiceOption.Lada.value: "lada",
}
examples = [
[
"Прокинувся ґазда вранці. Пішов, вичистив з-під коня, вичистив з-під бика, вичистив з-під овечок, вибрав молодняк, відніс його набік.",
VoiceOption.Mykyta.value,
],
[
"Пішов взяв сіна, дав корові. Пішов взяв сіна, дав бикові. Ячміню коняці насипав. Зайшов почистив корову, зайшов почистив бика, зайшов почистив коня, за яйця його мацнув.",
VoiceOption.Lada.value,
],
[
"Кінь ногою здригнув, на хазяїна ласкавим оком подивився. Тоді дядько пішов відкрив курей, гусей, качок, повиносив їм зерна, огірків нарізаних, нагодував. Коли чує – з хати дружина кличе. Зайшов. Дітки повмивані, сидять за столом, всі чекають тата. Взяв він ложку, перехрестив дітей, перехрестив лоба, почали снідати. Поснідали, він дістав пряників, роздав дітям. Діти зібралися, пішли в школу. Дядько вийшов, сів на призьбі, взяв сапку, почав мантачити. Мантачив-мантачив, коли – жінка виходить. Він їй ту сапку дає, ласкаво за сраку вщипнув, жінка до нього лагідно всміхнулася, пішла на город – сапати. Коли – йде пастух і товар кличе в череду. Повідмикав дядько овечок, коровку, бика, коня, все відпустив. Сів попри хати, дістав табАку, відірвав шмат газети, насипав, наслинив собі гарну таку цигарку. Благодать божа – і сонечко вже здійнялося над деревами. Дядько встромив цигарку в рота, дістав сірники, тільки чиркати – коли раптом з хати: Доброе утро! Московское время – шесть часов утра! Витяг дядько цигарку с рота, сплюнув набік, і сам собі каже: Ана маєш. Прокинулись, бляді!",
VoiceOption.Tetiana.value,
],
]
def inference(text, voice):
if not text:
raise gr.Error("Please paste your text.")
gr.Info("Starting...", duration=0.5)
speaker = voice_mapping[voice]
speaker = speaker_text = speaker_attributes = speaker
n_takes = 1
sigma = 0.8 # sampling sigma for decoder
sigma_tkndur = 0.666 # sampling sigma for duration
sigma_f0 = 1.0 # sampling sigma for f0
sigma_energy = 1.0 # sampling sigma for energy avg
token_dur_scaling = 1.0
f0_mean = 0
f0_std = 0
energy_mean = 0
energy_std = 0
if use_cuda:
speaker_id = trainset.get_speaker_id(speaker).cuda()
speaker_id_text, speaker_id_attributes = speaker_id, speaker_id
if speaker_text is not None:
speaker_id_text = trainset.get_speaker_id(speaker_text).cuda()
if speaker_attributes is not None:
speaker_id_attributes = trainset.get_speaker_id(speaker_attributes).cuda()
tensor_text = trainset.get_text(text).cuda()[None]
else:
speaker_id = trainset.get_speaker_id(speaker)
speaker_id_text, speaker_id_attributes = speaker_id, speaker_id
if speaker_text is not None:
speaker_id_text = trainset.get_speaker_id(speaker_text)
if speaker_attributes is not None:
speaker_id_attributes = trainset.get_speaker_id(speaker_attributes)
tensor_text = trainset.get_text(text)[None]
inference_start = time.time()
for take in range(n_takes):
with torch.autocast(device, enabled=False):
with torch.inference_mode():
outputs = radtts.infer(
speaker_id,
tensor_text,
sigma,
sigma_tkndur,
sigma_f0,
sigma_energy,
token_dur_scaling,
token_duration_max=100,
speaker_id_text=speaker_id_text,
speaker_id_attributes=speaker_id_attributes,
f0_mean=f0_mean,
f0_std=f0_std,
energy_mean=energy_mean,
energy_std=energy_std,
use_cuda=use_cuda,
)
mel = outputs["mel"]
gr.Info(
"Synthesized MEL spectrogram, converting to WAVE.", duration=0.5
)
wav_gen = vocoder_model(mel)
wav_gen_float = wav_gen.squeeze(0).cpu()
torchaudio.save("audio.wav", wav_gen_float, 22_050, encoding="PCM_S")
duration = len(wav_gen_float[0]) / 22_050
elapsed_time = time.time() - inference_start
rtf = elapsed_time / duration
speed_ratio = duration / elapsed_time
speech_rate = len(text.split(" ")) / duration
rtf_value = f"Real-Time Factor: {round(rtf, 4)}, time: {round(elapsed_time, 4)} seconds, audio duration: {round(duration, 4)} seconds. Speed ratio: {round(speed_ratio, 2)}x. Speech rate: {round(speech_rate, 4)} words-per-second."
gr.Success("Finished!", duration=0.5)
return [gr.Audio("audio.wav"), rtf_value]
try:
@spaces.GPU
def inference_zerogpu(text, voice):
return inference(text, voice)
except NameError:
print("ZeroGPU is not available, skipping...")
def inference_cpu(text, voice):
return inference(text, voice)
demo = gr.Blocks(
title=title,
analytics_enabled=False,
theme=gr.themes.Base(),
)
with demo:
gr.Markdown(description_head)
gr.Markdown("## Usage")
with gr.Row():
with gr.Column():
audio = gr.Audio(label="Synthesized audio")
rtf = gr.Markdown(
label="Real-Time Factor",
value="Here you will see how fast the model and the speaker is.",
)
with gr.Row():
with gr.Column():
text = gr.Text(
label="Text",
value="Сл+ава Укра+їні! — українське вітання, національне гасло.",
)
voice = gr.Radio(
label="Voice",
choices=[option.value for option in VoiceOption],
value=VoiceOption.Tetiana.value,
)
gr.Button("Run").click(
inference_zerogpu if use_zerogpu else inference_cpu,
concurrency_limit=concurrency_limit,
inputs=[text, voice],
outputs=[audio, rtf],
)
with gr.Row():
gr.Examples(
label="Choose an example",
inputs=[text, voice],
examples=examples,
)
gr.Markdown(description_foot)
gr.Markdown("### Gradio app uses:")
gr.Markdown(tech_env)
gr.Markdown(tech_libraries)
if __name__ == "__main__":
demo.queue()
demo.launch()
|