TTS-EN / app.py
diginoron's picture
Create app.py
b8b16cf verified
raw
history blame
1.18 kB
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()