Spaces:
Sleeping
Sleeping
Fix: status bar instead of gr.Progress overlay, per-batch progress, total_rows from datasets-server
Browse files1 unmodified line234567814 unmodified lines232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808 unmodified lines89909192939495969710 unmodified lines1081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661 unmodified line1681691701711721731741751761771781791801811821831841851861871881891901911921931941951967 unmodified lines2042052062072082092102112122132142152162172182192202212222232242252262272282292302312321 unmodified line2342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133147 unmodified lines3223233243253263273283293303313323333343353363373383393403413423433443453463473483491 unmodified line3513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983993 unmodified lines4034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574587 unmodified lines4664674684694704714724734744754 unmodified lines4804814824834844854863 unmodified lines4904914924934944954962 unmodified lines4995005015025035045055065075085095105115125135145152 unmodified lines5185195205215225235241 unmodified lineClassifies audio chunks in one or more HF datasets using the AST AudioSet model.Writes a ``<input>_classified`` dataset with id + audio + classification columns only.The audio column is kept as-is so the HF viewer shows an audio player."""from __future__ import annotations14 unmodified linesimport pandas as pdimport pyarrow as paimport pyarrow.parquet as pqimport torchfrom huggingface_hub import HfApi, hf_hub_downloadfrom music_detector import ChunkVerdict, _get_ast_runtime, judge_chunk_files_batched# ---------------------------------------------------------------------------# Constants# ---------------------------------------------------------------------------N_CPUS = os.cpu_count() or 2torch.set_num_threads(N_CPUS) # let BLAS use all CPUs for AST inferenceBATCH_SIZE = 32DEFAULT_THRESHOLD = 0.25DEFAULT_NAMESPACE = "fosters"LOG_MAX_LINES = 500 # keep only last N lines in the log box# New classifier columns added to every output shard_CLASSIFIER_FIELDS = [pa.field("music_score", pa.float32()),pa.field("has_music", pa.bool_()),pa.field("contaminated", pa.bool_()),pa.field("flags", pa.string()),pa.field("top_labels", pa.string()),pa.field("rms_db", pa.float32()),pa.field("clipping_ratio", pa.float32()),pa.field("max_silence_sec",pa.float32()),]_HF_FEATURES_CLASSIFIER = {"id": {"_type": "Value", "dtype": "string"},"audio": {"_type": "Audio"},"music_score": {"_type": "Value", "dtype": "float32"},"has_music": {"_type": "Value", "dtype": "bool"},"contaminated": {"_type": "Value", "dtype": "bool"},"flags": {"_type": "Value", "dtype": "string"},"top_labels": {"_type": "Value", "dtype": "string"},"rms_db": {"_type": "Value", "dtype": "float32"},"clipping_ratio": {"_type": "Value", "dtype": "float32"},"max_silence_sec":{"_type": "Value", "dtype": "float32"},}_OUTPUT_SCHEMA = pa.schema([pa.field("id", pa.string()),pa.field("audio", pa.struct([pa.field("bytes", pa.binary()),pa.field("path", pa.string()),])),*_CLASSIFIER_FIELDS,],metadata={"huggingface": json.dumps({"info": {"features": _HF_FEATURES_CLASSIFIER}})},)8 unmodified linesdef _default_out_repo(in_repo: str, suffix: str) -> str:parts = in_repo.split("/")name = parts[-1]ns = parts[0] if len(parts) > 1 else DEFAULT_NAMESPACEsuf = suffix.strip()if not suf.startswith("_"):suf = "_" + sufreturn f"{ns}/{name}{suf}"10 unmodified lines)def _build_output_row(chunk_id: str,audio_bytes: bytes,audio_path_hint: str,verdict: ChunkVerdict,) -> dict[str, Any]:return {"id": chunk_id,"audio": {"bytes": audio_bytes, "path": audio_path_hint},"music_score": verdict.music_score,"has_music": verdict.has_music,"contaminated": verdict.contaminated,"flags": ", ".join(verdict.flags),"top_labels": ", ".join(verdict.top_labels[:3]),"rms_db": verdict.rms_db,"clipping_ratio": verdict.clipping_ratio,"max_silence_sec":verdict.max_silence_sec,}def _rows_to_table(rows: list[dict[str, Any]]) -> pa.Table:audio_type = _OUTPUT_SCHEMA.field("audio").typereturn pa.table({"id": pa.array([r["id"] for r in rows], type=pa.string()),"audio": pa.array([{"bytes": r["audio"]["bytes"], "path": r["audio"]["path"]} for r in rows],type=audio_type,),"music_score": pa.array([r["music_score"] for r in rows], type=pa.float32()),"has_music": pa.array([r["has_music"] for r in rows], type=pa.bool_()),"contaminated": pa.array([r["contaminated"] for r in rows], type=pa.bool_()),"flags": pa.array([r["flags"] for r in rows], type=pa.string()),"top_labels": pa.array([r["top_labels"] for r in rows], type=pa.string()),"rms_db": pa.array([r["rms_db"] for r in rows], type=pa.float32()),"clipping_ratio": pa.array([r["clipping_ratio"] for r in rows], type=pa.float32()),"max_silence_sec":pa.array([r["max_silence_sec"] for r in rows], type=pa.float32()),},schema=_OUTPUT_SCHEMA,)def _fmt_verdict(chunk_id: str, v: ChunkVerdict) -> str:status = "🔴 MUSIC" if v.has_music else ("⚠️ TECH" if v.flags else "✅ ok")labels = ", ".join(v.top_labels[:2])flags = f" [{', '.join(v.flags)}]" if v.flags else ""return f" {chunk_id}: score={v.music_score:.3f} {labels}{flags} {status}"# ---------------------------------------------------------------------------# Per-repo generator# ---------------------------------------------------------------------------def _process_repo(repo_id: str,1 unmodified linemusic_threshold: float,private: bool,token: str | None,) -> Generator[tuple[list[str], dict[str, Any] | None], None, None]:"""Generator: process one repo. Yields (log_lines, stats_or_None).Yields after every BATCH_SIZE chunks so the UI stays responsive.Final yield carries the completed stats dict."""api = HfApi(token=token)short = repo_id.split("/")[-1]# --- list shards ---yield [f"[{_ts()}] {short}: listing parquet shards…"], Nonetry:shard_names = _list_parquet_shards(repo_id, token)except Exception as exc:yield [f"[{_ts()}] {short}: ✗ cannot list shards: {exc}"], {"repo": repo_id, "error": str(exc)}returnif not shard_names:msg = f"[{_ts()}] {short}: ✗ no data/train-*.parquet shards found"yield [msg], {"repo": repo_id, "error": "no shards"}returnyield [f"[{_ts()}] {short}: {len(shard_names)} shard(s) — creating output repo…"], Noneapi.create_repo(repo_id=out_repo, repo_type="dataset", exist_ok=True, private=private)stats: dict[str, Any] = {7 unmodified linesout_parts: list[Path] = []for shard_idx, shard_name in enumerate(shard_names):# --- download shard ---yield [f"[{_ts()}] {short}: shard {shard_idx+1}/{len(shard_names)} — downloading…"], Nonet_shard = time.time()try:shard_path = hf_hub_download(repo_id=repo_id, filename=shard_name,repo_type="dataset", token=token,)except Exception as exc:yield [f"[{_ts()}] {short}: ✗ download failed: {exc}"], Nonecontinuein_table = pq.read_table(shard_path)n_rows = len(in_table)audio_col = in_table.column("audio")id_col = in_table.column("id").to_pylist()yield [f"[{_ts()}] {short}: shard {shard_idx+1}/{len(shard_names)} — "f"{n_rows} rows downloaded in {time.time()-t_shard:.1f}s, classifying…"], Nonen_batches = (n_rows + BATCH_SIZE - 1) // BATCH_SIZEshard_rows: list[dict[str, Any]] = []for batch_idx in range(n_batches):1 unmodified lineb_end = min(b_start + BATCH_SIZE, n_rows)t_batch = time.time()batch_ids: list[str] = id_col[b_start:b_end]batch_audio_bytes: list[bytes] = [(audio_col[i].as_py() or {}).get("bytes") or b""for i in range(b_start, b_end)]batch_path_hints: list[str] = [(audio_col[i].as_py() or {}).get("path") or "chunk.mp3"for i in range(b_start, b_end)]# write temp audio files for the batchwith tempfile.TemporaryDirectory() as audio_tmp:audio_files: list[Path] = []for k, (ab, ph) in enumerate(zip(batch_audio_bytes, batch_path_hints)):suffix = Path(ph).suffix or ".mp3"p = Path(audio_tmp) / f"chunk_{k:04d}{suffix}"p.write_bytes(ab)audio_files.append(p)verdicts = judge_chunk_files_batched(audio_files,music_threshold=music_threshold,batch_size=len(audio_files),device="cpu",)# accumulate output rowsfor chunk_id, audio_bytes, ph, v in zip(batch_ids, batch_audio_bytes, batch_path_hints, verdicts):shard_rows.append(_build_output_row(chunk_id, audio_bytes, ph, v))# update statsn_cont = sum(v.contaminated for v in verdicts)n_music = sum(v.has_music for v in verdicts)n_tech = sum(bool(v.flags) and not v.has_music for v in verdicts)stats["total"] += b_end - b_startstats["contaminated"]+= n_contstats["has_music"] += n_musicstats["technical"] += n_techelapsed = time.time() - t_batchlog_lines = [""] # blank separatorlog_lines += [_fmt_verdict(cid, v) for cid, v in zip(batch_ids, verdicts)]log_lines += [f"[{_ts()}] batch {batch_idx+1}/{n_batches} "f"({b_end - b_start} chunks, {elapsed:.1f}s) "f"shard {shard_idx+1}/{len(shard_names)} "f"total {stats['total']} processed "f"contaminated {stats['contaminated']} "f"({100*stats['contaminated']/max(stats['total'],1):.1f}%)"]yield log_lines, None# write shardout_table = _rows_to_table(shard_rows)out_part = out_data_dir / f"part_{shard_idx:05d}.parquet"pq.write_table(out_table, out_part, row_group_size=1, compression="snappy")out_parts.append(out_part)yield [f"[{_ts()}] {short}: shard {shard_idx+1} written "f"({len(shard_rows)} rows, {out_part.stat().st_size//1024} KB)"], None# rename with final shard countn_shards = len(out_parts)final: list[Path] = []for i, p in enumerate(out_parts):dst = out_data_dir / f"train-{i:05d}-of-{n_shards:05d}.parquet"p.rename(dst)final.append(dst)# uploadyield [f"[{_ts()}] {short}: uploading {n_shards} shard(s) → {out_repo}…"], Nonet_up = time.time()api.upload_folder(folder_path=str(out_data_dir),7 unmodified linesf"contaminated={stats['contaminated']})"),)yield [f"[{_ts()}] {short}: ✓ uploaded in {time.time()-t_up:.1f}s "f"→ https://huggingface.co/datasets/{out_repo}"], stats# ---------------------------------------------------------------------------# Gradio handler# ---------------------------------------------------------------------------def classify_repos(repos_text: str,out_suffix: str,music_threshold: float,private: bool,hf_token_input: str,progress: gr.Progress = gr.Progress(),) -> Generator[tuple[str, pd.DataFrame, str], None, None]:"""Main Gradio generator. Yields (log_text, summary_df, links_md)."""token = hf_token_input.strip() or os.environ.get("HF_TOKEN") or Nonerepos = [r.strip()for line in repos_text.replace(",", "\n").splitlines()1 unmodified lineif r and "/" in r]if not repos:yield "No valid repo IDs (expected owner/name format).", pd.DataFrame(), ""returnlog: list[str] = []summary_rows: list[dict[str, Any]] = []links: list[str] = []def _emit() -> tuple[str, pd.DataFrame, str]:log_text = "\n".join(log[-LOG_MAX_LINES:])df = pd.DataFrame(summary_rows) if summary_rows else pd.DataFrame()links_md = "## Output datasets\n" + "\n".join(links) if links else ""return log_text, df, links_mdlog.append(f"[{_ts()}] Starting: {len(repos)} repo(s), threshold={music_threshold:.2f}")log.append(f"[{_ts()}] CPUs={N_CPUS}, BATCH_SIZE={BATCH_SIZE}, "f"HF_XET={'1' if os.environ.get('HF_XET_HIGH_PERFORMANCE') else 'off'}")# Warm up modelprogress(0.0, desc="Loading AST model…")log.append(f"[{_ts()}] Loading AST model (MIT/ast-finetuned-audioset-10-10-0.4593)…")yield _emit()try:_get_ast_runtime("cpu")log.append(f"[{_ts()}] Model ready ✓")except Exception as exc:log.append(f"[{_ts()}] ✗ model load failed: {exc}")yield _emit()returnyield _emit()t_total = time.time()for repo_idx, repo_id in enumerate(repos):out_repo = _default_out_repo(repo_id, out_suffix)log.append(f"\n[{_ts()}] {'─'*60}")log.append(f"[{_ts()}] [{repo_idx+1}/{len(repos)}] {repo_id}")log.append(f"[{_ts()}] output → {out_repo}")yield _emit()t_repo = time.time()final_stats: dict[str, Any] | None = Nonefor new_lines, maybe_stats in _process_repo(repo_id=repo_id,out_repo=out_repo,music_threshold=music_threshold,3 unmodified lineslog.extend(new_lines)if maybe_stats is not None:final_stats = maybe_stats# Update progress bar (rough estimate by chunk count)total_done = sum(r.get("total", 0) for r in summary_rows)if final_stats:total_done += final_stats.get("total", 0)progress((repo_idx + min((final_stats or {}).get("total", 0) / max((final_stats or {}).get("total", 1), 1), 1.0)) / len(repos),desc=f"[{repo_idx+1}/{len(repos)}] {repo_id.split('/')[-1]}",)yield _emit()# Build summary rowif final_stats and "error" not in final_stats:total = final_stats["total"]cont = final_stats["contaminated"]summary_rows.append({"repo": repo_id.split("/")[-1],"chunks": total,"contam_%": f"{100*cont/max(total,1):.1f}","music": final_stats["has_music"],"technical": final_stats["technical"],"time_s": f"{time.time()-t_repo:.0f}","output": out_repo,})links.append(f"- [{out_repo}](https://huggingface.co/datasets/{out_repo})")log.append(f"[{_ts()}] {repo_id.split('/')[-1]}: DONE "f"total={total} contaminated={cont} ({100*cont/max(total,1):.1f}%) "f"music={final_stats['has_music']} technical={final_stats['technical']} "f"elapsed={time.time()-t_repo:.0f}s")elif final_stats:err = final_stats.get("error", "unknown error")summary_rows.append({"repo": repo_id.split("/")[-1], "chunks": 0,"contam_%": "—", "music": 0, "technical": 0, "time_s": "—","output": f"ERROR: {err}",})log.append(f"[{_ts()}] {repo_id.split('/')[-1]}: ✗ {err}")progress((repo_idx + 1) / len(repos), desc=f"Done {repo_idx+1}/{len(repos)}")yield _emit()log.append(f"\n[{_ts()}] {'='*60}")log.append(f"[{_ts()}] All done in {time.time()-t_total:.0f}s")yield _emit()# ---------------------------------------------------------------------------7 unmodified lines[MIT/ast-finetuned-audioset-10-10-0.4593](https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593)(527 AudioSet classes, multi-label sigmoid).**Input** — chunk dataset repo IDs (one per line, `owner/name` format).**Output** — `<input>_classified` with: `id · audio · music_score · has_music · contaminated · flags · top_labels · rms_db · clipping_ratio · max_silence_sec`.Sort by `music_score` ↓ in the HF viewer and press play to spot-check flagged chunks."""with gr.Blocks(title="Chunk Classifier") as demo:4 unmodified linesrepos_input = gr.Textbox(label="Dataset repo IDs (one per line)",placeholder="fosters/my-book-chunks\nfosters/another-book-chunks",lines=6,)with gr.Column(scale=1):out_suffix = gr.Textbox(3 unmodified linesthreshold = gr.Slider(0.10, 0.60, value=DEFAULT_THRESHOLD, step=0.05,label="Music threshold",info="Lower = more sensitive. Default 0.25 biased to recall.",)private_toggle = gr.Checkbox(label="Private output", value=True)token_input = gr.Textbox(2 unmodified lines)run_btn = gr.Button("▶ Classify", variant="primary", size="lg")with gr.Row():with gr.Column(scale=3):log_out = gr.Textbox(label="Processing log (per-chunk detail, updates after every batch)",interactive=False,lines=30,max_lines=60,)with gr.Column(scale=2):summary_out = gr.Dataframe(label="Per-repo summary",headers=["repo", "chunks", "contam_%", "music", "technical", "time_s", "output"],wrap=True,)2 unmodified linesrun_btn.click(classify_repos,inputs=[repos_input, out_suffix, threshold, private_toggle, token_input],outputs=[log_out, summary_out, links_out],)demo.queue()1 unmodified line23456714 unmodified lines222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798 unmodified lines88899091929394959610 unmodified lines1071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691 unmodified line1711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022037 unmodified lines2112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422431 unmodified line2452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283297 unmodified lines3373383393403413423433443453463473483493503513523533543553563573583593603613623631 unmodified line3653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074083 unmodified lines4124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494507 unmodified lines4584594604614624634644654664 unmodified lines4714724734744754764773 unmodified lines4814824834844854864872 unmodified lines4904914924934944954964974984995005015025035045055065075085095105115125135142 unmodified lines5175185195205215225231 unmodified lineClassifies audio chunks in one or more HF datasets using the AST AudioSet model.Writes a ``<input>_classified`` dataset with id + audio + classification columns only."""from __future__ import annotations14 unmodified linesimport pandas as pdimport pyarrow as paimport pyarrow.parquet as pqimport requestsimport torchfrom huggingface_hub import HfApi, hf_hub_downloadfrom music_detector import _get_ast_runtime, judge_chunk_files_batched# ---------------------------------------------------------------------------# Constants# ---------------------------------------------------------------------------N_CPUS = os.cpu_count() or 2torch.set_num_threads(N_CPUS)BATCH_SIZE = 32DEFAULT_THRESHOLD = 0.25DEFAULT_NAMESPACE = "fosters"LOG_MAX_LINES = 600DATASETS_SERVER = "https://datasets-server.huggingface.co"_CLASSIFIER_FIELDS = [pa.field("music_score", pa.float32()),pa.field("has_music", pa.bool_()),pa.field("contaminated", pa.bool_()),pa.field("flags", pa.string()),pa.field("top_labels", pa.string()),pa.field("rms_db", pa.float32()),pa.field("clipping_ratio", pa.float32()),pa.field("max_silence_sec", pa.float32()),]_HF_FEATURES = {"id": {"_type": "Value", "dtype": "string"},"audio": {"_type": "Audio"},"music_score": {"_type": "Value", "dtype": "float32"},"has_music": {"_type": "Value", "dtype": "bool"},"contaminated": {"_type": "Value", "dtype": "bool"},"flags": {"_type": "Value", "dtype": "string"},"top_labels": {"_type": "Value", "dtype": "string"},"rms_db": {"_type": "Value", "dtype": "float32"},"clipping_ratio": {"_type": "Value", "dtype": "float32"},"max_silence_sec": {"_type": "Value", "dtype": "float32"},}_AUDIO_PA_TYPE = pa.struct([pa.field("bytes", pa.binary()), pa.field("path", pa.string())])_OUTPUT_SCHEMA = pa.schema([pa.field("id", pa.string()),pa.field("audio", _AUDIO_PA_TYPE),*_CLASSIFIER_FIELDS,],metadata={"huggingface": json.dumps({"info": {"features": _HF_FEATURES}})},)8 unmodified linesdef _default_out_repo(in_repo: str, suffix: str) -> str:parts = in_repo.split("/")ns = parts[0] if len(parts) > 1 else DEFAULT_NAMESPACEname = parts[-1]suf = suffix.strip()if not suf.startswith("_"):suf = "_" + sufreturn f"{ns}/{name}{suf}"10 unmodified lines)def _fetch_total_rows(repo_id: str, token: str | None) -> int | None:"""Get total row count via datasets-server (no download required)."""try:headers = {"Authorization": f"Bearer {token}"} if token else {}r = requests.get(f"{DATASETS_SERVER}/info",params={"dataset": repo_id, "config": "default"},headers=headers,timeout=20,)if r.ok:splits = r.json().get("dataset_info", {}).get("default", {}).get("splits", {})return splits.get("train", {}).get("num_examples")except Exception:passreturn Nonedef _rows_to_table(rows: list[dict[str, Any]]) -> pa.Table:return pa.table({"id": pa.array([r["id"] for r in rows], type=pa.string()),"audio": pa.array([{"bytes": r["audio_bytes"], "path": r["audio_path"]} for r in rows],type=_AUDIO_PA_TYPE,),"music_score": pa.array([r["music_score"] for r in rows], type=pa.float32()),"has_music": pa.array([r["has_music"] for r in rows], type=pa.bool_()),"contaminated": pa.array([r["contaminated"] for r in rows], type=pa.bool_()),"flags": pa.array([r["flags"] for r in rows], type=pa.string()),"top_labels": pa.array([r["top_labels"] for r in rows], type=pa.string()),"rms_db": pa.array([r["rms_db"] for r in rows], type=pa.float32()),"clipping_ratio": pa.array([r["clipping_ratio"] for r in rows], type=pa.float32()),"max_silence_sec": pa.array([r["max_silence_sec"] for r in rows], type=pa.float32()),},schema=_OUTPUT_SCHEMA,)def _fmt_chunk(chunk_id: str, v: Any) -> str: # v: ChunkVerdictif v.has_music:icon = "🔴"elif v.flags:icon = "⚠️"else:icon = "✅"labels = ", ".join(v.top_labels[:2])flags = f" [{', '.join(v.flags)}]" if v.flags else ""return f" {icon} {chunk_id} score={v.music_score:.3f} {labels}{flags}"# ---------------------------------------------------------------------------# Per-repo processing generator# ---------------------------------------------------------------------------# Yields: (new_log_lines: list[str], status: str, chunks_done: int, stats: dict | None)# chunks_done counts chunks processed so far in this repo (for caller progress math)def _process_repo(repo_id: str,1 unmodified linemusic_threshold: float,private: bool,token: str | None,) -> Generator[tuple[list[str], str, int, dict[str, Any] | None], None, None]:api = HfApi(token=token)short = repo_id.split("/")[-1]# List shardsyield [f"[{_ts()}] {short}: listing shards…"], "Listing shards…", 0, Nonetry:shard_names = _list_parquet_shards(repo_id, token)except Exception as exc:err = str(exc)yield [f"[{_ts()}] {short}: ✗ {err}"], f"Error: {err}", 0, {"repo": repo_id, "error": err}returnif not shard_names:msg = "no data/train-*.parquet shards found"yield [f"[{_ts()}] {short}: ✗ {msg}"], f"Error: {msg}", 0, {"repo": repo_id, "error": msg}return# Get total row count (for progress display, not critical)total_rows = _fetch_total_rows(repo_id, token)total_str = str(total_rows) if total_rows else "?"yield [f"[{_ts()}] {short}: {len(shard_names)} shard(s), {total_str} rows total"], f"Preparing… {len(shard_names)} shards, {total_str} chunks", 0, Noneapi.create_repo(repo_id=out_repo, repo_type="dataset", exist_ok=True, private=private)stats: dict[str, Any] = {7 unmodified linesout_parts: list[Path] = []for shard_idx, shard_name in enumerate(shard_names):yield [f"[{_ts()}] {short}: shard {shard_idx+1}/{len(shard_names)} — downloading…"], f"[{shard_idx+1}/{len(shard_names)}] downloading shard…", stats["total"], Nonet_dl = time.time()try:shard_path = hf_hub_download(repo_id=repo_id, filename=shard_name,repo_type="dataset", token=token,)except Exception as exc:yield [f"[{_ts()}] {short}: ✗ download failed: {exc}"], "Download error", stats["total"], Nonecontinuein_table = pq.read_table(shard_path)n_rows = len(in_table)audio_col = in_table.column("audio")id_col = in_table.column("id").to_pylist()n_batches = (n_rows + BATCH_SIZE - 1) // BATCH_SIZEyield [f"[{_ts()}] {short}: shard {shard_idx+1}/{len(shard_names)} — "f"{n_rows} rows, downloaded in {time.time()-t_dl:.1f}s"], (f"[{shard_idx+1}/{len(shard_names)}] {n_rows} rows — starting inference…"), stats["total"], Noneshard_rows: list[dict[str, Any]] = []for batch_idx in range(n_batches):1 unmodified lineb_end = min(b_start + BATCH_SIZE, n_rows)t_batch = time.time()batch_ids: list[str] = id_col[b_start:b_end]batch_audio: list[dict] = [(audio_col[i].as_py() or {}) for i in range(b_start, b_end)]batch_bytes: list[bytes] = [a.get("bytes") or b"" for a in batch_audio]batch_paths: list[str] = [a.get("path") or "chunk.mp3" for a in batch_audio]with tempfile.TemporaryDirectory() as audio_tmp:files: list[Path] = []for k, (ab, ph) in enumerate(zip(batch_bytes, batch_paths)):p = Path(audio_tmp) / f"c{k:04d}{Path(ph).suffix or '.mp3'}"p.write_bytes(ab)files.append(p)verdicts = judge_chunk_files_batched(files,music_threshold=music_threshold,batch_size=len(files),device="cpu",)for cid, ab, ph, v in zip(batch_ids, batch_bytes, batch_paths, verdicts):shard_rows.append({"id": cid, "audio_bytes": ab, "audio_path": ph,"music_score": v.music_score,"has_music": v.has_music,"contaminated": v.contaminated,"flags": ", ".join(v.flags),"top_labels": ", ".join(v.top_labels[:3]),"rms_db": v.rms_db,"clipping_ratio": v.clipping_ratio,"max_silence_sec": v.max_silence_sec,})n_cont = sum(v.contaminated for v in verdicts)n_music = sum(v.has_music for v in verdicts)n_tech = sum(bool(v.flags) and not v.has_music for v in verdicts)stats["total"] += b_end - b_startstats["contaminated"] += n_contstats["has_music"] += n_musicstats["technical"] += n_techelapsed = time.time() - t_batchdone = stats["total"]cont_pct = 100 * stats["contaminated"] / max(done, 1)# Build log lines: one per chunk + batch summarychunk_lines = [_fmt_chunk(cid, v) for cid, v in zip(batch_ids, verdicts)]summary_line = (f"[{_ts()}] batch {batch_idx+1}/{n_batches} "f"· shard {shard_idx+1}/{len(shard_names)} "f"· {elapsed:.1f}s "f"· total {done}/{total_str} chunks "f"· contaminated {stats['contaminated']} ({cont_pct:.1f}%)")status = (f"[{shard_idx+1}/{len(shard_names)}] "f"batch {batch_idx+1}/{n_batches} "f"· {done}/{total_str} chunks "f"· {cont_pct:.1f}% contaminated")yield [""] + chunk_lines + [summary_line], status, done, None# Write shardout_table = _rows_to_table(shard_rows)out_part = out_data_dir / f"part_{shard_idx:05d}.parquet"pq.write_table(out_table, out_part, row_group_size=1, compression="snappy")out_parts.append(out_part)yield [f"[{_ts()}] {short}: shard {shard_idx+1} written "f"({len(shard_rows)} rows, {out_part.stat().st_size // 1024} KB)"], f"Shard {shard_idx+1} written, uploading…", stats["total"], None# Rename + uploadn_shards = len(out_parts)for i, p in enumerate(out_parts):p.rename(out_data_dir / f"train-{i:05d}-of-{n_shards:05d}.parquet")yield [f"[{_ts()}] {short}: uploading {n_shards} shard(s) → {out_repo}…"], "Uploading…", stats["total"], Nonet_up = time.time()api.upload_folder(folder_path=str(out_data_dir),7 unmodified linesf"contaminated={stats['contaminated']})"),)url = f"https://huggingface.co/datasets/{out_repo}"yield [f"[{_ts()}] {short}: ✓ uploaded in {time.time()-t_up:.1f}s → {url}"], f"Done ✓ → {out_repo}", stats["total"], stats# ---------------------------------------------------------------------------# Gradio generator (no gr.Progress — avoids overlay)# ---------------------------------------------------------------------------# Outputs: log_text, status_text, summary_df, links_mddef classify_repos(repos_text: str,out_suffix: str,music_threshold: float,private: bool,hf_token_input: str,) -> Generator[tuple[str, str, pd.DataFrame, str], None, None]:token = hf_token_input.strip() or os.environ.get("HF_TOKEN") or Nonerepos = [r.strip()for line in repos_text.replace(",", "\n").splitlines()1 unmodified lineif r and "/" in r]if not repos:yield "No valid repo IDs (expected owner/name format).", "", pd.DataFrame(), ""returnlog: list[str] = []rows: list[dict[str, Any]] = []links: list[str] = []def _snapshot(status: str) -> tuple[str, str, pd.DataFrame, str]:log_text = "\n".join(log[-LOG_MAX_LINES:])df = pd.DataFrame(rows) if rows else pd.DataFrame()links_md = "## Output datasets\n" + "\n".join(links) if links else ""return log_text, status, df, links_mdlog.append(f"[{_ts()}] {len(repos)} repo(s) · threshold={music_threshold:.2f} · CPUs={N_CPUS}")log.append(f"[{_ts()}] HF_XET_HIGH_PERFORMANCE={os.environ.get('HF_XET_HIGH_PERFORMANCE','off')}")log.append(f"[{_ts()}] Loading AST model…")yield _snapshot("Loading AST model…")try:_get_ast_runtime("cpu")log.append(f"[{_ts()}] Model ready ✓")except Exception as exc:log.append(f"[{_ts()}] ✗ model load failed: {exc}")yield _snapshot(f"Error: {exc}")returnyield _snapshot("Model ready")t_total = time.time()for repo_idx, repo_id in enumerate(repos):out_repo = _default_out_repo(repo_id, out_suffix)log.append(f"\n[{_ts()}] {'─'*56}")log.append(f"[{_ts()}] [{repo_idx+1}/{len(repos)}] {repo_id} → {out_repo}")t_repo = time.time()final_stats: dict[str, Any] | None = Nonefor new_lines, status, chunks_done, maybe_stats in _process_repo(repo_id=repo_id,out_repo=out_repo,music_threshold=music_threshold,3 unmodified lineslog.extend(new_lines)if maybe_stats is not None:final_stats = maybe_statsyield _snapshot(f"[{repo_idx+1}/{len(repos)}] {status}")# Summary rowif final_stats and "error" not in final_stats:n = final_stats["total"]cont = final_stats["contaminated"]rows.append({"repo": repo_id.split("/")[-1],"chunks": n,"contam_%": f"{100*cont/max(n,1):.1f}","music": final_stats["has_music"],"technical": final_stats["technical"],"time_s": f"{time.time()-t_repo:.0f}","output": out_repo,})links.append(f"- [{out_repo}](https://huggingface.co/datasets/{out_repo})")log.append(f"[{_ts()}] DONE total={n} contaminated={cont} ({100*cont/max(n,1):.1f}%) "f"music={final_stats['has_music']} technical={final_stats['technical']} "f"elapsed={time.time()-t_repo:.0f}s")elif final_stats:rows.append({"repo": repo_id.split("/")[-1], "chunks": 0,"contam_%": "—", "music": 0, "technical": 0,"time_s": "—", "output": f"ERROR: {final_stats.get('error')}",})yield _snapshot(f"[{repo_idx+1}/{len(repos)}] done")log.append(f"\n[{_ts()}] {'='*56}")log.append(f"[{_ts()}] All done in {time.time()-t_total:.0f}s")yield _snapshot("All done ✓")# ---------------------------------------------------------------------------7 unmodified lines[MIT/ast-finetuned-audioset-10-10-0.4593](https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593)(527 AudioSet classes, multi-label sigmoid).**Input** — chunk dataset repo IDs (one per line).**Output** — `<input>_classified` with `id · audio · music_score · has_music · contaminated · flags · top_labels · rms_db · clipping_ratio · max_silence_sec`.Sort by `music_score ↓` in the HF viewer and press play to spot-check."""with gr.Blocks(title="Chunk Classifier") as demo:4 unmodified linesrepos_input = gr.Textbox(label="Dataset repo IDs (one per line)",placeholder="fosters/my-book-chunks\nfosters/another-book-chunks",lines=5,)with gr.Column(scale=1):out_suffix = gr.Textbox(3 unmodified linesthreshold = gr.Slider(0.10, 0.60, value=DEFAULT_THRESHOLD, step=0.05,label="Music threshold",info="Lower = more sensitive (recall-biased). Default 0.25.",)private_toggle = gr.Checkbox(label="Private output", value=True)token_input = gr.Textbox(2 unmodified lines)run_btn = gr.Button("▶ Classify", variant="primary", size="lg")# Status line — replaces gr.Progress() which caused overlay issuesstatus_out = gr.Textbox(label="Status",interactive=False,lines=1,max_lines=1,)with gr.Row():with gr.Column(scale=3):log_out = gr.Textbox(label="Processing log (updates every batch ~30s)",interactive=False,lines=28,max_lines=60,)with gr.Column(scale=2):summary_out = gr.Dataframe(label="Summary",headers=["repo", "chunks", "contam_%", "music", "technical", "time_s", "output"],wrap=True,)2 unmodified linesrun_btn.click(classify_repos,inputs=[repos_input, out_suffix, threshold, private_toggle, token_input],outputs=[log_out, status_out, summary_out, links_out],)demo.queue()
app.py+215-216