explore-vits / app.py
ylacombe's picture
ylacombe HF staff
Update app.py
baccf9b
raw
history blame
3.7 kB
import torch
from transformers import pipeline
import numpy as np
import gradio as gr
def _grab_best_device(use_gpu=True):
if torch.cuda.device_count() > 0 and use_gpu:
device = "cuda"
else:
device = "cpu"
return device
device = _grab_best_device()
default_model_per_language = {
"english": "kakao-enterprise/vits-ljs",
"spanish": "facebook/mms-tts-spa",
}
models_per_language = {
"english": [
("Irish Male Speaker", "ylacombe/vits_ljs_irish_male_monospeaker_2"),
("Welsh Female Speaker", "ylacombe/vits_ljs_welsh_female_monospeaker_2"),
("Welsh Male Speaker", "ylacombe/vits_ljs_welsh_male_monospeaker_2"),
("Scottish Female Speaker", "ylacombe/vits_ljs_scottish_female_monospeaker"),
],
"spanish": [
("Male Chilean Speaker", "ylacombe/mms-spa-finetuned-chilean-monospeaker"),
("Female Argentinian Speaker", "ylacombe/mms-spa-finetuned-argentinian-monospeaker"),
("Male Colombian Speaker", "ylacombe/mms-spa-finetuned-colombian-monospeaker"),
],
}
pipe_dict = {
"pipe": [pipeline("text-to-speech", model=l[1], device=0) for l in models_per_language["english"]],
"original_pipe": pipeline("text-to-speech", model=default_model_per_language["english"], device=0),
"language": "english",
}
title = "# VITS"
description = """
TODO
"""
max_speakers = 15
# Inference
def generate_audio(text, language):
if pipe_dict["language"] != language:
gr.Warning(f"Language has changed - loading corresponding models: {default_model_per_language[language]}")
pipe_dict["language"] = language
pipe_dict["original_pipe"] = pipeline("text-to-speech", model=default_model_per_language[language], device=0)
pipe_dict["pipe"] = [pipeline("text-to-speech", model=l[1], device=0) for l in models_per_language["english"]]
out = []
# first generate original model result
output = pipe_dict["original_pipe"](text)
output = gr.Audio(value = (output["sampling_rate"], output["audio"].squeeze()), type="numpy", autoplay=False, label=f"Prediction from the original checkpoint {default_model_per_language[language]}", show_label=True,
visible=True)
out.append(output)
for i in range(min(len(pipe_dict["pipe"]), max_speakers - 1)):
output = pipe_dict["pipe"][i](text)
output = gr.Audio(value = (output["sampling_rate"], output["audio"].squeeze()), type="numpy", autoplay=False, label=f"Finetuned {models_per_language[language][i][0]}", show_label=True,
visible=True)
out.append(output)
out.extend([gr.Audio(visible=False)]*(max_speakers-(len(out))))
return out
# Gradio blocks demo
with gr.Blocks() as demo_blocks:
gr.Markdown(title)
gr.Markdown(description)
with gr.Row():
with gr.Column():
inp_text = gr.Textbox(label="Input Text", info="What would you like VITS to synthesise?")
btn = gr.Button("Generate Audio!")
language = gr.Dropdown(
default_model_per_language.keys(),
value = "english",
label = "language",
info = "Language that you want to test"
)
with gr.Column():
outputs = []
for i in range(max_speakers):
out_audio = gr.Audio(type="numpy", autoplay=False, label=f"Generated Audio - speaker {i}", show_label=True, visible=False)
outputs.append(out_audio)
btn.click(generate_audio, [inp_text, language], outputs)
demo_blocks.queue().launch()