NeuralFalcon commited on
Commit
cd352a1
·
verified ·
1 Parent(s): c864752

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -3
app.py CHANGED
@@ -5,6 +5,8 @@ import uuid
5
  from pydub import AudioSegment
6
  from pydub.silence import split_on_silence
7
  import re
 
 
8
 
9
  def clean_file_name(file_path):
10
  # Get the base file name and extension
@@ -48,14 +50,70 @@ def calculate_duration(file_path):
48
  return duration_seconds
49
 
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  def process_audio(audio_file, seconds=0.05):
 
 
 
 
 
 
 
 
52
  keep_silence = int(seconds * 1000)
53
- output_audio_file = remove_silence(audio_file, minimum_silence=keep_silence)
 
 
 
 
 
 
 
54
  before = calculate_duration(audio_file)
55
  after = calculate_duration(output_audio_file)
 
56
  text = f"Old Duration: {before:.3f} seconds \nNew Duration: {after:.3f} seconds"
57
- return output_audio_file, output_audio_file, text
58
 
 
 
59
  # def ui():
60
  # theme = gr.themes.Soft(font=[gr.themes.GoogleFont("Source Sans Pro"), "Arial", "sans-serif"])
61
  # css = ".gradio-container {max-width: none !important;} .tab-content {padding: 20px;}"
@@ -117,7 +175,9 @@ def ui():
117
  <p style="font-size:1.05em; color:#555; margin:0 0 10px;">
118
  Upload an MP3 or WAV file, and it will remove silent parts from it.
119
  </p>
120
-
 
 
121
  <p style="font-size:0.9em; color:#777;">
122
  Install locally on your computer, enjoy unlimited runs with no waiting queue
123
  <a href="https://github.com/NeuralFalconYT/Remove-Silence-From-Audio" target="_blank" style="text-decoration:none;">
@@ -290,6 +350,7 @@ import click
290
  @click.option("--debug", is_flag=True, default=False, help="Enable debug mode.")
291
  @click.option("--share", is_flag=True, default=False, help="Enable sharing of the interface.")
292
  def main(debug, share):
 
293
  demo=ui()
294
  demo.queue().launch(debug=debug, share=share)
295
  if __name__ == "__main__":
 
5
  from pydub import AudioSegment
6
  from pydub.silence import split_on_silence
7
  import re
8
+ import time
9
+
10
 
11
  def clean_file_name(file_path):
12
  # Get the base file name and extension
 
50
  return duration_seconds
51
 
52
 
53
+ # Some user uploads may trigger Hugging Face privacy policy checks, which can lead to account suspension
54
+ FILE_TIMESTAMPS = {}
55
+
56
+ def track_file(file_path):
57
+ FILE_TIMESTAMPS[file_path] = time.time()
58
+
59
+
60
+ def cleanup_tracked_files(max_age_seconds=3600):
61
+ now = time.time()
62
+ to_delete = []
63
+
64
+ for file_path, created_time in FILE_TIMESTAMPS.items():
65
+ if now - created_time > max_age_seconds:
66
+ if os.path.exists(file_path):
67
+ try:
68
+ os.remove(file_path)
69
+ print(f"🗑️ Deleted: {file_path}")
70
+ except Exception as e:
71
+ print(f"⚠️ Error deleting {file_path}: {e}")
72
+
73
+ to_delete.append(file_path)
74
+
75
+ # Remove from tracking
76
+ for f in to_delete:
77
+ FILE_TIMESTAMPS.pop(f, None)
78
+
79
+
80
+
81
+ def start_cleanup_worker(interval=3600):
82
+ def worker():
83
+ while True:
84
+ print("🔄 Cleaning tracked files...")
85
+ cleanup_tracked_files()
86
+ time.sleep(interval)
87
+
88
+ import threading
89
+ threading.Thread(target=worker, daemon=True).start()
90
+
91
+
92
  def process_audio(audio_file, seconds=0.05):
93
+ if audio_file is None:
94
+ return None, None, "No file uploaded"
95
+
96
+ if not os.path.exists(audio_file):
97
+ return None, None, "File not found"
98
+
99
+ track_file(audio_file)
100
+
101
  keep_silence = int(seconds * 1000)
102
+
103
+ output_audio_file = remove_silence(
104
+ audio_file,
105
+ minimum_silence=keep_silence
106
+ )
107
+
108
+ track_file(output_audio_file)
109
+
110
  before = calculate_duration(audio_file)
111
  after = calculate_duration(output_audio_file)
112
+
113
  text = f"Old Duration: {before:.3f} seconds \nNew Duration: {after:.3f} seconds"
 
114
 
115
+ return output_audio_file, output_audio_file, text
116
+
117
  # def ui():
118
  # theme = gr.themes.Soft(font=[gr.themes.GoogleFont("Source Sans Pro"), "Arial", "sans-serif"])
119
  # css = ".gradio-container {max-width: none !important;} .tab-content {padding: 20px;}"
 
175
  <p style="font-size:1.05em; color:#555; margin:0 0 10px;">
176
  Upload an MP3 or WAV file, and it will remove silent parts from it.
177
  </p>
178
+ <p style="font-size:0.8em; color:#999;">
179
+ ⚠️ Please don’t upload copyrighted content — it can take this Space offline.
180
+ </p>
181
  <p style="font-size:0.9em; color:#777;">
182
  Install locally on your computer, enjoy unlimited runs with no waiting queue
183
  <a href="https://github.com/NeuralFalconYT/Remove-Silence-From-Audio" target="_blank" style="text-decoration:none;">
 
350
  @click.option("--debug", is_flag=True, default=False, help="Enable debug mode.")
351
  @click.option("--share", is_flag=True, default=False, help="Enable sharing of the interface.")
352
  def main(debug, share):
353
+ start_cleanup_worker()
354
  demo=ui()
355
  demo.queue().launch(debug=debug, share=share)
356
  if __name__ == "__main__":