Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gtts import gTTS
|
3 |
+
|
4 |
+
# Step 1: Define a function to convert text to speech in multiple languages
|
5 |
+
def multilingual_tts(prompt, language):
|
6 |
+
# Map user-friendly language names to gTTS language codes
|
7 |
+
lang_code = {
|
8 |
+
"Korean": "ko",
|
9 |
+
"English": "en",
|
10 |
+
"Russian": "ru",
|
11 |
+
"Bulgarian": "bg"
|
12 |
+
}
|
13 |
+
|
14 |
+
if language not in lang_code:
|
15 |
+
return "Unsupported language", None
|
16 |
+
|
17 |
+
# Convert the input text to speech
|
18 |
+
tts = gTTS(prompt, lang=lang_code[language])
|
19 |
+
audio_file = f"output_{lang_code[language]}.mp3"
|
20 |
+
tts.save(audio_file)
|
21 |
+
|
22 |
+
return f"Text read in {language}", audio_file
|
23 |
+
|
24 |
+
# Step 2: Create Gradio interface
|
25 |
+
with gr.Blocks() as demo:
|
26 |
+
gr.Markdown("## Multilingual Text-to-Speech (TTS)")
|
27 |
+
|
28 |
+
with gr.Row():
|
29 |
+
input_prompt = gr.Textbox(label="Enter your text:")
|
30 |
+
language_selector = gr.Dropdown(
|
31 |
+
choices=["Korean", "English", "Russian", "Bulgarian"],
|
32 |
+
label="Select Language",
|
33 |
+
value="English"
|
34 |
+
)
|
35 |
+
|
36 |
+
output_text = gr.Textbox(label="Status")
|
37 |
+
output_audio = gr.Audio(label="Generated Speech", type="filepath")
|
38 |
+
generate_button = gr.Button("Generate Speech")
|
39 |
+
|
40 |
+
generate_button.click(
|
41 |
+
multilingual_tts,
|
42 |
+
inputs=[input_prompt, language_selector],
|
43 |
+
outputs=[output_text, output_audio]
|
44 |
+
)
|
45 |
+
|
46 |
+
# Run the app
|
47 |
+
if __name__ == "__main__":
|
48 |
+
demo.launch()
|