Spaces:
Sleeping
Sleeping
File size: 9,524 Bytes
8c10ae6 dd6f0b4 ee9445c 8c10ae6 dd6f0b4 8c10ae6 dd6f0b4 8c10ae6 ee9445c 8c10ae6 ee9445c 8c10ae6 ee9445c dd6f0b4 8c10ae6 dd6f0b4 8c10ae6 dd6f0b4 8c10ae6 dd6f0b4 8c10ae6 dd6f0b4 8c10ae6 dd6f0b4 8c10ae6 dd6f0b4 ee9445c dd6f0b4 ee9445c dd6f0b4 ee9445c 8c10ae6 ee9445c 8c10ae6 ee9445c 8c10ae6 dd6f0b4 ee9445c 8c10ae6 ee9445c 8c10ae6 dd6f0b4 ee9445c dd6f0b4 8c10ae6 dd6f0b4 0ab7f43 dd6f0b4 0ab7f43 dd6f0b4 0ab7f43 dd6f0b4 8c10ae6 dd6f0b4 8c10ae6 ee9445c 0ab7f43 ee9445c 8c10ae6 dd6f0b4 8c10ae6 ee9445c dd6f0b4 8c10ae6 dd6f0b4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 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 | import os
import json
import time
import re
import tempfile
from io import BytesIO
import threading
import gradio as gr
from dotenv import load_dotenv
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
import numpy as np
import librosa
import soundfile as sf
# === Load Google Drive service account ===
load_dotenv()
SERVICE_ACCOUNT_JSON = os.environ.get("SERVICE_ACCOUNT_JSON")
if not SERVICE_ACCOUNT_JSON:
raise RuntimeError("Missing SERVICE_ACCOUNT_JSON")
creds_dict = json.loads(SERVICE_ACCOUNT_JSON)
creds = service_account.Credentials.from_service_account_info(
creds_dict,
scopes=["https://www.googleapis.com/auth/drive.readonly"]
)
drive_service = build("drive", "v3", credentials=creds)
# === Google Drive audio file indexing ===
ROOT_FOLDER_ID = "1l7-LY9Vf9lDs8ltjDqckxrqUl_hj3W4d"
def _list_audio_files(folder_id, path=""):
results = drive_service.files().list(
q=f"'{folder_id}' in parents and trashed = false",
fields="files(id, name, mimeType)"
).execute()
file_map = {}
for item in results.get("files", []):
name = item["name"]
full_path = f"{path}/{name}".strip("/")
if item["mimeType"] == "application/vnd.google-apps.folder":
file_map.update(_list_audio_files(item["id"], full_path))
elif item["mimeType"].startswith("audio/"):
file_map[full_path] = item["id"]
return file_map
audio_library = _list_audio_files(ROOT_FOLDER_ID)
# === Parse metadata from filenames ===
pattern = r'_T(\d+)_|0*(\d+)\.|_(\d)_'
titles = {}
for filename, fid in audio_library.items():
if not filename.lower().endswith(('.flac', '.wav', '.aac')):
continue
parts = filename.split('/')
if len(parts) < 3:
continue
match = re.search(pattern, filename)
if match:
num_str = next(g for g in match.groups() if g is not None)
num = int(num_str)
else:
print(f"β οΈ Could not parse track number: {filename}")
continue
if '._instrumental_ASHQ' in filename:
stem = 'instrumental_ASHQ'
elif '._instrumental_AS' in filename or '._instrumentalAS' in filename:
stem = 'instrumental_AS'
elif '._instrumental' in filename:
stem = 'instrumental'
elif '._vocals_ASHQ' in filename:
stem = 'vocals_ASHQ'
elif '._vocals_AS' in filename or '._vocalsAS' in filename:
stem = 'vocals_AS'
elif '._vocals' in filename:
stem = 'vocals'
else:
stem = 'orig'
titles[(parts[1], parts[2], num, stem)] = fid
# === Audio choices ===
format_choices = ['AAC-96kbps', 'AAC-256kbps', 'FLACs']
stem_choices = [
'orig', 'instrumental', 'instrumental_AS', 'instrumental_ASHQ',
'vocals', 'vocals_AS', 'vocals_ASHQ'
]
# === In-memory storage ===
audio_tracks = {
(f, s): gr.Audio(label=f"{f} / {s}", type="numpy", visible=False)
for f in format_choices for s in stem_choices
}
downloaded_audio_flacs = {} # (format, stem) -> BytesIO FLAC
# === Download helper ===
def _download_audio(library, frmt, track, stem):
file_id = titles.get((library, frmt, track, stem))
if not file_id:
raise ValueError(f"Missing file: {library}, {frmt}, {track}, {stem}")
buffer = BytesIO()
request = drive_service.files().get_media(fileId=file_id)
downloader = MediaIoBaseDownload(buffer, request)
done = False
while not done:
status, done = downloader.next_chunk()
print(f"β¬οΈ Downloaded {int(status.progress() * 100)}%")
buffer.seek(0)
with tempfile.NamedTemporaryFile(suffix=".audio", delete=False) as tmp:
tmp.write(buffer.read())
tmp_path = tmp.name
y, sr = librosa.load(tmp_path, sr=None, mono=False) # keep stereo
os.remove(tmp_path)
if y.ndim == 2:
y = y.T # Convert from (channels, samples) β (samples, channels)
flac_buf = BytesIO()
sf.write(flac_buf, y, sr, format="FLAC")
flac_buf.seek(0)
downloaded_audio_flacs[(frmt, stem)] = flac_buf
# === Load UI handler ===
def _load(library, track, formats, stems):
selected_keys = set((f, s) for f in formats for s in stems)
outputs = []
status_lines = []
for (f, s), audio in audio_tracks.items():
if (f, s) in selected_keys:
try:
_download_audio(library, f, track, s)
flac_buf = downloaded_audio_flacs[(f, s)]
flac_buf.seek(0)
y, sr = sf.read(flac_buf, dtype='float32')
outputs.append(gr.update(value=(sr, y), visible=True, label=f"{f} / {s}"))
# status_lines.append(f"β
Loaded {f} / {s}")
except Exception as e:
outputs.append(gr.update(value=None, visible=False))
status_lines.append(f"β Failed {f} / {s}: {str(e)}")
else:
outputs.append(gr.update(value=None, visible=False))
available_keys = [f"{f} / {s}" for (f, s) in downloaded_audio_flacs.keys()]
instrumental_key = None
for k in available_keys:
if 'instrumental' in k:
instrumental_key = k
break
vocals_key = None
for k in available_keys:
if 'vocals' in k:
vocals_key = k
break
mix1_update = gr.update(choices=available_keys, value=instrumental_key)
mix2_update = gr.update(choices=available_keys, value=vocals_key)
status_text_output = "\n".join(status_lines) if status_lines else ""
return outputs + [gr.update(value=status_text_output), mix1_update, mix2_update]
# === Mix handler ===
def _mix_tracks(track1_key, track2_key, level, subtract_mix):
if not track1_key or not track2_key:
raise gr.Error("Please select both tracks.")
key1 = tuple(track1_key.split(" / "))
key2 = tuple(track2_key.split(" / "))
buf1 = downloaded_audio_flacs.get(key1)
buf2 = downloaded_audio_flacs.get(key2)
if buf1 is None or buf2 is None:
raise gr.Error("Selected tracks must be loaded first.")
buf1.seek(0)
buf2.seek(0)
y1, sr1 = sf.read(buf1, dtype='float32')
y2, sr2 = sf.read(buf2, dtype='float32')
if sr1 != sr2:
raise gr.Error("Sampling rates do not match.")
min_len = min(len(y1), len(y2))
y1 = y1[:min_len]
y2 = y2[:min_len]
y1_level = y2_level = 1.
if level < .5:
y2_level = 2 * level
else:
y1_level = 2 - (2 * level)
if subtract_mix:
mixed = y1 - level * y2
else:
# mixed = np.clip(y1_level * y1 + y2_level * y2, -1.0, 1.0)
mixed = np.clip((1-level) * y1 + level * y2, -1.0, 1.0)
return (sr1, mixed)
# === UI ===
with gr.Blocks() as demo:
gr.Markdown("# π΅ Stems Evaluation for Realize Music")
gr.Markdown(
"βοΈ **Warning**: This app is not compatible with **Safari** or mobile browsers! Please use a desktop browser."
)
with gr.Tab("Stem Selection"):
with gr.Row():
library_dropdown = gr.Dropdown(
choices=["UM-00600753511770", "Virgin-1405031933546", "Warner-5034644802213"],
label="Select Library",
value="Warner-5034644802213"
)
track_dropdown = gr.Dropdown(choices=[1, 2, 3, 4], label="Track Number")
load_button = gr.Button("Load")
with gr.Row():
selected_formats = gr.CheckboxGroup(
choices=format_choices,
label="Audio Input Formats",
value=["AAC-96kbps"]
)
selected_stems = gr.CheckboxGroup(
choices=stem_choices,
label="Stems to Retrieve",
value=["orig", "instrumental_AS", "vocals_AS"]
)
status_text = gr.Textbox(label="Status", interactive=False)
gr.Markdown("### π§ Loaded Audio Grid")
with gr.Column():
for stem in stem_choices:
with gr.Row():
for frmt in format_choices:
audio_tracks[(frmt, stem)].render()
# === Mix Tab ===
with gr.Tab("π Mix Tracks"):
gr.Markdown("### Mix any two loaded audio tracks")
gr.Markdown("For **subtracting**, orig-vocals=1.0 should be instrumental and orig-instrumental=1.0 should be vocals. And subtracting low-res from high-quality should show the residual difference. But note, playback is normalized and artifacts might sound much greater than they are.")
gr.Markdown("(After changing the mix level, you have to select `Mix Tracks` again.)")
with gr.Row():
mix1_dropdown = gr.Dropdown(label="Track 1")
mix2_dropdown = gr.Dropdown(label="Track 2")
mix_slider = gr.Slider(minimum=0, maximum=1, value=0.5, step=0.01, label="Mix (Trk1<->Trk2)")
mix_subtract_chk = gr.Checkbox(label="subtract (Trk1-Trk2)", value=False)
mix_button = gr.Button("Mix Tracks")
mix_output = gr.Audio(type="numpy", label="Mixed Output")
mix_button.click(
fn=_mix_tracks,
inputs=[mix1_dropdown, mix2_dropdown, mix_slider, mix_subtract_chk],
outputs=mix_output
)
load_button.click(
fn=_load,
inputs=[library_dropdown, track_dropdown, selected_formats, selected_stems],
outputs=list(audio_tracks.values()) + [status_text, mix1_dropdown, mix2_dropdown]
)
if __name__ == "__main__":
demo.launch() |