Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,48 +4,49 @@ from pydub import AudioSegment
|
|
4 |
from pydub.effects import compress_dynamic_range
|
5 |
from pydub.silence import split_on_silence
|
6 |
|
7 |
-
def remove_silence(audio_file, silence_thresh, min_silence_len):
|
8 |
-
#
|
9 |
audio = AudioSegment.from_file(audio_file)
|
10 |
|
11 |
-
#
|
12 |
-
compressed_audio = compress_dynamic_range(audio, threshold
|
13 |
|
14 |
-
#
|
15 |
chunks = split_on_silence(compressed_audio,
|
16 |
-
#
|
17 |
min_silence_len=min_silence_len,
|
18 |
|
19 |
-
#
|
20 |
silence_thresh=silence_thresh
|
21 |
)
|
22 |
|
23 |
-
#
|
24 |
if not chunks:
|
25 |
-
return audio_file #
|
26 |
|
27 |
-
#
|
28 |
audio_without_silence = sum(chunks)
|
29 |
|
30 |
-
#
|
31 |
output_file = "audio_without_silence.wav"
|
32 |
audio_without_silence.export(output_file, format="wav")
|
33 |
|
34 |
return output_file
|
35 |
|
36 |
-
#
|
37 |
iface = gr.Interface(
|
38 |
fn=remove_silence,
|
39 |
inputs=[
|
40 |
gr.Audio(sources=["upload", "microphone"], type="filepath", label="Upload or Record Audio File"),
|
41 |
-
gr.Slider(minimum=-100, maximum=0, step=1, value=-16, label="Silence Threshold (dBFS)"),
|
42 |
-
gr.Slider(minimum=10, maximum=5000, step=10, value=500, label="Minimum Silence Length (ms)")
|
|
|
43 |
],
|
44 |
outputs=gr.Audio(type="filepath", label="Audio Without Silence"),
|
45 |
title="Remove Silence from Audio",
|
46 |
-
description="Upload an audio file or record audio and get the audio with silence removed. Adjust the silence threshold
|
47 |
)
|
48 |
|
49 |
-
#
|
50 |
if __name__ == "__main__":
|
51 |
iface.launch()
|
|
|
4 |
from pydub.effects import compress_dynamic_range
|
5 |
from pydub.silence import split_on_silence
|
6 |
|
7 |
+
def remove_silence(audio_file, silence_thresh, min_silence_len, compression_threshold):
|
8 |
+
# Load the audio file
|
9 |
audio = AudioSegment.from_file(audio_file)
|
10 |
|
11 |
+
# Apply compression to the audio file
|
12 |
+
compressed_audio = compress_dynamic_range(audio, threshold=compression_threshold)
|
13 |
|
14 |
+
# Split the audio into parts without silence
|
15 |
chunks = split_on_silence(compressed_audio,
|
16 |
+
# Minimum silence length (in milliseconds)
|
17 |
min_silence_len=min_silence_len,
|
18 |
|
19 |
+
# Silence threshold (in dBFS)
|
20 |
silence_thresh=silence_thresh
|
21 |
)
|
22 |
|
23 |
+
# Check if any chunks were found
|
24 |
if not chunks:
|
25 |
+
return audio_file # Return the original file if no silence chunks were found
|
26 |
|
27 |
+
# Combine chunks without silence
|
28 |
audio_without_silence = sum(chunks)
|
29 |
|
30 |
+
# Export the audio file without silence
|
31 |
output_file = "audio_without_silence.wav"
|
32 |
audio_without_silence.export(output_file, format="wav")
|
33 |
|
34 |
return output_file
|
35 |
|
36 |
+
# Create Gradio interface
|
37 |
iface = gr.Interface(
|
38 |
fn=remove_silence,
|
39 |
inputs=[
|
40 |
gr.Audio(sources=["upload", "microphone"], type="filepath", label="Upload or Record Audio File"),
|
41 |
+
gr.Slider(minimum=-100, maximum=0, step=1, value=-16, label="Silence Threshold (dBFS)", description="The threshold in decibels (dBFS) below which audio is considered silence."),
|
42 |
+
gr.Slider(minimum=10, maximum=5000, step=10, value=500, label="Minimum Silence Length (ms)", description="The minimum length of silence (in milliseconds) to be detected."),
|
43 |
+
gr.Slider(minimum=-100, maximum=0, step=1, value=-20, label="Compression Threshold (dBFS)", description="The threshold in decibels (dBFS) for audio compression. Lower values compress more.")
|
44 |
],
|
45 |
outputs=gr.Audio(type="filepath", label="Audio Without Silence"),
|
46 |
title="Remove Silence from Audio",
|
47 |
+
description="Upload an audio file or record audio and get the audio with silence removed. Adjust the silence threshold, minimum silence length, and compression threshold."
|
48 |
)
|
49 |
|
50 |
+
# Run the app
|
51 |
if __name__ == "__main__":
|
52 |
iface.launch()
|