Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gtts import gTTS
|
3 |
+
from pydub import AudioSegment
|
4 |
+
from io import BytesIO
|
5 |
+
|
6 |
+
# Step 1: Define a function to generate and merge TTS audio for multiple languages
|
7 |
+
def multilingual_tts(korean_text, british_english_text, american_english_text):
|
8 |
+
# Language mapping
|
9 |
+
texts = {
|
10 |
+
"ko": korean_text,
|
11 |
+
"en-gb": british_english_text, # British English
|
12 |
+
"en-us": american_english_text, # American English
|
13 |
+
}
|
14 |
+
|
15 |
+
combined_audio = AudioSegment.silent(duration=0) # Empty audio to start
|
16 |
+
|
17 |
+
for lang, text in texts.items():
|
18 |
+
if text.strip(): # Process only if text is provided
|
19 |
+
tld = 'co.uk' if lang == "en-gb" else 'com'
|
20 |
+
tts = gTTS(text, lang="en" if lang.startswith("en") else lang, tld=tld)
|
21 |
+
audio_file = BytesIO()
|
22 |
+
tts.write_to_fp(audio_file)
|
23 |
+
audio_file.seek(0)
|
24 |
+
tts_audio = AudioSegment.from_file(audio_file, format="mp3")
|
25 |
+
combined_audio += tts_audio + AudioSegment.silent(duration=500) # Add silence between languages
|
26 |
+
|
27 |
+
# Save combined audio to a file
|
28 |
+
output_file = "combined_output.mp3"
|
29 |
+
combined_audio.export(output_file, format="mp3")
|
30 |
+
|
31 |
+
return output_file
|
32 |
+
|
33 |
+
# Step 2: Create Gradio interface
|
34 |
+
with gr.Blocks() as demo:
|
35 |
+
gr.Markdown("## Multilingual TTS: Generate a Single Audio File")
|
36 |
+
|
37 |
+
with gr.Row():
|
38 |
+
korean_input = gr.Textbox(label="Enter Korean Text:", placeholder="안녕하세요")
|
39 |
+
british_english_input = gr.Textbox(label="Enter British English Text:", placeholder="Hello (British)")
|
40 |
+
american_english_input = gr.Textbox(label="Enter American English Text:", placeholder="Hello (American)")
|
41 |
+
|
42 |
+
output_audio = gr.Audio(label="Generated Speech", type="filepath")
|
43 |
+
generate_button = gr.Button("Generate Speech")
|
44 |
+
|
45 |
+
generate_button.click(
|
46 |
+
multilingual_tts,
|
47 |
+
inputs=[korean_input, british_english_input, american_english_input],
|
48 |
+
outputs=output_audio
|
49 |
+
)
|
50 |
+
|
51 |
+
# Run the app
|
52 |
+
if __name__ == "__main__":
|
53 |
+
demo.launch()
|