Spaces:
Running
Running
File size: 11,161 Bytes
b794742 4408097 b794742 4408097 b794742 4408097 b794742 4408097 b794742 4408097 b794742 d2eb80b b794742 4408097 b794742 4408097 b794742 4408097 b794742 4408097 b794742 4408097 b794742 |
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 |
# AGPL: a notification must be added stating that changes have been made to that file.
import os
import shutil
from pathlib import Path
import streamlit as st
from random import randint
from tortoise.api import MODELS_DIR
from tortoise.inference import (
infer_on_texts,
run_and_save_tts,
split_and_recombine_text,
)
from tortoise.utils.diffusion import SAMPLERS
from app_utils.filepicker import st_file_selector
from app_utils.conf import TortoiseConfig
from app_utils.funcs import (
timeit,
load_model,
list_voices,
load_voice_conditionings,
)
LATENT_MODES = [
"Tortoise original (bad)",
"average per 4.27s (broken on small files)",
"average per voice file (broken on small files)",
]
def main():
conf = TortoiseConfig()
with st.expander("Create New Voice", expanded=True):
if "file_uploader_key" not in st.session_state:
st.session_state["file_uploader_key"] = str(randint(1000, 100000000))
st.session_state["text_input_key"] = str(randint(1000, 100000000))
uploaded_files = st.file_uploader(
"Upload Audio Samples for a New Voice",
accept_multiple_files=True,
type=["wav"],
key=st.session_state["file_uploader_key"]
)
voice_name = st.text_input(
"New Voice Name",
help="Enter a name for your new voice.",
value="",
key=st.session_state["text_input_key"]
)
create_voice_button = st.button(
"Create Voice",
disabled = ((voice_name.strip() == "") | (len(uploaded_files) == 0))
)
if create_voice_button:
st.write(st.session_state)
with st.spinner(f"Creating new voice: {voice_name}"):
new_voice_name = voice_name.strip().replace(" ", "_")
voices_dir = f'./tortoise/voices/{new_voice_name}/'
if os.path.exists(voices_dir):
shutil.rmtree(voices_dir)
os.makedirs(voices_dir)
for index, uploaded_file in enumerate(uploaded_files):
bytes_data = uploaded_file.read()
with open(f"{voices_dir}voice_sample{index}.wav", "wb") as wav_file:
wav_file.write(bytes_data)
st.session_state["text_input_key"] = str(randint(1000, 100000000))
st.session_state["file_uploader_key"] = str(randint(1000, 100000000))
st.experimental_rerun()
text = st.text_area(
"Text",
help="Text to speak.",
value="The expressiveness of autoregressive transformers is literally nuts! I absolutely adore them.",
)
voices = [v for v in os.listdir("tortoise/voices") if v != "cond_latent_example"]
voice = st.selectbox(
"Voice",
voices,
help="Selects the voice to use for generation. See options in voices/ directory (and add your own!) "
"Use the & character to join two voices together. Use a comma to perform inference on multiple voices.",
index=0,
)
preset = st.selectbox(
"Preset",
(
"single_sample",
"ultra_fast",
"very_fast",
"ultra_fast_old",
"fast",
"standard",
"high_quality",
),
help="Which voice preset to use.",
index=1,
)
with st.expander("Advanced"):
col1, col2 = st.columns(2)
with col1:
"""#### Model parameters"""
candidates = st.number_input(
"Candidates",
help="How many output candidates to produce per-voice.",
value=1,
)
latent_averaging_mode = st.radio(
"Latent averaging mode",
LATENT_MODES,
help="How voice samples should be averaged together.",
index=0,
)
sampler = st.radio(
"Sampler",
#SAMPLERS,
["dpm++2m", "p", "ddim"],
help="Diffusion sampler. Note that dpm++2m is experimental and typically requires more steps.",
index=1,
)
steps = st.number_input(
"Steps",
help="Override the steps used for diffusion (default depends on preset)",
value=10,
)
seed = st.number_input(
"Seed",
help="Random seed which can be used to reproduce results.",
value=-1,
)
if seed == -1:
seed = None
voice_fixer = st.checkbox(
"Voice fixer",
help="Use `voicefixer` to improve audio quality. This is a post-processing step which can be applied to any output.",
value=True,
)
"""#### Directories"""
output_path = st.text_input(
"Output Path", help="Where to store outputs.", value="results/"
)
with col2:
"""#### Optimizations"""
high_vram = not st.checkbox(
"Low VRAM",
help="Re-enable default offloading behaviour of tortoise",
value=True,
)
half = st.checkbox(
"Half-Precision",
help="Enable autocast to half precision for autoregressive model",
value=False,
)
kv_cache = st.checkbox(
"Key-Value Cache",
help="Enable kv_cache usage, leading to drastic speedups but worse memory usage",
value=True,
)
cond_free = st.checkbox(
"Conditioning Free",
help="Force conditioning free diffusion",
value=True,
)
no_cond_free = st.checkbox(
"Force Not Conditioning Free",
help="Force disable conditioning free diffusion",
value=False,
)
"""#### Text Splitting"""
min_chars_to_split = st.number_input(
"Min Chars to Split",
help="Minimum number of characters to split text on",
min_value=50,
value=200,
step=1,
)
"""#### Debug"""
produce_debug_state = st.checkbox(
"Produce Debug State",
help="Whether or not to produce debug_state.pth, which can aid in reproducing problems. Defaults to true.",
value=True,
)
ar_checkpoint = "."
diff_checkpoint = "."
if st.button("Update Basic Settings"):
conf.update(
EXTRA_VOICES_DIR=extra_voices_dir,
LOW_VRAM=not high_vram,
AR_CHECKPOINT=ar_checkpoint,
DIFF_CHECKPOINT=diff_checkpoint,
)
ar_checkpoint = None
diff_checkpoint = None
tts = load_model(MODELS_DIR, high_vram, kv_cache, ar_checkpoint, diff_checkpoint)
if st.button("Start"):
assert latent_averaging_mode
assert preset
assert voice
def show_generation(fp, filename: str):
"""
audio_buffer = BytesIO()
save_gen_with_voicefix(g, audio_buffer, squeeze=False)
torchaudio.save(audio_buffer, g, 24000, format='wav')
"""
st.audio(str(fp), format="audio/wav")
st.download_button(
"Download sample",
str(fp),
file_name=filename, # this doesn't actually seem to work lol
)
with st.spinner(
f"Generating {candidates} candidates for voice {voice} (seed={seed}). You can see progress in the terminal"
):
os.makedirs(output_path, exist_ok=True)
selected_voices = voice.split(",")
for k, selected_voice in enumerate(selected_voices):
if "&" in selected_voice:
voice_sel = selected_voice.split("&")
else:
voice_sel = [selected_voice]
voice_samples, conditioning_latents = load_voice_conditionings(
voice_sel, []
)
voice_path = Path(os.path.join(output_path, selected_voice))
with timeit(
f"Generating {candidates} candidates for voice {selected_voice} (seed={seed})"
):
nullable_kwargs = {
k: v
for k, v in zip(
["sampler", "diffusion_iterations", "cond_free"],
[sampler, steps, cond_free],
)
if v is not None
}
def call_tts(text: str):
return tts.tts_with_preset(
text,
k=candidates,
voice_samples=voice_samples,
conditioning_latents=conditioning_latents,
preset=preset,
use_deterministic_seed=seed,
return_deterministic_state=True,
cvvp_amount=0.0,
half=half,
latent_averaging_mode=LATENT_MODES.index(
latent_averaging_mode
),
**nullable_kwargs,
)
if len(text) < min_chars_to_split:
filepaths = run_and_save_tts(
call_tts,
text,
voice_path,
return_deterministic_state=True,
return_filepaths=True,
voicefixer=voice_fixer,
)
for i, fp in enumerate(filepaths):
show_generation(fp, f"{selected_voice}-text-{i}.wav")
else:
desired_length = int(min_chars_to_split)
texts = split_and_recombine_text(
text, desired_length, desired_length + 100
)
filepaths = infer_on_texts(
call_tts,
texts,
voice_path,
return_deterministic_state=True,
return_filepaths=True,
lines_to_regen=set(range(len(texts))),
voicefixer=voice_fixer,
)
for i, fp in enumerate(filepaths):
show_generation(fp, f"{selected_voice}-text-{i}.wav")
if produce_debug_state:
"""Debug states can be found in the output directory"""
if __name__ == "__main__":
main()
|