Text-to-Speech / app.py
Learnix-AI-Lab's picture
Upload 2 files
dbee05b verified
Raw History Blame Contribute Delete
2.09 kB
import gradio as gr
from gtts import gTTS
import tempfile
import os
LANGUAGES = {
"English": "en",
"Bangla": "bn",
"Hindi": "hi",
"Arabic": "ar",
"Spanish": "es",
"French": "fr",
}
def text_to_speech(text, language):
if not text or text.strip() == "":
raise gr.Error("Please enter some text first.")
lang_code = LANGUAGES[language]
# Create temporary mp3 file
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
temp_file.close()
tts = gTTS(text=text, lang=lang_code)
tts.save(temp_file.name)
return temp_file.name
with gr.Blocks(title="Text to Speech Converter") as app:
gr.Markdown(
"""
# 🎙️ Text to Speech Converter
Convert text into natural voice audio.
"""
)
with gr.Row():
with gr.Column():
text_input = gr.Textbox(
label="Enter your text",
placeholder="Type your text here...",
lines=6
)
language_input = gr.Dropdown(
choices=list(LANGUAGES.keys()),
value="English",
label="Select language"
)
convert_btn = gr.Button("Convert to Speech", variant="primary")
with gr.Column():
audio_output = gr.Audio(
label="Generated Audio",
type="filepath",
format="mp3"
)
convert_btn.click(
fn=text_to_speech,
inputs=[text_input, language_input],
outputs=audio_output
)
gr.Examples(
examples=[
["Hello, welcome to my text to speech converter.", "English"],
["আমি বাংলায় কথা বলছি।", "Bangla"],
["नमस्ते, यह एक टेक्स्ट टू स्पीच ऐप है।", "Hindi"],
],
inputs=[text_input, language_input],
)
if __name__ == "__main__":
app.launch()