Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,31 +4,33 @@ import demucs.separate
|
|
4 |
import shlex
|
5 |
import os
|
6 |
import spaces
|
|
|
7 |
|
8 |
# Check if CUDA is available
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
# Define the inference function
|
12 |
@spaces.GPU
|
13 |
-
def inference(audio_file, model_name,
|
14 |
"""
|
15 |
-
Performs inference using Demucs.
|
16 |
|
17 |
Args:
|
18 |
audio_file: The audio file to separate.
|
19 |
model_name: The name of the Demucs model to use.
|
20 |
-
|
|
|
|
|
|
|
21 |
mp3: Whether to save the output as MP3.
|
22 |
mp3_bitrate: The bitrate of the output MP3 file.
|
23 |
|
24 |
Returns:
|
25 |
-
|
26 |
"""
|
27 |
|
28 |
-
# Construct the command line arguments
|
29 |
cmd = f"demucs -n {model_name} --clip-mode clamp --shifts=1"
|
30 |
-
if two_stems:
|
31 |
-
cmd += f" --two-stems={two_stems}"
|
32 |
if mp3:
|
33 |
cmd += f" --mp3 --mp3-bitrate={mp3_bitrate}"
|
34 |
cmd += f" {audio_file.name}"
|
@@ -38,9 +40,28 @@ def inference(audio_file, model_name, two_stems, mp3, mp3_bitrate):
|
|
38 |
|
39 |
# Get the output file paths
|
40 |
output_dir = os.path.join("separated", model_name, os.path.splitext(os.path.basename(audio_file.name))[0])
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
# Define the Gradio interface
|
46 |
iface = gr.Interface(
|
@@ -48,13 +69,16 @@ iface = gr.Interface(
|
|
48 |
inputs=[
|
49 |
gr.Audio(type="filepath"),
|
50 |
gr.Dropdown(["htdemucs", "htdemucs_ft", "htdemucs_6s", "hdemucs_mmi", "mdx", "mdx_extra", "mdx_q", "mdx_extra_q"], label="Model Name"),
|
51 |
-
gr.
|
|
|
|
|
|
|
52 |
gr.Checkbox(label="Save as MP3"),
|
53 |
gr.Slider(128, 320, step=32, label="MP3 Bitrate", visible=False),
|
54 |
],
|
55 |
-
outputs=gr.
|
56 |
-
title="Demucs Music Source Separation",
|
57 |
-
description="Separate vocals, drums, bass, and other instruments from your music using Demucs.",
|
58 |
)
|
59 |
|
60 |
# Launch the Gradio interface
|
|
|
4 |
import shlex
|
5 |
import os
|
6 |
import spaces
|
7 |
+
import subprocess
|
8 |
|
9 |
# Check if CUDA is available
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
|
12 |
# Define the inference function
|
13 |
@spaces.GPU
|
14 |
+
def inference(audio_file, model_name, vocals, drums, bass, other, mp3, mp3_bitrate):
|
15 |
"""
|
16 |
+
Performs inference using Demucs and mixes the selected stems.
|
17 |
|
18 |
Args:
|
19 |
audio_file: The audio file to separate.
|
20 |
model_name: The name of the Demucs model to use.
|
21 |
+
vocals: Whether to include vocals in the mix.
|
22 |
+
drums: Whether to include drums in the mix.
|
23 |
+
bass: Whether to include bass in the mix.
|
24 |
+
other: Whether to include other instruments in the mix.
|
25 |
mp3: Whether to save the output as MP3.
|
26 |
mp3_bitrate: The bitrate of the output MP3 file.
|
27 |
|
28 |
Returns:
|
29 |
+
The path to the mixed audio file.
|
30 |
"""
|
31 |
|
32 |
+
# Construct the command line arguments for Demucs
|
33 |
cmd = f"demucs -n {model_name} --clip-mode clamp --shifts=1"
|
|
|
|
|
34 |
if mp3:
|
35 |
cmd += f" --mp3 --mp3-bitrate={mp3_bitrate}"
|
36 |
cmd += f" {audio_file.name}"
|
|
|
40 |
|
41 |
# Get the output file paths
|
42 |
output_dir = os.path.join("separated", model_name, os.path.splitext(os.path.basename(audio_file.name))[0])
|
43 |
+
stems = {
|
44 |
+
"vocals": os.path.join(output_dir, "vocals.wav"),
|
45 |
+
"drums": os.path.join(output_dir, "drums.wav"),
|
46 |
+
"bass": os.path.join(output_dir, "bass.wav"),
|
47 |
+
"other": os.path.join(output_dir, "other.wav"),
|
48 |
+
}
|
49 |
|
50 |
+
# Mix the selected stems
|
51 |
+
selected_stems = [stems[stem] for stem, include in zip(["vocals", "drums", "bass", "other"], [vocals, drums, bass, other]) if include]
|
52 |
+
if not selected_stems:
|
53 |
+
raise gr.Error("Please select at least one stem to mix.")
|
54 |
+
|
55 |
+
output_file = os.path.join(output_dir, "mixed.wav")
|
56 |
+
if len(selected_stems) == 1:
|
57 |
+
# If only one stem is selected, just copy it
|
58 |
+
os.rename(selected_stems[0], output_file)
|
59 |
+
else:
|
60 |
+
# Otherwise, use ffmpeg to mix the stems
|
61 |
+
ffmpeg_cmd = ["ffmpeg", "-y", "-i"] + selected_stems + ["-filter_complex", "amix=inputs=" + str(len(selected_stems)) + ":duration=longest", output_file]
|
62 |
+
subprocess.run(ffmpeg_cmd, check=True)
|
63 |
+
|
64 |
+
return output_file
|
65 |
|
66 |
# Define the Gradio interface
|
67 |
iface = gr.Interface(
|
|
|
69 |
inputs=[
|
70 |
gr.Audio(type="filepath"),
|
71 |
gr.Dropdown(["htdemucs", "htdemucs_ft", "htdemucs_6s", "hdemucs_mmi", "mdx", "mdx_extra", "mdx_q", "mdx_extra_q"], label="Model Name"),
|
72 |
+
gr.Checkbox(label="Vocals", value=True),
|
73 |
+
gr.Checkbox(label="Drums", value=True),
|
74 |
+
gr.Checkbox(label="Bass", value=True),
|
75 |
+
gr.Checkbox(label="Other", value=True),
|
76 |
gr.Checkbox(label="Save as MP3"),
|
77 |
gr.Slider(128, 320, step=32, label="MP3 Bitrate", visible=False),
|
78 |
],
|
79 |
+
outputs=gr.Audio(type="filepath"),
|
80 |
+
title="Demucs Music Source Separation and Mixing",
|
81 |
+
description="Separate vocals, drums, bass, and other instruments from your music using Demucs and mix the selected stems.",
|
82 |
)
|
83 |
|
84 |
# Launch the Gradio interface
|