vuxuanhoan commited on
Commit
a689c10
·
verified ·
1 Parent(s): 071ff53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -14
app.py CHANGED
@@ -10,7 +10,7 @@ AUDIO_DIR = 'audio_files'
10
  MAX_FILE_AGE = 24 * 60 * 60 # maximum age of audio files in seconds (24 hours)
11
 
12
  # Hàm chuyển đổi văn bản thành giọng nói sử dụng Edge TTS
13
- async def text_to_speech(text, lang, tld):
14
  tts = edge_tts.Communicate(text, voice=lang) # Chọn giọng nói tương ứng với ngôn ngữ
15
  fp = io.BytesIO()
16
 
@@ -22,6 +22,7 @@ async def text_to_speech(text, lang, tld):
22
  file_name = str(time.time()) + '.mp3' # Đổi định dạng thành mp3
23
  file_path = os.path.join(AUDIO_DIR, file_name)
24
 
 
25
  with open(file_path, 'wb') as f:
26
  f.write(fp.read())
27
 
@@ -36,16 +37,16 @@ def delete_old_audio_files():
36
  os.remove(file_path)
37
 
38
  # Hàm chuyển đổi file .txt thành giọng nói
39
- async def txt_to_speech(file, lang, tld):
40
  with open(file.name, 'r') as f:
41
  text = f.read()
42
- return await text_to_speech(text, lang, tld)
43
 
44
  # Hàm chuyển đổi file .docx thành giọng nói
45
- async def docx_to_speech(file, lang, tld):
46
  doc = Document(file.name)
47
  text = "\n".join([para.text for para in doc.paragraphs]) # Lấy tất cả văn bản từ các đoạn
48
- return await text_to_speech(text, lang, tld)
49
 
50
  # Tạo giao diện Gradio với tab
51
  with gr.Blocks() as iface:
@@ -53,39 +54,36 @@ with gr.Blocks() as iface:
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=["vi-VN-NamMinhNeural", "en-US-JennyNeural"], label="Select language:") # Thay đổi danh sách giọng nói cho phù hợp
56
- tld_input = gr.Dropdown(choices=["com", "co.uk", "ca"], label="Select TLD:", value="com") # Bạn có thể điều chỉnh TLD
57
 
58
  audio_output, file_output = gr.Audio(label="Audio"), gr.File(label="Audio File")
59
 
60
  # Sử dụng asyncio để gọi coroutine
61
- gr.Button("Convert").click(fn=lambda text, lang, tld: asyncio.run(text_to_speech(text, lang, tld)),
62
- inputs=[text_input, lang_input, tld_input],
63
  outputs=[audio_output, file_output])
64
 
65
  with gr.Tab("TXT to Speech"):
66
  gr.Markdown("### Convert .txt file to speech")
67
  file_input = gr.File(label="Upload your .txt file")
68
  lang_input_file = gr.Dropdown(choices=["vi-VN-NamMinhNeural", "en-US-JennyNeural"], label="Select language:")
69
- tld_input_file = gr.Dropdown(choices=["com", "co.uk", "ca"], label="Select TLD:", value="com") # Bạn có thể điều chỉnh TLD
70
 
71
  audio_output_file, file_output_file = gr.Audio(label="Audio"), gr.File(label="Audio File")
72
 
73
  # Sử dụng asyncio để gọi coroutine
74
- gr.Button("Convert").click(fn=lambda file, lang, tld: asyncio.run(txt_to_speech(file, lang, tld)),
75
- inputs=[file_input, lang_input_file, tld_input_file],
76
  outputs=[audio_output_file, file_output_file])
77
 
78
  with gr.Tab("DOCX to Speech"):
79
  gr.Markdown("### Convert .docx file to speech")
80
  docx_file_input = gr.File(label="Upload your .docx file")
81
  lang_input_docx = gr.Dropdown(choices=["vi-VN-NamMinhNeural", "en-US-JennyNeural"], label="Select language:")
82
- tld_input_docx = gr.Dropdown(choices=["com", "co.uk", "ca"], label="Select TLD:", value="com") # Bạn có thể điều chỉnh TLD
83
 
84
  audio_output_docx, file_output_docx = gr.Audio(label="Audio"), gr.File(label="Audio File")
85
 
86
  # Sử dụng asyncio để gọi coroutine
87
- gr.Button("Convert").click(fn=lambda file, lang, tld: asyncio.run(docx_to_speech(file, lang, tld)),
88
- inputs=[docx_file_input, lang_input_docx, tld_input_docx],
89
  outputs=[audio_output_docx, file_output_docx])
90
 
91
  iface.launch(enable_queue=True)
 
10
  MAX_FILE_AGE = 24 * 60 * 60 # maximum age of audio files in seconds (24 hours)
11
 
12
  # Hàm chuyển đổi văn bản thành giọng nói sử dụng Edge TTS
13
+ async def text_to_speech(text, lang):
14
  tts = edge_tts.Communicate(text, voice=lang) # Chọn giọng nói tương ứng với ngôn ngữ
15
  fp = io.BytesIO()
16
 
 
22
  file_name = str(time.time()) + '.mp3' # Đổi định dạng thành mp3
23
  file_path = os.path.join(AUDIO_DIR, file_name)
24
 
25
+ # Lưu tệp âm thanh vào thư mục AUDIO_DIR
26
  with open(file_path, 'wb') as f:
27
  f.write(fp.read())
28
 
 
37
  os.remove(file_path)
38
 
39
  # Hàm chuyển đổi file .txt thành giọng nói
40
+ async def txt_to_speech(file, lang):
41
  with open(file.name, 'r') as f:
42
  text = f.read()
43
+ return await text_to_speech(text, lang)
44
 
45
  # Hàm chuyển đổi file .docx thành giọng nói
46
+ async def docx_to_speech(file, lang):
47
  doc = Document(file.name)
48
  text = "\n".join([para.text for para in doc.paragraphs]) # Lấy tất cả văn bản từ các đoạn
49
+ return await text_to_speech(text, lang)
50
 
51
  # Tạo giao diện Gradio với tab
52
  with gr.Blocks() as iface:
 
54
  gr.Markdown("### Convert text to speech")
55
  text_input = gr.Textbox(lines=10, label="Enter your text here:")
56
  lang_input = gr.Dropdown(choices=["vi-VN-NamMinhNeural", "en-US-JennyNeural"], label="Select language:") # Thay đổi danh sách giọng nói cho phù hợp
 
57
 
58
  audio_output, file_output = gr.Audio(label="Audio"), gr.File(label="Audio File")
59
 
60
  # Sử dụng asyncio để gọi coroutine
61
+ gr.Button("Convert").click(fn=lambda text, lang: asyncio.run(text_to_speech(text, lang)),
62
+ inputs=[text_input, lang_input],
63
  outputs=[audio_output, file_output])
64
 
65
  with gr.Tab("TXT to Speech"):
66
  gr.Markdown("### Convert .txt file to speech")
67
  file_input = gr.File(label="Upload your .txt file")
68
  lang_input_file = gr.Dropdown(choices=["vi-VN-NamMinhNeural", "en-US-JennyNeural"], label="Select language:")
 
69
 
70
  audio_output_file, file_output_file = gr.Audio(label="Audio"), gr.File(label="Audio File")
71
 
72
  # Sử dụng asyncio để gọi coroutine
73
+ gr.Button("Convert").click(fn=lambda file, lang: asyncio.run(txt_to_speech(file, lang)),
74
+ inputs=[file_input, lang_input_file],
75
  outputs=[audio_output_file, file_output_file])
76
 
77
  with gr.Tab("DOCX to Speech"):
78
  gr.Markdown("### Convert .docx file to speech")
79
  docx_file_input = gr.File(label="Upload your .docx file")
80
  lang_input_docx = gr.Dropdown(choices=["vi-VN-NamMinhNeural", "en-US-JennyNeural"], label="Select language:")
 
81
 
82
  audio_output_docx, file_output_docx = gr.Audio(label="Audio"), gr.File(label="Audio File")
83
 
84
  # Sử dụng asyncio để gọi coroutine
85
+ gr.Button("Convert").click(fn=lambda file, lang: asyncio.run(docx_to_speech(file, lang)),
86
+ inputs=[docx_file_input, lang_input_docx],
87
  outputs=[audio_output_docx, file_output_docx])
88
 
89
  iface.launch(enable_queue=True)