Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
import uuid
|
|
@@ -6,352 +5,563 @@ 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 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def remove_silence(file_path, minimum_silence=50):
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
def calculate_duration(file_path):
|
| 48 |
-
audio
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
def track_file(file_path):
|
| 57 |
-
|
| 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 |
-
|
| 67 |
-
|
| 68 |
os.remove(file_path)
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 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 |
-
|
| 85 |
-
|
|
|
|
|
|
|
| 86 |
time.sleep(interval)
|
| 87 |
-
|
| 88 |
import threading
|
| 89 |
-
threading.Thread(target=worker, daemon=True)
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
def process_audio(audio_file, seconds=0.05):
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
#
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
#
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
#
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
#
|
| 134 |
-
|
| 135 |
-
# )
|
| 136 |
-
# return demo
|
| 137 |
-
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
def ui():
|
|
|
|
| 142 |
theme = gr.themes.Soft(
|
| 143 |
-
font=[gr.themes.GoogleFont("
|
| 144 |
)
|
| 145 |
-
|
| 146 |
css = """
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
border:
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
font-size: 1.05em;
|
| 159 |
-
}
|
| 160 |
-
|
| 161 |
-
button.primary:hover {
|
| 162 |
-
background-color: #1e40af !important;
|
| 163 |
-
}
|
| 164 |
-
"""
|
| 165 |
-
|
| 166 |
-
with gr.Blocks(theme=theme, css=css) as demo:
|
| 167 |
-
|
| 168 |
-
# Header
|
| 169 |
-
gr.HTML("""
|
| 170 |
-
<div style="text-align:center; margin:20px auto; max-width:800px;">
|
| 171 |
-
<h1 style="font-size:2.4em; margin-bottom:6px;">
|
| 172 |
-
🔇 Remove Silence From Audio
|
| 173 |
-
</h1>
|
| 174 |
-
|
| 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;">
|
| 184 |
-
Download Link
|
| 185 |
-
</a>
|
| 186 |
-
</p>
|
| 187 |
-
</div>
|
| 188 |
-
""")
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
with gr.Row():
|
| 195 |
-
# LEFT: Inputs
|
| 196 |
-
with gr.Column(scale=1):
|
| 197 |
-
audio_input = gr.Audio(
|
| 198 |
-
label="Upload Audio",
|
| 199 |
-
type="filepath",
|
| 200 |
-
sources=["upload", "microphone"]
|
| 201 |
-
)
|
| 202 |
-
|
| 203 |
-
silence_threshold = gr.Number(
|
| 204 |
-
label="Keep Silence Upto (In seconds)",
|
| 205 |
-
value=0.05
|
| 206 |
-
)
|
| 207 |
-
|
| 208 |
-
submit_btn = gr.Button(
|
| 209 |
-
"🔇 Remove Silence",
|
| 210 |
-
variant="primary"
|
| 211 |
-
)
|
| 212 |
-
|
| 213 |
-
# RIGHT: Outputs
|
| 214 |
-
with gr.Column(scale=1):
|
| 215 |
-
audio_output = gr.Audio(label="Play Audio")
|
| 216 |
-
file_output = gr.File(label="Download Audio File")
|
| 217 |
-
duration_output = gr.Textbox(label="Duration")
|
| 218 |
-
|
| 219 |
-
submit_btn.click(
|
| 220 |
-
fn=process_audio, # <-- your function
|
| 221 |
-
inputs=[audio_input, silence_threshold],
|
| 222 |
-
outputs=[audio_output, file_output, duration_output]
|
| 223 |
-
)
|
| 224 |
-
gr.HTML("""
|
| 225 |
-
<style>
|
| 226 |
-
/* Futuristic Footer Styles */
|
| 227 |
-
@keyframes cyber-border-flow {
|
| 228 |
-
0% {background-position: 0% 50%;}
|
| 229 |
-
50% {background-position: 100% 50%;}
|
| 230 |
-
100% {background-position: 0% 50%;}
|
| 231 |
}
|
| 232 |
|
| 233 |
-
|
| 234 |
-
margin:
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
border-radius: 20px;
|
| 238 |
-
background: linear-gradient(90deg, #ff00cc, #333399, #00f2ff);
|
| 239 |
-
background-size: 200% 200%;
|
| 240 |
-
animation: cyber-border-flow 4s ease infinite;
|
| 241 |
-
box-shadow: 0 0 15px rgba(0, 242, 255, 0.2);
|
| 242 |
}
|
| 243 |
-
|
| 244 |
-
.
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
position: relative;
|
| 250 |
-
overflow: hidden;
|
| 251 |
}
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
.cyber-promo-inner::before {
|
| 255 |
content: "";
|
| 256 |
-
position:
|
| 257 |
-
top: 0;
|
|
|
|
|
|
|
|
|
|
| 258 |
background-image:
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
background-size: 40px 40px;
|
| 262 |
pointer-events: none;
|
| 263 |
-
z-index:
|
| 264 |
}
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
text-
|
| 272 |
-
|
| 273 |
-
margin: 0 0 10px 0;
|
| 274 |
-
text-shadow: 0 0 10px rgba(0, 204, 255, 0.6);
|
| 275 |
-
position: relative;
|
| 276 |
-
z-index: 1;
|
| 277 |
}
|
| 278 |
-
|
| 279 |
-
.
|
| 280 |
-
|
| 281 |
-
font-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
}
|
| 288 |
-
|
| 289 |
-
.
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
border-radius: 6px;
|
| 296 |
-
|
| 297 |
-
font-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
transition: all 0.3s ease;
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
}
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
background: #
|
| 310 |
-
color:
|
| 311 |
-
box-shadow: 0
|
| 312 |
-
transform: scale(1.02);
|
| 313 |
}
|
| 314 |
|
| 315 |
-
.
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
position: relative;
|
| 320 |
-
z-index: 1;
|
| 321 |
}
|
| 322 |
-
|
| 323 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
text-decoration: none;
|
| 325 |
transition: color 0.2s;
|
| 326 |
}
|
| 327 |
-
|
| 328 |
-
|
|
|
|
|
|
|
| 329 |
}
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
</div>
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
return demo
|
| 347 |
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
if __name__ == "__main__":
|
| 357 |
-
main()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import uuid
|
|
|
|
| 5 |
from pydub.silence import split_on_silence
|
| 6 |
import re
|
| 7 |
import time
|
| 8 |
+
import logging
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
|
| 11 |
+
# Configure logging
|
| 12 |
+
logging.basicConfig(
|
| 13 |
+
level=logging.INFO,
|
| 14 |
+
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 15 |
+
)
|
| 16 |
+
logger = logging.getLogger(__name__)
|
| 17 |
+
|
| 18 |
+
# Constants
|
| 19 |
+
MAX_FILE_SIZE_MB = 500
|
| 20 |
+
SUPPORTED_FORMATS = {'mp3', 'wav', 'ogg', 'm4a', 'flac'}
|
| 21 |
+
FILE_TIMESTAMPS = {}
|
| 22 |
|
| 23 |
+
def validate_audio_file(file_path):
|
| 24 |
+
"""Validate audio file format and size"""
|
| 25 |
+
if not file_path or not os.path.exists(file_path):
|
| 26 |
+
return False, "❌ File not found or invalid path"
|
| 27 |
+
|
| 28 |
+
file_size_mb = os.path.getsize(file_path) / (1024 * 1024)
|
| 29 |
+
if file_size_mb > MAX_FILE_SIZE_MB:
|
| 30 |
+
return False, f"❌ File size ({file_size_mb:.1f}MB) exceeds {MAX_FILE_SIZE_MB}MB limit"
|
| 31 |
+
|
| 32 |
+
file_ext = os.path.splitext(file_path)[1].lower().strip('.')
|
| 33 |
+
if file_ext not in SUPPORTED_FORMATS:
|
| 34 |
+
return False, f"❌ Format '{file_ext}' not supported. Use: {', '.join(SUPPORTED_FORMATS)}"
|
| 35 |
+
|
| 36 |
+
return True, "✅ File validated"
|
| 37 |
|
| 38 |
def clean_file_name(file_path):
|
| 39 |
+
"""Generate clean output file name with UUID"""
|
| 40 |
+
try:
|
| 41 |
+
file_name = os.path.basename(file_path)
|
| 42 |
+
file_name, file_extension = os.path.splitext(file_name)
|
| 43 |
+
|
| 44 |
+
# Replace non-alphanumeric characters with underscore
|
| 45 |
+
cleaned = re.sub(r'[^a-zA-Z\d]+', '_', file_name)
|
| 46 |
+
|
| 47 |
+
# Remove multiple underscores
|
| 48 |
+
clean_file_name = re.sub(r'_+', '_', cleaned).strip('_')
|
| 49 |
+
|
| 50 |
+
# Add UUID for uniqueness
|
| 51 |
+
random_uuid = uuid.uuid4().hex[:6]
|
| 52 |
+
|
| 53 |
+
output_dir = os.path.dirname(file_path)
|
| 54 |
+
clean_file_path = os.path.join(
|
| 55 |
+
output_dir,
|
| 56 |
+
f"{clean_file_name}_{random_uuid}{file_extension}"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
return clean_file_path
|
| 60 |
+
except Exception as e:
|
| 61 |
+
logger.error(f"Error cleaning file name: {e}")
|
| 62 |
+
raise
|
| 63 |
|
| 64 |
def remove_silence(file_path, minimum_silence=50):
|
| 65 |
+
"""Remove silence from audio with error handling"""
|
| 66 |
+
try:
|
| 67 |
+
logger.info(f"Processing audio file: {file_path}")
|
| 68 |
+
|
| 69 |
+
# Load audio
|
| 70 |
+
sound = AudioSegment.from_file(file_path)
|
| 71 |
+
|
| 72 |
+
if len(sound) == 0:
|
| 73 |
+
raise ValueError("Audio file is empty")
|
| 74 |
+
|
| 75 |
+
logger.info(f"Audio loaded: {len(sound)}ms duration")
|
| 76 |
+
|
| 77 |
+
# Split on silence
|
| 78 |
+
audio_chunks = split_on_silence(
|
| 79 |
+
sound,
|
| 80 |
+
min_silence_len=100,
|
| 81 |
+
silence_thresh=-45,
|
| 82 |
+
keep_silence=minimum_silence
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
if not audio_chunks:
|
| 86 |
+
logger.warning("No audio chunks detected after silence removal")
|
| 87 |
+
return file_path # Return original if nothing to remove
|
| 88 |
+
|
| 89 |
+
# Combine chunks
|
| 90 |
+
combined = AudioSegment.empty()
|
| 91 |
+
for chunk in audio_chunks:
|
| 92 |
+
combined += chunk
|
| 93 |
+
|
| 94 |
+
# Generate output path
|
| 95 |
+
output_path = clean_file_name(file_path)
|
| 96 |
+
|
| 97 |
+
# Export with error handling
|
| 98 |
+
combined.export(output_path, format=os.path.splitext(file_path)[1].lower().strip('.'))
|
| 99 |
+
logger.info(f"Audio processed successfully: {output_path}")
|
| 100 |
+
|
| 101 |
+
return output_path
|
| 102 |
+
|
| 103 |
+
except Exception as e:
|
| 104 |
+
logger.error(f"Error removing silence: {e}")
|
| 105 |
+
raise
|
| 106 |
|
| 107 |
def calculate_duration(file_path):
|
| 108 |
+
"""Calculate audio duration in seconds"""
|
| 109 |
+
try:
|
| 110 |
+
if not os.path.exists(file_path):
|
| 111 |
+
return 0
|
| 112 |
+
audio = AudioSegment.from_file(file_path)
|
| 113 |
+
duration_seconds = len(audio) / 1000.0
|
| 114 |
+
return duration_seconds
|
| 115 |
+
except Exception as e:
|
| 116 |
+
logger.error(f"Error calculating duration: {e}")
|
| 117 |
+
return 0
|
| 118 |
|
| 119 |
def track_file(file_path):
|
| 120 |
+
"""Track file for cleanup"""
|
| 121 |
+
if file_path and os.path.exists(file_path):
|
| 122 |
+
FILE_TIMESTAMPS[file_path] = time.time()
|
| 123 |
|
| 124 |
def cleanup_tracked_files(max_age_seconds=3600):
|
| 125 |
+
"""Clean up old tracked files"""
|
| 126 |
now = time.time()
|
| 127 |
to_delete = []
|
| 128 |
+
|
| 129 |
+
for file_path, created_time in list(FILE_TIMESTAMPS.items()):
|
| 130 |
if now - created_time > max_age_seconds:
|
| 131 |
+
try:
|
| 132 |
+
if os.path.exists(file_path):
|
| 133 |
os.remove(file_path)
|
| 134 |
+
logger.info(f"Cleaned up: {file_path}")
|
| 135 |
+
except Exception as e:
|
| 136 |
+
logger.error(f"Error cleaning file {file_path}: {e}")
|
| 137 |
+
|
| 138 |
to_delete.append(file_path)
|
| 139 |
+
|
|
|
|
| 140 |
for f in to_delete:
|
| 141 |
FILE_TIMESTAMPS.pop(f, None)
|
| 142 |
|
|
|
|
|
|
|
| 143 |
def start_cleanup_worker(interval=3600):
|
| 144 |
+
"""Start background cleanup worker"""
|
| 145 |
def worker():
|
| 146 |
while True:
|
| 147 |
+
try:
|
| 148 |
+
cleanup_tracked_files()
|
| 149 |
+
except Exception as e:
|
| 150 |
+
logger.error(f"Cleanup worker error: {e}")
|
| 151 |
time.sleep(interval)
|
| 152 |
+
|
| 153 |
import threading
|
| 154 |
+
thread = threading.Thread(target=worker, daemon=True)
|
| 155 |
+
thread.start()
|
| 156 |
+
logger.info("Cleanup worker started")
|
| 157 |
|
|
|
|
| 158 |
def process_audio(audio_file, seconds=0.05):
|
| 159 |
+
"""Main audio processing function with comprehensive error handling"""
|
| 160 |
+
try:
|
| 161 |
+
# Validate input
|
| 162 |
+
if audio_file is None:
|
| 163 |
+
return None, None, "⚠️ No file uploaded. Please select an audio file."
|
| 164 |
+
|
| 165 |
+
if not os.path.exists(audio_file):
|
| 166 |
+
return None, None, "❌ File not found. Please try uploading again."
|
| 167 |
+
|
| 168 |
+
# Validate file
|
| 169 |
+
is_valid, message = validate_audio_file(audio_file)
|
| 170 |
+
if not is_valid:
|
| 171 |
+
return None, None, message
|
| 172 |
+
|
| 173 |
+
# Validate silence parameter
|
| 174 |
+
if seconds < 0 or seconds > 5:
|
| 175 |
+
return None, None, "⚠️ Silence duration must be between 0-5 seconds"
|
| 176 |
+
|
| 177 |
+
track_file(audio_file)
|
| 178 |
+
|
| 179 |
+
# Calculate before duration
|
| 180 |
+
before = calculate_duration(audio_file)
|
| 181 |
+
|
| 182 |
+
if before == 0:
|
| 183 |
+
return None, None, "❌ Could not read audio file. File may be corrupted."
|
| 184 |
+
|
| 185 |
+
logger.info(f"Original duration: {before:.3f}s")
|
| 186 |
+
|
| 187 |
+
# Process audio
|
| 188 |
+
keep_silence = int(seconds * 1000)
|
| 189 |
+
output_audio_file = remove_silence(audio_file, minimum_silence=keep_silence)
|
| 190 |
+
|
| 191 |
+
track_file(output_audio_file)
|
| 192 |
+
|
| 193 |
+
# Calculate after duration
|
| 194 |
+
after = calculate_duration(output_audio_file)
|
| 195 |
+
|
| 196 |
+
# Calculate reduction percentage
|
| 197 |
+
reduction_percent = ((before - after) / before * 100) if before > 0 else 0
|
| 198 |
+
|
| 199 |
+
# Format result message
|
| 200 |
+
result_text = f"""✅ Processing Complete
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
+
📊 Duration Comparison:
|
| 203 |
+
• Original: {before:.3f} seconds
|
| 204 |
+
• Processed: {after:.3f} seconds
|
| 205 |
+
• Removed: {before - after:.3f} seconds ({reduction_percent:.1f}%)
|
| 206 |
|
| 207 |
+
⚙️ Settings:
|
| 208 |
+
• Keep Silence Up To: {seconds:.3f} seconds
|
| 209 |
+
"""
|
| 210 |
+
|
| 211 |
+
logger.info(f"Processing complete - Reduction: {reduction_percent:.1f}%")
|
| 212 |
+
|
| 213 |
+
return output_audio_file, output_audio_file, result_text
|
| 214 |
+
|
| 215 |
+
except ValueError as e:
|
| 216 |
+
error_msg = f"❌ Audio Error: {str(e)}"
|
| 217 |
+
logger.error(error_msg)
|
| 218 |
+
return None, None, error_msg
|
| 219 |
+
except Exception as e:
|
| 220 |
+
error_msg = f"❌ Processing Error: {str(e)}"
|
| 221 |
+
logger.error(error_msg)
|
| 222 |
+
return None, None, error_msg
|
| 223 |
|
| 224 |
def ui():
|
| 225 |
+
"""Create professional Gradio UI"""
|
| 226 |
theme = gr.themes.Soft(
|
| 227 |
+
font=[gr.themes.GoogleFont("Inter"), "system-ui", "sans-serif"]
|
| 228 |
)
|
| 229 |
+
|
| 230 |
css = """
|
| 231 |
+
:root {
|
| 232 |
+
--primary: #3b82f6;
|
| 233 |
+
--primary-dark: #1e40af;
|
| 234 |
+
--success: #10b981;
|
| 235 |
+
--danger: #ef4444;
|
| 236 |
+
--warning: #f59e0b;
|
| 237 |
+
--bg-dark: #0f172a;
|
| 238 |
+
--bg-card: #1e293b;
|
| 239 |
+
--border: #334155;
|
| 240 |
+
--text-primary: #f1f5f9;
|
| 241 |
+
--text-secondary: #cbd5e1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
}
|
| 243 |
|
| 244 |
+
* {
|
| 245 |
+
margin: 0;
|
| 246 |
+
padding: 0;
|
| 247 |
+
box-sizing: border-box;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
}
|
| 249 |
+
|
| 250 |
+
.gradio-container {
|
| 251 |
+
max-width: 1000px !important;
|
| 252 |
+
background: linear-gradient(135deg, var(--bg-dark) 0%, #1a2340 100%) !important;
|
| 253 |
+
border-radius: 0 !important;
|
| 254 |
+
padding: 0 !important;
|
|
|
|
|
|
|
| 255 |
}
|
| 256 |
+
|
| 257 |
+
.gradio-container::before {
|
|
|
|
| 258 |
content: "";
|
| 259 |
+
position: fixed;
|
| 260 |
+
top: 0;
|
| 261 |
+
left: 0;
|
| 262 |
+
right: 0;
|
| 263 |
+
bottom: 0;
|
| 264 |
background-image:
|
| 265 |
+
radial-gradient(circle at 20% 50%, rgba(59, 130, 246, 0.1) 0%, transparent 50%),
|
| 266 |
+
radial-gradient(circle at 80% 80%, rgba(16, 185, 129, 0.1) 0%, transparent 50%);
|
|
|
|
| 267 |
pointer-events: none;
|
| 268 |
+
z-index: -1;
|
| 269 |
}
|
| 270 |
+
|
| 271 |
+
/* Header Styles */
|
| 272 |
+
.header-container {
|
| 273 |
+
background: linear-gradient(135deg, rgba(59, 130, 246, 0.1) 0%, rgba(16, 185, 129, 0.05) 100%);
|
| 274 |
+
border-bottom: 1px solid var(--border);
|
| 275 |
+
padding: 40px 30px;
|
| 276 |
+
text-align: center;
|
| 277 |
+
margin-bottom: 30px;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
}
|
| 279 |
+
|
| 280 |
+
.header-container h1 {
|
| 281 |
+
font-size: 2.5em;
|
| 282 |
+
font-weight: 700;
|
| 283 |
+
background: linear-gradient(135deg, var(--primary) 0%, var(--success) 100%);
|
| 284 |
+
-webkit-background-clip: text;
|
| 285 |
+
-webkit-text-fill-color: transparent;
|
| 286 |
+
margin-bottom: 12px;
|
| 287 |
+
letter-spacing: -0.5px;
|
| 288 |
}
|
| 289 |
+
|
| 290 |
+
.header-container p {
|
| 291 |
+
color: var(--text-secondary);
|
| 292 |
+
font-size: 1.05em;
|
| 293 |
+
margin-bottom: 8px;
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
.warning-box {
|
| 297 |
+
background: rgba(245, 158, 11, 0.1);
|
| 298 |
+
border-left: 3px solid var(--warning);
|
| 299 |
+
padding: 12px 16px;
|
| 300 |
border-radius: 6px;
|
| 301 |
+
color: #fbbf24;
|
| 302 |
+
font-size: 0.9em;
|
| 303 |
+
margin-top: 12px;
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
/* Card Styles */
|
| 307 |
+
.card-container {
|
| 308 |
+
background: var(--bg-card);
|
| 309 |
+
border: 1px solid var(--border);
|
| 310 |
+
border-radius: 12px;
|
| 311 |
+
padding: 24px;
|
| 312 |
+
margin-bottom: 24px;
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
.input-section, .output-section {
|
| 316 |
+
background: var(--bg-card);
|
| 317 |
+
border: 1px solid var(--border);
|
| 318 |
+
border-radius: 12px;
|
| 319 |
+
padding: 24px;
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
.section-title {
|
| 323 |
+
color: var(--text-primary);
|
| 324 |
+
font-size: 1.1em;
|
| 325 |
+
font-weight: 600;
|
| 326 |
+
margin-bottom: 18px;
|
| 327 |
+
display: flex;
|
| 328 |
+
align-items: center;
|
| 329 |
+
gap: 8px;
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
/* Input Components */
|
| 333 |
+
.gradio-textbox, .gradio-audio, .gradio-number {
|
| 334 |
+
color: var(--text-primary) !important;
|
| 335 |
+
background: rgba(30, 41, 59, 0.6) !important;
|
| 336 |
+
border: 1px solid var(--border) !important;
|
| 337 |
+
border-radius: 8px !important;
|
| 338 |
+
}
|
| 339 |
+
|
| 340 |
+
.gradio-textbox:focus, .gradio-audio:focus, .gradio-number:focus {
|
| 341 |
+
border-color: var(--primary) !important;
|
| 342 |
+
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1) !important;
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
.gradio-label {
|
| 346 |
+
color: var(--text-primary) !important;
|
| 347 |
+
font-weight: 500 !important;
|
| 348 |
+
margin-bottom: 8px !important;
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
.gradio-label > span {
|
| 352 |
+
color: var(--text-secondary) !important;
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
/* Button Styles */
|
| 356 |
+
button.primary, button.secondary {
|
| 357 |
+
font-weight: 600;
|
| 358 |
+
border-radius: 8px;
|
| 359 |
+
padding: 14px 24px !important;
|
| 360 |
+
font-size: 1em;
|
| 361 |
+
border: none !important;
|
| 362 |
transition: all 0.3s ease;
|
| 363 |
+
cursor: pointer;
|
| 364 |
+
text-transform: uppercase;
|
| 365 |
+
letter-spacing: 0.5px;
|
| 366 |
}
|
| 367 |
+
|
| 368 |
+
button.primary {
|
| 369 |
+
background: linear-gradient(135deg, var(--primary) 0%, #2563eb 100%) !important;
|
| 370 |
+
color: white !important;
|
| 371 |
+
box-shadow: 0 4px 15px rgba(59, 130, 246, 0.3);
|
|
|
|
| 372 |
}
|
| 373 |
|
| 374 |
+
button.primary:hover {
|
| 375 |
+
background: linear-gradient(135deg, var(--primary-dark) 0%, #1e40af 100%) !important;
|
| 376 |
+
box-shadow: 0 6px 20px rgba(59, 130, 246, 0.4);
|
| 377 |
+
transform: translateY(-2px);
|
|
|
|
|
|
|
| 378 |
}
|
| 379 |
+
|
| 380 |
+
button.primary:active {
|
| 381 |
+
transform: translateY(0);
|
| 382 |
+
}
|
| 383 |
+
|
| 384 |
+
/* Result Message Styles */
|
| 385 |
+
.result-box {
|
| 386 |
+
background: rgba(16, 185, 129, 0.1);
|
| 387 |
+
border-left: 3px solid var(--success);
|
| 388 |
+
padding: 16px;
|
| 389 |
+
border-radius: 6px;
|
| 390 |
+
font-family: 'Courier New', monospace;
|
| 391 |
+
color: #86efac;
|
| 392 |
+
line-height: 1.6;
|
| 393 |
+
white-space: pre-wrap;
|
| 394 |
+
}
|
| 395 |
+
|
| 396 |
+
.result-box.error {
|
| 397 |
+
background: rgba(239, 68, 68, 0.1);
|
| 398 |
+
border-left-color: var(--danger);
|
| 399 |
+
color: #fca5a5;
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
/* Row Spacing */
|
| 403 |
+
.gradio-row {
|
| 404 |
+
gap: 24px !important;
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
/* Footer */
|
| 408 |
+
.footer-container {
|
| 409 |
+
background: rgba(0, 0, 0, 0.2);
|
| 410 |
+
border-top: 1px solid var(--border);
|
| 411 |
+
padding: 30px;
|
| 412 |
+
text-align: center;
|
| 413 |
+
margin-top: 40px;
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
.footer-container p {
|
| 417 |
+
color: var(--text-secondary);
|
| 418 |
+
font-size: 0.9em;
|
| 419 |
+
margin-bottom: 8px;
|
| 420 |
+
}
|
| 421 |
+
|
| 422 |
+
.footer-container a {
|
| 423 |
+
color: var(--primary);
|
| 424 |
text-decoration: none;
|
| 425 |
transition: color 0.2s;
|
| 426 |
}
|
| 427 |
+
|
| 428 |
+
.footer-container a:hover {
|
| 429 |
+
color: var(--primary) !important;
|
| 430 |
+
text-decoration: underline;
|
| 431 |
}
|
| 432 |
+
|
| 433 |
+
/* Loading Animation */
|
| 434 |
+
@keyframes pulse {
|
| 435 |
+
0%, 100% { opacity: 1; }
|
| 436 |
+
50% { opacity: 0.7; }
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
.gradio-button.loading {
|
| 440 |
+
animation: pulse 1.5s cubic-bezier(0.4, 0, 0.6, 1) infinite;
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
/* Responsive */
|
| 444 |
+
@media (max-width: 768px) {
|
| 445 |
+
.header-container h1 {
|
| 446 |
+
font-size: 1.8em;
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
.gradio-row {
|
| 450 |
+
flex-direction: column !important;
|
| 451 |
+
}
|
| 452 |
+
|
| 453 |
+
.input-section, .output-section {
|
| 454 |
+
padding: 16px;
|
| 455 |
+
}
|
| 456 |
+
}
|
| 457 |
+
"""
|
| 458 |
+
|
| 459 |
+
with gr.Blocks(theme=theme, css=css, title="Audio Silence Remover") as demo:
|
| 460 |
+
|
| 461 |
+
# Header
|
| 462 |
+
gr.HTML("""
|
| 463 |
+
<div class="header-container">
|
| 464 |
+
<h1>🔇 Audio Silence Remover</h1>
|
| 465 |
+
<p>Remove silent parts from your audio files instantly</p>
|
| 466 |
+
<p style="font-size: 0.95em; color: #94a3b8;">Process MP3, WAV, OGG, M4A, and FLAC files up to 500MB</p>
|
| 467 |
+
<div class="warning-box">
|
| 468 |
+
⚠️ Don't upload copyrighted content without permission
|
| 469 |
+
</div>
|
| 470 |
</div>
|
| 471 |
+
""")
|
| 472 |
+
|
| 473 |
+
with gr.Row():
|
| 474 |
+
# Left Column - Inputs
|
| 475 |
+
with gr.Column(scale=1):
|
| 476 |
+
gr.HTML("<div class='section-title'>📥 Input</div>")
|
| 477 |
+
|
| 478 |
+
with gr.Group(elem_classes="input-section"):
|
| 479 |
+
audio_input = gr.Audio(
|
| 480 |
+
label="🎵 Upload Audio File",
|
| 481 |
+
type="filepath",
|
| 482 |
+
sources=["upload", "microphone"],
|
| 483 |
+
info="Drag & drop or click to upload (max 500MB)"
|
| 484 |
+
)
|
| 485 |
+
|
| 486 |
+
silence_threshold = gr.Number(
|
| 487 |
+
label="⏱️ Keep Silence Up To (Seconds)",
|
| 488 |
+
value=0.05,
|
| 489 |
+
minimum=0,
|
| 490 |
+
maximum=5,
|
| 491 |
+
step=0.01,
|
| 492 |
+
info="Amount of silence to preserve between words"
|
| 493 |
+
)
|
| 494 |
+
|
| 495 |
+
submit_btn = gr.Button(
|
| 496 |
+
"🔇 Remove Silence",
|
| 497 |
+
variant="primary",
|
| 498 |
+
size="lg",
|
| 499 |
+
scale=2
|
| 500 |
+
)
|
| 501 |
+
|
| 502 |
+
# Right Column - Outputs
|
| 503 |
+
with gr.Column(scale=1):
|
| 504 |
+
gr.HTML("<div class='section-title'>📤 Output</div>")
|
| 505 |
+
|
| 506 |
+
with gr.Group(elem_classes="output-section"):
|
| 507 |
+
audio_output = gr.Audio(
|
| 508 |
+
label="🎧 Play Processed Audio",
|
| 509 |
+
info="Preview your processed audio"
|
| 510 |
+
)
|
| 511 |
+
|
| 512 |
+
file_output = gr.File(
|
| 513 |
+
label="💾 Download File",
|
| 514 |
+
info="Click to download your cleaned audio"
|
| 515 |
+
)
|
| 516 |
+
|
| 517 |
+
duration_output = gr.Textbox(
|
| 518 |
+
label="📊 Processing Details",
|
| 519 |
+
interactive=False,
|
| 520 |
+
lines=8
|
| 521 |
+
)
|
| 522 |
+
|
| 523 |
+
# Click handler
|
| 524 |
+
submit_btn.click(
|
| 525 |
+
fn=process_audio,
|
| 526 |
+
inputs=[audio_input, silence_threshold],
|
| 527 |
+
outputs=[audio_output, file_output, duration_output]
|
| 528 |
+
)
|
| 529 |
+
|
| 530 |
+
# Footer
|
| 531 |
+
gr.HTML("""
|
| 532 |
+
<div class="footer-container">
|
| 533 |
+
<p>✨ Made with <span style="color: #ef4444;">❤️</span> for content creators</p>
|
| 534 |
+
<p>Download & run locally: <a href="https://github.com/NeuralFalconYT/Remove-Silence-From-Audio" target="_blank">GitHub Repository</a></p>
|
| 535 |
+
</div>
|
| 536 |
+
""")
|
| 537 |
+
|
| 538 |
return demo
|
| 539 |
|
| 540 |
+
def main():
|
| 541 |
+
"""Main entry point"""
|
| 542 |
+
import click
|
| 543 |
+
|
| 544 |
+
@click.command()
|
| 545 |
+
@click.option("--debug", is_flag=True, default=False, help="Enable debug mode")
|
| 546 |
+
@click.option("--share", is_flag=True, default=False, help="Generate public link")
|
| 547 |
+
@click.option("--server-name", default="127.0.0.1", help="Server address")
|
| 548 |
+
@click.option("--server-port", type=int, default=7860, help="Server port")
|
| 549 |
+
def run(debug, share, server_name, server_port):
|
| 550 |
+
logger.info("Starting Audio Silence Remover...")
|
| 551 |
+
start_cleanup_worker()
|
| 552 |
+
|
| 553 |
+
demo = ui()
|
| 554 |
+
|
| 555 |
+
demo.queue(max_size=10).launch(
|
| 556 |
+
debug=debug,
|
| 557 |
+
share=share,
|
| 558 |
+
server_name=server_name,
|
| 559 |
+
server_port=server_port,
|
| 560 |
+
show_error=True,
|
| 561 |
+
show_tips=False
|
| 562 |
+
)
|
| 563 |
+
|
| 564 |
+
run()
|
| 565 |
+
|
| 566 |
if __name__ == "__main__":
|
| 567 |
+
main()
|