Spaces:
Running
Running
import gradio as gr | |
from gtts import gTTS | |
from pydub import AudioSegment | |
from pydub.playback import play | |
from io import BytesIO | |
# Step 1: Define a function to generate and merge TTS audio for multiple languages | |
def multilingual_tts(korean_text, english_text, russian_text, bulgarian_text): | |
# Language mapping | |
texts = { | |
"ko": korean_text, | |
"en": english_text, | |
"ru": russian_text, | |
"bg": bulgarian_text, | |
} | |
combined_audio = AudioSegment.silent(duration=0) # Empty audio to start | |
for lang, text in texts.items(): | |
if text.strip(): # Process only if text is provided | |
tts = gTTS(text, lang=lang) | |
audio_file = BytesIO() | |
tts.write_to_fp(audio_file) | |
audio_file.seek(0) | |
tts_audio = AudioSegment.from_file(audio_file, format="mp3") | |
combined_audio += tts_audio + AudioSegment.silent(duration=500) # Add silence between languages | |
# Save combined audio to a file | |
output_file = "combined_output.mp3" | |
combined_audio.export(output_file, format="mp3") | |
return output_file | |
# Step 2: Create Gradio interface | |
with gr.Blocks() as demo: | |
gr.Markdown("## Multilingual TTS: Generate a Single Audio File") | |
with gr.Row(): | |
korean_input = gr.Textbox(label="Enter Korean Text:", placeholder="안녕하세요") | |
english_input = gr.Textbox(label="Enter English Text:", placeholder="Hello") | |
russian_input = gr.Textbox(label="Enter Russian Text:", placeholder="Привет") | |
bulgarian_input = gr.Textbox(label="Enter Bulgarian Text:", placeholder="Здравейте") | |
output_audio = gr.Audio(label="Generated Speech", type="filepath") | |
generate_button = gr.Button("Generate Speech") | |
generate_button.click( | |
multilingual_tts, | |
inputs=[korean_input, english_input, russian_input, bulgarian_input], | |
outputs=output_audio | |
) | |
# Run the app | |
if __name__ == "__main__": | |
demo.launch() | |