Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,12 @@ import time
|
|
12 |
|
13 |
console = Console()
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
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, List[str], gr.HTML]:
|
17 |
log_messages = []
|
@@ -19,16 +25,17 @@ def inference(audio_file: str, model_name: str, vocals: bool, drums: bool, bass:
|
|
19 |
def stream_log(message, style=""):
|
20 |
formatted_message = f"[{model_name}] {message}"
|
21 |
log_messages.append(formatted_message)
|
22 |
-
|
|
|
23 |
|
24 |
-
yield
|
25 |
time.sleep(1) # Simulate initialization time
|
26 |
|
27 |
-
yield
|
28 |
time.sleep(0.5) # Simulate loading time
|
29 |
|
30 |
if audio_file is None:
|
31 |
-
yield
|
32 |
raise gr.Error("Please upload an audio file")
|
33 |
|
34 |
# Use absolute paths
|
@@ -44,7 +51,7 @@ def inference(audio_file: str, model_name: str, vocals: bool, drums: bool, bass:
|
|
44 |
audio_file
|
45 |
]
|
46 |
|
47 |
-
yield
|
48 |
time.sleep(0.5) # Simulate preparation time
|
49 |
|
50 |
try:
|
@@ -68,65 +75,65 @@ def inference(audio_file: str, model_name: str, vocals: bool, drums: bool, bass:
|
|
68 |
|
69 |
if process.returncode != 0:
|
70 |
error_output = process.stderr.read()
|
71 |
-
yield
|
72 |
raise gr.Error(f"Demucs separation failed. Check the logs for details.")
|
73 |
|
74 |
except Exception as e:
|
75 |
-
yield
|
76 |
raise gr.Error(f"An unexpected error occurred: {str(e)}")
|
77 |
|
78 |
-
yield
|
79 |
time.sleep(0.5) # Pause for effect
|
80 |
|
81 |
-
yield
|
82 |
time.sleep(0.5) # Simulate processing time
|
83 |
|
84 |
# Change the stem search directory using full path
|
85 |
stem_search_dir = os.path.join(base_output_dir, model_name, os.path.splitext(os.path.basename(audio_file))[0])
|
86 |
-
yield
|
87 |
|
88 |
stems: Dict[str, str] = {}
|
89 |
for stem in ["vocals", "drums", "bass", "other"]:
|
90 |
stem_path = os.path.join(stem_search_dir, f"{stem}.wav")
|
91 |
-
yield
|
92 |
if os.path.exists(stem_path):
|
93 |
stems[stem] = stem_path
|
94 |
-
yield
|
95 |
else:
|
96 |
-
yield
|
97 |
|
98 |
if not stems:
|
99 |
-
yield
|
100 |
stem_search_dir = os.path.join(base_output_dir, model_name)
|
101 |
for stem in ["vocals", "drums", "bass", "other"]:
|
102 |
stem_path = os.path.join(stem_search_dir, f"{stem}.wav")
|
103 |
-
yield
|
104 |
if os.path.exists(stem_path):
|
105 |
stems[stem] = stem_path
|
106 |
-
yield
|
107 |
else:
|
108 |
-
yield
|
109 |
|
110 |
-
yield
|
111 |
|
112 |
selected_stems: List[str] = []
|
113 |
for stem, selected in zip(["vocals", "drums", "bass", "other"], [vocals, drums, bass, other]):
|
114 |
if selected:
|
115 |
-
yield
|
116 |
if stem in stems:
|
117 |
selected_stems.append(stems[stem])
|
118 |
-
yield
|
119 |
else:
|
120 |
-
yield
|
121 |
|
122 |
-
yield
|
123 |
|
124 |
if not selected_stems:
|
125 |
-
yield
|
126 |
raise gr.Error("Please select at least one stem to mix and ensure it was successfully separated.")
|
127 |
|
128 |
output_file: str = os.path.join(output_dir, "mixed.wav")
|
129 |
-
yield
|
130 |
time.sleep(0.5) # Simulate mixing time
|
131 |
|
132 |
mixed_audio: AudioSegment = AudioSegment.empty()
|
@@ -135,20 +142,19 @@ def inference(audio_file: str, model_name: str, vocals: bool, drums: bool, bass:
|
|
135 |
mixed_audio.export(output_file, format="wav")
|
136 |
|
137 |
if mp3:
|
138 |
-
yield
|
139 |
time.sleep(0.5) # Simulate conversion time
|
140 |
mp3_output_file: str = os.path.splitext(output_file)[0] + ".mp3"
|
141 |
mixed_audio.export(mp3_output_file, format="mp3", bitrate=str(mp3_bitrate) + "k")
|
142 |
output_file = mp3_output_file
|
143 |
|
144 |
-
yield
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
border_style="green"
|
150 |
-
).render()
|
151 |
)
|
|
|
152 |
|
153 |
# Define the Gradio interface
|
154 |
with gr.Blocks() as iface:
|
|
|
12 |
|
13 |
console = Console()
|
14 |
|
15 |
+
def fade_text(text, duration=0.5):
|
16 |
+
for i in range(10):
|
17 |
+
opacity = i / 10
|
18 |
+
yield f"<div style='opacity: {opacity}; transition: opacity 0.05s;'>{text}</div>"
|
19 |
+
time.sleep(duration / 10)
|
20 |
+
|
21 |
@spaces.GPU
|
22 |
def inference(audio_file: str, model_name: str, vocals: bool, drums: bool, bass: bool, other: bool, mp3: bool, mp3_bitrate: int) -> Tuple[str, List[str], gr.HTML]:
|
23 |
log_messages = []
|
|
|
25 |
def stream_log(message, style=""):
|
26 |
formatted_message = f"[{model_name}] {message}"
|
27 |
log_messages.append(formatted_message)
|
28 |
+
for frame in fade_text(f"<pre style='margin-bottom: 0;{style}'>{formatted_message}</pre>"):
|
29 |
+
yield None, None, gr.HTML(frame)
|
30 |
|
31 |
+
yield from stream_log("Initializing Demucs...", "color: #4CAF50; font-weight: bold;")
|
32 |
time.sleep(1) # Simulate initialization time
|
33 |
|
34 |
+
yield from stream_log("Loading audio file...", "color: #2196F3;")
|
35 |
time.sleep(0.5) # Simulate loading time
|
36 |
|
37 |
if audio_file is None:
|
38 |
+
yield from stream_log("Error: No audio file provided", "color: #F44336;")
|
39 |
raise gr.Error("Please upload an audio file")
|
40 |
|
41 |
# Use absolute paths
|
|
|
51 |
audio_file
|
52 |
]
|
53 |
|
54 |
+
yield from stream_log("Preparing separation process...", "color: #FF9800;")
|
55 |
time.sleep(0.5) # Simulate preparation time
|
56 |
|
57 |
try:
|
|
|
75 |
|
76 |
if process.returncode != 0:
|
77 |
error_output = process.stderr.read()
|
78 |
+
yield from stream_log(f"Error: Separation failed", "color: #F44336;")
|
79 |
raise gr.Error(f"Demucs separation failed. Check the logs for details.")
|
80 |
|
81 |
except Exception as e:
|
82 |
+
yield from stream_log(f"Unexpected error: {str(e)}", "color: #F44336;")
|
83 |
raise gr.Error(f"An unexpected error occurred: {str(e)}")
|
84 |
|
85 |
+
yield from stream_log("Separation completed successfully!", "color: #4CAF50; font-weight: bold;")
|
86 |
time.sleep(0.5) # Pause for effect
|
87 |
|
88 |
+
yield from stream_log("Processing stems...", "color: #9C27B0;")
|
89 |
time.sleep(0.5) # Simulate processing time
|
90 |
|
91 |
# Change the stem search directory using full path
|
92 |
stem_search_dir = os.path.join(base_output_dir, model_name, os.path.splitext(os.path.basename(audio_file))[0])
|
93 |
+
yield from stream_log(f"Searching for stems in: {stem_search_dir}")
|
94 |
|
95 |
stems: Dict[str, str] = {}
|
96 |
for stem in ["vocals", "drums", "bass", "other"]:
|
97 |
stem_path = os.path.join(stem_search_dir, f"{stem}.wav")
|
98 |
+
yield from stream_log(f"Checking for {stem} stem at: {stem_path}")
|
99 |
if os.path.exists(stem_path):
|
100 |
stems[stem] = stem_path
|
101 |
+
yield from stream_log(f"Found {stem} stem")
|
102 |
else:
|
103 |
+
yield from stream_log(f"Warning: {stem} stem not found")
|
104 |
|
105 |
if not stems:
|
106 |
+
yield from stream_log("Error: No stems found. Checking alternative directory...")
|
107 |
stem_search_dir = os.path.join(base_output_dir, model_name)
|
108 |
for stem in ["vocals", "drums", "bass", "other"]:
|
109 |
stem_path = os.path.join(stem_search_dir, f"{stem}.wav")
|
110 |
+
yield from stream_log(f"Checking for {stem} stem at: {stem_path}")
|
111 |
if os.path.exists(stem_path):
|
112 |
stems[stem] = stem_path
|
113 |
+
yield from stream_log(f"Found {stem} stem")
|
114 |
else:
|
115 |
+
yield from stream_log(f"Warning: {stem} stem not found")
|
116 |
|
117 |
+
yield from stream_log(f"All found stems: {list(stems.keys())}")
|
118 |
|
119 |
selected_stems: List[str] = []
|
120 |
for stem, selected in zip(["vocals", "drums", "bass", "other"], [vocals, drums, bass, other]):
|
121 |
if selected:
|
122 |
+
yield from stream_log(f"{stem} is selected by user")
|
123 |
if stem in stems:
|
124 |
selected_stems.append(stems[stem])
|
125 |
+
yield from stream_log(f"Selected {stem} stem for mixing")
|
126 |
else:
|
127 |
+
yield from stream_log(f"Warning: {stem} was selected but not found")
|
128 |
|
129 |
+
yield from stream_log(f"Final selected stems: {selected_stems}")
|
130 |
|
131 |
if not selected_stems:
|
132 |
+
yield from stream_log("Error: No stems selected for mixing", "color: #F44336;")
|
133 |
raise gr.Error("Please select at least one stem to mix and ensure it was successfully separated.")
|
134 |
|
135 |
output_file: str = os.path.join(output_dir, "mixed.wav")
|
136 |
+
yield from stream_log("Mixing selected stems...", "color: #FF5722;")
|
137 |
time.sleep(0.5) # Simulate mixing time
|
138 |
|
139 |
mixed_audio: AudioSegment = AudioSegment.empty()
|
|
|
142 |
mixed_audio.export(output_file, format="wav")
|
143 |
|
144 |
if mp3:
|
145 |
+
yield from stream_log(f"Converting to MP3...", "color: #795548;")
|
146 |
time.sleep(0.5) # Simulate conversion time
|
147 |
mp3_output_file: str = os.path.splitext(output_file)[0] + ".mp3"
|
148 |
mixed_audio.export(mp3_output_file, format="mp3", bitrate=str(mp3_bitrate) + "k")
|
149 |
output_file = mp3_output_file
|
150 |
|
151 |
+
yield from stream_log("Process completed successfully!", "color: #4CAF50; font-weight: bold;")
|
152 |
+
final_message = Panel(
|
153 |
+
Text("Separation and mixing completed successfully!", style="bold green"),
|
154 |
+
title="Demucs Result",
|
155 |
+
border_style="green"
|
|
|
|
|
156 |
)
|
157 |
+
yield output_file, list(stems.values()), gr.HTML(console.export_html(final_message))
|
158 |
|
159 |
# Define the Gradio interface
|
160 |
with gr.Blocks() as iface:
|