Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torchaudio
|
3 |
+
import gradio as gr
|
4 |
+
import os
|
5 |
+
from demucs.pretrained import get_model
|
6 |
+
from demucs.apply import apply_model
|
7 |
+
|
8 |
+
# Load mdx_extra model
|
9 |
+
model = get_model('mdx_extra')
|
10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
11 |
+
model.to(device)
|
12 |
+
|
13 |
+
output_dir = "separated_mdx_extra"
|
14 |
+
os.makedirs(output_dir, exist_ok=True)
|
15 |
+
|
16 |
+
def separate_audio(audio_path):
|
17 |
+
wav, sr = torchaudio.load(audio_path)
|
18 |
+
wav = wav.to(device)
|
19 |
+
|
20 |
+
sources = apply_model(model, wav[None], device=device)
|
21 |
+
|
22 |
+
sources_dict = {}
|
23 |
+
stems = ["vocals", "instrumental"]
|
24 |
+
|
25 |
+
for source, stem in zip(sources[0], stems):
|
26 |
+
stem_path = os.path.join(output_dir, f"{stem}.wav")
|
27 |
+
torchaudio.save(stem_path, source.cpu(), sr)
|
28 |
+
sources_dict[stem] = stem_path
|
29 |
+
|
30 |
+
return sources_dict["vocals"], sources_dict["instrumental"]
|
31 |
+
|
32 |
+
# Gradio Interface
|
33 |
+
interface = gr.Interface(
|
34 |
+
fn=separate_audio,
|
35 |
+
inputs=gr.Audio(type="filepath"),
|
36 |
+
outputs=[gr.Audio(label="Vocals"), gr.Audio(label="Instrumental")],
|
37 |
+
title="AI Music Separator (Demucs)",
|
38 |
+
description="Upload a song, and AI will separate vocals and instrumental."
|
39 |
+
)
|
40 |
+
|
41 |
+
if __name__ == "__main__":
|
42 |
+
interface.launch()
|