pl_debates_test / pl_debates_test.py
directtt's picture
Upload folder using huggingface_hub
910b6d1 verified
# coding=utf-8
# pl_debates_test.py
import os
import csv
import datasets
_CITATION = """
@misc{pl_politicians_2025,
title = {Polish Politicians Speech Dataset},
author = {directtt},
year = {2025}
}
"""
# Base URLs on the Hub — replace YOUR-USER/YOUR-REPO accordingly
_BASE_URL = "https://huggingface.co/datasets/directtt/pl_debates_test/resolve/main/"
_TRANSCRIPT_URL = _BASE_URL + "transcript/pl/test.tsv"
_AUDIO_TAR_URL = _BASE_URL + "audio_pl.tar"
class PlDebatesTest(datasets.GeneratorBasedBuilder):
"""Polish politicians’ speech audio dataset."""
BUILDER_CONFIGS = [
datasets.BuilderConfig(
name="pl_debates_test",
version=datasets.Version("1.0.0"),
description="Studio‐recorded short utterances by Polish politicians.",
)
]
def _info(self):
return datasets.DatasetInfo(
description="Studio‐recorded short utterances by Polish politicians.",
features=datasets.Features({
"path": datasets.Value("string"),
"audio": datasets.Audio(sampling_rate=16_000),
"sentence": datasets.Value("string"),
"age": datasets.Value("int32"),
"gender": datasets.Value("string"),
"speech_type": datasets.Value("string"),
"source": datasets.Value("string"),
}),
supervised_keys=None,
homepage="https://huggingface.co/datasets/directtt/pl_debates_test",
citation=_CITATION,
)
def _split_generators(self, dl_manager):
if dl_manager.is_streaming:
# streaming: download the tar and the TSV, but don't extract to disk
tsv_path = dl_manager.download(_TRANSCRIPT_URL)
tar_path = dl_manager.download(_AUDIO_TAR_URL)
archives = [dl_manager.iter_archive(tar_path)]
return [
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"tsv_path": tsv_path,
"archives": archives,
"streaming": True,
},
),
]
else:
# eager: download & extract everything
downloaded = dl_manager.download_and_extract({
"tsv": _TRANSCRIPT_URL,
"tar": _AUDIO_TAR_URL,
})
return [
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"tsv_path": downloaded["tsv"],
"audio_root": downloaded["tar"],
"streaming": False,
},
),
]
def _generate_examples(self, tsv_path, streaming, archives=None, audio_root=None):
# 1) load metadata
meta = {}
with open(tsv_path, encoding="utf-8") as f:
reader = csv.DictReader(f, delimiter="\t")
for row in reader:
meta[row["path"]] = row
key = 0
if streaming:
# iterate inside the tar archive without extracting
for archive in archives:
for path_in_tar, fileobj in archive:
# path_in_tar looks like "audio/pl/<speaker>/<fname>.wav"
if not path_in_tar.endswith(".wav"):
continue
# build the rel_path for lookup
rel_path = path_in_tar.replace("\\", "/") # normalize on Windows
row = meta.get(rel_path)
if row is None:
continue
# read the bytes and yield them
audio_bytes = fileobj.read()
yield key, {
"path": rel_path,
"audio": {"path": None, "bytes": audio_bytes},
"sentence": row["sentence"],
"age": int(row.get("age", -1)),
"gender": row["gender"],
"speech_type": row["speech_type"],
"source": row["source"],
}
key += 1
else:
# eager: walk the extracted folder on disk
pl_root = os.path.join(audio_root, "audio", "pl")
for speaker in sorted(os.listdir(pl_root)):
sp_dir = os.path.join(pl_root, speaker)
if not os.path.isdir(sp_dir):
continue
for fname in sorted(os.listdir(sp_dir)):
if not fname.endswith(".wav"):
continue
rel_path = os.path.join("audio", "pl", speaker, fname)
row = meta.get(rel_path)
if row is None:
continue
yield key, {
"path": rel_path,
"audio": os.path.join(sp_dir, fname),
"sentence": row["sentence"],
"age": int(row.get("age", -1)),
"gender": row.get("gender", "unknown") or "unknown",
"speech_type": row["speech_type"],
"source": row["source"],
}
key += 1