Spaces:
Running
Running
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,222 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
|
| 3 |
-
from transformers import pipeline
|
| 4 |
-
|
| 5 |
-
import numpy as np
|
| 6 |
-
import gradio as gr
|
| 7 |
-
|
| 8 |
-
def _grab_best_device(use_gpu=True):
|
| 9 |
-
if torch.cuda.device_count() > 0 and use_gpu:
|
| 10 |
-
device = 0 #"cuda"
|
| 11 |
-
else:
|
| 12 |
-
device = -1 #"cpu"
|
| 13 |
-
#device = 0 if torch.cuda.is_available() else -1
|
| 14 |
-
|
| 15 |
-
return device
|
| 16 |
-
|
| 17 |
-
device = _grab_best_device()
|
| 18 |
-
|
| 19 |
-
default_model_per_language = {
|
| 20 |
-
"spanish": "facebook/mms-tts-spa",
|
| 21 |
-
"tamil": "facebook/mms-tts-tam",
|
| 22 |
-
"gujarati": "facebook/mms-tts-guj",
|
| 23 |
-
"marathi": "facebook/mms-tts-mar",
|
| 24 |
-
#"english": "kakao-enterprise/vits-ljs",
|
| 25 |
-
"english": "facebook/mms-tts-eng",
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
models_per_language = {
|
| 29 |
-
"english": [
|
| 30 |
-
"ylacombe/vits_ljs_midlands_male_monospeaker",
|
| 31 |
-
],
|
| 32 |
-
"spanish-ch": [
|
| 33 |
-
"ylacombe/mms-spa-finetuned-chilean-monospeaker",
|
| 34 |
-
],
|
| 35 |
-
"spanish-co": [
|
| 36 |
-
"ylacombe/mms-spa-finetuned-colombian-monospeaker",
|
| 37 |
-
],
|
| 38 |
-
"tamil": [
|
| 39 |
-
"ylacombe/mms-tam-finetuned-monospeaker",
|
| 40 |
-
],
|
| 41 |
-
"gujarati" : ["ylacombe/mms-guj-finetuned-monospeaker"],
|
| 42 |
-
"marathi": ["ylacombe/mms-mar-finetuned-monospeaker"]
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
HUB_PATH = "ylacombe/vits_ljs_midlands_male_monospeaker"
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
pipe_dict = {
|
| 49 |
-
"current_model": "ylacombe/vits_ljs_midlands_male_monospeaker",
|
| 50 |
-
"pipe": pipeline("text-to-speech", model=HUB_PATH, device=device),
|
| 51 |
-
"original_pipe": pipeline("text-to-speech", model=default_model_per_language["english"], device=device),
|
| 52 |
-
"language": "english",
|
| 53 |
-
}
|
| 54 |
-
|
| 55 |
-
title = """
|
| 56 |
-
# Explore MMS finetuning
|
| 57 |
-
## Or how to access truely multilingual TTS
|
| 58 |
-
|
| 59 |
-
Massively Multilingual Speech (MMS) models are light-weight, low-latency TTS models based on the [VITS architecture](https://huggingface.co/docs/transformers/model_doc/vits).
|
| 60 |
-
|
| 61 |
-
Meta's [MMS](https://arxiv.org/abs/2305.13516) project, aiming to provide speech technology across a diverse range of languages. You can find more details about the supported languages and their ISO 639-3 codes in the [MMS Language Coverage Overview](https://dl.fbaipublicfiles.com/mms/misc/language_coverage_mms.html),
|
| 62 |
-
and see all MMS-TTS checkpoints on the Hugging Face Hub: [facebook/mms-tts](https://huggingface.co/models?sort=trending&search=facebook%2Fmms-tts).
|
| 63 |
-
|
| 64 |
-
Coupled with the right data and the right training recipe, you can get an excellent finetuned version of every MMS checkpoints in **20 minutes** with as little as **80 to 150 samples**.
|
| 65 |
-
|
| 66 |
-
Training recipe available in this [github repository](https://github.com/ylacombe/finetune-hf-vits)!
|
| 67 |
-
"""
|
| 68 |
-
|
| 69 |
-
max_speakers = 15
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
# Inference
|
| 73 |
-
def generate_audio(text, model_id, language):
|
| 74 |
-
|
| 75 |
-
if pipe_dict["language"] != language:
|
| 76 |
-
gr.Warning(f"Language has changed - loading new default model: {default_model_per_language[language]}")
|
| 77 |
-
pipe_dict["language"] = language
|
| 78 |
-
pipe_dict["original_pipe"] = pipeline("text-to-speech", model=default_model_per_language[language], device=device)
|
| 79 |
-
|
| 80 |
-
if pipe_dict["current_model"] != model_id:
|
| 81 |
-
gr.Warning("Model has changed - loading new model")
|
| 82 |
-
pipe_dict["pipe"] = pipeline("text-to-speech", model=model_id, device=device)
|
| 83 |
-
pipe_dict["current_model"] = model_id
|
| 84 |
-
|
| 85 |
-
num_speakers = pipe_dict["pipe"].model.config.num_speakers
|
| 86 |
-
|
| 87 |
-
out = []
|
| 88 |
-
# first generate original model result
|
| 89 |
-
output = pipe_dict["original_pipe"](text)
|
| 90 |
-
output = gr.Audio(value = (output["sampling_rate"], output["audio"].squeeze()), type="numpy", autoplay=False, label=f"Non finetuned model prediction {default_model_per_language[language]}", show_label=True,
|
| 91 |
-
visible=True)
|
| 92 |
-
out.append(output)
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
if num_speakers>1:
|
| 96 |
-
for i in range(min(num_speakers, max_speakers - 1)):
|
| 97 |
-
forward_params = {"speaker_id": i}
|
| 98 |
-
output = pipe_dict["pipe"](text, forward_params=forward_params)
|
| 99 |
-
|
| 100 |
-
output = gr.Audio(value = (output["sampling_rate"], output["audio"].squeeze()), type="numpy", autoplay=False, label=f"Generated Audio - speaker {i}", show_label=True,
|
| 101 |
-
visible=True)
|
| 102 |
-
out.append(output)
|
| 103 |
-
out.extend([gr.Audio(visible=False)]*(max_speakers-num_speakers))
|
| 104 |
-
else:
|
| 105 |
-
output = pipe_dict["pipe"](text)
|
| 106 |
-
output = gr.Audio(value = (output["sampling_rate"], output["audio"].squeeze()), type="numpy", autoplay=False, label="Generated Audio - Mono speaker", show_label=True,
|
| 107 |
-
visible=True)
|
| 108 |
-
out.append(output)
|
| 109 |
-
out.extend([gr.Audio(visible=False)]*(max_speakers-2))
|
| 110 |
-
return out
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
css = """
|
| 114 |
-
#container{
|
| 115 |
-
margin: 0 auto;
|
| 116 |
-
max-width: 80rem;
|
| 117 |
-
}
|
| 118 |
-
#intro{
|
| 119 |
-
max-width: 100%;
|
| 120 |
-
text-align: center;
|
| 121 |
-
margin: 0 auto;
|
| 122 |
-
}
|
| 123 |
-
"""
|
| 124 |
-
# Gradio blocks demo
|
| 125 |
-
with gr.Blocks(css=css) as demo_blocks:
|
| 126 |
-
gr.Markdown(title, elem_id="intro")
|
| 127 |
-
|
| 128 |
-
with gr.Row():
|
| 129 |
-
with gr.Column():
|
| 130 |
-
inp_text = gr.Textbox(label="Input Text", info="What sentence would you like to synthesise?")
|
| 131 |
-
btn = gr.Button("Generate Audio!")
|
| 132 |
-
language = gr.Dropdown(
|
| 133 |
-
default_model_per_language.keys(),
|
| 134 |
-
value = "spanish",
|
| 135 |
-
label = "language",
|
| 136 |
-
info = "Language that you want to test"
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
model_id = gr.Dropdown(
|
| 140 |
-
models_per_language["spanish"],
|
| 141 |
-
value="ylacombe/mms-spa-finetuned-colombian-monospeaker",
|
| 142 |
-
label="Model",
|
| 143 |
-
info="Model you want to test",
|
| 144 |
-
)
|
| 145 |
-
|
| 146 |
-
with gr.Column():
|
| 147 |
-
outputs = []
|
| 148 |
-
for i in range(max_speakers):
|
| 149 |
-
out_audio = gr.Audio(type="numpy", autoplay=False, label=f"Generated Audio - speaker {i}", show_label=True, visible=False)
|
| 150 |
-
outputs.append(out_audio)
|
| 151 |
-
|
| 152 |
-
with gr.Accordion("Datasets and models details", open=False):
|
| 153 |
-
gr.Markdown("""
|
| 154 |
-
|
| 155 |
-
For each language, we used 100 to 150 samples of a single speaker to finetune the model.
|
| 156 |
-
|
| 157 |
-
### Spanish
|
| 158 |
-
|
| 159 |
-
* **Model**: [Spanish MMS TTS](https://huggingface.co/facebook/mms-tts-spa).
|
| 160 |
-
* **Datasets**:
|
| 161 |
-
- [Chilean Spanish TTS dataset](https://huggingface.co/datasets/ylacombe/google-chilean-spanish).
|
| 162 |
-
|
| 163 |
-
### Tamil
|
| 164 |
-
|
| 165 |
-
* **Model**: [Tamil MMS TTS](https://huggingface.co/facebook/mms-tts-tam).
|
| 166 |
-
* **Datasets**:
|
| 167 |
-
- [Tamil TTS dataset](https://huggingface.co/datasets/ylacombe/google-tamil).
|
| 168 |
-
|
| 169 |
-
### Gujarati
|
| 170 |
-
|
| 171 |
-
* **Model**: [Gujarati MMS TTS](https://huggingface.co/facebook/mms-tts-guj).
|
| 172 |
-
* **Datasets**:
|
| 173 |
-
- [Gujarati TTS dataset](https://huggingface.co/datasets/ylacombe/google-gujarati).
|
| 174 |
-
|
| 175 |
-
### Marathi
|
| 176 |
-
|
| 177 |
-
* **Model**: [Marathi MMS TTS](https://huggingface.co/facebook/mms-tts-mar).
|
| 178 |
-
* **Datasets**:
|
| 179 |
-
- [Marathi TTS dataset](https://huggingface.co/datasets/ylacombe/google-chilean-marathi).
|
| 180 |
-
|
| 181 |
-
### English
|
| 182 |
-
|
| 183 |
-
* **Model**: [VITS-ljs](https://huggingface.co/kakao-enterprise/vits-ljs)
|
| 184 |
-
* **Dataset**: [British Isles Accent](https://huggingface.co/datasets/ylacombe/english_dialects). For each accent, we used 100 to 150 samples of a single speaker to finetune [VITS-ljs](https://huggingface.co/kakao-enterprise/vits-ljs).
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
""")
|
| 188 |
-
|
| 189 |
-
with gr.Accordion("Run VITS and MMS with transformers", open=False):
|
| 190 |
-
gr.Markdown(
|
| 191 |
-
"""
|
| 192 |
-
```bash
|
| 193 |
-
pip install transformers
|
| 194 |
-
```
|
| 195 |
-
```py
|
| 196 |
-
from transformers import pipeline
|
| 197 |
-
import scipy
|
| 198 |
-
pipe = pipeline("text-to-speech", model="kakao-enterprise/vits-ljs", device=0)
|
| 199 |
-
|
| 200 |
-
results = pipe("A cinematic shot of a baby racoon wearing an intricate italian priest robe")
|
| 201 |
-
|
| 202 |
-
# write to a wav file
|
| 203 |
-
scipy.io.wavfile.write("audio_vits.wav", rate=results["sampling_rate"], data=results["audio"].squeeze())
|
| 204 |
-
```
|
| 205 |
-
"""
|
| 206 |
-
)
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
language.change(lambda language: gr.Dropdown(
|
| 210 |
-
models_per_language[language],
|
| 211 |
-
value=models_per_language[language][0],
|
| 212 |
-
label="Model",
|
| 213 |
-
info="Model you want to test",
|
| 214 |
-
),
|
| 215 |
-
language,
|
| 216 |
-
model_id
|
| 217 |
-
)
|
| 218 |
-
|
| 219 |
-
btn.click(generate_audio, [inp_text, model_id, language], outputs)
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
demo_blocks.queue().launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|