Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
import os
|
3 |
import subprocess
|
4 |
import spaces
|
|
|
5 |
from typing import Tuple, List, Dict
|
6 |
from pydub import AudioSegment
|
7 |
from rich.console import Console
|
@@ -9,8 +10,13 @@ from rich.panel import Panel
|
|
9 |
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn, TimeRemainingColumn
|
10 |
from rich.text import Text
|
11 |
import time
|
|
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
14 |
|
15 |
def fade_text(text, duration=0.5):
|
16 |
for i in range(10):
|
@@ -21,8 +27,7 @@ def fade_text(text, duration=0.5):
|
|
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 = []
|
24 |
-
|
25 |
-
|
26 |
def stream_log(message, style=""):
|
27 |
formatted_message = f"[{model_name}] {message}"
|
28 |
log_messages.append(formatted_message)
|
@@ -44,11 +49,17 @@ def inference(audio_file: str, model_name: str, vocals: bool, drums: bool, bass:
|
|
44 |
output_dir = os.path.join(base_output_dir, model_name, os.path.splitext(os.path.basename(audio_file))[0])
|
45 |
os.makedirs(output_dir, exist_ok=True)
|
46 |
|
47 |
-
#
|
|
|
|
|
|
|
|
|
|
|
48 |
cmd = [
|
49 |
"python", "-m", "demucs",
|
50 |
"--out", base_output_dir,
|
51 |
"-n", model_name,
|
|
|
52 |
audio_file
|
53 |
]
|
54 |
|
@@ -56,14 +67,20 @@ def inference(audio_file: str, model_name: str, vocals: bool, drums: bool, bass:
|
|
56 |
time.sleep(0.5) # Simulate preparation time
|
57 |
|
58 |
try:
|
|
|
|
|
|
|
|
|
|
|
59 |
# Run the Demucs command
|
60 |
-
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
61 |
|
62 |
-
# Simulate a loading animation
|
|
|
63 |
with Progress(
|
64 |
SpinnerColumn(),
|
65 |
TextColumn("[progress.description]{task.description}"),
|
66 |
-
BarColumn(),
|
67 |
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
|
68 |
TimeRemainingColumn(),
|
69 |
console=console
|
@@ -137,10 +154,27 @@ def inference(audio_file: str, model_name: str, vocals: bool, drums: bool, bass:
|
|
137 |
yield from stream_log("Mixing selected stems...", "color: #FF5722;")
|
138 |
time.sleep(0.5) # Simulate mixing time
|
139 |
|
140 |
-
|
141 |
-
|
142 |
-
mixed_audio
|
143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
if mp3:
|
146 |
yield from stream_log(f"Converting to MP3...", "color: #795548;")
|
@@ -153,7 +187,8 @@ def inference(audio_file: str, model_name: str, vocals: bool, drums: bool, bass:
|
|
153 |
final_message = Panel(
|
154 |
Text("Separation and mixing completed successfully!", style="bold green"),
|
155 |
title="Demucs Result",
|
156 |
-
border_style="green"
|
|
|
157 |
)
|
158 |
console.print(final_message)
|
159 |
yield output_file, list(stems.values()), gr.HTML(console.export_html())
|
|
|
2 |
import os
|
3 |
import subprocess
|
4 |
import spaces
|
5 |
+
import torch
|
6 |
from typing import Tuple, List, Dict
|
7 |
from pydub import AudioSegment
|
8 |
from rich.console import Console
|
|
|
10 |
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn, TimeRemainingColumn
|
11 |
from rich.text import Text
|
12 |
import time
|
13 |
+
import shutil
|
14 |
|
15 |
+
# Get the terminal width, or use a default if not available
|
16 |
+
terminal_width = shutil.get_terminal_size((80, 20)).columns
|
17 |
+
|
18 |
+
# Create a console with a specific width
|
19 |
+
console = Console(width=min(terminal_width, 100)) # Limit to 100 columns max
|
20 |
|
21 |
def fade_text(text, duration=0.5):
|
22 |
for i in range(10):
|
|
|
27 |
@spaces.GPU
|
28 |
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]:
|
29 |
log_messages = []
|
30 |
+
|
|
|
31 |
def stream_log(message, style=""):
|
32 |
formatted_message = f"[{model_name}] {message}"
|
33 |
log_messages.append(formatted_message)
|
|
|
49 |
output_dir = os.path.join(base_output_dir, model_name, os.path.splitext(os.path.basename(audio_file))[0])
|
50 |
os.makedirs(output_dir, exist_ok=True)
|
51 |
|
52 |
+
# Check if CUDA is available
|
53 |
+
cuda_available = torch.cuda.is_available()
|
54 |
+
device = "cuda" if cuda_available else "cpu"
|
55 |
+
yield from stream_log(f"Using device: {device}", "color: #4CAF50; font-weight: bold;")
|
56 |
+
|
57 |
+
# Construct the Demucs command with full paths and GPU flag
|
58 |
cmd = [
|
59 |
"python", "-m", "demucs",
|
60 |
"--out", base_output_dir,
|
61 |
"-n", model_name,
|
62 |
+
"--device", device,
|
63 |
audio_file
|
64 |
]
|
65 |
|
|
|
67 |
time.sleep(0.5) # Simulate preparation time
|
68 |
|
69 |
try:
|
70 |
+
# Set CUDA_VISIBLE_DEVICES environment variable
|
71 |
+
env = os.environ.copy()
|
72 |
+
if cuda_available:
|
73 |
+
env["CUDA_VISIBLE_DEVICES"] = "0" # Use the first GPU
|
74 |
+
|
75 |
# Run the Demucs command
|
76 |
+
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, env=env)
|
77 |
|
78 |
+
# Simulate a loading animation with adjusted width
|
79 |
+
progress_width = min(terminal_width - 20, 60) # Adjust the width of the progress bar
|
80 |
with Progress(
|
81 |
SpinnerColumn(),
|
82 |
TextColumn("[progress.description]{task.description}"),
|
83 |
+
BarColumn(bar_width=progress_width),
|
84 |
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
|
85 |
TimeRemainingColumn(),
|
86 |
console=console
|
|
|
154 |
yield from stream_log("Mixing selected stems...", "color: #FF5722;")
|
155 |
time.sleep(0.5) # Simulate mixing time
|
156 |
|
157 |
+
if selected_stems:
|
158 |
+
# Load the first stem as the base
|
159 |
+
mixed_audio: AudioSegment = AudioSegment.from_wav(selected_stems[0])
|
160 |
+
|
161 |
+
# Overlay the remaining stems
|
162 |
+
for stem_path in selected_stems[1:]:
|
163 |
+
overlay_audio = AudioSegment.from_wav(stem_path)
|
164 |
+
|
165 |
+
# Ensure both segments have the same duration
|
166 |
+
max_length = max(len(mixed_audio), len(overlay_audio))
|
167 |
+
mixed_audio = mixed_audio.pad_to_width(max_length)
|
168 |
+
overlay_audio = overlay_audio.pad_to_width(max_length)
|
169 |
+
|
170 |
+
# Overlay the audio
|
171 |
+
mixed_audio = mixed_audio.overlay(overlay_audio)
|
172 |
+
|
173 |
+
# Export the mixed audio
|
174 |
+
mixed_audio.export(output_file, format="wav")
|
175 |
+
else:
|
176 |
+
yield from stream_log("Error: No stems to mix", "color: #F44336;")
|
177 |
+
raise gr.Error("No stems were selected or found for mixing.")
|
178 |
|
179 |
if mp3:
|
180 |
yield from stream_log(f"Converting to MP3...", "color: #795548;")
|
|
|
187 |
final_message = Panel(
|
188 |
Text("Separation and mixing completed successfully!", style="bold green"),
|
189 |
title="Demucs Result",
|
190 |
+
border_style="green",
|
191 |
+
width=min(terminal_width - 2, 98) # Adjust panel width
|
192 |
)
|
193 |
console.print(final_message)
|
194 |
yield output_file, list(stems.values()), gr.HTML(console.export_html())
|