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

# Function to convert text to speech and save as an audio file
def text_to_speech(text, language="hr"):
    # Create a gTTS object
    tts = gTTS(text=text, lang=language, slow=False)
    
    # Save the audio file
    audio_file = "output.mp3"
    tts.save(audio_file)
    
    return audio_file

# Gradio interface
def tts_demo(text, language):
    # Convert text to speech
    audio_file = text_to_speech(text, language)
    
    # Return the audio file to be played in the Gradio interface
    return audio_file

# Define Gradio inputs and outputs
inputs = [
    gr.Textbox(label="Enter Text", placeholder="Type something here..."),
    gr.Dropdown(label="Language", choices=["en", "es", "fr", "de", "hr"], value="hr")
]
outputs = gr.Audio(label="Generated Audio", type="filepath")

# Create the Gradio interface
iface = gr.Interface(
    fn=tts_demo,
    inputs=inputs,
    outputs=outputs,
    title="Text-to-Speech Demo",
    description="Enter text and select a language to generate and play audio."
)

# Launch the app
iface.launch()