fabiogra commited on
Commit
72ad181
1 Parent(s): 9baeeda

feat: add a function to clean tmp after 30 min

Browse files
app/helpers.py CHANGED
@@ -5,6 +5,7 @@ from base64 import b64encode
5
  from io import BytesIO
6
  from pathlib import Path
7
  from loguru import logger as log
 
8
 
9
  import matplotlib.pyplot as plt
10
  import numpy as np
@@ -173,3 +174,37 @@ def file_size_is_valid(response):
173
  st.error(
174
  f"The file is too large to download. Maximum size allowed: {max_size_mb}MB. Duplicate this space to remove any limit."
175
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  from io import BytesIO
6
  from pathlib import Path
7
  from loguru import logger as log
8
+ import time
9
 
10
  import matplotlib.pyplot as plt
11
  import numpy as np
 
174
  st.error(
175
  f"The file is too large to download. Maximum size allowed: {max_size_mb}MB. Duplicate this space to remove any limit."
176
  )
177
+
178
+
179
+ def _get_files_to_not_delete():
180
+ not_delete = []
181
+ if os.environ.get("PREPARE_SAMPLES"):
182
+ for filename in ["sample_songs.json", "separate_songs.json"]:
183
+ try:
184
+ with open(filename) as f:
185
+ not_delete += list(json.load(f).keys())
186
+ except Exception as e:
187
+ log.warning(e)
188
+ return not_delete
189
+
190
+
191
+ def _remove_file_older_than(file_path: str, max_age_limit: float):
192
+ # If the file is older than the age limit, delete it
193
+ if os.path.getmtime(file_path) < max_age_limit:
194
+ try:
195
+ os.remove(file_path)
196
+ except OSError as e:
197
+ log.warning(f"Error: Could not delete {file_path}. Reason: {e.strerror}")
198
+
199
+
200
+ def delete_old_files(directory: str, age_limit_seconds: int):
201
+ files_to_not_delete = _get_files_to_not_delete()
202
+ age_limit = time.time() - age_limit_seconds
203
+
204
+ # Walk through the directory
205
+ for dirpath, dirnames, filenames in os.walk(directory):
206
+ if dirpath.split("/")[-1] not in files_to_not_delete:
207
+ for filename in filenames:
208
+ if filename.split(".")[0] not in files_to_not_delete:
209
+ file_path = os.path.join(dirpath, filename)
210
+ _remove_file_older_than(file_path, age_limit)
app/pages/About.py CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
2
 
3
  from header import header
4
  from footer import footer
 
5
 
6
 
7
  def body():
@@ -152,3 +153,4 @@ if __name__ == "__main__":
152
  header(logo_and_title=False)
153
  body()
154
  footer()
 
 
2
 
3
  from header import header
4
  from footer import footer
5
+ from helpers import delete_old_files
6
 
7
 
8
  def body():
 
153
  header(logo_and_title=False)
154
  body()
155
  footer()
156
+ delete_old_files("/tmp", 60 * 30)
app/pages/Karaoke.py CHANGED
@@ -14,6 +14,7 @@ from helpers import (
14
  load_audio_segment,
15
  streamlit_player,
16
  local_audio,
 
17
  )
18
 
19
  from service.vocal_remover.runner import separate, load_model
@@ -182,3 +183,4 @@ if __name__ == "__main__":
182
  header()
183
  body()
184
  footer()
 
 
14
  load_audio_segment,
15
  streamlit_player,
16
  local_audio,
17
+ delete_old_files,
18
  )
19
 
20
  from service.vocal_remover.runner import separate, load_model
 
183
  header()
184
  body()
185
  footer()
186
+ delete_old_files("/tmp", 60 * 30)
app/pages/Separate.py CHANGED
@@ -15,6 +15,7 @@ from helpers import (
15
  st_local_audio,
16
  url_is_valid,
17
  file_size_is_valid,
 
18
  )
19
  from service.demucs_runner import separator
20
  from service.vocal_remover.runner import load_model, separate
@@ -281,3 +282,4 @@ if __name__ == "__main__":
281
  header()
282
  body()
283
  footer()
 
 
15
  st_local_audio,
16
  url_is_valid,
17
  file_size_is_valid,
18
+ delete_old_files,
19
  )
20
  from service.demucs_runner import separator
21
  from service.vocal_remover.runner import load_model, separate
 
282
  header()
283
  body()
284
  footer()
285
+ delete_old_files("/tmp", 60 * 30)