Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,291 +1,187 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
import torch.nn.functional as F
|
| 4 |
-
import librosa
|
| 5 |
-
import soundfile as sf
|
| 6 |
-
import numpy as np
|
| 7 |
-
from huggingface_hub import hf_hub_download
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
from attend import Attend
|
| 13 |
-
except ImportError:
|
| 14 |
-
pass
|
| 15 |
-
|
| 16 |
-
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 17 |
-
|
| 18 |
-
def safe_attend_forward(self, q, k, v, mask=None):
|
| 19 |
-
return F.scaled_dot_product_attention(q, k, v, attn_mask=mask, dropout_p=0.0)
|
| 20 |
-
|
| 21 |
-
try:
|
| 22 |
-
Attend.forward = safe_attend_forward
|
| 23 |
-
except Exception:
|
| 24 |
-
pass
|
| 25 |
-
|
| 26 |
-
def load_model():
|
| 27 |
-
ckpt = hf_hub_download(
|
| 28 |
-
repo_id="Tachyeon/IAM-RoFormer-Model-Weights",
|
| 29 |
-
filename="v11_consensus_epoch_30.pt"
|
| 30 |
-
)
|
| 31 |
-
model = BSRoformer(
|
| 32 |
-
dim=512, depth=12, stereo=True, num_stems=4,
|
| 33 |
-
time_transformer_depth=1, freq_transformer_depth=1,
|
| 34 |
-
flash_attn=True
|
| 35 |
-
).to(DEVICE)
|
| 36 |
-
|
| 37 |
-
state = torch.load(ckpt, map_location=DEVICE)
|
| 38 |
-
model.load_state_dict(state["model"] if "model" in state else state)
|
| 39 |
-
model.eval()
|
| 40 |
-
return model
|
| 41 |
-
|
| 42 |
-
model = load_model()
|
| 43 |
-
|
| 44 |
-
def separate_audio(path):
|
| 45 |
-
if not path:
|
| 46 |
-
return [None]*4
|
| 47 |
-
|
| 48 |
-
mix, sr = librosa.load(path, sr=44100, mono=False)
|
| 49 |
-
if mix.ndim == 1:
|
| 50 |
-
mix = np.stack([mix, mix])
|
| 51 |
-
|
| 52 |
-
x = torch.tensor(mix).float().to(DEVICE)[None]
|
| 53 |
-
L = x.shape[-1]
|
| 54 |
-
|
| 55 |
-
out = torch.zeros(1,4,2,L, device=DEVICE)
|
| 56 |
-
cnt = torch.zeros_like(out)
|
| 57 |
-
|
| 58 |
-
chunk = 44100*10
|
| 59 |
-
hop = chunk - 44100
|
| 60 |
-
|
| 61 |
-
with torch.no_grad(), torch.autocast("cuda", enabled=torch.cuda.is_available()):
|
| 62 |
-
for s in range(0, L, hop):
|
| 63 |
-
e = min(s+chunk, L)
|
| 64 |
-
part = x[:,:,s:e]
|
| 65 |
-
if part.shape[-1] < chunk:
|
| 66 |
-
part = F.pad(part,(0,chunk-part.shape[-1]))
|
| 67 |
-
pred = model(part)
|
| 68 |
-
out[:,:,:,s:e] += pred[:,:,:,:e-s]
|
| 69 |
-
cnt[:,:,:,s:e] += 1
|
| 70 |
-
|
| 71 |
-
stems = (out / cnt.clamp(min=1)).cpu().numpy()[0]
|
| 72 |
-
files=[]
|
| 73 |
-
for i in range(4):
|
| 74 |
-
f=f"stem_{i}.wav"
|
| 75 |
-
sf.write(f, stems[i].T, sr)
|
| 76 |
-
files.append(f)
|
| 77 |
-
return files
|
| 78 |
-
|
| 79 |
-
# ================= UI CSS + layout (polished) =================
|
| 80 |
css = """
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
:
|
| 84 |
-
--
|
| 85 |
-
--
|
| 86 |
-
--
|
| 87 |
-
--
|
| 88 |
-
--
|
| 89 |
-
--
|
| 90 |
-
--panel-2: rgba(255,255,255,0.02);
|
| 91 |
-
--radius: 12px;
|
| 92 |
}
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
margin: 0;
|
| 98 |
-
padding: 0;
|
| 99 |
-
background: linear-gradient(180deg, var(--bg1), var(--bg2)) !important;
|
| 100 |
-
color: var(--ink);
|
| 101 |
-
font-family: Poppins, sans-serif;
|
| 102 |
-
overflow: auto;
|
| 103 |
}
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
max-width: 1160px;
|
| 108 |
margin: 0 auto;
|
| 109 |
-
padding: 48px 40px;
|
| 110 |
-
box-sizing: border-box;
|
| 111 |
-
display: grid;
|
| 112 |
-
grid-template-rows: auto 1fr;
|
| 113 |
-
gap: 36px;
|
| 114 |
}
|
| 115 |
|
| 116 |
-
/*
|
| 117 |
-
.
|
| 118 |
-
|
| 119 |
-
flex-direction:column;
|
| 120 |
-
gap:6px;
|
| 121 |
}
|
|
|
|
| 122 |
.logo {
|
| 123 |
-
font-
|
| 124 |
-
font-
|
| 125 |
-
letter-spacing:1px;
|
|
|
|
| 126 |
}
|
|
|
|
| 127 |
.tagline {
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
}
|
| 133 |
|
| 134 |
-
/*
|
| 135 |
-
.
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
align-items: start; /* ensure top baseline alignment */
|
| 140 |
}
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
/* style the Gradio audio drop area so it's visually lighter and slightly shorter */
|
| 147 |
-
.left .gradio-audio {
|
| 148 |
-
background: var(--panel) !important;
|
| 149 |
-
border-radius: var(--radius) !important;
|
| 150 |
-
border: 1px solid rgba(255,255,255,0.04) !important;
|
| 151 |
-
padding: 12px !important;
|
| 152 |
-
box-sizing: border-box;
|
| 153 |
-
min-height: 260px; /* reduced height so stems line up better */
|
| 154 |
-
display:flex;
|
| 155 |
-
align-items:center;
|
| 156 |
-
justify-content:center;
|
| 157 |
}
|
| 158 |
|
| 159 |
-
/*
|
| 160 |
-
.
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
}
|
| 163 |
|
| 164 |
-
/*
|
| 165 |
-
.
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
border: none !important;
|
| 175 |
-
box-shadow: 0 8px 30px rgba(0,0,0,0.25);
|
| 176 |
}
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
grid-template-columns: 1fr 1fr;
|
| 182 |
-
gap: 22px;
|
| 183 |
-
align-items: start;
|
| 184 |
}
|
| 185 |
|
| 186 |
-
/*
|
| 187 |
-
.stem
|
| 188 |
-
background: var(--
|
|
|
|
| 189 |
border-radius: 14px;
|
| 190 |
-
padding:
|
| 191 |
-
border: 1px solid rgba(255,255,255,0.03);
|
| 192 |
-
box-sizing: border-box;
|
| 193 |
-
min-height: 140px; /* consistent stem surface height */
|
| 194 |
display: flex;
|
| 195 |
flex-direction: column;
|
| 196 |
-
|
| 197 |
-
justify-content: center;
|
| 198 |
}
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
font-
|
| 203 |
-
font-weight:500;
|
| 204 |
color: var(--accent);
|
| 205 |
-
margin-
|
| 206 |
}
|
| 207 |
|
| 208 |
-
|
| 209 |
-
.stem-surface .gradio-audio label {
|
| 210 |
-
display:none !important;
|
| 211 |
-
}
|
| 212 |
-
|
| 213 |
-
/* smaller inner audio player to avoid odd spacing */
|
| 214 |
-
.stem-surface .gradio-audio {
|
| 215 |
background: transparent !important;
|
| 216 |
-
border:
|
| 217 |
-
padding: 0 !important;
|
| 218 |
-
min-height: 88px !important;
|
| 219 |
-
display:flex;
|
| 220 |
-
align-items:center;
|
| 221 |
-
justify-content:center;
|
| 222 |
}
|
| 223 |
|
| 224 |
-
/*
|
| 225 |
-
.
|
| 226 |
-
|
| 227 |
-
max-height: 36px;
|
| 228 |
-
opacity: 0.95;
|
| 229 |
}
|
| 230 |
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
font-size: 12px;
|
| 240 |
-
opacity: 0.95;
|
| 241 |
}
|
| 242 |
|
| 243 |
-
/*
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
.stems { grid-template-columns: 1fr 1fr; gap:16px; }
|
| 247 |
-
.app { padding: 28px 20px; }
|
| 248 |
}
|
| 249 |
"""
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
# -----------------------------
|
| 4 |
+
# CSS (FINAL, POLISHED)
|
| 5 |
+
# -----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
css = """
|
| 7 |
+
:root {
|
| 8 |
+
--bg-1: #1a0c12;
|
| 9 |
+
--bg-2: #3a1523;
|
| 10 |
+
--surface: rgba(255,255,255,0.035);
|
| 11 |
+
--surface-soft: rgba(255,255,255,0.025);
|
| 12 |
+
--border: rgba(255,255,255,0.08);
|
| 13 |
+
--text: #f5f5f5;
|
| 14 |
+
--muted: rgba(255,255,255,0.65);
|
| 15 |
+
--accent: #ff5c8a;
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
+
body {
|
| 19 |
+
background: linear-gradient(135deg, var(--bg-1), var(--bg-2));
|
| 20 |
+
font-family: Poppins, system-ui, sans-serif;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
|
| 23 |
+
.gradio-container {
|
| 24 |
+
max-width: 1200px !important;
|
|
|
|
| 25 |
margin: 0 auto;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
+
/* Header */
|
| 29 |
+
.header {
|
| 30 |
+
margin-bottom: 28px;
|
|
|
|
|
|
|
| 31 |
}
|
| 32 |
+
|
| 33 |
.logo {
|
| 34 |
+
font-size: 42px;
|
| 35 |
+
font-weight: 800;
|
| 36 |
+
letter-spacing: 1px;
|
| 37 |
+
color: var(--text);
|
| 38 |
}
|
| 39 |
+
|
| 40 |
.tagline {
|
| 41 |
+
margin-top: 6px;
|
| 42 |
+
font-size: 14px;
|
| 43 |
+
color: var(--accent);
|
| 44 |
+
letter-spacing: 0.3px;
|
| 45 |
}
|
| 46 |
|
| 47 |
+
/* Section text */
|
| 48 |
+
.section-title {
|
| 49 |
+
font-size: 20px;
|
| 50 |
+
font-weight: 600;
|
| 51 |
+
margin-bottom: 4px;
|
|
|
|
| 52 |
}
|
| 53 |
|
| 54 |
+
.section-sub {
|
| 55 |
+
font-size: 14px;
|
| 56 |
+
color: var(--muted);
|
| 57 |
+
margin-bottom: 14px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
}
|
| 59 |
|
| 60 |
+
/* Main grid — THIS FIXES ALIGNMENT */
|
| 61 |
+
.main-grid {
|
| 62 |
+
display: grid;
|
| 63 |
+
grid-template-columns: 2fr 1fr 1fr;
|
| 64 |
+
grid-template-rows: auto auto;
|
| 65 |
+
gap: 22px;
|
| 66 |
+
align-items: stretch;
|
| 67 |
}
|
| 68 |
|
| 69 |
+
/* Upload area */
|
| 70 |
+
.upload-box {
|
| 71 |
+
grid-row: 1 / span 2;
|
| 72 |
+
background: var(--surface);
|
| 73 |
+
border: 1px solid var(--border);
|
| 74 |
+
border-radius: 14px;
|
| 75 |
+
padding: 20px;
|
| 76 |
+
display: flex;
|
| 77 |
+
flex-direction: column;
|
| 78 |
+
justify-content: space-between;
|
|
|
|
|
|
|
| 79 |
}
|
| 80 |
|
| 81 |
+
.upload-box .gradio-audio {
|
| 82 |
+
background: transparent !important;
|
| 83 |
+
border: none !important;
|
|
|
|
|
|
|
|
|
|
| 84 |
}
|
| 85 |
|
| 86 |
+
/* Stem boxes */
|
| 87 |
+
.stem {
|
| 88 |
+
background: var(--surface-soft);
|
| 89 |
+
border: 1px solid var(--border);
|
| 90 |
border-radius: 14px;
|
| 91 |
+
padding: 14px;
|
|
|
|
|
|
|
|
|
|
| 92 |
display: flex;
|
| 93 |
flex-direction: column;
|
| 94 |
+
justify-content: space-between;
|
|
|
|
| 95 |
}
|
| 96 |
|
| 97 |
+
.stem-title {
|
| 98 |
+
font-size: 14px;
|
| 99 |
+
font-weight: 500;
|
|
|
|
| 100 |
color: var(--accent);
|
| 101 |
+
margin-bottom: 6px;
|
| 102 |
}
|
| 103 |
|
| 104 |
+
.stem .gradio-audio {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
background: transparent !important;
|
| 106 |
+
border: none !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
}
|
| 108 |
|
| 109 |
+
/* Button */
|
| 110 |
+
.action-row {
|
| 111 |
+
margin-top: 18px;
|
|
|
|
|
|
|
| 112 |
}
|
| 113 |
|
| 114 |
+
.primary-btn {
|
| 115 |
+
width: 100%;
|
| 116 |
+
height: 48px;
|
| 117 |
+
border-radius: 12px;
|
| 118 |
+
background: linear-gradient(135deg, #ff7aa2, #ffb36b);
|
| 119 |
+
color: #111;
|
| 120 |
+
font-weight: 700;
|
| 121 |
+
letter-spacing: 0.3px;
|
|
|
|
|
|
|
| 122 |
}
|
| 123 |
|
| 124 |
+
/* Footer cleanup */
|
| 125 |
+
footer {
|
| 126 |
+
display: none !important;
|
|
|
|
|
|
|
| 127 |
}
|
| 128 |
"""
|
| 129 |
|
| 130 |
+
# -----------------------------
|
| 131 |
+
# APP
|
| 132 |
+
# -----------------------------
|
| 133 |
+
with gr.Blocks(css=css) as demo:
|
| 134 |
+
|
| 135 |
+
# Header
|
| 136 |
+
gr.HTML("""
|
| 137 |
+
<div class="header">
|
| 138 |
+
<div class="logo">SWARA STUDIO</div>
|
| 139 |
+
<div class="tagline">Separating Music Into Its elements</div>
|
| 140 |
+
</div>
|
| 141 |
+
""")
|
| 142 |
+
|
| 143 |
+
# Section intro
|
| 144 |
+
gr.HTML("""
|
| 145 |
+
<div class="section-title">Select a track</div>
|
| 146 |
+
<div class="section-sub">We’ll break it down into individual parts</div>
|
| 147 |
+
""")
|
| 148 |
+
|
| 149 |
+
# Main aligned grid
|
| 150 |
+
with gr.HTML('<div class="main-grid">'):
|
| 151 |
+
|
| 152 |
+
# Upload box (left, spans 2 rows)
|
| 153 |
+
with gr.HTML('<div class="upload-box">'):
|
| 154 |
+
audio_in = gr.Audio(label="Audio", type="filepath")
|
| 155 |
+
gr.HTML('</div>')
|
| 156 |
+
|
| 157 |
+
# Vocals
|
| 158 |
+
with gr.HTML('<div class="stem">'):
|
| 159 |
+
gr.HTML('<div class="stem-title">Vocals</div>')
|
| 160 |
+
vocals = gr.Audio(label=None)
|
| 161 |
+
gr.HTML('</div>')
|
| 162 |
+
|
| 163 |
+
# Drums
|
| 164 |
+
with gr.HTML('<div class="stem">'):
|
| 165 |
+
gr.HTML('<div class="stem-title">Drums</div>')
|
| 166 |
+
drums = gr.Audio(label=None)
|
| 167 |
+
gr.HTML('</div>')
|
| 168 |
+
|
| 169 |
+
# Bass
|
| 170 |
+
with gr.HTML('<div class="stem">'):
|
| 171 |
+
gr.HTML('<div class="stem-title">Bass</div>')
|
| 172 |
+
bass = gr.Audio(label=None)
|
| 173 |
+
gr.HTML('</div>')
|
| 174 |
+
|
| 175 |
+
# Other
|
| 176 |
+
with gr.HTML('<div class="stem">'):
|
| 177 |
+
gr.HTML('<div class="stem-title">Other</div>')
|
| 178 |
+
other = gr.Audio(label=None)
|
| 179 |
+
gr.HTML('</div>')
|
| 180 |
+
|
| 181 |
+
gr.HTML('</div>') # end grid
|
| 182 |
+
|
| 183 |
+
# Action button
|
| 184 |
+
with gr.Row(elem_classes="action-row"):
|
| 185 |
+
separate = gr.Button("Separate", elem_classes="primary-btn")
|
| 186 |
+
|
| 187 |
+
demo.launch()
|