vuxuanhoan commited on
Commit
431f3dc
·
verified ·
1 Parent(s): d568754

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -29
app.py CHANGED
@@ -9,6 +9,11 @@ from docx import Document
9
  AUDIO_DIR = 'audio_files' # Thư mục để lưu tệp âm thanh
10
  MAX_FILE_AGE = 24 * 60 * 60 # Thời gian lưu trữ tệp âm thanh (24 giờ)
11
 
 
 
 
 
 
12
  async def text_to_speech(text, lang):
13
  tts = edge_tts.Communicate(text, voice=lang)
14
 
@@ -40,35 +45,41 @@ async def docx_to_speech(file, lang):
40
  return await text_to_speech(text, lang)
41
 
42
  # Tạo giao diện Gradio
43
- with gr.Blocks() as iface:
44
- with gr.Tab("Text to Speech"):
45
- gr.Markdown("### Convert text to speech")
46
- text_input = gr.Textbox(lines=10, label="Enter your text here:")
47
- lang_input = gr.Dropdown(choices=["vi-VN-NamMinhNeural", "en-US-JennyNeural"], label="Select language:")
48
-
49
- audio_output, file_output = gr.Audio(label="Audio"), gr.File(label="Audio File")
50
- gr.Button("Convert").click(fn=lambda text, lang: asyncio.run(text_to_speech(text, lang)),
51
- inputs=[text_input, lang_input],
52
- outputs=[audio_output, file_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
- with gr.Tab("TXT to Speech"):
55
- gr.Markdown("### Convert .txt file to speech")
56
- file_input = gr.File(label="Upload your .txt file")
57
- lang_input_file = gr.Dropdown(choices=["vi-VN-NamMinhNeural", "en-US-JennyNeural"], label="Select language:")
58
-
59
- audio_output_file, file_output_file = gr.Audio(label="Audio"), gr.File(label="Audio File")
60
- gr.Button("Convert").click(fn=lambda file, lang: asyncio.run(txt_to_speech(file, lang)),
61
- inputs=[file_input, lang_input_file],
62
- outputs=[audio_output_file, file_output_file])
63
 
64
- with gr.Tab("DOCX to Speech"):
65
- gr.Markdown("### Convert .docx file to speech")
66
- docx_file_input = gr.File(label="Upload your .docx file")
67
- lang_input_docx = gr.Dropdown(choices=["vi-VN-NamMinhNeural", "en-US-JennyNeural"], label="Select language:")
68
-
69
- audio_output_docx, file_output_docx = gr.Audio(label="Audio"), gr.File(label="Audio File")
70
- gr.Button("Convert").click(fn=lambda file, lang: asyncio.run(docx_to_speech(file, lang)),
71
- inputs=[docx_file_input, lang_input_docx],
72
- outputs=[audio_output_docx, file_output_docx])
73
 
74
- iface.launch(enable_queue=True)
 
 
9
  AUDIO_DIR = 'audio_files' # Thư mục để lưu tệp âm thanh
10
  MAX_FILE_AGE = 24 * 60 * 60 # Thời gian lưu trữ tệp âm thanh (24 giờ)
11
 
12
+ # Hàm để lấy tất cả các giọng nói có sẵn
13
+ async def get_voices():
14
+ voices = await edge_tts.list_voices()
15
+ return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
16
+
17
  async def text_to_speech(text, lang):
18
  tts = edge_tts.Communicate(text, voice=lang)
19
 
 
45
  return await text_to_speech(text, lang)
46
 
47
  # Tạo giao diện Gradio
48
+ async def create_interface():
49
+ voices = await get_voices() # Lấy danh sách giọng nói
50
+
51
+ with gr.Blocks() as iface:
52
+ with gr.Tab("Text to Speech"):
53
+ gr.Markdown("### Convert text to speech")
54
+ text_input = gr.Textbox(lines=10, label="Enter your text here:")
55
+ lang_input = gr.Dropdown(choices=list(voices.keys()), label="Select language:") # Cập nhật dropdown giọng nói
56
+
57
+ audio_output, file_output = gr.Audio(label="Audio"), gr.File(label="Audio File")
58
+ gr.Button("Convert").click(fn=lambda text, lang: asyncio.run(text_to_speech(text, voices[lang])),
59
+ inputs=[text_input, lang_input],
60
+ outputs=[audio_output, file_output])
61
+
62
+ with gr.Tab("TXT to Speech"):
63
+ gr.Markdown("### Convert .txt file to speech")
64
+ file_input = gr.File(label="Upload your .txt file")
65
+ lang_input_file = gr.Dropdown(choices=list(voices.keys()), label="Select language:") # Cập nhật dropdown giọng nói
66
+
67
+ audio_output_file, file_output_file = gr.Audio(label="Audio"), gr.File(label="Audio File")
68
+ gr.Button("Convert").click(fn=lambda file, lang: asyncio.run(txt_to_speech(file, voices[lang])),
69
+ inputs=[file_input, lang_input_file],
70
+ outputs=[audio_output_file, file_output_file])
71
 
72
+ with gr.Tab("DOCX to Speech"):
73
+ gr.Markdown("### Convert .docx file to speech")
74
+ docx_file_input = gr.File(label="Upload your .docx file")
75
+ lang_input_docx = gr.Dropdown(choices=list(voices.keys()), label="Select language:") # Cập nhật dropdown giọng nói
76
+
77
+ audio_output_docx, file_output_docx = gr.Audio(label="Audio"), gr.File(label="Audio File")
78
+ gr.Button("Convert").click(fn=lambda file, lang: asyncio.run(docx_to_speech(file, voices[lang])),
79
+ inputs=[docx_file_input, lang_input_docx],
80
+ outputs=[audio_output_docx, file_output_docx])
81
 
82
+ iface.launch(enable_queue=True)
 
 
 
 
 
 
 
 
83
 
84
+ # Chạy ứng dụng
85
+ asyncio.run(create_interface())