Spaces:
Sleeping
Sleeping
Download app.py from Learnix-AI-Lab/Text-to-Speech: direct link, hf CLI and curl.
- Browser
- Download file 2.09 kB
-
https://huggingface.co/spaces/Learnix-AI-Lab/Text-to-Speech/resolve/main/app.py
- Command line
-
hf download hf://spaces/Learnix-AI-Lab/Text-to-Speech/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Learnix-AI-Lab/Text-to-Speech/resolve/main/app.py
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() |