Spaces:
Sleeping
Sleeping
Download app.py from tommct/RealizeMusicStemComparator: direct link, hf CLI and curl.
- Browser
- Download file 9.52 kB
-
https://huggingface.co/spaces/tommct/RealizeMusicStemComparator/resolve/main/app.py
- Command line
-
hf download hf://spaces/tommct/RealizeMusicStemComparator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/tommct/RealizeMusicStemComparator/resolve/main/app.py
9.52 kB
| 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() |