2023-02-02 21:13:28 +00:00
|
|
|
import os
|
|
|
|
import argparse
|
|
|
|
import gradio as gr
|
2023-02-04 01:50:57 +00:00
|
|
|
import torch
|
2023-02-02 21:13:28 +00:00
|
|
|
import torchaudio
|
|
|
|
import time
|
2023-02-04 01:50:57 +00:00
|
|
|
|
2023-02-02 21:13:28 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from tortoise.api import TextToSpeech
|
|
|
|
from tortoise.utils.audio import load_audio, load_voice, load_voices
|
|
|
|
|
2023-02-03 04:20:01 +00:00
|
|
|
def inference(text, emotion, prompt, voice, mic_audio, preset, seed, candidates, num_autoregressive_samples, diffusion_iterations, temperature, progress=gr.Progress()):
|
2023-02-03 01:25:03 +00:00
|
|
|
if voice != "microphone":
|
2023-02-02 21:13:28 +00:00
|
|
|
voices = [voice]
|
|
|
|
else:
|
|
|
|
voices = []
|
|
|
|
|
2023-02-03 01:25:03 +00:00
|
|
|
if emotion == "Custom" and prompt.strip() != "":
|
2023-02-02 21:13:28 +00:00
|
|
|
text = f"[{prompt},] {text}"
|
2023-02-03 01:25:03 +00:00
|
|
|
elif emotion != "None":
|
|
|
|
text = f"[I am really {emotion.lower()},] {text}"
|
2023-02-02 21:13:28 +00:00
|
|
|
|
2023-02-03 01:25:03 +00:00
|
|
|
if voice == "microphone":
|
2023-02-02 21:13:28 +00:00
|
|
|
if mic_audio is None:
|
2023-02-03 01:25:03 +00:00
|
|
|
raise gr.Error("Please provide audio from mic when choosing `microphone` as a voice input")
|
2023-02-04 01:50:57 +00:00
|
|
|
mic = load_audio(mic_audio, 22050)
|
|
|
|
voice_samples, conditioning_latents = [mic], None
|
2023-02-02 21:13:28 +00:00
|
|
|
else:
|
2023-02-04 01:50:57 +00:00
|
|
|
voice_samples, conditioning_latents = load_voice(voice)
|
|
|
|
|
|
|
|
if voice_samples is not None:
|
|
|
|
sample_voice = voice_samples[0]
|
|
|
|
conditioning_latents = tts.get_conditioning_latents(voice_samples)
|
|
|
|
torch.save(conditioning_latents, os.path.join(f'./tortoise/voices/{voice}/', f'latents.pth'))
|
|
|
|
voice_samples = None
|
|
|
|
else:
|
|
|
|
sample_voice = None
|
2023-02-02 21:13:28 +00:00
|
|
|
|
2023-02-03 01:25:03 +00:00
|
|
|
if seed == 0:
|
|
|
|
seed = None
|
|
|
|
|
2023-02-02 21:13:28 +00:00
|
|
|
start_time = time.time()
|
2023-02-03 04:20:01 +00:00
|
|
|
|
2023-02-04 01:50:57 +00:00
|
|
|
presets = {
|
|
|
|
'ultra_fast': {'num_autoregressive_samples': 16, 'diffusion_iterations': 30, 'cond_free': False},
|
|
|
|
'fast': {'num_autoregressive_samples': 96, 'diffusion_iterations': 80},
|
|
|
|
'standard': {'num_autoregressive_samples': 256, 'diffusion_iterations': 200},
|
|
|
|
'high_quality': {'num_autoregressive_samples': 256, 'diffusion_iterations': 400},
|
|
|
|
'none': {'num_autoregressive_samples': num_autoregressive_samples, 'diffusion_iterations': diffusion_iterations},
|
|
|
|
}
|
|
|
|
settings = {
|
|
|
|
'temperature': temperature, 'length_penalty': 1.0, 'repetition_penalty': 2.0,
|
|
|
|
'top_p': .8,
|
|
|
|
'cond_free_k': 2.0, 'diffusion_temperature': 1.0,
|
|
|
|
|
|
|
|
'voice_samples': voice_samples,
|
|
|
|
'conditioning_latents': conditioning_latents,
|
|
|
|
'use_deterministic_seed': seed,
|
|
|
|
'return_deterministic_state': True,
|
|
|
|
'k': candidates,
|
|
|
|
'progress': progress,
|
|
|
|
}
|
|
|
|
settings.update(presets[preset])
|
|
|
|
gen, additionals = tts.tts( text, **settings )
|
|
|
|
seed = additionals[0]
|
2023-02-02 21:13:28 +00:00
|
|
|
|
2023-02-03 04:20:01 +00:00
|
|
|
info = f"{datetime.now()} | Voice: {','.join(voices)} | Text: {text} | Quality: {preset} preset / {num_autoregressive_samples} samples / {diffusion_iterations} iterations | Temperature: {temperature} | Time Taken (s): {time.time()-start_time} | Seed: {seed}\n"
|
2023-02-03 01:25:03 +00:00
|
|
|
with open("results.log", "a") as f:
|
2023-02-03 04:20:01 +00:00
|
|
|
f.write(info)
|
2023-02-02 21:13:28 +00:00
|
|
|
|
|
|
|
timestamp = int(time.time())
|
|
|
|
outdir = f"./results/{voice}/{timestamp}/"
|
|
|
|
|
|
|
|
os.makedirs(outdir, exist_ok=True)
|
|
|
|
|
|
|
|
with open(os.path.join(outdir, f'input.txt'), 'w') as f:
|
2023-02-03 04:20:01 +00:00
|
|
|
f.write(f"{text}\n\n{info}")
|
2023-02-02 21:13:28 +00:00
|
|
|
|
|
|
|
if isinstance(gen, list):
|
|
|
|
for j, g in enumerate(gen):
|
|
|
|
torchaudio.save(os.path.join(outdir, f'result_{j}.wav'), g.squeeze(0).cpu(), 24000)
|
2023-02-04 01:50:57 +00:00
|
|
|
|
|
|
|
output_voice = gen[0]
|
2023-02-02 21:13:28 +00:00
|
|
|
else:
|
|
|
|
torchaudio.save(os.path.join(outdir, f'result.wav'), gen.squeeze(0).cpu(), 24000)
|
2023-02-04 01:50:57 +00:00
|
|
|
output_voice = gen
|
2023-02-02 21:13:28 +00:00
|
|
|
|
2023-02-04 01:50:57 +00:00
|
|
|
output_voice = (24000, output_voice.squeeze().cpu().numpy())
|
|
|
|
|
|
|
|
if sample_voice is not None:
|
|
|
|
sample_voice = (22050, sample_voice.squeeze().cpu().numpy())
|
2023-02-02 21:13:28 +00:00
|
|
|
|
2023-02-04 01:50:57 +00:00
|
|
|
return (
|
|
|
|
sample_voice,
|
|
|
|
output_voice,
|
|
|
|
seed
|
|
|
|
)
|
|
|
|
|
|
|
|
def main():
|
2023-02-03 01:25:03 +00:00
|
|
|
text = gr.Textbox(lines=4, label="Prompt")
|
2023-02-02 21:13:28 +00:00
|
|
|
emotion = gr.Radio(
|
2023-02-03 01:25:03 +00:00
|
|
|
["None", "Happy", "Sad", "Angry", "Disgusted", "Arrogant", "Custom"],
|
|
|
|
value="None",
|
|
|
|
label="Emotion",
|
2023-02-02 21:13:28 +00:00
|
|
|
type="value",
|
|
|
|
)
|
2023-02-03 01:25:03 +00:00
|
|
|
prompt = gr.Textbox(lines=1, label="Custom Emotion (if selected)")
|
2023-02-02 21:13:28 +00:00
|
|
|
preset = gr.Radio(
|
2023-02-03 01:25:03 +00:00
|
|
|
["ultra_fast", "fast", "standard", "high_quality", "none"],
|
|
|
|
value="none",
|
|
|
|
label="Preset",
|
2023-02-02 21:13:28 +00:00
|
|
|
type="value",
|
|
|
|
)
|
2023-02-03 04:56:30 +00:00
|
|
|
candidates = gr.Slider(value=1, minimum=1, maximum=6, label="Candidates")
|
|
|
|
num_autoregressive_samples = gr.Slider(value=128, minimum=0, maximum=512, step=1, label="Samples")
|
|
|
|
diffusion_iterations = gr.Slider(value=128, minimum=0, maximum=512, step=1, label="Iterations")
|
2023-02-02 21:13:28 +00:00
|
|
|
temperature = gr.Slider(value=0.2, minimum=0, maximum=1, step=0.1, label="Temperature")
|
|
|
|
|
|
|
|
voice = gr.Dropdown(
|
2023-02-03 04:20:01 +00:00
|
|
|
os.listdir(os.path.join("tortoise", "voices")) + ["random", "microphone", "disabled"],
|
2023-02-03 01:25:03 +00:00
|
|
|
label="Voice",
|
2023-02-02 21:13:28 +00:00
|
|
|
type="value",
|
|
|
|
)
|
|
|
|
mic_audio = gr.Audio(
|
2023-02-03 01:25:03 +00:00
|
|
|
label="Microphone Source",
|
2023-02-02 21:13:28 +00:00
|
|
|
source="microphone",
|
|
|
|
type="filepath",
|
|
|
|
)
|
2023-02-03 01:25:03 +00:00
|
|
|
seed = gr.Number(value=0, precision=0, label="Seed")
|
2023-02-02 21:13:28 +00:00
|
|
|
|
2023-02-03 01:25:03 +00:00
|
|
|
selected_voice = gr.Audio(label="Source Sample")
|
|
|
|
output_audio = gr.Audio(label="Output")
|
|
|
|
usedSeed = gr.Textbox(label="Seed", placeholder="0", interactive=False)
|
2023-02-02 21:13:28 +00:00
|
|
|
|
|
|
|
interface = gr.Interface(
|
|
|
|
fn=inference,
|
|
|
|
inputs=[
|
|
|
|
text,
|
|
|
|
emotion,
|
|
|
|
prompt,
|
|
|
|
voice,
|
|
|
|
mic_audio,
|
|
|
|
preset,
|
|
|
|
seed,
|
|
|
|
candidates,
|
|
|
|
num_autoregressive_samples,
|
|
|
|
diffusion_iterations,
|
|
|
|
temperature
|
|
|
|
],
|
2023-02-03 01:25:03 +00:00
|
|
|
outputs=[selected_voice, output_audio, usedSeed],
|
2023-02-04 01:50:57 +00:00
|
|
|
allow_flagging='never'
|
2023-02-02 21:13:28 +00:00
|
|
|
)
|
|
|
|
interface.queue().launch(share=args.share)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-02-04 01:50:57 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("--share", action='store_true', help="Lets Gradio return a public URL to use anywhere")
|
|
|
|
parser.add_argument("--low-vram", action='store_true', help="Disables some optimizations that increases VRAM usage")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
tts = TextToSpeech(minor_optimizations=not args.low_vram)
|
|
|
|
|
2023-02-03 01:25:03 +00:00
|
|
|
main()
|