Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
from parler_tts import ParlerTTSForConditionalGeneration
|
| 5 |
+
from transformers import AutoTokenizer, AutoFeatureExtractor, set_seed
|
| 6 |
+
|
| 7 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
repo_id = "ylacombe/parler_tts_300M_v0.09"
|
| 11 |
+
# TODO: change repo id
|
| 12 |
+
|
| 13 |
+
model = ParlerTTSForConditionalGeneration.from_pretrained(repo_id).to(device)
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(repo_id)
|
| 15 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(repo_id)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
SAMPLE_RATE = feature_extractor.sampling_rate
|
| 19 |
+
SEED = 41
|
| 20 |
+
|
| 21 |
+
default_text = "Please surprise me and speak in whatever voice you enjoy."
|
| 22 |
+
|
| 23 |
+
title = "# Parler-TTS </div>"
|
| 24 |
+
|
| 25 |
+
examples = [
|
| 26 |
+
[
|
| 27 |
+
"'This is the best time of my life, Bartley,' she said happily.",
|
| 28 |
+
"A female speaker with a slightly low-pitched, quite monotone voice delivers her words at a slightly faster-than-average pace in a confined space with very clear audio.",
|
| 29 |
+
],
|
| 30 |
+
[
|
| 31 |
+
"Montrose also, after having experienced still more variety of good and bad fortune, threw down his arms, and retired out of the kingdom. ",
|
| 32 |
+
"A male speaker with a slightly high-pitched voice delivering his words at a slightly slow pace in a small, confined space with a touch of background noise and a quite monotone tone.",
|
| 33 |
+
],
|
| 34 |
+
[
|
| 35 |
+
"montrose also after having experienced still more variety of good and bad fortune threw down his arms and retired out of the kingdom",
|
| 36 |
+
"A male speaker with a low-pitched voice delivering his words at a fast pace in a small, confined space with a lot of background noise and an animated tone.",
|
| 37 |
+
],
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def gen_tts(text, description):
|
| 42 |
+
inputs = tokenizer(description, return_tensors="pt").to(device)
|
| 43 |
+
prompt = tokenizer(text, return_tensors="pt").to(device)
|
| 44 |
+
|
| 45 |
+
set_seed(SEED)
|
| 46 |
+
generation = model.generate(
|
| 47 |
+
input_ids=inputs.input_ids, prompt_input_ids=prompt.input_ids, do_sample=True, temperature=1.0
|
| 48 |
+
)
|
| 49 |
+
audio_arr = generation.cpu().numpy().squeeze()
|
| 50 |
+
|
| 51 |
+
return (SAMPLE_RATE, audio_arr)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
css = """
|
| 55 |
+
#share-btn-container {
|
| 56 |
+
display: flex;
|
| 57 |
+
padding-left: 0.5rem !important;
|
| 58 |
+
padding-right: 0.5rem !important;
|
| 59 |
+
background-color: #000000;
|
| 60 |
+
justify-content: center;
|
| 61 |
+
align-items: center;
|
| 62 |
+
border-radius: 9999px !important;
|
| 63 |
+
width: 13rem;
|
| 64 |
+
margin-top: 10px;
|
| 65 |
+
margin-left: auto;
|
| 66 |
+
flex: unset !important;
|
| 67 |
+
}
|
| 68 |
+
#share-btn {
|
| 69 |
+
all: initial;
|
| 70 |
+
color: #ffffff;
|
| 71 |
+
font-weight: 600;
|
| 72 |
+
cursor: pointer;
|
| 73 |
+
font-family: 'IBM Plex Sans', sans-serif;
|
| 74 |
+
margin-left: 0.5rem !important;
|
| 75 |
+
padding-top: 0.25rem !important;
|
| 76 |
+
padding-bottom: 0.25rem !important;
|
| 77 |
+
right:0;
|
| 78 |
+
}
|
| 79 |
+
#share-btn * {
|
| 80 |
+
all: unset !important;
|
| 81 |
+
}
|
| 82 |
+
#share-btn-container div:nth-child(-n+2){
|
| 83 |
+
width: auto !important;
|
| 84 |
+
min-height: 0px !important;
|
| 85 |
+
}
|
| 86 |
+
#share-btn-container .wrap {
|
| 87 |
+
display: none !important;
|
| 88 |
+
}
|
| 89 |
+
"""
|
| 90 |
+
with gr.Blocks(css=css) as block:
|
| 91 |
+
gr.Markdown(title)
|
| 92 |
+
with gr.Row():
|
| 93 |
+
with gr.Column():
|
| 94 |
+
input_text = gr.Textbox(label="Input Text", lines=2, value=default_text, elem_id="input_text")
|
| 95 |
+
description = gr.Textbox(label="Description", lines=2, value="", elem_id="input_description")
|
| 96 |
+
run_button = gr.Button("Generate Audio", variant="primary")
|
| 97 |
+
with gr.Column():
|
| 98 |
+
audio_out = gr.Audio(label="Parler-TTS generation", type="numpy", elem_id="audio_out")
|
| 99 |
+
|
| 100 |
+
inputs = [input_text, description]
|
| 101 |
+
outputs = [audio_out]
|
| 102 |
+
gr.Examples(examples=examples, fn=gen_tts, inputs=inputs, outputs=outputs, cache_examples=True)
|
| 103 |
+
run_button.click(fn=gen_tts, inputs=inputs, outputs=outputs, queue=True)
|
| 104 |
+
|
| 105 |
+
block.queue()
|
| 106 |
+
block.launch(share=True)
|