vocaltwin / app.py
GeminiAi's picture
Update app.py
2232b15 verified
raw
history blame
2.45 kB
import gradio as gr
import torchaudio
from tortoise.api import TextToSpeech
from tortoise.utils.audio import load_voice
# Initialize TorToiSe
tts = TextToSpeech()
# List of available voices
AVAILABLE_VOICES = [
"angie", "daniel", "deniro", "emma", "freeman", "geralt", "halle",
"jlaw", "lj", "myself", "pat", "snakes", "tom", "train_atkins",
"train_dotrice", "train_kennard", "weaver", "william"
]
# Function to generate speech
def generate_speech(text, voice):
if not text:
return "Please enter some text."
try:
# Load the selected voice
voice_samples, conditioning_latents = load_voice(voice)
# Generate speech
gen = tts.tts_with_preset(
text,
voice_samples=voice_samples,
conditioning_latents=conditioning_latents,
preset="fast"
)
# Save the output
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)}"
# Gradio interface
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:
# Title and description
gr.Markdown(
"""
# 🎀 **TorToiSe Text-to-Speech**
**Generate natural-sounding speech in multiple voices!**
"""
)
# Inputs
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" # Default voice
)
# Output
output_audio = gr.Audio(label="🎧 **Generated Speech**", type="filepath")
# Submit button
submit_button = gr.Button("✨ **Generate Speech**")
# Link button to function
submit_button.click(
generate_speech,
inputs=[text_input, voice_input],
outputs=output_audio
)
# Launch the app
demo.launch()