File size: 1,430 Bytes
04e3d32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from gtts import gTTS

# Step 1: Define a function to convert text to speech in multiple languages
def multilingual_tts(prompt, language):
    # Map user-friendly language names to gTTS language codes
    lang_code = {
        "Korean": "ko",
        "English": "en",
        "Russian": "ru",
        "Bulgarian": "bg"
    }
    
    if language not in lang_code:
        return "Unsupported language", None
    
    # Convert the input text to speech
    tts = gTTS(prompt, lang=lang_code[language])
    audio_file = f"output_{lang_code[language]}.mp3"
    tts.save(audio_file)
    
    return f"Text read in {language}", audio_file

# Step 2: Create Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("## Multilingual Text-to-Speech (TTS)")
    
    with gr.Row():
        input_prompt = gr.Textbox(label="Enter your text:")
        language_selector = gr.Dropdown(
            choices=["Korean", "English", "Russian", "Bulgarian"],
            label="Select Language",
            value="English"
        )
    
    output_text = gr.Textbox(label="Status")
    output_audio = gr.Audio(label="Generated Speech", type="filepath")
    generate_button = gr.Button("Generate Speech")
    
    generate_button.click(
        multilingual_tts, 
        inputs=[input_prompt, language_selector], 
        outputs=[output_text, output_audio]
    )

# Run the app
if __name__ == "__main__":
    demo.launch()