File size: 1,245 Bytes
560bcfb 0149ced 186f296 18c6b7a 186f296 18c6b7a f714f5f 18c6b7a f714f5f 0149ced 18c6b7a 0149ced 18c6b7a 0149ced 18c6b7a 0149ced 18c6b7a 58d8c1b |
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 |
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) |