vuxuanhoan commited on
Commit
cbac426
·
verified ·
1 Parent(s): 413c470

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -21
app.py CHANGED
@@ -6,28 +6,28 @@ import time
6
  import asyncio
7
  from docx import Document
8
 
9
- 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):
14
  tts = edge_tts.Communicate(text, voice=lang)
15
- fp = io.BytesIO()
 
16
 
17
  # Lưu âm thanh vào bộ nhớ
18
- await tts.save(fp, 'audio/mp3') # Định dạng mp3
19
- fp.seek(0)
20
 
21
  os.makedirs(AUDIO_DIR, exist_ok=True) # Đảm bảo thư mục tồn tại
22
- file_name = str(time.time()) + '.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
 
29
  delete_old_audio_files()
30
- return file_path, file_path
31
 
32
  def delete_old_audio_files():
33
  now = time.time()
@@ -36,28 +36,24 @@ def delete_old_audio_files():
36
  if now - os.path.getmtime(file_path) > MAX_FILE_AGE:
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:
53
  with gr.Tab("Text to Speech"):
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:")
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])
@@ -68,8 +64,6 @@ with gr.Blocks() as iface:
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])
@@ -80,8 +74,6 @@ with gr.Blocks() as iface:
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])
 
6
  import asyncio
7
  from docx import Document
8
 
9
+ AUDIO_DIR = 'audio_files' # Thư mục lưu tệp âm thanh
10
+ MAX_FILE_AGE = 24 * 60 * 60 # Thời gian tối đa 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
+ audio_fp = io.BytesIO() # Tạo một file âm thanh trong bộ nhớ
15
+ metadata_fp = io.BytesIO() # Tạo một file metadata trong bộ nhớ
16
 
17
  # Lưu âm thanh vào bộ nhớ
18
+ await tts.save(audio_fp, 'audio/mp3') # Định dạng mp3
19
+ audio_fp.seek(0)
20
 
21
  os.makedirs(AUDIO_DIR, exist_ok=True) # Đảm bảo thư mục tồn tại
22
+ audio_file_name = f"{time.time()}.mp3"
23
+ audio_file_path = os.path.join(AUDIO_DIR, audio_file_name)
24
 
25
  # Lưu tệp âm thanh vào thư mục AUDIO_DIR
26
+ with open(audio_file_path, 'wb') as f:
27
+ f.write(audio_fp.read())
28
 
29
  delete_old_audio_files()
30
+ return audio_file_path, audio_file_path # Trả về đường dẫn tệp âm thanh
31
 
32
  def delete_old_audio_files():
33
  now = time.time()
 
36
  if now - os.path.getmtime(file_path) > MAX_FILE_AGE:
37
  os.remove(file_path)
38
 
 
39
  async def txt_to_speech(file, lang):
40
  with open(file.name, 'r') as f:
41
  text = f.read()
42
  return await text_to_speech(text, lang)
43
 
 
44
  async def docx_to_speech(file, lang):
45
  doc = Document(file.name)
46
  text = "\n".join([para.text for para in doc.paragraphs]) # Lấy tất cả văn bản từ các đoạn
47
  return await text_to_speech(text, lang)
48
 
49
+ # Tạo giao diện Gradio
50
  with gr.Blocks() as iface:
51
  with gr.Tab("Text to Speech"):
52
  gr.Markdown("### Convert text to speech")
53
  text_input = gr.Textbox(lines=10, label="Enter your text here:")
54
  lang_input = gr.Dropdown(choices=["vi-VN-NamMinhNeural", "en-US-JennyNeural"], label="Select language:")
 
 
55
 
56
+ audio_output, file_output = gr.Audio(label="Audio"), gr.File(label="Audio File")
57
  gr.Button("Convert").click(fn=lambda text, lang: asyncio.run(text_to_speech(text, lang)),
58
  inputs=[text_input, lang_input],
59
  outputs=[audio_output, file_output])
 
64
  lang_input_file = gr.Dropdown(choices=["vi-VN-NamMinhNeural", "en-US-JennyNeural"], label="Select language:")
65
 
66
  audio_output_file, file_output_file = gr.Audio(label="Audio"), gr.File(label="Audio File")
 
 
67
  gr.Button("Convert").click(fn=lambda file, lang: asyncio.run(txt_to_speech(file, lang)),
68
  inputs=[file_input, lang_input_file],
69
  outputs=[audio_output_file, file_output_file])
 
74
  lang_input_docx = gr.Dropdown(choices=["vi-VN-NamMinhNeural", "en-US-JennyNeural"], label="Select language:")
75
 
76
  audio_output_docx, file_output_docx = gr.Audio(label="Audio"), gr.File(label="Audio File")
 
 
77
  gr.Button("Convert").click(fn=lambda file, lang: asyncio.run(docx_to_speech(file, lang)),
78
  inputs=[docx_file_input, lang_input_docx],
79
  outputs=[audio_output_docx, file_output_docx])