Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files16 unmodified lines1718192021229 unmodified lines32333435363738394041424344454647484950515253545556578 unmodified lines6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816910 unmodified lines18018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921035 unmodified lines2462472482492502512524 unmodified lines2572582592602612622632642656 unmodified lines2722732742752762772787 unmodified lines28628728828929029129216 unmodified linesos.environ.setdefault("HF_XET_HIGH_PERFORMANCE", "1")N_CPUS = os.cpu_count() or 2DATASETS_SERVER = "https://datasets-server.huggingface.co"ONNX_MODEL_ID = "fosters/wavlm-base-plus-sv-onnx"9 unmodified lineswith _init_lock:if _model is None:_feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(ONNX_MODEL_ID)# ONNX Runtime: thread-safe, no GIL concerns_model = ORTModelForAudioXVector.from_pretrained(ONNX_MODEL_ID)return _feature_extractor, _modeldef _embed(audio_array: np.ndarray, sr: int, max_sec: int) -> np.ndarray:fe, mdl = _load_model()waveform = torch.tensor(audio_array, dtype=torch.float32)if waveform.ndim == 2:waveform = waveform.mean(0)if sr != TARGET_SR:waveform = torchaudio.functional.resample(waveform, sr, TARGET_SR)waveform = waveform[: max_sec * TARGET_SR]inputs = fe(waveform.numpy(), sampling_rate=TARGET_SR, return_tensors="pt")out = mdl(**inputs)return out.embeddings.squeeze().numpy()def _fetch_audio_urls(repo_id: str, n: int, token: str | None) -> tuple[list[str], str]:"""Get direct audio URLs via datasets-server. Returns (urls, debug_info)."""headers = {"Authorization": f"Bearer {token}"} if token else {}resp = requests.get(f"{DATASETS_SERVER}/rows",8 unmodified linesif not rows:return [], "datasets-server: empty rows"# Inspect actual audio field structure for debuggingsample_audio = rows[0]["row"].get("audio", {})audio_keys = list(sample_audio.keys()) if isinstance(sample_audio, dict) else type(sample_audio).__name__urls = []for row in rows:audio = row["row"].get("audio", {})# audio can be a dict {"src": ...} or a list [{"src": ...}, ...]if isinstance(audio, list):audio = audio[0] if audio else {}if isinstance(audio, dict) and "src" in audio:urls.append(audio["src"])if not urls:return [], f"datasets-server: no src in audio field (keys={audio_keys})"return urls, f"datasets-server ok (audio keys={audio_keys})"def _download_one(url: str, token: str | None, max_sec: int) -> tuple[np.ndarray, dict]:headers = {"Authorization": f"Bearer {token}"} if token else {}t0 = time.time()resp = requests.get(url, headers=headers, timeout=30)resp.raise_for_status()dl_ms = (time.time() - t0) * 1000t1 = time.time()audio_array, sr = sf.read(io.BytesIO(resp.content))decode_ms = (time.time() - t1) * 1000t2 = time.time()emb = _embed(audio_array, sr, max_sec)embed_ms = (time.time() - t2) * 1000return emb, {"size_kb": len(resp.content) // 1024,"dl_ms": int(dl_ms),"decode_ms": int(decode_ms),"embed_ms": int(embed_ms),}def _streaming_fallback(repo: str, n_samples: int, audio_sec: int, token: str | None) -> list[np.ndarray]:"""Fallback: use datasets streaming when datasets-server is unavailable."""from datasets import load_dataset, Audio as HFAudiods = load_dataset(repo, split="train", streaming=True, token=token)ds = ds.cast_column("audio", HFAudio(decode=False))embs = []for j, row in enumerate(ds):if j >= n_samples:breakraw = row["audio"]audio_bytes = raw.get("bytes") or open(raw["path"], "rb").read()audio_array, sr = sf.read(io.BytesIO(audio_bytes))embs.append(_embed(audio_array, sr, audio_sec))return embsdef _process_repo(repo: str, n_samples: int, audio_sec: int, token: str | None) -> tuple[str, np.ndarray | None, str]:t0 = time.time()try:urls, api_debug = _fetch_audio_urls(repo, n_samples, token)fetch_ms = int((time.time() - t0) * 1000)if urls:# Fast path: parallel downloads via datasets-server URLswith concurrent.futures.ThreadPoolExecutor(max_workers=len(urls)) as ex:futures = [ex.submit(_download_one, u, token, audio_sec) for u in urls]results = [f.result() for f in futures]embs = [r[0] for r in results]s = results[0][1]total_ms = int((time.time() - t0) * 1000)log = (f"{repo.split('/')[-1]}: "f"api={fetch_ms}ms dl={s['dl_ms']}ms "f"decode={s['decode_ms']}ms embed={s['embed_ms']}ms "f"total={total_ms}ms ({s['size_kb']}KB/file) [{api_debug}]")else:# Slow fallback: streaming (downloads parquet shard)t1 = time.time()embs = _streaming_fallback(repo, n_samples, audio_sec, token)total_ms = int((time.time() - t0) * 1000)log = (f"{repo.split('/')[-1]}: streaming fallback "f"total={total_ms}ms [reason: {api_debug}]")if not embs:return repo, None, f"{repo}: no audio samples extracted"return repo, np.mean(embs, axis=0), logexcept Exception as exc:return repo, None, f"{repo}: ERROR {exc}"def identify_speakers(repo_ids_text: str,samples_per_book: int,10 unmodified linesprogress(0, desc="Loading modelβ¦")_load_model()progress(0.02, desc=f"Processing {len(repos)} datasets in parallelβ¦")embeddings: dict[str, np.ndarray] = {}logs: list[str] = []errors: list[str] = []done = 0# Process repos in parallel β capped at N_CPUS since embed is the bottleneckwith concurrent.futures.ThreadPoolExecutor(max_workers=N_CPUS) as ex:future_to_repo = {ex.submit(_process_repo, repo, int(samples_per_book), int(audio_sec), token): repofor repo in repos}for future in concurrent.futures.as_completed(future_to_repo):repo, emb, log = future.result()done += 1progress(done / len(repos), desc=f"[{done}/{len(repos)}] {repo.split('/')[-1]}")if emb is not None:embeddings[repo] = emblogs.append(log)else:errors.append(log)if not embeddings:return pd.DataFrame(), "No embeddings extracted.", "\n".join(errors)repo_names = list(embeddings.keys())emb_matrix = np.stack([embeddings[r] for r in repo_names])35 unmodified linesn_speakers = len(set(labels))summary = f"β {len(repo_names)} books β {n_speakers} unique speakers"debug = "\n".join(logs)if errors:debug += "\n\nERRORS:\n" + "\n".join(errors)return df, summary, debug4 unmodified linesFinds unique speakers across multiple HF audio datasets. Each dataset is assumed to have**one speaker** (e.g. an audiobook). The app fetches audio directly via the datasets-server API(no full parquet download), processes datasets in parallel, and clusters by voice similarity.**Model:** [microsoft/wavlm-base-plus-sv](https://huggingface.co/microsoft/wavlm-base-plus-sv) βlanguage-agnostic speaker embeddings, works for any language.---6 unmodified lines- *Audio length (sec)* β seconds of each chunk to use for embedding. 5 sec is sufficient for a clear voice.- *Same-speaker threshold* β cosine similarity cutoff. Raise if too many books merge into one speaker; lower if one person gets split across IDs.- *HF Token* β only needed for **private** repos.3. Click **Identify Speakers**. Datasets are processed in parallel β expect ~30β60 sec for 35 books.## Output columns7 unmodified lines**Tip:** Sort by `speaker_id` to see all books by the same narrator grouped together.The **Errors / Timing** box shows per-dataset timing breakdown (API fetch / download / decode / embed) β useful for diagnosing slow datasets."""with gr.Blocks(title="Speaker Identifier") as demo:16 unmodified lines171819202122239 unmodified lines3334353637383940414243444546474849505152535455565758596061626364656667686970718 unmodified lines808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814910 unmodified lines16016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421535 unmodified lines2512522532542552562574 unmodified lines2622632642652662672682692706 unmodified lines2772782792802812822837 unmodified lines29129229329429529629716 unmodified linesos.environ.setdefault("HF_XET_HIGH_PERFORMANCE", "1")N_CPUS = os.cpu_count() or 2BATCH_SIZE = 64 # max clips per ONNX forward passDATASETS_SERVER = "https://datasets-server.huggingface.co"ONNX_MODEL_ID = "fosters/wavlm-base-plus-sv-onnx"9 unmodified lineswith _init_lock:if _model is None:_feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(ONNX_MODEL_ID)_model = ORTModelForAudioXVector.from_pretrained(ONNX_MODEL_ID)return _feature_extractor, _modeldef _to_array(audio_array: np.ndarray, sr: int, max_sec: int) -> np.ndarray:"""Resample to TARGET_SR, convert to mono, trim to max_sec."""waveform = torch.tensor(audio_array, dtype=torch.float32)if waveform.ndim == 2:waveform = waveform.mean(0)if sr != TARGET_SR:waveform = torchaudio.functional.resample(waveform, sr, TARGET_SR)return waveform[: max_sec * TARGET_SR].numpy().astype(np.float32)def _batch_embed(arrays: list[np.ndarray]) -> np.ndarray:"""Run all clips through the model in batches. Returns (N, D) embeddings."""fe, mdl = _load_model()all_embs = []for i in range(0, len(arrays), BATCH_SIZE):batch = arrays[i : i + BATCH_SIZE]inputs = fe(batch, sampling_rate=TARGET_SR, return_tensors="pt", padding=True)out = mdl(**inputs)# embeddings shape: (batch, D)embs = out.embeddings.detach().numpy()if embs.ndim == 1:embs = embs[np.newaxis]all_embs.append(embs)return np.concatenate(all_embs, axis=0)# ββ Audio fetching ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef _fetch_audio_urls(repo_id: str, n: int, token: str | None) -> tuple[list[str], str]:headers = {"Authorization": f"Bearer {token}"} if token else {}resp = requests.get(f"{DATASETS_SERVER}/rows",8 unmodified linesif not rows:return [], "datasets-server: empty rows"sample_audio = rows[0]["row"].get("audio", {})audio_keys = list(sample_audio.keys()) if isinstance(sample_audio, dict) else type(sample_audio).__name__urls = []for row in rows:audio = row["row"].get("audio", {})if isinstance(audio, list):audio = audio[0] if audio else {}if isinstance(audio, dict) and "src" in audio:urls.append(audio["src"])if not urls:return [], f"no src in audio (keys={audio_keys})"return urls, "ok"def _download_audio(url: str, token: str | None, max_sec: int) -> tuple[np.ndarray, int]:"""Download one audio file. Returns (array_at_TARGET_SR, size_kb)."""headers = {"Authorization": f"Bearer {token}"} if token else {}resp = requests.get(url, headers=headers, timeout=30)resp.raise_for_status()audio_array, sr = sf.read(io.BytesIO(resp.content))return _to_array(audio_array, sr, max_sec), len(resp.content) // 1024def _fetch_repo_audio(repo: str, n_samples: int, audio_sec: int, token: str | None) -> tuple[str, list[np.ndarray], str]:"""Fetch + download audio for one repo. Returns (repo, arrays, log)."""t0 = time.time()try:urls, api_status = _fetch_audio_urls(repo, n_samples, token)fetch_ms = int((time.time() - t0) * 1000)if urls:with concurrent.futures.ThreadPoolExecutor(max_workers=len(urls)) as ex:results = list(ex.map(lambda u: _download_audio(u, token, audio_sec), urls))arrays = [r[0] for r in results]avg_kb = int(np.mean([r[1] for r in results]))dl_ms = int((time.time() - t0) * 1000) - fetch_mslog = f"api={fetch_ms}ms dl={dl_ms}ms ({avg_kb}KB/file) [{api_status}]"return repo, arrays, log# Streaming fallbackfrom datasets import load_dataset, Audio as HFAudiods = load_dataset(repo, split="train", streaming=True, token=token)ds = ds.cast_column("audio", HFAudio(decode=False))arrays = []for j, row in enumerate(ds):if j >= n_samples:breakraw = row["audio"]audio_bytes = raw.get("bytes") or open(raw["path"], "rb").read()a, sr = sf.read(io.BytesIO(audio_bytes))arrays.append(_to_array(a, sr, audio_sec))total_ms = int((time.time() - t0) * 1000)return repo, arrays, f"streaming fallback total={total_ms}ms [reason: {api_status}]"except Exception as exc:return repo, [], f"ERROR: {exc}"# ββ Main ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef identify_speakers(repo_ids_text: str,samples_per_book: int,10 unmodified linesprogress(0, desc="Loading modelβ¦")_load_model()# ββ Phase 1: download all audio in parallel (I/O bound) ββββββββββββββββββprogress(0.05, desc=f"Downloading audio from {len(repos)} datasetsβ¦")repo_arrays: dict[str, list[np.ndarray]] = {}fetch_logs: list[str] = []errors: list[str] = []with concurrent.futures.ThreadPoolExecutor(max_workers=N_CPUS * 2) as ex:futures = {ex.submit(_fetch_repo_audio, repo, int(samples_per_book), int(audio_sec), token): repofor repo in repos}done = 0for future in concurrent.futures.as_completed(futures):repo, arrays, log = future.result()done += 1progress(0.05 + 0.55 * done / len(repos), desc=f"[{done}/{len(repos)}] {repo.split('/')[-1]}")if arrays:repo_arrays[repo] = arraysfetch_logs.append(f"{repo.split('/')[-1]}: {log}")else:errors.append(f"{repo.split('/')[-1]}: {log}")if not repo_arrays:return pd.DataFrame(), "No audio downloaded.", "\n".join(errors)# ββ Phase 2: batch embed all clips in one shot βββββββββββββββββββββββββββprogress(0.60, desc="Batch embedding all clipsβ¦")all_arrays: list[np.ndarray] = []repo_slices: dict[str, tuple[int, int]] = {}for repo in repos:if repo in repo_arrays:start = len(all_arrays)all_arrays.extend(repo_arrays[repo])repo_slices[repo] = (start, len(all_arrays))t_embed = time.time()all_embeddings = _batch_embed(all_arrays) # (N_clips, D)embed_ms = int((time.time() - t_embed) * 1000)fetch_logs.append(f"batch embed: {len(all_arrays)} clips in {embed_ms}ms "f"({embed_ms // len(all_arrays)}ms/clip avg)")# ββ Phase 3: average per repo, cluster βββββββββββββββββββββββββββββββββββprogress(0.95, desc="Clusteringβ¦")embeddings: dict[str, np.ndarray] = {}for repo, (start, end) in repo_slices.items():embeddings[repo] = all_embeddings[start:end].mean(axis=0)repo_names = list(embeddings.keys())emb_matrix = np.stack([embeddings[r] for r in repo_names])35 unmodified linesn_speakers = len(set(labels))summary = f"β {len(repo_names)} books β {n_speakers} unique speakers"debug = "\n".join(fetch_logs)if errors:debug += "\n\nERRORS:\n" + "\n".join(errors)return df, summary, debug4 unmodified linesFinds unique speakers across multiple HF audio datasets. Each dataset is assumed to have**one speaker** (e.g. an audiobook). The app fetches audio directly via the datasets-server API(no full parquet download), downloads in parallel, then embeds all clips in one batched forward pass.**Model:** [microsoft/wavlm-base-plus-sv](https://huggingface.co/microsoft/wavlm-base-plus-sv) (ONNX) βlanguage-agnostic speaker embeddings, works for any language.---6 unmodified lines- *Audio length (sec)* β seconds of each chunk to use for embedding. 5 sec is sufficient for a clear voice.- *Same-speaker threshold* β cosine similarity cutoff. Raise if too many books merge into one speaker; lower if one person gets split across IDs.- *HF Token* β only needed for **private** repos.3. Click **Identify Speakers**. Downloads run in parallel, then all clips are embedded in one batch β expect ~20β40 sec for 35 books.## Output columns7 unmodified lines**Tip:** Sort by `speaker_id` to see all books by the same narrator grouped together.The **Errors / Timing** box shows per-dataset timing and batch embed stats β useful for diagnosing slow datasets."""with gr.Blocks(title="Speaker Identifier") as demo:
app.py+104-99