englissi commited on
Commit
279d45e
·
verified ·
1 Parent(s): 3ad3916

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -21
app.py CHANGED
@@ -1,37 +1,53 @@
1
  import gradio as gr
2
  from gtts import gTTS
3
- from langdetect import detect
 
 
4
 
5
- # Step 1: Define a function to detect language and convert text to speech
6
- def auto_detect_tts(prompt):
7
- try:
8
- # Detect language of the input text
9
- lang_code = detect(prompt)
10
-
11
- # Convert the input text to speech
12
- tts = gTTS(prompt, lang=lang_code)
13
- audio_file = f"output_{lang_code}.mp3"
14
- tts.save(audio_file)
15
-
16
- return f"Detected language: {lang_code.upper()} ({prompt})", audio_file
17
- except Exception as e:
18
- return f"Error: {str(e)}", None
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  # Step 2: Create Gradio interface
21
  with gr.Blocks() as demo:
22
- gr.Markdown("## Auto-Detect Language Text-to-Speech (TTS)")
23
 
24
  with gr.Row():
25
- input_prompt = gr.Textbox(label="Enter your text:")
 
 
 
26
 
27
- output_text = gr.Textbox(label="Status")
28
  output_audio = gr.Audio(label="Generated Speech", type="filepath")
29
  generate_button = gr.Button("Generate Speech")
30
 
31
  generate_button.click(
32
- auto_detect_tts,
33
- inputs=input_prompt,
34
- outputs=[output_text, output_audio]
35
  )
36
 
37
  # Run the app
 
1
  import gradio as gr
2
  from gtts import gTTS
3
+ from pydub import AudioSegment
4
+ from pydub.playback import play
5
+ from io import BytesIO
6
 
7
+ # Step 1: Define a function to generate and merge TTS audio for multiple languages
8
+ def multilingual_tts(korean_text, english_text, russian_text, bulgarian_text):
9
+ # Language mapping
10
+ texts = {
11
+ "ko": korean_text,
12
+ "en": english_text,
13
+ "ru": russian_text,
14
+ "bg": bulgarian_text,
15
+ }
16
+
17
+ combined_audio = AudioSegment.silent(duration=0) # Empty audio to start
18
+
19
+ for lang, text in texts.items():
20
+ if text.strip(): # Process only if text is provided
21
+ tts = gTTS(text, lang=lang)
22
+ audio_file = BytesIO()
23
+ tts.write_to_fp(audio_file)
24
+ audio_file.seek(0)
25
+ tts_audio = AudioSegment.from_file(audio_file, format="mp3")
26
+ combined_audio += tts_audio + AudioSegment.silent(duration=500) # Add silence between languages
27
+
28
+ # Save combined audio to a file
29
+ output_file = "combined_output.mp3"
30
+ combined_audio.export(output_file, format="mp3")
31
+
32
+ return output_file
33
 
34
  # Step 2: Create Gradio interface
35
  with gr.Blocks() as demo:
36
+ gr.Markdown("## Multilingual TTS: Generate a Single Audio File")
37
 
38
  with gr.Row():
39
+ korean_input = gr.Textbox(label="Enter Korean Text:", placeholder="안녕하세요")
40
+ english_input = gr.Textbox(label="Enter English Text:", placeholder="Hello")
41
+ russian_input = gr.Textbox(label="Enter Russian Text:", placeholder="Привет")
42
+ bulgarian_input = gr.Textbox(label="Enter Bulgarian Text:", placeholder="Здравейте")
43
 
 
44
  output_audio = gr.Audio(label="Generated Speech", type="filepath")
45
  generate_button = gr.Button("Generate Speech")
46
 
47
  generate_button.click(
48
+ multilingual_tts,
49
+ inputs=[korean_input, english_input, russian_input, bulgarian_input],
50
+ outputs=output_audio
51
  )
52
 
53
  # Run the app