Spaces:
Runtime error
Runtime error
Commit
·
3b4a0f4
1
Parent(s):
ba6efe9
update app
Browse files- app.py +28 -26
- denoisers/SpectralGating.py +4 -6
app.py
CHANGED
@@ -10,6 +10,20 @@ import torchaudio
|
|
10 |
import yaml
|
11 |
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def run_app(model_filename, config_filename):
|
15 |
model_path = hf_hub_download(repo_id="BorisovMaksim/demucs", filename=model_filename)
|
@@ -20,37 +34,25 @@ def run_app(model_filename, config_filename):
|
|
20 |
checkpoint = torch.load(model_path, map_location=torch.device('cpu'))
|
21 |
model.load_state_dict(checkpoint['model_state_dict'])
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
src_path = Path("cache_wav/original/{}.wav".format(str(uuid.uuid4())))
|
26 |
-
tgt_path = Path("cache_wav/denoised/{}.wav".format(str(uuid.uuid4())))
|
27 |
-
src_path.parent.mkdir(exist_ok=True, parents=True)
|
28 |
-
tgt_path.parent.mkdir(exist_ok=True, parents=True)
|
29 |
-
(ffmpeg.input(audio)
|
30 |
-
.output(src_path.as_posix(), acodec='pcm_s16le', ac=1, ar=22050)
|
31 |
-
.run()
|
32 |
-
)
|
33 |
-
wav, rate = torchaudio.load(audio)
|
34 |
-
reduced_noise = model.predict(wav)
|
35 |
-
torchaudio.save(tgt_path, reduced_noise, rate)
|
36 |
-
return tgt_path
|
37 |
-
|
38 |
-
demo = gr.Interface(
|
39 |
-
fn=denoising_transform,
|
40 |
inputs=gr.Audio(label="Source Audio", source="microphone", type='filepath'),
|
41 |
-
outputs=gr.Audio(label="
|
42 |
-
examples=[
|
43 |
-
["testing/wavs/p232_071.wav"],
|
44 |
-
["testing/wavs/p232_284.wav"],
|
45 |
-
],
|
46 |
-
title="Denoising"
|
47 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
|
50 |
-
demo.launch()
|
51 |
-
|
52 |
if __name__ == "__main__":
|
53 |
model_filename = "original_sr/Demucs_original_sr_epoch3.pt"
|
54 |
config_filename = "original_sr/config.yaml"
|
55 |
run_app(model_filename, config_filename)
|
56 |
-
|
|
|
10 |
import yaml
|
11 |
|
12 |
|
13 |
+
def denoising_transform(audio, model):
|
14 |
+
src_path = Path("cache_wav/original/{}.wav".format(str(uuid.uuid4())))
|
15 |
+
tgt_path = Path("cache_wav/denoised/{}.wav".format(str(uuid.uuid4())))
|
16 |
+
src_path.parent.mkdir(exist_ok=True, parents=True)
|
17 |
+
tgt_path.parent.mkdir(exist_ok=True, parents=True)
|
18 |
+
(ffmpeg.input(audio)
|
19 |
+
.output(src_path.as_posix(), acodec='pcm_s16le', ac=1, ar=22050)
|
20 |
+
.run()
|
21 |
+
)
|
22 |
+
wav, rate = torchaudio.load(audio)
|
23 |
+
reduced_noise = model.predict(wav)
|
24 |
+
torchaudio.save(tgt_path, reduced_noise, rate)
|
25 |
+
return tgt_path
|
26 |
+
|
27 |
|
28 |
def run_app(model_filename, config_filename):
|
29 |
model_path = hf_hub_download(repo_id="BorisovMaksim/demucs", filename=model_filename)
|
|
|
34 |
checkpoint = torch.load(model_path, map_location=torch.device('cpu'))
|
35 |
model.load_state_dict(checkpoint['model_state_dict'])
|
36 |
|
37 |
+
interface_demucs = gr.Interface(
|
38 |
+
fn=lambda x: denoising_transform(x, model),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
inputs=gr.Audio(label="Source Audio", source="microphone", type='filepath'),
|
40 |
+
outputs=gr.Audio(label="Demucs", type='filepath')
|
|
|
|
|
|
|
|
|
|
|
41 |
)
|
42 |
+
interface_spectral_gating = gr.Interface(
|
43 |
+
fn=lambda x: denoising_transform(x, SpectralGating()),
|
44 |
+
inputs=gr.Audio(label="Source Audio", source="microphone", type='filepath'),
|
45 |
+
outputs=gr.Audio(label="Spectral Gating", type='filepath')
|
46 |
+
)
|
47 |
+
gr.Parallel(interface_demucs, interface_spectral_gating,
|
48 |
+
title="Denoising",
|
49 |
+
examples=[
|
50 |
+
["testing/wavs/p232_071.wav"],
|
51 |
+
["testing/wavs/p232_284.wav"],
|
52 |
+
]).launch()
|
53 |
|
54 |
|
|
|
|
|
55 |
if __name__ == "__main__":
|
56 |
model_filename = "original_sr/Demucs_original_sr_epoch3.pt"
|
57 |
config_filename = "original_sr/config.yaml"
|
58 |
run_app(model_filename, config_filename)
|
|
denoisers/SpectralGating.py
CHANGED
@@ -4,7 +4,7 @@ import torchaudio
|
|
4 |
|
5 |
|
6 |
class SpectralGating(torch.nn.Module):
|
7 |
-
def __init__(self, rate=
|
8 |
super(SpectralGating, self).__init__()
|
9 |
self.rate = rate
|
10 |
|
@@ -12,11 +12,9 @@ class SpectralGating(torch.nn.Module):
|
|
12 |
reduced_noise = torch.Tensor(nr.reduce_noise(y=wav, sr=self.rate))
|
13 |
return reduced_noise
|
14 |
|
15 |
-
def predict(self,
|
16 |
-
|
17 |
-
reduced_noise
|
18 |
-
torchaudio.save(out_path, reduced_noise, rate)
|
19 |
-
return out_path
|
20 |
|
21 |
|
22 |
|
|
|
4 |
|
5 |
|
6 |
class SpectralGating(torch.nn.Module):
|
7 |
+
def __init__(self, rate=48000):
|
8 |
super(SpectralGating, self).__init__()
|
9 |
self.rate = rate
|
10 |
|
|
|
12 |
reduced_noise = torch.Tensor(nr.reduce_noise(y=wav, sr=self.rate))
|
13 |
return reduced_noise
|
14 |
|
15 |
+
def predict(self, wav):
|
16 |
+
reduced_noise = torch.Tensor(nr.reduce_noise(y=wav, sr=self.rate))
|
17 |
+
return reduced_noise
|
|
|
|
|
18 |
|
19 |
|
20 |
|