Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from speechbrain.pretrained import SepformerSeparation as separator
|
| 3 |
+
import torchaudio
|
| 4 |
+
import torch
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
class AudioDenoiser:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
# Initialize the SepFormer model for audio enhancement
|
| 10 |
+
self.model = separator.from_hparams(
|
| 11 |
+
source="speechbrain/sepformer-dns4-16k-enhancement",
|
| 12 |
+
savedir='pretrained_models/sepformer-dns4-16k-enhancement'
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# Create output directory if it doesn't exist
|
| 16 |
+
os.makedirs("enhanced_audio", exist_ok=True)
|
| 17 |
+
|
| 18 |
+
def enhance_audio(self, audio_path):
|
| 19 |
+
"""
|
| 20 |
+
Process the input audio file and return the enhanced version
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
audio_path (str): Path to the input audio file
|
| 24 |
+
|
| 25 |
+
Returns:
|
| 26 |
+
str: Path to the enhanced audio file
|
| 27 |
+
"""
|
| 28 |
+
try:
|
| 29 |
+
# Separate and enhance the audio
|
| 30 |
+
est_sources = self.model.separate_file(path=audio_path)
|
| 31 |
+
|
| 32 |
+
# Generate output filename
|
| 33 |
+
output_path = os.path.join("enhanced_audio", "enhanced_audio.wav")
|
| 34 |
+
|
| 35 |
+
# Save the enhanced audio
|
| 36 |
+
torchaudio.save(
|
| 37 |
+
output_path,
|
| 38 |
+
est_sources[:, :, 0].detach().cpu(),
|
| 39 |
+
16000 # Sample rate
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
return output_path
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
raise gr.Error(f"Error processing audio: {str(e)}")
|
| 46 |
+
|
| 47 |
+
def create_gradio_interface():
|
| 48 |
+
# Initialize the denoiser
|
| 49 |
+
denoiser = AudioDenoiser()
|
| 50 |
+
|
| 51 |
+
# Create the Gradio interface
|
| 52 |
+
interface = gr.Interface(
|
| 53 |
+
fn=denoiser.enhance_audio,
|
| 54 |
+
inputs=gr.Audio(
|
| 55 |
+
type="filepath",
|
| 56 |
+
label="Upload Noisy Audio"
|
| 57 |
+
),
|
| 58 |
+
outputs=gr.Audio(
|
| 59 |
+
label="Enhanced Audio"
|
| 60 |
+
),
|
| 61 |
+
title="Audio Denoising using SepFormer",
|
| 62 |
+
description="""
|
| 63 |
+
This application uses the SepFormer model from SpeechBrain to enhance audio quality
|
| 64 |
+
by removing background noise. Upload any noisy audio file to get started.
|
| 65 |
+
""",
|
| 66 |
+
article="""
|
| 67 |
+
This application uses the SepFormer model trained on the DNS4 dataset.
|
| 68 |
+
For more information, visit the [SpeechBrain documentation](https://speechbrain.github.io/).
|
| 69 |
+
"""
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
return interface
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
# Create and launch the interface
|
| 76 |
+
demo = create_gradio_interface()
|
| 77 |
+
demo.launch()
|