opentts / app.py
pr0fixi's picture
Update app.py
18c6b7a verified
import gradio as gr
from TTS.api import TTS
# Initialize Coqui TTS with a pre-trained model
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False)
def generate_speech(text):
"""
Generate speech from the input text.
Saves the audio to a temporary file and returns the file path.
"""
output_file = "/tmp/output.wav" # Temporary file for the generated audio
try:
tts.tts_to_file(text=text, file_path=output_file)
return output_file # Return the path to the generated audio
except Exception as e:
return f"Error generating speech: {str(e)}"
# Create a Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# Coqui TTS Demo")
gr.Markdown("Type some text and hear it spoken!")
with gr.Row():
input_text = gr.Textbox(label="Enter Text", placeholder="Type something...", lines=3)
with gr.Row():
submit_button = gr.Button("Generate Speech")
with gr.Row():
output_audio = gr.Audio(label="Generated Speech", type="filepath")
# Connect the button click to the generate_speech function
submit_button.click(generate_speech, inputs=input_text, outputs=output_audio)
# Launch the app
demo.launch(share=True)