Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
import edge_tts
|
3 |
import asyncio
|
4 |
-
import tempfile
|
5 |
import os
|
6 |
-
|
7 |
from pathlib import Path
|
|
|
8 |
|
9 |
# Crea una directory per i file audio
|
10 |
AUDIO_DIR = Path("audio_files")
|
@@ -26,7 +26,6 @@ async def text_to_speech(text, voice, rate, pitch):
|
|
26 |
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
|
27 |
|
28 |
# Crea un nome file univoco basato sul contenuto
|
29 |
-
import hashlib
|
30 |
content_hash = hashlib.md5(f"{text}{voice}{rate}{pitch}".encode()).hexdigest()
|
31 |
output_path = AUDIO_DIR / f"{content_hash}.mp3"
|
32 |
|
@@ -38,21 +37,33 @@ async def text_to_speech(text, voice, rate, pitch):
|
|
38 |
|
39 |
# Funzione di pulizia per rimuovere i file più vecchi
|
40 |
def cleanup_old_files(directory: Path, max_files: int = 100, max_age_hours: int = 24):
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
async def tts_interface(text, voice, rate, pitch):
|
58 |
# Esegui la pulizia prima di generare un nuovo file
|
@@ -68,7 +79,6 @@ async def create_demo():
|
|
68 |
|
69 |
description = """
|
70 |
Convert text to speech using Microsoft Edge TTS. Adjust speech rate and pitch: 0 is default, positive values increase, negative values decrease.
|
71 |
-
|
72 |
Original Space by innoai
|
73 |
"""
|
74 |
|
@@ -99,4 +109,4 @@ async def main():
|
|
99 |
demo.launch(show_api=True)
|
100 |
|
101 |
if __name__ == "__main__":
|
102 |
-
asyncio.run(main())
|
|
|
1 |
import gradio as gr
|
2 |
import edge_tts
|
3 |
import asyncio
|
|
|
4 |
import os
|
5 |
+
import datetime
|
6 |
from pathlib import Path
|
7 |
+
import hashlib
|
8 |
|
9 |
# Crea una directory per i file audio
|
10 |
AUDIO_DIR = Path("audio_files")
|
|
|
26 |
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
|
27 |
|
28 |
# Crea un nome file univoco basato sul contenuto
|
|
|
29 |
content_hash = hashlib.md5(f"{text}{voice}{rate}{pitch}".encode()).hexdigest()
|
30 |
output_path = AUDIO_DIR / f"{content_hash}.mp3"
|
31 |
|
|
|
37 |
|
38 |
# Funzione di pulizia per rimuovere i file più vecchi
|
39 |
def cleanup_old_files(directory: Path, max_files: int = 100, max_age_hours: int = 24):
|
40 |
+
try:
|
41 |
+
files = list(directory.glob("*.mp3"))
|
42 |
+
|
43 |
+
# Rimuovi i file più vecchi di max_age_hours
|
44 |
+
current_time = datetime.datetime.now()
|
45 |
+
for file in files:
|
46 |
+
try:
|
47 |
+
file_age = current_time - datetime.datetime.fromtimestamp(file.stat().st_mtime)
|
48 |
+
if file_age.total_seconds() > (max_age_hours * 3600):
|
49 |
+
try:
|
50 |
+
file.unlink()
|
51 |
+
except (PermissionError, OSError):
|
52 |
+
continue
|
53 |
+
except (OSError, ValueError):
|
54 |
+
continue
|
55 |
+
|
56 |
+
# Se ci sono ancora troppi file, rimuovi i più vecchi
|
57 |
+
files = list(directory.glob("*.mp3"))
|
58 |
+
if len(files) > max_files:
|
59 |
+
files.sort(key=lambda x: x.stat().st_mtime)
|
60 |
+
for file in files[:-max_files]:
|
61 |
+
try:
|
62 |
+
file.unlink()
|
63 |
+
except (PermissionError, OSError):
|
64 |
+
continue
|
65 |
+
except Exception as e:
|
66 |
+
print(f"Error during cleanup: {e}")
|
67 |
|
68 |
async def tts_interface(text, voice, rate, pitch):
|
69 |
# Esegui la pulizia prima di generare un nuovo file
|
|
|
79 |
|
80 |
description = """
|
81 |
Convert text to speech using Microsoft Edge TTS. Adjust speech rate and pitch: 0 is default, positive values increase, negative values decrease.
|
|
|
82 |
Original Space by innoai
|
83 |
"""
|
84 |
|
|
|
109 |
demo.launch(show_api=True)
|
110 |
|
111 |
if __name__ == "__main__":
|
112 |
+
asyncio.run(main())
|