hivecorp commited on
Commit
79ae00c
·
verified ·
1 Parent(s): f6232e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -5
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import tempfile
2
  import os
3
  from pathlib import Path
 
 
4
 
5
  import edge_tts
6
  import gradio as gr
@@ -457,6 +459,17 @@ def create_download_link(audio_path):
457
  </a>
458
  """
459
 
 
 
 
 
 
 
 
 
 
 
 
460
  async def text_to_speech_edge(text, language_code, speaker, tashkeel_checkbox=False):
461
  # Define the character limit
462
  char_limit = 100000000
@@ -467,12 +480,21 @@ async def text_to_speech_edge(text, language_code, speaker, tashkeel_checkbox=Fa
467
  voice = language_dict[language_code][speaker]
468
  communicate = edge_tts.Communicate(text, voice)
469
 
470
- # Create a temporary file inside /tmp/gradio/ (accessible but auto-deleted later)
471
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3", dir="/tmp/gradio/") as tmp_file:
472
- tmp_path = tmp_file.name
 
 
 
 
 
473
  await communicate.save(tmp_path)
474
-
475
- return text, tmp_path, create_download_link(tmp_path)
 
 
 
 
476
 
477
  def get_speakers(language):
478
  print(language)
 
1
  import tempfile
2
  import os
3
  from pathlib import Path
4
+ import time
5
+ from threading import Timer
6
 
7
  import edge_tts
8
  import gradio as gr
 
459
  </a>
460
  """
461
 
462
+ def cleanup_file(filepath, delay=300): # 300 seconds = 5 minutes
463
+ def delete_file():
464
+ try:
465
+ if os.path.exists(filepath):
466
+ os.remove(filepath)
467
+ print(f"Cleaned up file: {filepath}")
468
+ except Exception as e:
469
+ print(f"Error cleaning up file {filepath}: {e}")
470
+
471
+ Timer(delay, delete_file).start()
472
+
473
  async def text_to_speech_edge(text, language_code, speaker, tashkeel_checkbox=False):
474
  # Define the character limit
475
  char_limit = 100000000
 
480
  voice = language_dict[language_code][speaker]
481
  communicate = edge_tts.Communicate(text, voice)
482
 
483
+ # Create temp directory if it doesn't exist
484
+ temp_dir = os.path.join(tempfile.gettempdir(), "gradio")
485
+ os.makedirs(temp_dir, exist_ok=True)
486
+
487
+ # Generate unique filename
488
+ tmp_path = os.path.join(temp_dir, f"audio_{int(time.time())}_{os.urandom(4).hex()}.mp3")
489
+
490
+ try:
491
  await communicate.save(tmp_path)
492
+ # Schedule file cleanup after 5 minutes
493
+ cleanup_file(tmp_path)
494
+ return text, tmp_path, create_download_link(tmp_path)
495
+ except Exception as e:
496
+ print(f"Error generating audio: {e}")
497
+ return f"Error: {str(e)}", None, None
498
 
499
  def get_speakers(language):
500
  print(language)