the-walking-fish commited on
Commit
e53525d
·
verified ·
1 Parent(s): 0ab94df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -23
app.py CHANGED
@@ -5,15 +5,14 @@ import tempfile
5
  import anyio
6
  import ffmpeg
7
  import os
8
- import glob
9
  import time
 
10
 
11
  language_dict = tts_order_voice
12
 
13
  async def text_to_speech_edge(text, language_code):
14
  voice = language_dict[language_code]
15
  communicate = edge_tts.Communicate(text, voice)
16
-
17
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
18
  tmp_path = tmp_file.name
19
 
@@ -21,26 +20,29 @@ async def text_to_speech_edge(text, language_code):
21
 
22
  new_temp_path = tmp_path.replace(".mp3", ".wav")
23
  (
24
- ffmpeg.input(tmp_path)
25
- .output(new_temp_path)
26
- .run()
27
  )
28
-
29
- # 清理原始的 MP3 臨時文件
30
- os.remove(tmp_path)
31
-
32
  return new_temp_path
33
 
34
- async def cleanup_temp_files(interval=3600):
 
35
  while True:
36
- await anyio.sleep(interval)
37
- temp_dir = tempfile.gettempdir()
38
  now = time.time()
39
-
40
- # 刪除過期的臨時文件
41
- for temp_file in glob.glob(os.path.join(temp_dir, "*.wav")):
42
- if os.path.isfile(temp_file) and now - os.path.getmtime(temp_file) > interval:
43
- os.remove(temp_file)
 
 
 
 
 
 
 
 
44
 
45
  input_text = gr.Textbox(lines=5, label="輸入文本")
46
  output_audio = gr.Audio(type="filepath", label="導出文件")
@@ -49,10 +51,5 @@ language = gr.Dropdown(choices=list(language_dict.keys()), value=default_languag
49
 
50
  interface = gr.Interface(fn=text_to_speech_edge, inputs=[input_text, language], outputs=[output_audio], title="Edge TTS 文字轉語音")
51
 
52
- async def main():
53
- async with anyio.create_task_group() as task_group:
54
- await task_group.spawn(interface.launch, backend="asyncio")
55
- await task_group.spawn(cleanup_temp_files, interval=3600) # 每隔1小時清理一次臨時文件
56
-
57
  if __name__ == "__main__":
58
- anyio.run(main)
 
5
  import anyio
6
  import ffmpeg
7
  import os
 
8
  import time
9
+ import threading
10
 
11
  language_dict = tts_order_voice
12
 
13
  async def text_to_speech_edge(text, language_code):
14
  voice = language_dict[language_code]
15
  communicate = edge_tts.Communicate(text, voice)
 
16
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
17
  tmp_path = tmp_file.name
18
 
 
20
 
21
  new_temp_path = tmp_path.replace(".mp3", ".wav")
22
  (
23
+ ffmpeg.input(tmp_path)
24
+ .output(new_temp_path)
25
+ .run()
26
  )
 
 
 
 
27
  return new_temp_path
28
 
29
+ def cleanup_temp_files(temp_dir, max_age_seconds=3600):
30
+ """定期清理超過 max_age_seconds 的臨時文件"""
31
  while True:
 
 
32
  now = time.time()
33
+ for filename in os.listdir(temp_dir):
34
+ file_path = os.path.join(temp_dir, filename)
35
+ if os.path.isfile(file_path):
36
+ file_age = now - os.path.getmtime(file_path)
37
+ if file_age > max_age_seconds:
38
+ os.remove(file_path)
39
+ print(f"已刪除過期的臨時文件: {file_path}")
40
+ time.sleep(3600) # 每隔一小時執行一次清理
41
+
42
+ # 啟動清理臨時文件的線程
43
+ tempfile_tempdir = tempfile.gettempdir()
44
+ cleanup_thread = threading.Thread(target=cleanup_temp_files, args=(tempfile_tempdir,), daemon=True)
45
+ cleanup_thread.start()
46
 
47
  input_text = gr.Textbox(lines=5, label="輸入文本")
48
  output_audio = gr.Audio(type="filepath", label="導出文件")
 
51
 
52
  interface = gr.Interface(fn=text_to_speech_edge, inputs=[input_text, language], outputs=[output_audio], title="Edge TTS 文字轉語音")
53
 
 
 
 
 
 
54
  if __name__ == "__main__":
55
+ anyio.run(interface.launch, backend="asyncio")