Spaces:
Sleeping
Sleeping
File size: 2,008 Bytes
362d2fe 279d45e 362d2fe 279d45e 362d2fe 279d45e 362d2fe 279d45e 362d2fe 279d45e 362d2fe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
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()
|