Ahsen Khaliq commited on
Commit
d6aa604
1 Parent(s): a724289

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchaudio
3
+ import numpy as np
4
+ import scipy
5
+ import stempeg
6
+ import os
7
+ from openunmix import predict
8
+ import gradio as gr
9
+ import stempeg
10
+
11
+ torch.hub.download_url_to_file('https://github.com/AK391/open-unmix-pytorch/blob/master/test.wav?raw=true', 'test.wav')
12
+
13
+ use_cuda = torch.cuda.is_available()
14
+ device = torch.device("cuda" if use_cuda else "cpu")
15
+
16
+
17
+ def inference(audio):
18
+ start = 0
19
+ stop = 7
20
+ audio, rate = stempeg.read_stems(
21
+ audio.name,
22
+ sample_rate=44100,
23
+ start=start,
24
+ duration=stop-start,
25
+ )
26
+ estimates = predict.separate(
27
+ audio=torch.as_tensor(audio).float(),
28
+ rate=44100,
29
+ device=device,
30
+ )
31
+ target_path = str("target.wav")
32
+
33
+ estimates_numpy = {}
34
+ for target, estimate in estimates.items():
35
+ estimates_numpy[target] = torch.squeeze(estimate).detach().cpu().numpy().T
36
+
37
+ stempeg.write_stems(
38
+ target_path,
39
+ estimates_numpy,
40
+ sample_rate=rate,
41
+ writer=stempeg.FilesWriter(multiprocess=True, output_sample_rate=44100),
42
+ )
43
+ return 'vocals.wav', 'drums.wav', 'bass.wav', 'other.wav'
44
+
45
+ inputs = gr.inputs.Audio(label="Input Audio", type="file")
46
+ outputs = [gr.outputs.Audio(label="Vocals", type="file"),
47
+ gr.outputs.Audio(label="Drums", type="file"),
48
+ gr.outputs.Audio(label="Bass", type="file"),
49
+ gr.outputs.Audio(label="Other Audio", type="file")]
50
+
51
+
52
+ title = "OPEN-UNMIX"
53
+ description = "gradio demo for OPEN-UNMIX, reference implementation for music source separation. To use it, simply add your audio, or click one of the examples to load them. Read more at the links below."
54
+ article = "<p style='text-align: center'><a href='https://joss.theoj.org/papers/10.21105/joss.01667'>Open-Unmix - A Reference Implementation for Music Source Separation</a> | <a href='https://github.com/sigsep/open-unmix-pytorch'>Github Repo</a></p>"
55
+
56
+ examples = [['test.wav']]
57
+
58
+ gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, examples=examples, analytics_enabled=False).launch()