Spaces:
Running on Zero
Running on Zero
Audio Compress: sample real audio to report sample rate / channels / codec
Browse files- features/audio_compress.py +90 -6
features/audio_compress.py
CHANGED
|
@@ -278,9 +278,11 @@ def resolve_source(repo: str, config: str, token: str, revision: Optional[str])
|
|
| 278 |
first = next(iter(splits.values()))[0]
|
| 279 |
fs = HfFileSystem(token=token)
|
| 280 |
with fs.open(f"datasets/{repo}@{quote(rev, safe='')}/{first}", "rb") as fh:
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
|
|
|
|
|
|
| 284 |
|
| 285 |
return {
|
| 286 |
"repo": repo,
|
|
@@ -292,9 +294,85 @@ def resolve_source(repo: str, config: str, token: str, revision: Optional[str])
|
|
| 292 |
"rows": rows,
|
| 293 |
"features": features,
|
| 294 |
"audio_cols": audio_cols,
|
|
|
|
| 295 |
}
|
| 296 |
|
| 297 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
def features_from_schema(schema: pa.Schema) -> dict:
|
| 299 |
"""Features (as a dict) from the parquet `huggingface` metadata, falling back to the arrow schema."""
|
| 300 |
raw = (schema.metadata or {}).get(b"huggingface")
|
|
@@ -329,8 +407,9 @@ def source_summary_md(meta: dict) -> str:
|
|
| 329 |
lines.append(f"| `{split}` | {len(files):,} | {human_size(size) if size else '?'} | {f'{rows:,}' if rows else '?'} |")
|
| 330 |
lines.append("")
|
| 331 |
if meta["audio_cols"]:
|
| 332 |
-
|
| 333 |
-
|
|
|
|
| 334 |
else:
|
| 335 |
lines.append("- ❌ **No `Audio` column found** in this subset.")
|
| 336 |
others = [c for c in meta["features"] if c not in meta["audio_cols"]]
|
|
@@ -556,7 +635,12 @@ def load_config(oauth_token: Optional[gr.OAuthToken], session_token: Optional[st
|
|
| 556 |
return *empty, f"❌ Cannot read subset `{config}`:\n\n```\n{type(exc).__name__}: {exc}\n```"
|
| 557 |
splits = list(meta["splits"])
|
| 558 |
audio = list(meta["audio_cols"])
|
| 559 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 560 |
return (
|
| 561 |
gr.update(choices=splits, value=splits[0] if splits else None),
|
| 562 |
gr.update(choices=audio, value=audio),
|
|
|
|
| 278 |
first = next(iter(splits.values()))[0]
|
| 279 |
fs = HfFileSystem(token=token)
|
| 280 |
with fs.open(f"datasets/{repo}@{quote(rev, safe='')}/{first}", "rb") as fh:
|
| 281 |
+
pf = pq.ParquetFile(fh)
|
| 282 |
+
schema = pf.schema_arrow
|
| 283 |
+
features = features_from_schema(schema)
|
| 284 |
+
audio_cols = {name: feat.get("sampling_rate") for name, feat in features.items() if feat.get("_type") == "Audio"}
|
| 285 |
+
probe = {col: probe_audio(pf, col) for col in audio_cols}
|
| 286 |
|
| 287 |
return {
|
| 288 |
"repo": repo,
|
|
|
|
| 294 |
"rows": rows,
|
| 295 |
"features": features,
|
| 296 |
"audio_cols": audio_cols,
|
| 297 |
+
"probe": probe,
|
| 298 |
}
|
| 299 |
|
| 300 |
|
| 301 |
+
#: Max compressed size of the column chunk read to sample the audio (keeps Load fast).
|
| 302 |
+
PROBE_MAX_BYTES = 64 * 1024 * 1024
|
| 303 |
+
PROBE_ROWS = 8
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
def probe_audio(pf: pq.ParquetFile, col: str) -> dict:
|
| 307 |
+
"""Read a few audio values of the first row group and inspect them with `soundfile.info`.
|
| 308 |
+
|
| 309 |
+
Many datasets declare `Audio()` without a sampling rate (audio kept at its native rate),
|
| 310 |
+
so the metadata alone cannot tell the sample rate / channels / codec.
|
| 311 |
+
"""
|
| 312 |
+
try:
|
| 313 |
+
if pf.metadata.num_row_groups == 0:
|
| 314 |
+
return {"error": "empty shard"}
|
| 315 |
+
rg = pf.metadata.row_group(0)
|
| 316 |
+
idx = pf.schema_arrow.get_field_index(col)
|
| 317 |
+
chunk = sum(
|
| 318 |
+
rg.column(i).total_compressed_size
|
| 319 |
+
for i in range(rg.num_columns)
|
| 320 |
+
if rg.column(i).path_in_schema.split(".")[0] == col
|
| 321 |
+
) if idx >= 0 else 0
|
| 322 |
+
if chunk > PROBE_MAX_BYTES:
|
| 323 |
+
return {"error": f"first row group too large to sample ({human_size(chunk)})"}
|
| 324 |
+
values = pf.read_row_group(0, columns=[col]).column(col).to_pylist()
|
| 325 |
+
except Exception as exc: # noqa: BLE001
|
| 326 |
+
return {"error": f"{type(exc).__name__}: {exc}"}
|
| 327 |
+
|
| 328 |
+
rates, channels, codecs, durations, errors = set(), set(), set(), [], []
|
| 329 |
+
for value in [v for v in values if v and v.get("bytes")][:PROBE_ROWS]:
|
| 330 |
+
try:
|
| 331 |
+
info = sf.info(io.BytesIO(value["bytes"]))
|
| 332 |
+
except Exception: # noqa: BLE001
|
| 333 |
+
ext = PurePosixPath(value.get("path") or "").suffix or "unknown format"
|
| 334 |
+
errors.append(ext)
|
| 335 |
+
continue
|
| 336 |
+
rates.add(info.samplerate)
|
| 337 |
+
channels.add(info.channels)
|
| 338 |
+
codecs.add(f"{info.format} {info.subtype}")
|
| 339 |
+
durations.append(info.duration)
|
| 340 |
+
if not rates and not errors:
|
| 341 |
+
return {"error": "no embedded audio bytes in the sampled rows"}
|
| 342 |
+
return {
|
| 343 |
+
"rates": sorted(rates),
|
| 344 |
+
"channels": sorted(channels),
|
| 345 |
+
"codecs": sorted(codecs),
|
| 346 |
+
"avg_seconds": sum(durations) / len(durations) if durations else None,
|
| 347 |
+
"sampled": len(durations) + len(errors),
|
| 348 |
+
"undecodable": errors,
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
def describe_audio(col: str, declared_sr: Optional[int], probe: Optional[dict]) -> str:
|
| 353 |
+
"""One line describing an audio column: declared metadata + what the samples contain."""
|
| 354 |
+
declared = f"declared {declared_sr:,} Hz" if declared_sr else "no sampling_rate declared"
|
| 355 |
+
probe = probe or {}
|
| 356 |
+
if probe.get("error"):
|
| 357 |
+
return f"`{col}` — {declared}; could not sample the audio ({probe['error']})"
|
| 358 |
+
parts = []
|
| 359 |
+
if probe.get("rates"):
|
| 360 |
+
parts.append(" / ".join(f"{sr:,} Hz" for sr in probe["rates"]))
|
| 361 |
+
if probe.get("channels"):
|
| 362 |
+
parts.append(" / ".join({1: "mono", 2: "stereo"}.get(c, f"{c} ch") for c in probe["channels"]))
|
| 363 |
+
if probe.get("codecs"):
|
| 364 |
+
parts.append(", ".join(probe["codecs"]))
|
| 365 |
+
if probe.get("avg_seconds"):
|
| 366 |
+
parts.append(f"~{probe['avg_seconds']:.1f} s / clip")
|
| 367 |
+
line = f"`{col}` — {' · '.join(parts)} _(sampled {probe.get('sampled', 0)} rows; {declared})_"
|
| 368 |
+
if probe.get("undecodable"):
|
| 369 |
+
line += (
|
| 370 |
+
f"\n - ⚠️ {len(probe['undecodable'])} sampled clip(s) cannot be decoded by libsndfile "
|
| 371 |
+
f"({', '.join(sorted(set(probe['undecodable'])))}) — the job would fail on them."
|
| 372 |
+
)
|
| 373 |
+
return line
|
| 374 |
+
|
| 375 |
+
|
| 376 |
def features_from_schema(schema: pa.Schema) -> dict:
|
| 377 |
"""Features (as a dict) from the parquet `huggingface` metadata, falling back to the arrow schema."""
|
| 378 |
raw = (schema.metadata or {}).get(b"huggingface")
|
|
|
|
| 407 |
lines.append(f"| `{split}` | {len(files):,} | {human_size(size) if size else '?'} | {f'{rows:,}' if rows else '?'} |")
|
| 408 |
lines.append("")
|
| 409 |
if meta["audio_cols"]:
|
| 410 |
+
lines.append("- 🎧 **Audio columns:**")
|
| 411 |
+
for col, sr in meta["audio_cols"].items():
|
| 412 |
+
lines.append(f" - {describe_audio(col, sr, meta.get('probe', {}).get(col))}")
|
| 413 |
else:
|
| 414 |
lines.append("- ❌ **No `Audio` column found** in this subset.")
|
| 415 |
others = [c for c in meta["features"] if c not in meta["audio_cols"]]
|
|
|
|
| 635 |
return *empty, f"❌ Cannot read subset `{config}`:\n\n```\n{type(exc).__name__}: {exc}\n```"
|
| 636 |
splits = list(meta["splits"])
|
| 637 |
audio = list(meta["audio_cols"])
|
| 638 |
+
# For the estimate: declared rate, else the most common sampled rate / channel count.
|
| 639 |
+
probes = [p for p in meta.get("probe", {}).values() if p.get("rates")]
|
| 640 |
+
meta["sampling_rate"] = next((sr for sr in meta["audio_cols"].values() if sr), None) or (
|
| 641 |
+
max(probes[0]["rates"]) if probes else None
|
| 642 |
+
)
|
| 643 |
+
meta["channels"] = max(probes[0]["channels"]) if probes else None
|
| 644 |
return (
|
| 645 |
gr.update(choices=splits, value=splits[0] if splits else None),
|
| 646 |
gr.update(choices=audio, value=audio),
|