Spaces:
Runtime error
Runtime error
Commit
·
b80b88c
1
Parent(s):
685fbad
update app
Browse files- app.py +18 -11
- denoisers/demucs.py +5 -0
app.py
CHANGED
|
@@ -5,29 +5,34 @@ from pathlib import Path
|
|
| 5 |
from denoisers.SpectralGating import SpectralGating
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
from denoisers.demucs import Demucs
|
| 8 |
-
import hydra
|
| 9 |
-
from omegaconf import DictConfig
|
| 10 |
import torch
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
model.load_state_dict(checkpoint['model_state_dict'])
|
| 20 |
|
| 21 |
def denoising_transform(audio):
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
src_path.parent.mkdir(exist_ok=True, parents=True)
|
| 25 |
tgt_path.parent.mkdir(exist_ok=True, parents=True)
|
| 26 |
(ffmpeg.input(audio)
|
| 27 |
.output(src_path.as_posix(), acodec='pcm_s16le', ac=1, ar=22050)
|
| 28 |
.run()
|
| 29 |
)
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
return tgt_path
|
| 32 |
|
| 33 |
demo = gr.Interface(
|
|
@@ -45,5 +50,7 @@ def run_app(cfg: DictConfig):
|
|
| 45 |
demo.launch()
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
|
|
|
| 5 |
from denoisers.SpectralGating import SpectralGating
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
from denoisers.demucs import Demucs
|
|
|
|
|
|
|
| 8 |
import torch
|
| 9 |
+
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)
|
| 16 |
+
config_path = hf_hub_download(repo_id="BorisovMaksim/demucs", filename=config_filename)
|
| 17 |
+
with open(config_path, 'r') as f:
|
| 18 |
+
config = yaml.safe_load(f)
|
| 19 |
+
model = Demucs(config['demucs'])
|
| 20 |
+
checkpoint = torch.load(model_path, map_location=torch.device('cpu'))
|
| 21 |
model.load_state_dict(checkpoint['model_state_dict'])
|
| 22 |
|
| 23 |
def denoising_transform(audio):
|
| 24 |
+
# Path(__file__).parent.resolve()
|
| 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(
|
|
|
|
| 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 |
|
denoisers/demucs.py
CHANGED
|
@@ -83,3 +83,8 @@ class Demucs(torch.nn.Module):
|
|
| 83 |
x = decoder(x + outs[i])
|
| 84 |
x = pad_cut_batch_audio(x, model_input.shape)
|
| 85 |
return x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
x = decoder(x + outs[i])
|
| 84 |
x = pad_cut_batch_audio(x, model_input.shape)
|
| 85 |
return x
|
| 86 |
+
|
| 87 |
+
def predict(self, wav):
|
| 88 |
+
prediction = self.forward(torch.reshape(wav, (1, 1, -1)))
|
| 89 |
+
return prediction.detach()[0]
|
| 90 |
+
|