Datasets:
File size: 19,446 Bytes
636201a | 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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 | # /// script
# requires-python = ">=3.13"
# dependencies = [
# "beautifulsoup4>=4.13",
# "charset-normalizer>=3.4",
# "pyarrow>=19",
# "pypdf>=5",
# ]
# ///
"""Build a Hugging Face dataset from the downloaded transcript archive.
Run with uv so the script's optional dependencies are installed automatically.
This script reads SQLite metadata without changing the database and never uploads.
"""
from __future__ import annotations
import argparse
import hashlib
import json
import re
import shutil
import sqlite3
import tarfile
from collections import Counter
from contextlib import closing
from datetime import UTC, datetime
from html import unescape
from pathlib import Path
from typing import TYPE_CHECKING, cast
from xml.etree import ElementTree
import pyarrow as pa
import pyarrow.parquet as pq
from bs4 import BeautifulSoup
from charset_normalizer import from_bytes
from pypdf import PdfReader
from pypdf.errors import PyPdfError
if TYPE_CHECKING:
from collections.abc import Iterator
type Record = dict[str, str | None]
_TIMING = re.compile(r"^\s*(?:\d+:)?\d{2}:\d{2}[.,]\d+\s*-->\s*", re.MULTILINE)
_SCHEMA = pa.schema(
[
pa.field(name, pa.string())
for name in (
"id",
"podcast",
"episode",
"text",
"source_url",
"feed_url",
"episode_url",
"published_at",
"format",
"raw_path",
"sha256",
"extraction_status",
"extraction_method",
"extraction_error",
)
],
)
_BATCH_SIZE = 100
_MIN_HTML_TRANSCRIPT_LENGTH = 200
def normalize_text(text: str) -> str:
return "\n".join(
cleaned for line in text.splitlines() if (cleaned := " ".join(line.split()))
)
def decode_text(content: bytes) -> str:
try:
return content.decode("utf-8-sig")
except (UnicodeError,): # noqa: B013
if match := from_bytes(content).best():
return str(match)
msg = "Could not detect the text encoding"
raise ValueError(msg) from None
def text_format(text: str, suffix: str) -> str:
start = text.lstrip()
if start.startswith("WEBVTT"):
return "vtt"
if _TIMING.search(text):
return "srt"
if suffix == ".json" or (start.startswith("{") and '"segments"' in start[:2000]):
return "json"
if start.startswith("<"):
return (
"podlove"
if "http://podlove.org/simple-transcripts" in text[:2000]
else "html"
)
if "\x00" in text:
msg = "Unsupported binary content"
raise ValueError(msg)
return "txt"
def detect_format(content: bytes, suffix: str) -> tuple[str, str]:
if content.startswith(b"%PDF-") or suffix.lower() == ".pdf":
return "pdf", ""
text = decode_text(content)
return text_format(text, suffix.lower()), text
def subtitle_text(text: str) -> str:
cues: list[str] = []
for block in re.split(r"\n\s*\n", text.replace("\r\n", "\n")):
lines = block.splitlines()
if (
timing_index := next(
(index for index, line in enumerate(lines) if _TIMING.match(line)),
None,
)
) is None:
continue
cue = " ".join(lines[timing_index + 1 :])
cue = re.sub(r"<v(?:\.[^ >]+)?\s+([^>]+)>", r"\1: ", cue)
cue = re.sub(r"<[^>]+>", "", cue)
if cleaned := normalize_text(unescape(cue)):
cues.append(cleaned)
if not cues:
msg = "No subtitle cues found"
raise ValueError(msg)
return "\n".join(cues)
def json_segment_text(segment: object) -> str:
if not isinstance(segment, dict):
msg = "A transcript segment is not an object"
raise TypeError(msg)
fields = cast("dict[str, object]", segment)
body = fields.get("body", fields.get("text"))
if not isinstance(body, str):
msg = "A transcript segment has no body or text"
raise TypeError(msg)
if isinstance(speaker := fields.get("speaker"), str) and speaker:
return f"{speaker}: {body}"
return body
def json_text(text: str) -> str:
data: object = json.loads(text)
if isinstance(data, dict):
fields = cast("dict[str, object]", data)
if isinstance(body := fields.get("text"), str) and body.strip():
return body
data = fields.get("segments")
if isinstance(data, list) and data:
return "\n".join(json_segment_text(segment) for segment in data)
msg = "No transcript text or segments found in JSON"
raise ValueError(msg)
def html_text(text: str) -> tuple[str, str]:
soup = BeautifulSoup(text, "html.parser")
for element in soup.select(
"script, style, nav, header, footer, aside, noscript, form, button, "
"svg, iframe, time",
):
element.decompose()
candidates = soup.select(
'[itemprop="transcript"], [id*="transcript"], [class*="transcript"]',
)
if (
candidates
and len(
body := max(
(element.get_text("\n", strip=True) for element in candidates),
key=len,
),
)
>= _MIN_HTML_TRANSCRIPT_LENGTH
):
return body, "html_transcript"
if article := soup.select_one("article, main, [role='main']"):
return article.get_text("\n", strip=True), "html_article"
return soup.get_text("\n", strip=True), "html_body"
def extract_text(path: Path, content: bytes) -> tuple[str, str, str]:
file_format, text = detect_format(content, path.suffix)
method = file_format
match file_format:
case "pdf":
text = "\n".join(
page.extract_text() or "" for page in PdfReader(path).pages
)
case "srt" | "vtt":
text = subtitle_text(text)
case "json":
text = json_text(text)
case "podlove":
root = ElementTree.fromstring(text)
text = "\n".join(
"".join(item.itertext())
for item in root.iter()
if item.tag.rsplit("}", 1)[-1] == "item"
)
case "html":
text, method = html_text(text)
if not (text := normalize_text(text)):
msg = "No readable transcript text extracted"
raise ValueError(msg)
return text, file_format, method
def archive_files(source: Path) -> Iterator[Path]:
for path in sorted(source.rglob("*")):
if path.is_file() and not any(
part in {".DS_Store", ".git", ".cache", "__pycache__"}
or part.startswith("._")
for part in path.relative_to(source).parts
):
if path.is_symlink():
msg = f"Symlinked transcript is not supported: {path}"
raise ValueError(msg)
yield path
def load_metadata(database: Path | None) -> dict[str, Record]:
if database is None:
return {}
metadata: dict[str, Record] = {}
with closing(
sqlite3.connect(f"{database.resolve().as_uri()}?mode=ro", uri=True),
) as connection:
connection.row_factory = sqlite3.Row
rows = connection.execute(
"SELECT e.transcriptDownloadPath AS path, e.title AS episode, "
'f.title AS podcast, e."podcast:transcript:url" AS source_url, '
"e.feedXmlUrl AS feed_url, e.link AS episode_url, "
"e.pubDate AS published_at FROM episodes_extended AS e "
"LEFT JOIN feeds_extended AS f ON f.xmlUrl = e.feedXmlUrl "
"WHERE e.transcriptDownloadPath IS NOT NULL ORDER BY e.enclosureUrl",
)
for row in rows:
if not isinstance(stored_path := row["path"], str):
continue
key = Path(*Path(stored_path).parts[-2:]).as_posix()
metadata.setdefault(
key,
{
name: value if isinstance(value := row[name], str) else None
for name in (
"podcast",
"episode",
"source_url",
"feed_url",
"episode_url",
"published_at",
)
},
)
return metadata
def transcript_record(path: Path, source: Path, metadata: dict[str, Record]) -> Record:
relative = path.relative_to(source).as_posix()
content = path.read_bytes()
record: Record = {
"id": hashlib.sha256(relative.encode()).hexdigest(),
"podcast": path.parent.name,
"episode": path.stem if path.suffix else path.name,
"text": None,
"source_url": None,
"feed_url": None,
"episode_url": None,
"published_at": None,
"format": path.suffix.lstrip(".") or "unknown",
"raw_path": f"raw/{relative}",
"sha256": hashlib.sha256(content).hexdigest(),
"extraction_status": "ok",
"extraction_method": None,
"extraction_error": None,
}
record.update(
{key: value for key, value in metadata.get(relative, {}).items() if value},
)
try:
record["text"], record["format"], record["extraction_method"] = extract_text(
path,
content,
)
except (
OSError,
ValueError,
TypeError,
UnicodeError,
ElementTree.ParseError,
PyPdfError,
) as error:
record["extraction_status"] = "error"
record["extraction_error"] = f"{type(error).__name__}: {error}"
return record
def dataset_card(summary: dict[str, object], repo_id: str) -> str:
return f"""---
pretty_name: Podcast Transcripts
tags:
- podcasts
- transcripts
configs:
- config_name: default
data_files:
- split: train
path: data/train.parquet
---
# Podcast Transcripts
Downloaded podcast transcripts, with original files and extracted text. Generated
with [overcast-to-sqlite](https://github.com/hbmartin/overcast-to-sqlite) on
{summary['generated_at']}. This snapshot contains {summary['files']} archived files,
of which {summary['extracted']} have extracted text and {summary['failed']} have
extraction errors. `{summary['metadata_matched']}` rows have original source URLs.
## Contents and schema
`data/train.parquet` contains one row per archived file. `raw-transcripts.tar.gz`
preserves all original files under `raw/`, including their directory structure.
Multiple files can describe the same
episode, and identical content can occur in different rows. `train` is a single
collection, not a curated training/evaluation split.
| Column | Meaning |
| --- | --- |
| id | SHA-256 of the relative archive path; stable for a given path |
| podcast / episode | SQLite titles when matched, otherwise directory/filename |
| text | Extracted UTF-8 text; null when extraction fails |
| source_url | Original transcript URL when available |
| feed_url / episode_url | RSS feed and episode page URLs when available |
| published_at | Publication date as recorded in the feed, without date normalization |
| format | Detected content format; extension fallback for extraction failures |
| raw_path | Original file's path inside `raw-transcripts.tar.gz` |
| sha256 | SHA-256 of the original file bytes; usable for deduplication |
| extraction_status | `ok` or `error` |
| extraction_method | Text extraction strategy, including HTML selection strategy |
| extraction_error | Error description when extraction fails |
## Processing and limitations
Formats are detected from content and filename hints, including subtitle content
stored as `.bin` and PDFs stored as `.octet-stream`. Subtitle indices and timing
lines are removed; WebVTT voice labels are retained when present. JSON full text
or segments and Podlove XML items are read. PDFs use embedded text extraction
without OCR. HTML scripts and common navigation elements are removed; transcript
containers are preferred, then article/main content, then the remaining body.
`html_article` and `html_body` rows can include episode notes or other page text.
Plain text preserves speaker labels and any timing markers already in its text.
Whitespace is normalized. No translation, content deduplication, or transcript
accuracy correction is performed. Failed extractions remain as rows with null
text, and their original files remain available. See `extraction-report.json`.
Metadata uses an explicit selection of feed/episode fields from a read-only SQLite
connection. Source URLs and dates are null when no matching metadata is available.
## Loading
```python
from datasets import load_dataset
dataset = load_dataset("{repo_id}", split="train")
readable = dataset.filter(lambda row: row["extraction_status"] == "ok")
```
To download and unpack all original files:
```sh
uvx --from huggingface_hub hf download {repo_id} raw-transcripts.tar.gz \\
--repo-type dataset --local-dir .
tar -xzf raw-transcripts.tar.gz
```
Each row's `raw_path` points to its file in the extracted `raw/` directory. The
bundle avoids thousands of individual file commits when publishing updates.
## Regeneration
The standalone generator and its dependency declarations are included in
`scripts/generate_transcript_dataset.py`. With the original archive and optional
Overcast SQLite database available, run:
```sh
uv run scripts/generate_transcript_dataset.py archive/transcripts \\
--database overcast.db --output archive/huggingface/podcast-transcripts \\
--repo-id {repo_id}
```
Generation is local. Upload the generated output directory to this dataset repo
using `hf upload --repo-type dataset`.
## Licensing and provenance
Transcripts originate from the podcasts identified in each row. Copyright and
any applicable licenses remain with their respective owners; this collection
does not grant a blanket license to the transcript content. The generator code
is covered by the project's Apache-2.0 license, included as `scripts/LICENSE`.
"""
def write_raw_archive(files: list[Path], source: Path, output: Path) -> None:
archive_path = output / "raw-transcripts.tar.gz"
temporary_archive = archive_path.with_suffix(".gz.tmp")
with tarfile.open(temporary_archive, "w:gz", compresslevel=6) as archive:
for path in files:
member = tarfile.TarInfo(f"raw/{path.relative_to(source).as_posix()}")
member.size = path.stat().st_size
member.mode = 0o644
with path.open("rb") as content:
archive.addfile(member, content)
temporary_archive.replace(archive_path)
if (raw_directory := output / "raw").is_dir():
shutil.rmtree(raw_directory)
def generate_dataset(
source: Path,
output: Path,
database: Path | None,
repo_id: str = "YOUR_USERNAME/podcast-transcripts",
) -> dict[str, object]:
source, output = source.resolve(), output.resolve()
if not source.is_dir():
msg = f"Transcript directory does not exist: {source}"
raise ValueError(msg)
if output.is_relative_to(source) or source.is_relative_to(output):
msg = "Source and output directories must not overlap"
raise ValueError(msg)
files = list(archive_files(source))
if not files:
msg = "No transcript files found"
raise ValueError(msg)
metadata = load_metadata(database)
(output / "data").mkdir(parents=True, exist_ok=True)
formats: Counter[str] = Counter()
methods: Counter[str] = Counter()
failures: list[Record] = []
batch: list[Record] = []
matched = 0
parquet_path = output / "data/train.parquet"
temporary_parquet = parquet_path.with_suffix(".parquet.tmp")
with pq.ParquetWriter(
temporary_parquet,
_SCHEMA,
compression="zstd",
) as writer:
for index, path in enumerate(files, start=1):
record = transcript_record(path, source, metadata)
formats[cast("str", record["format"])] += 1
methods[record["extraction_method"] or "error"] += 1
matched += bool(record["source_url"])
if record["extraction_status"] == "error":
failures.append(
{key: record[key] for key in ("raw_path", "extraction_error")},
)
batch.append(record)
if len(batch) >= _BATCH_SIZE:
writer.write_table(pa.Table.from_pylist(batch, schema=_SCHEMA))
batch.clear()
if index % 500 == 0:
print(f"Processed {index}/{len(files)} transcripts", flush=True)
if batch:
writer.write_table(pa.Table.from_pylist(batch, schema=_SCHEMA))
temporary_parquet.replace(parquet_path)
print("Bundling original transcripts", flush=True)
write_raw_archive(files, source, output)
summary: dict[str, object] = {
"generated_at": datetime.now(UTC).isoformat(),
"files": len(files),
"extracted": len(files) - len(failures),
"failed": len(failures),
"metadata_matched": matched,
"formats": dict(sorted(formats.items())),
"methods": dict(sorted(methods.items())),
"failures": failures,
}
(output / "extraction-report.json").write_text(
json.dumps(summary, ensure_ascii=False, indent=2) + "\n",
encoding="utf-8",
)
(output / "README.md").write_text(dataset_card(summary, repo_id), encoding="utf-8")
(output / ".gitattributes").write_text(
"*.tar.gz filter=lfs diff=lfs merge=lfs -text\n"
"*.parquet filter=lfs diff=lfs merge=lfs -text\n",
encoding="utf-8",
)
(output / "scripts").mkdir(exist_ok=True)
script = Path(__file__).resolve()
shutil.copy2(script, output / "scripts" / script.name)
if (license_path := script.parent.parent / "LICENSE").is_file():
shutil.copy2(license_path, output / "scripts/LICENSE")
return summary
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"source",
nargs="?",
type=Path,
default=Path("archive/transcripts"),
)
parser.add_argument(
"--output",
type=Path,
default=Path("archive/huggingface/podcast-transcripts"),
)
parser.add_argument(
"--repo-id",
default="YOUR_USERNAME/podcast-transcripts",
help="Hugging Face dataset ID used in the generated loading example",
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
"--database",
type=Path,
help="Optional read-only metadata database",
)
group.add_argument(
"--no-database",
action="store_true",
help="Use directory/filename metadata only",
)
args = parser.parse_args()
database = args.database
if database is None and not args.no_database and Path("overcast.db").is_file():
database = Path("overcast.db")
try:
summary = generate_dataset(args.source, args.output, database, args.repo_id)
except (OSError, ValueError, sqlite3.Error) as error:
parser.exit(1, f"Dataset generation failed: {error}\n")
print(
json.dumps(
{key: value for key, value in summary.items() if key != "failures"},
indent=2,
),
)
print(f"Dataset written to {args.output.resolve()}")
if __name__ == "__main__":
main()
|