Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +81 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
LANGUAGES = {
|
| 8 |
+
"English": "en",
|
| 9 |
+
"Bangla": "bn",
|
| 10 |
+
"Hindi": "hi",
|
| 11 |
+
"Arabic": "ar",
|
| 12 |
+
"Spanish": "es",
|
| 13 |
+
"French": "fr",
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def text_to_speech(text, language):
|
| 18 |
+
if not text or text.strip() == "":
|
| 19 |
+
raise gr.Error("Please enter some text first.")
|
| 20 |
+
|
| 21 |
+
lang_code = LANGUAGES[language]
|
| 22 |
+
|
| 23 |
+
# Create temporary mp3 file
|
| 24 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 25 |
+
temp_file.close()
|
| 26 |
+
|
| 27 |
+
tts = gTTS(text=text, lang=lang_code)
|
| 28 |
+
tts.save(temp_file.name)
|
| 29 |
+
|
| 30 |
+
return temp_file.name
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
with gr.Blocks(title="Text to Speech Converter") as app:
|
| 34 |
+
gr.Markdown(
|
| 35 |
+
"""
|
| 36 |
+
# 🎙️ Text to Speech Converter
|
| 37 |
+
Convert text into natural voice audio.
|
| 38 |
+
"""
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
with gr.Row():
|
| 42 |
+
with gr.Column():
|
| 43 |
+
text_input = gr.Textbox(
|
| 44 |
+
label="Enter your text",
|
| 45 |
+
placeholder="Type your text here...",
|
| 46 |
+
lines=6
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
language_input = gr.Dropdown(
|
| 50 |
+
choices=list(LANGUAGES.keys()),
|
| 51 |
+
value="English",
|
| 52 |
+
label="Select language"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
convert_btn = gr.Button("Convert to Speech", variant="primary")
|
| 56 |
+
|
| 57 |
+
with gr.Column():
|
| 58 |
+
audio_output = gr.Audio(
|
| 59 |
+
label="Generated Audio",
|
| 60 |
+
type="filepath",
|
| 61 |
+
format="mp3"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
convert_btn.click(
|
| 65 |
+
fn=text_to_speech,
|
| 66 |
+
inputs=[text_input, language_input],
|
| 67 |
+
outputs=audio_output
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
gr.Examples(
|
| 71 |
+
examples=[
|
| 72 |
+
["Hello, welcome to my text to speech converter.", "English"],
|
| 73 |
+
["আমি বাংলায় কথা বলছি।", "Bangla"],
|
| 74 |
+
["नमस्ते, यह एक टेक्स्ट टू स्पीच ऐप है।", "Hindi"],
|
| 75 |
+
],
|
| 76 |
+
inputs=[text_input, language_input],
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
app.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
gTTS
|