Spaces:
Running
Running
zach
commited on
Commit
·
5d6d1ef
1
Parent(s):
ba3994f
Update file clean up logic to delete audio files older than 30 minuteds when writing new audio file to static/audio directory.
Browse files- src/config.py +0 -33
- src/integrations/elevenlabs_api.py +1 -1
- src/utils.py +42 -3
src/config.py
CHANGED
@@ -13,7 +13,6 @@ Key Features:
|
|
13 |
import atexit
|
14 |
import logging
|
15 |
import os
|
16 |
-
import shutil
|
17 |
|
18 |
# Third-Party Library Imports
|
19 |
from dotenv import load_dotenv
|
@@ -45,35 +44,3 @@ if DEBUG:
|
|
45 |
AUDIO_DIR = os.path.join(os.getcwd(), "static", "audio")
|
46 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
47 |
logger.info(f"Audio directory set to {AUDIO_DIR}")
|
48 |
-
|
49 |
-
|
50 |
-
def cleanup_audio_directory_contents() -> None:
|
51 |
-
"""
|
52 |
-
Delete all audio files within AUDIO_DIR, leaving the directory intact.
|
53 |
-
|
54 |
-
This function is intended to be registered to run when the application exits.
|
55 |
-
It assumes that AUDIO_DIR contains only audio files (or symbolic links), and no subdirectories.
|
56 |
-
"""
|
57 |
-
if not os.path.exists(AUDIO_DIR):
|
58 |
-
logger.info(
|
59 |
-
"Audio directory %s does not exist. Nothing to clean up.", AUDIO_DIR
|
60 |
-
)
|
61 |
-
return
|
62 |
-
|
63 |
-
# Use os.scandir for efficient directory iteration.
|
64 |
-
with os.scandir(AUDIO_DIR) as entries:
|
65 |
-
for entry in entries:
|
66 |
-
if entry.is_file() or entry.is_symlink():
|
67 |
-
try:
|
68 |
-
os.unlink(entry.path)
|
69 |
-
logger.info("Deleted file: %s", entry.path)
|
70 |
-
except Exception as exc:
|
71 |
-
logger.error(
|
72 |
-
"Failed to delete file %s. Reason: %s", entry.path, exc
|
73 |
-
)
|
74 |
-
else:
|
75 |
-
logger.warning("Skipping non-file entry: %s", entry.path)
|
76 |
-
|
77 |
-
|
78 |
-
# Register the cleanup function to be called on normal program termination.
|
79 |
-
atexit.register(cleanup_audio_directory_contents)
|
|
|
13 |
import atexit
|
14 |
import logging
|
15 |
import os
|
|
|
16 |
|
17 |
# Third-Party Library Imports
|
18 |
from dotenv import load_dotenv
|
|
|
44 |
AUDIO_DIR = os.path.join(os.getcwd(), "static", "audio")
|
45 |
os.makedirs(AUDIO_DIR, exist_ok=True)
|
46 |
logger.info(f"Audio directory set to {AUDIO_DIR}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/integrations/elevenlabs_api.py
CHANGED
@@ -23,7 +23,7 @@ Functions:
|
|
23 |
from dataclasses import dataclass
|
24 |
import logging
|
25 |
import random
|
26 |
-
from typing import Optional, Union
|
27 |
|
28 |
# Third-Party Library Imports
|
29 |
from elevenlabs import ElevenLabs, TextToVoiceCreatePreviewsRequestOutputFormat
|
|
|
23 |
from dataclasses import dataclass
|
24 |
import logging
|
25 |
import random
|
26 |
+
from typing import Optional, Tuple, Union
|
27 |
|
28 |
# Third-Party Library Imports
|
29 |
from elevenlabs import ElevenLabs, TextToVoiceCreatePreviewsRequestOutputFormat
|
src/utils.py
CHANGED
@@ -14,6 +14,7 @@ Functions:
|
|
14 |
import base64
|
15 |
import os
|
16 |
import random
|
|
|
17 |
from typing import Tuple
|
18 |
|
19 |
# Local Application Imports
|
@@ -123,12 +124,46 @@ def validate_character_description_length(character_description: str) -> None:
|
|
123 |
)
|
124 |
|
125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
def save_base64_audio_to_file(base64_audio: str, filename: str) -> str:
|
127 |
"""
|
128 |
Decode a base64-encoded audio string and write the resulting binary data to a file
|
129 |
-
within the preconfigured AUDIO_DIR directory.
|
130 |
-
|
131 |
-
|
|
|
|
|
132 |
|
133 |
Args:
|
134 |
base64_audio (str): The base64-encoded string representing the audio data.
|
@@ -148,6 +183,10 @@ def save_base64_audio_to_file(base64_audio: str, filename: str) -> str:
|
|
148 |
# Construct the full absolute file path within the AUDIO_DIR directory.
|
149 |
file_path = os.path.join(AUDIO_DIR, filename)
|
150 |
|
|
|
|
|
|
|
|
|
151 |
# Write the binary audio data to the file.
|
152 |
with open(file_path, "wb") as audio_file:
|
153 |
audio_file.write(audio_bytes)
|
|
|
14 |
import base64
|
15 |
import os
|
16 |
import random
|
17 |
+
import time
|
18 |
from typing import Tuple
|
19 |
|
20 |
# Local Application Imports
|
|
|
124 |
)
|
125 |
|
126 |
|
127 |
+
def delete_files_older_than(directory: str, minutes: int = 30) -> None:
|
128 |
+
"""
|
129 |
+
Delete all files in the specified directory that are older than a given number of minutes.
|
130 |
+
|
131 |
+
This function checks each file in the given directory and removes it if its last modification
|
132 |
+
time is older than the specified threshold. By default, the threshold is set to 30 minutes.
|
133 |
+
|
134 |
+
Args:
|
135 |
+
directory (str): The path to the directory where files will be checked and possibly deleted.
|
136 |
+
minutes (int, optional): The age threshold in minutes. Files older than this will be deleted.
|
137 |
+
Defaults to 30 minutes.
|
138 |
+
|
139 |
+
Returns: None
|
140 |
+
"""
|
141 |
+
# Get the current time in seconds since the epoch.
|
142 |
+
now = time.time()
|
143 |
+
# Convert the minutes threshold to seconds.
|
144 |
+
cutoff = now - (minutes * 60)
|
145 |
+
|
146 |
+
# Iterate over all files in the directory.
|
147 |
+
for filename in os.listdir(directory):
|
148 |
+
file_path = os.path.join(directory, filename)
|
149 |
+
file_mod_time = os.path.getmtime(file_path)
|
150 |
+
# If the file's modification time is older than the cutoff, delete it.
|
151 |
+
if file_mod_time < cutoff:
|
152 |
+
try:
|
153 |
+
os.remove(file_path)
|
154 |
+
print(f"Deleted: {file_path}")
|
155 |
+
except Exception as e:
|
156 |
+
print(f"Error deleting {file_path}: {e}")
|
157 |
+
|
158 |
+
|
159 |
def save_base64_audio_to_file(base64_audio: str, filename: str) -> str:
|
160 |
"""
|
161 |
Decode a base64-encoded audio string and write the resulting binary data to a file
|
162 |
+
within the preconfigured AUDIO_DIR directory. Prior to writing the bytes to an audio
|
163 |
+
file all files within the directory which are more than 30 minutes old are deleted.
|
164 |
+
This function verifies the file was created, logs the absolute and relative file
|
165 |
+
paths, and returns a path relative to the current working directory (which is what
|
166 |
+
Gradio requires to serve static files).
|
167 |
|
168 |
Args:
|
169 |
base64_audio (str): The base64-encoded string representing the audio data.
|
|
|
183 |
# Construct the full absolute file path within the AUDIO_DIR directory.
|
184 |
file_path = os.path.join(AUDIO_DIR, filename)
|
185 |
|
186 |
+
# Delete all audio files older than 30 minutes before writing new audio file.
|
187 |
+
num_minutes = 30
|
188 |
+
delete_files_older_than(AUDIO_DIR, num_minutes)
|
189 |
+
|
190 |
# Write the binary audio data to the file.
|
191 |
with open(file_path, "wb") as audio_file:
|
192 |
audio_file.write(audio_bytes)
|