Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torchaudio
|
| 4 |
+
from torchaudio.transforms import Resample
|
| 5 |
+
import moviepy.editor as mp
|
| 6 |
+
import numpy as np
|
| 7 |
+
from denoiser.pretrained import master64 # Import Facebook denoiser pre-trained model
|
| 8 |
+
from denoiser.denoiser import Denoiser # Denoising wrapper
|
| 9 |
+
|
| 10 |
+
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 11 |
+
print("Device - ", DEVICE)
|
| 12 |
+
|
| 13 |
+
# Load Facebook denoiser model
|
| 14 |
+
model = master64()
|
| 15 |
+
den = Denoiser(model).to(DEVICE)
|
| 16 |
+
den.eval()
|
| 17 |
+
|
| 18 |
+
def identity(video_path):
|
| 19 |
+
print(video_path)
|
| 20 |
+
|
| 21 |
+
# Extract audio from the video
|
| 22 |
+
video = mp.VideoFileClip(video_path)
|
| 23 |
+
audio = video.audio
|
| 24 |
+
wav_file = "tmp.wav"
|
| 25 |
+
audio.write_audiofile(wav_file)
|
| 26 |
+
print("Wav stored.")
|
| 27 |
+
|
| 28 |
+
# Load audio
|
| 29 |
+
waveform, sr = torchaudio.load(wav_file)
|
| 30 |
+
waveform = waveform.to(DEVICE)
|
| 31 |
+
|
| 32 |
+
# Resample if necessary
|
| 33 |
+
target_sr = 48000
|
| 34 |
+
if sr != target_sr:
|
| 35 |
+
resampler = Resample(orig_freq=sr, new_freq=target_sr).to(DEVICE)
|
| 36 |
+
waveform = resampler(waveform)
|
| 37 |
+
sr = target_sr
|
| 38 |
+
|
| 39 |
+
# Process audio in chunks to avoid memory issues
|
| 40 |
+
chunk_duration = 10 # seconds
|
| 41 |
+
chunk_size = sr * chunk_duration
|
| 42 |
+
num_chunks = int(np.ceil(waveform.shape[1] / chunk_size))
|
| 43 |
+
|
| 44 |
+
enhanced_chunks = []
|
| 45 |
+
for i in range(num_chunks):
|
| 46 |
+
start = i * chunk_size
|
| 47 |
+
end = min((i + 1) * chunk_size, waveform.shape[1])
|
| 48 |
+
chunk = waveform[:, start:end]
|
| 49 |
+
enhanced_chunk = den(chunk.unsqueeze(0))
|
| 50 |
+
enhanced_chunks.append(enhanced_chunk.squeeze(0))
|
| 51 |
+
|
| 52 |
+
# Combine enhanced audio
|
| 53 |
+
enhanced_audio = torch.cat(enhanced_chunks, dim=1).cpu()
|
| 54 |
+
|
| 55 |
+
# Save enhanced audio
|
| 56 |
+
output_audio_path = "enhanced_aud.wav"
|
| 57 |
+
torchaudio.save(output_audio_path, enhanced_audio, sr)
|
| 58 |
+
|
| 59 |
+
# Replace audio in video
|
| 60 |
+
enhanced_audio_clip = mp.AudioFileClip(output_audio_path)
|
| 61 |
+
final_video = video.set_audio(enhanced_audio_clip)
|
| 62 |
+
output_video_path = "output_video.mp4"
|
| 63 |
+
final_video.write_videofile(output_video_path,
|
| 64 |
+
codec='libx264',
|
| 65 |
+
audio_codec='aac',
|
| 66 |
+
temp_audiofile='temp-audio.m4a',
|
| 67 |
+
remove_temp=True)
|
| 68 |
+
return output_video_path
|
| 69 |
+
|
| 70 |
+
demo = gr.Interface(
|
| 71 |
+
fn=identity,
|
| 72 |
+
title="NoNoise - THE BEST AUDIO DENOISER",
|
| 73 |
+
description="NoNoise is the only platform you need for removing all kinds of background noise from your videos!!",
|
| 74 |
+
examples=[['audiopure_og.mov'], ['example.mp4']],
|
| 75 |
+
cache_examples=True,
|
| 76 |
+
inputs=gr.Video(label="Input Video", source="upload"),
|
| 77 |
+
outputs=gr.Video(label="Output Video"),
|
| 78 |
+
)
|
| 79 |
+
demo.launch()
|