englissi commited on
Commit
362d2fe
·
verified ·
1 Parent(s): 42859d3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
+ if __name__ == "__main__":
39
+ demo.launch()