Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
-
import torch
|
3 |
import os
|
4 |
-
import
|
|
|
5 |
from pydub import AudioSegment
|
6 |
-
from typing import Tuple, Dict, List
|
7 |
-
from demucs.apply import apply_model
|
8 |
-
from demucs.separate import load_track
|
9 |
-
from demucs.pretrained import get_model
|
10 |
-
from demucs.audio import save_audio
|
11 |
|
12 |
-
device: str = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
-
|
14 |
-
# Define the inference function
|
15 |
-
@spaces.GPU
|
16 |
def inference(audio_file: str, model_name: str, vocals: bool, drums: bool, bass: bool, other: bool, mp3: bool, mp3_bitrate: int) -> Tuple[str, gr.HTML]:
|
17 |
-
separator = get_model(name=model_name)
|
18 |
log_messages = []
|
19 |
|
20 |
def stream_log(message):
|
@@ -24,77 +14,60 @@ def inference(audio_file: str, model_name: str, vocals: bool, drums: bool, bass:
|
|
24 |
yield None, stream_log("Starting separation process...")
|
25 |
yield None, stream_log(f"Loading audio file: {audio_file}")
|
26 |
|
27 |
-
# Check if audio_file is None
|
28 |
if audio_file is None:
|
29 |
yield None, stream_log("Error: No audio file provided")
|
30 |
raise gr.Error("Please upload an audio file")
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
elif wav.shape[0] > 2:
|
45 |
-
wav = wav[:2] # If more than 2 channels, keep only the first two
|
46 |
-
|
47 |
-
wav = wav.to(device)
|
48 |
-
|
49 |
-
ref = wav.mean(0)
|
50 |
-
wav = (wav - ref.view(1, -1))
|
51 |
-
yield None, stream_log("Audio loaded successfully. Applying model...")
|
52 |
|
53 |
-
# Use apply_model as a standalone function
|
54 |
try:
|
55 |
-
|
56 |
-
|
57 |
-
yield None, stream_log(f"Model application result shape: {result.shape if hasattr(result, 'shape') else 'N/A'}")
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
sources = result
|
63 |
-
else:
|
64 |
-
raise ValueError(f"Unexpected result type from apply_model: {type(result)}")
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
74 |
except Exception as e:
|
75 |
-
yield None, stream_log(f"Unexpected error
|
76 |
-
raise gr.Error(f"An unexpected error occurred
|
77 |
|
78 |
-
|
79 |
-
sources = [source * ref.view(1, -1) + ref.view(1, -1) for source in sources]
|
80 |
-
|
81 |
-
yield None, stream_log("Model applied. Processing stems...")
|
82 |
|
83 |
-
output_dir: str = os.path.join("separated", model_name, os.path.splitext(os.path.basename(audio_file))[0])
|
84 |
-
os.makedirs(output_dir, exist_ok=True)
|
85 |
stems: Dict[str, str] = {}
|
86 |
-
for stem
|
87 |
-
stem_path
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
|
92 |
-
selected_stems: List[str] = [stems[stem] for stem
|
93 |
if not selected_stems:
|
94 |
raise gr.Error("Please select at least one stem to mix.")
|
95 |
|
96 |
output_file: str = os.path.join(output_dir, "mixed.wav")
|
97 |
yield None, stream_log("Mixing selected stems...")
|
|
|
98 |
if len(selected_stems) == 1:
|
99 |
os.rename(selected_stems[0], output_file)
|
100 |
else:
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
+
import subprocess
|
4 |
+
from typing import Tuple, List, Dict
|
5 |
from pydub import AudioSegment
|
|
|
|
|
|
|
|
|
|
|
6 |
|
|
|
|
|
|
|
|
|
7 |
def inference(audio_file: str, model_name: str, vocals: bool, drums: bool, bass: bool, other: bool, mp3: bool, mp3_bitrate: int) -> Tuple[str, gr.HTML]:
|
|
|
8 |
log_messages = []
|
9 |
|
10 |
def stream_log(message):
|
|
|
14 |
yield None, stream_log("Starting separation process...")
|
15 |
yield None, stream_log(f"Loading audio file: {audio_file}")
|
16 |
|
|
|
17 |
if audio_file is None:
|
18 |
yield None, stream_log("Error: No audio file provided")
|
19 |
raise gr.Error("Please upload an audio file")
|
20 |
|
21 |
+
output_dir = os.path.join("separated", model_name, os.path.splitext(os.path.basename(audio_file))[0])
|
22 |
+
os.makedirs(output_dir, exist_ok=True)
|
23 |
+
|
24 |
+
# Construct the Demucs command
|
25 |
+
cmd = [
|
26 |
+
"python", "-m", "demucs",
|
27 |
+
"--out", output_dir,
|
28 |
+
"-n", model_name,
|
29 |
+
audio_file
|
30 |
+
]
|
31 |
+
|
32 |
+
yield None, stream_log(f"Running Demucs command: {' '.join(cmd)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
|
|
34 |
try:
|
35 |
+
# Run the Demucs command
|
36 |
+
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
|
|
37 |
|
38 |
+
# Stream the output
|
39 |
+
for line in process.stdout:
|
40 |
+
yield None, stream_log(line.strip())
|
|
|
|
|
|
|
41 |
|
42 |
+
# Wait for the process to complete
|
43 |
+
process.wait()
|
44 |
+
|
45 |
+
if process.returncode != 0:
|
46 |
+
error_output = process.stderr.read()
|
47 |
+
yield None, stream_log(f"Error: Demucs command failed with return code {process.returncode}")
|
48 |
+
yield None, stream_log(f"Error output: {error_output}")
|
49 |
+
raise gr.Error(f"Demucs separation failed. Check the logs for details.")
|
50 |
+
|
51 |
except Exception as e:
|
52 |
+
yield None, stream_log(f"Unexpected error: {str(e)}")
|
53 |
+
raise gr.Error(f"An unexpected error occurred: {str(e)}")
|
54 |
|
55 |
+
yield None, stream_log("Separation completed. Processing stems...")
|
|
|
|
|
|
|
56 |
|
|
|
|
|
57 |
stems: Dict[str, str] = {}
|
58 |
+
for stem in ["vocals", "drums", "bass", "other"]:
|
59 |
+
stem_path = os.path.join(output_dir, model_name, f"{stem}.wav")
|
60 |
+
if os.path.exists(stem_path):
|
61 |
+
stems[stem] = stem_path
|
62 |
+
yield None, stream_log(f"Found {stem} stem")
|
63 |
|
64 |
+
selected_stems: List[str] = [stems[stem] for stem in stems if locals()[stem]]
|
65 |
if not selected_stems:
|
66 |
raise gr.Error("Please select at least one stem to mix.")
|
67 |
|
68 |
output_file: str = os.path.join(output_dir, "mixed.wav")
|
69 |
yield None, stream_log("Mixing selected stems...")
|
70 |
+
|
71 |
if len(selected_stems) == 1:
|
72 |
os.rename(selected_stems[0], output_file)
|
73 |
else:
|