don0726 commited on
Commit
2cd5b38
·
verified ·
1 Parent(s): 5703b97

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import tempfile
4
+ import soundfile as sf
5
+ import librosa
6
+ import numpy as np
7
+
8
+ from demucs.pretrained import get_model
9
+ from demucs.apply import apply_model
10
+
11
+ # Load Demucs model
12
+ model = get_model("htdemucs")
13
+ model.cpu()
14
+ model.eval()
15
+
16
+
17
+ def remove_vocals(audio_file):
18
+
19
+ # load audio
20
+ audio, sr = librosa.load(audio_file, sr=44100, mono=False)
21
+
22
+ if audio.ndim == 1:
23
+ audio = np.expand_dims(audio, axis=0)
24
+
25
+ audio_tensor = torch.tensor(audio).unsqueeze(0)
26
+
27
+ with torch.no_grad():
28
+ sources = apply_model(model, audio_tensor)
29
+
30
+ # sources order:
31
+ # 0 drums
32
+ # 1 bass
33
+ # 2 other
34
+ # 3 vocals
35
+
36
+ drums = sources[0][0].cpu().numpy()
37
+ bass = sources[0][1].cpu().numpy()
38
+ other = sources[0][2].cpu().numpy()
39
+
40
+ # mix everything except vocals
41
+ background = drums + bass + other
42
+
43
+ output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav").name
44
+
45
+ sf.write(output_file, background.T, sr)
46
+
47
+ return output_file
48
+
49
+
50
+ interface = gr.Interface(
51
+ fn=remove_vocals,
52
+ inputs=gr.Audio(type="filepath", label="Upload Audio"),
53
+ outputs=gr.Audio(label="Background Music + Expressions"),
54
+ title="Vocal Remover (Keep Music + Expressions)"
55
+ )
56
+
57
+ interface.launch()