Ivan000's picture
Update app.py
5f8b366 verified
# app.py
import gradio as gr
from pydub import AudioSegment
from pydub.effects import compress_dynamic_range
from pydub.silence import split_on_silence
def remove_silence(audio_file, silence_thresh, min_silence_len, compression_threshold, amplification):
# Load the audio file
audio = AudioSegment.from_file(audio_file)
# Apply compression to the audio file
compressed_audio = compress_dynamic_range(audio, threshold=compression_threshold)
# Split the audio into parts without silence
chunks = split_on_silence(compressed_audio,
# Minimum silence length (in milliseconds)
min_silence_len=min_silence_len,
# Silence threshold (in dBFS)
silence_thresh=silence_thresh
)
# Check if any chunks were found
if not chunks:
return audio_file # Return the original file if no silence chunks were found
# Combine chunks without silence
audio_without_silence = sum(chunks)
# Amplify the audio
amplified_audio = audio_without_silence + amplification
# Export the amplified audio file
output_file = "audio_without_silence.wav"
amplified_audio.export(output_file, format="wav")
return output_file
# Create Gradio interface
iface = gr.Interface(
fn=remove_silence,
inputs=[
gr.Audio(sources=["upload", "microphone"], type="filepath", label="Upload or Record Audio File"),
gr.Slider(minimum=-100, maximum=0, step=1, value=-16, label="Silence Threshold (dBFS): The threshold in decibels (dBFS) below which audio is considered silence."),
gr.Slider(minimum=10, maximum=5000, step=10, value=500, label="Minimum Silence Length (ms): The minimum length of silence (in milliseconds) to be detected."),
gr.Slider(minimum=-100, maximum=0, step=1, value=-20, label="Compression Threshold (dBFS): The threshold in decibels (dBFS) for audio compression. Lower values compress more."),
gr.Slider(minimum=0, maximum=30, step=1, value=0, label="Amplification (dB): Amplify the volume of the processed audio. Range from 0 dB (no change) to 30 dB.")
],
outputs=gr.Audio(type="filepath", label="Processed Audio Without Silence"),
title="Remove Silence from Audio",
description="Upload an audio file or record audio and get the audio with silence removed. Adjust the silence threshold, minimum silence length, compression threshold, and amplification."
)
# Run the app
if __name__ == "__main__":
iface.launch()