|
import gradio as gr |
|
import torchaudio |
|
from tortoise.api import TextToSpeech |
|
from tortoise.utils.audio import load_voice |
|
|
|
|
|
tts = TextToSpeech() |
|
|
|
|
|
AVAILABLE_VOICES = [ |
|
"angie", "daniel", "deniro", "emma", "freeman", "geralt", "halle", |
|
"jlaw", "lj", "myself", "pat", "snakes", "tom", "train_atkins", |
|
"train_dotrice", "train_kennard", "weaver", "william" |
|
] |
|
|
|
|
|
def generate_speech(text, voice): |
|
if not text: |
|
return "Please enter some text." |
|
|
|
try: |
|
|
|
voice_samples, conditioning_latents = load_voice(voice) |
|
|
|
|
|
gen = tts.tts_with_preset( |
|
text, |
|
voice_samples=voice_samples, |
|
conditioning_latents=conditioning_latents, |
|
preset="fast" |
|
) |
|
|
|
|
|
output_file = "output.wav" |
|
torchaudio.save(output_file, gen.squeeze(0).cpu(), 24000) |
|
|
|
return output_file |
|
except Exception as e: |
|
return f"Error generating speech: {str(e)}" |
|
|
|
|
|
with gr.Blocks( |
|
theme=gr.themes.Soft(primary_hue="teal", secondary_hue="pink"), |
|
css=".gradio-container {background: linear-gradient(135deg, #f5f7fa, #c3cfe2);} " |
|
"button {background: linear-gradient(135deg, #6a11cb, #2575fc); color: white; border: none; padding: 10px 20px; border-radius: 5px;} " |
|
"button:hover {background: linear-gradient(135deg, #2575fc, #6a11cb);} " |
|
) as demo: |
|
|
|
gr.Markdown( |
|
""" |
|
# π€ **TorToiSe Text-to-Speech** |
|
**Generate natural-sounding speech in multiple voices!** |
|
""" |
|
) |
|
|
|
|
|
with gr.Row(): |
|
text_input = gr.Textbox( |
|
lines=5, |
|
label="π **Enter Text**", |
|
placeholder="Type your text here..." |
|
) |
|
voice_input = gr.Dropdown( |
|
choices=AVAILABLE_VOICES, |
|
label="π£οΈ **Select Voice**", |
|
value="emma" |
|
) |
|
|
|
|
|
output_audio = gr.Audio(label="π§ **Generated Speech**", type="filepath") |
|
|
|
|
|
submit_button = gr.Button("β¨ **Generate Speech**") |
|
|
|
|
|
submit_button.click( |
|
generate_speech, |
|
inputs=[text_input, voice_input], |
|
outputs=output_audio |
|
) |
|
|
|
|
|
demo.launch() |