File size: 1,826 Bytes
21772a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
import gradio as gr
import torch
from transformers import pipeline

# Load the available pre-trained TTS models from Hugging Face
models = {
    "FastSpeech2": "espnet/fastspeech2-ljspeech",
    "Tacotron2": "tacotron2-ljspeech",
    "Glow-TTS": "glow-tts-ljspeech"
}

# Set up a function to generate TTS from selected model
def generate_audio(model_name, input_text):
    # Load the model using the Hugging Face pipeline
    if model_name == "FastSpeech2":
        model = pipeline("text-to-speech", model="espnet/fastspeech2-ljspeech")
    elif model_name == "Tacotron2":
        model = pipeline("text-to-speech", model="tacotron2-ljspeech")
    elif model_name == "Glow-TTS":
        model = pipeline("text-to-speech", model="glow-tts-ljspeech")
    
    # Generate the audio from the input text
    audio = model(input_text)
    return audio

# Create a Gradio interface with model selection and text input
def gradio_interface():
    with gr.Blocks() as demo:
        gr.Markdown("### Select a model, input text and generate audio.")
        
        # Dropdown to select model
        model_dropdown = gr.Dropdown(choices=list(models.keys()), label="Select TTS Model", default="FastSpeech2")
        
        # Text input for user input text
        text_input = gr.Textbox(label="Input Text", placeholder="Type your text here...")
        
        # Button to generate audio
        generate_button = gr.Button("Generate Audio")
        
        # Output audio player
        audio_output = gr.Audio(type="filepath", label="Generated Audio")

        # Generate audio when button is clicked
        generate_button.click(fn=generate_audio, inputs=[model_dropdown, text_input], outputs=audio_output)

    demo.launch()

if __name__ == "__main__":
    gradio_interface()