Spaces:
Running
on
T4
Running
on
T4
File size: 5,171 Bytes
fca9b48 320bb9e fca9b48 320bb9e fca9b48 |
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 |
import os
import torch
import gradio as gr
import torchaudio
import time
from datetime import datetime
from tortoise.api import TextToSpeech
from tortoise.utils.text import split_and_recombine_text
from tortoise.utils.audio import load_audio, load_voice, load_voices
VOICE_OPTIONS = [
"angie",
"cond_latent_example",
"deniro",
"freeman",
"halle",
"lj",
"myself",
"pat2",
"snakes",
"tom",
"train_daws",
"train_dreams",
"train_grace",
"train_lescault",
"weaver",
"applejack",
"daniel",
"emma",
"geralt",
"jlaw",
"mol",
"pat",
"rainbow",
"tim_reynolds",
"train_atkins",
"train_dotrice",
"train_empire",
"train_kennard",
"train_mouse",
"william",
"random", # special option for random voice
"disabled", # special option for disabled voice
]
def inference(
text,
script,
name,
voice,
voice_b,
preset,
seed,
regenerate,
split_by_newline,
):
if regenerate.strip() == "":
regenerate = None
if name.strip() == "":
raise gr.Error("No name provided")
if text is None or text.strip() == "":
with open(script.name) as f:
text = f.read()
if text.strip() == "":
raise gr.Error("Please provide either text or script file with content.")
if split_by_newline == "Yes":
texts = list(filter(lambda x: x.strip() != "", text.split("\n")))
else:
texts = split_and_recombine_text(text)
os.makedirs(os.path.join("longform", name), exist_ok=True)
if regenerate is not None:
regenerate = list(map(int, regenerate.split()))
voices = [voice]
if voice_b != "disabled":
voices.append(voice_b)
if len(voices) == 1:
voice_samples, conditioning_latents = load_voice(voice)
else:
voice_samples, conditioning_latents = load_voices(voices)
start_time = time.time()
all_parts = []
for j, text in enumerate(texts):
if regenerate is not None and j + 1 not in regenerate:
all_parts.append(
load_audio(os.path.join("longform", name, f"{j+1}.wav"), 24000)
)
continue
gen = tts.tts_with_preset(
text,
voice_samples=voice_samples,
conditioning_latents=conditioning_latents,
preset=preset,
k=1,
use_deterministic_seed=seed,
)
gen = gen.squeeze(0).cpu()
torchaudio.save(os.path.join("longform", name, f"{j+1}.wav"), gen, 24000)
all_parts.append(gen)
full_audio = torch.cat(all_parts, dim=-1)
os.makedirs("outputs", exist_ok=True)
torchaudio.save(os.path.join("outputs", f"{name}.wav"), full_audio, 24000)
with open("Tortoise_TTS_Runs_Scripts.log", "a") as f:
f.write(
f"{datetime.now()} | Voice: {','.join(voices)} | Text: {text} | Quality: {preset} | Time Taken (s): {time.time()-start_time} | Seed: {seed}\n"
)
output_texts = [f"({j+1}) {texts[j]}" for j in range(len(texts))]
return ((24000, full_audio.squeeze().cpu().numpy()), "\n".join(output_texts))
def main():
text = gr.Textbox(
lines=4,
label="Text (Provide either text, or upload a newline separated text file below):",
)
script = gr.File(label="Upload a text file")
name = gr.Textbox(
lines=1, label="Name of the output file / folder to store intermediate results:"
)
preset = gr.Radio(
["ultra_fast", "fast", "standard", "high_quality"],
value="fast",
label="Preset mode (determines quality with tradeoff over speed):",
type="value",
)
voice = gr.Dropdown(
VOICE_OPTIONS, value="angie", label="Select voice:", type="value"
)
voice_b = gr.Dropdown(
VOICE_OPTIONS,
value="disabled",
label="(Optional) Select second voice:",
type="value",
)
seed = gr.Number(value=0, precision=0, label="Seed (for reproducibility):")
regenerate = gr.Textbox(
lines=1,
label="Comma-separated indices of clips to regenerate [starting from 1]",
)
split_by_newline = gr.Radio(
["Yes", "No"],
label="Split by newline (If [No], it will automatically try to find relevant splits):",
type="value",
value="No",
)
output_audio = gr.Audio(label="Combined audio:")
output_text = gr.Textbox(label="Split texts with indices:", lines=10)
interface = gr.Interface(
fn=inference,
inputs=[
text,
script,
name,
voice,
voice_b,
preset,
seed,
regenerate,
split_by_newline,
],
outputs=[output_audio, output_text],
)
interface.launch(share=True)
if __name__ == "__main__":
tts = TextToSpeech(kv_cache=True, use_deepspeed=True, half=True)
with open("Tortoise_TTS_Runs_Scripts.log", "a") as f:
f.write(
f"\n\n-------------------------Tortoise TTS Scripts Logs, {datetime.now()}-------------------------\n"
)
main() |