File size: 1,177 Bytes
b8b16cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from gtts import gTTS
import os

def text_to_speech(text, lang="en"):
    """
    Convert text to speech using gTTS and return the audio file path.
    
    Args:
        text (str): The text to convert to speech.
        lang (str): Language code (default is 'en' for English).
    
    Returns:
        str: Path to the generated audio file.
    """
    try:
        # Create gTTS object
        tts = gTTS(text=text, lang=lang, slow=False)
        
        # Save the audio file
        output_file = "output.mp3"
        tts.save(output_file)
        
        return output_file
    
    except Exception as e:
        return f"An error occurred: {str(e)}"

# Define Gradio interface
demo = gr.Interface(
    fn=text_to_speech,
    inputs=[
        gr.Textbox(label="Enter text to convert to speech", placeholder="Type your text here..."),
        gr.Dropdown(choices=["en", "es", "fr"], label="Select Language", value="en")
    ],
    outputs=gr.Audio(label="Generated Speech"),
    title="Text-to-Speech with gTTS",
    description="Enter text and select a language to generate speech."
)

# Launch the app
if __name__ == "__main__":
    demo.launch()