File size: 5,668 Bytes
d3e285e
 
 
ae3ee40
 
 
 
 
 
 
 
 
 
 
 
910b6d1
d3e285e
 
 
 
ae3ee40
 
 
 
d3e285e
 
 
 
 
 
 
 
ae3ee40
 
d3e285e
ae3ee40
d3e285e
 
 
 
910b6d1
ae3ee40
d3e285e
ae3ee40
 
 
 
 
 
 
910b6d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae3ee40
910b6d1
 
ae3ee40
 
 
 
 
 
 
910b6d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae3ee40
910b6d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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