File size: 7,776 Bytes
28cc287
9f6d2ae
 
28cc287
 
9f6d2ae
 
 
 
58b647c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f6d2ae
 
 
 
 
 
3476ae7
9f6d2ae
58b647c
 
 
 
 
 
 
 
9f6d2ae
 
28cc287
3476ae7
 
 
 
58b647c
3476ae7
9f6d2ae
 
 
 
 
3476ae7
9f6d2ae
 
8ac1acc
 
 
 
 
 
9f6d2ae
 
 
 
 
58b647c
 
 
 
 
28cc287
 
 
 
 
 
 
 
 
 
 
 
8ac1acc
58b647c
8ac1acc
9f6d2ae
 
 
58b647c
9f6d2ae
58b647c
9f6d2ae
28cc287
9f6d2ae
58b647c
9f6d2ae
8ac1acc
9f6d2ae
 
58b647c
9f6d2ae
 
3f69367
 
 
 
 
 
 
 
 
 
 
9f6d2ae
 
 
 
 
 
28cc287
9f6d2ae
 
 
58b647c
9f6d2ae
58b647c
 
 
9f6d2ae
 
58b647c
3f69367
 
 
 
 
 
 
 
 
 
 
 
 
28cc287
9f6d2ae
28cc287
 
 
 
 
3f69367
 
 
 
9f6d2ae
28cc287
 
58b647c
 
 
28cc287
58b647c
9f6d2ae
8ac1acc
28cc287
 
9f6d2ae
 
 
 
 
28cc287
 
9f6d2ae
 
 
 
28cc287
7f5e50e
28cc287
9f6d2ae
858a490
9f6d2ae
28cc287
 
3f69367
 
28cc287
 
 
 
 
3f69367
28cc287
 
 
 
9f6d2ae
 
 
28cc287
 
 
 
 
9f6d2ae
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
from collections import defaultdict
import os
import glob
import csv
from tqdm.auto import tqdm

import datasets


_DESCRIPTION = """
A large-scale multilingual speech corpus for representation learning, semi-supervised learning and interpretation.
"""

_CITATION = """
@inproceedings{wang-etal-2021-voxpopuli,
    title = "{V}ox{P}opuli: A Large-Scale Multilingual Speech Corpus for Representation Learning, 
    Semi-Supervised Learning and Interpretation",
    author = "Wang, Changhan  and
      Riviere, Morgane  and
      Lee, Ann  and
      Wu, Anne  and
      Talnikar, Chaitanya  and
      Haziza, Daniel  and
      Williamson, Mary  and
      Pino, Juan  and
      Dupoux, Emmanuel",
    booktitle = "Proceedings of the 59th Annual Meeting of the Association for Computational Linguistics 
    and the 11th International Joint Conference on Natural Language Processing (Volume 1: Long Papers)",
    month = aug,
    year = "2021",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2021.acl-long.80",
    doi = "10.18653/v1/2021.acl-long.80",
    pages = "993--1003",
}
"""

_HOMEPAGE = "https://github.com/facebookresearch/voxpopuli"

_LICENSE = "CC0, also see https://www.europarl.europa.eu/legal-notice/en/"


_LANGUAGES = sorted(
    [
        "en", "de", "fr", "es", "pl", "it", "ro", "hu", "cs", "nl", "fi", "hr",
        "sk", "sl", "et", "lt", "pt", "bg", "el", "lv", "mt", "sv", "da"
    ]
)
_LANGUAGES_V2 = [f"{x}_v2" for x in _LANGUAGES]

_ASR_LANGUAGES = [
    "en", "de", "fr", "es", "pl", "it", "ro", "hu", "cs", "nl", "fi", "hr",
    "sk", "sl", "et", "lt"
]
_ASR_ACCENTED_LANGUAGES = [
    "en_accented"
]

_YEARS = list(range(2009, 2020 + 1))

# unnecessary
_CONFIG_TO_LANGS = {
    "400k": _LANGUAGES,
    "100k": _LANGUAGES,
    "10k": _LANGUAGES,
    "asr": _ASR_LANGUAGES,  # + _ASR_ACCENTED_LANGUAGES
}

_CONFIG_TO_YEARS = {
    "400k": _YEARS + [f"{y}_2" for y in _YEARS],
    "100k": _YEARS,
    "10k": [2019, 2020],
    "asr": _YEARS,
}
for lang in _LANGUAGES:
    _CONFIG_TO_YEARS[lang] = _YEARS
    # _CONFIG_TO_YEARS[lang] = [2020]

for lang in _LANGUAGES_V2:
    _CONFIG_TO_YEARS[lang] = _YEARS + [f"{y}_2" for y in _YEARS]


_BASE_URL = "https://dl.fbaipublicfiles.com/voxpopuli/"

_DATA_URL = _BASE_URL + "audios/{lang}_{year}.tar"

_ASR_DATA_URL = _BASE_URL + "audios/original_{year}.tar"

_UNLABELLED_META_URL = _BASE_URL + "annotations/unlabelled_v2.tsv.gz"

_ASR_META_URL = _BASE_URL + "annotations/asr/asr_{lang}.tsv.gz"


class VoxpopuliConfig(datasets.BuilderConfig):
    """BuilderConfig for VoxPopuli."""

    def __init__(self, name, **kwargs):
        """
        Args:
          name: `string`, name of dataset config
          **kwargs: keyword arguments forwarded to super.
        """
        super().__init__(name=name, **kwargs)
        name = name.split("_")[0]
        self.languages = [name] if name in _LANGUAGES else _CONFIG_TO_LANGS[name]
        self.years = _CONFIG_TO_YEARS[name]


class Voxpopuli(datasets.GeneratorBasedBuilder):
    """The VoxPopuli dataset."""

    VERSION = datasets.Version("1.3.0")  # not sure
    BUILDER_CONFIGS = [
        VoxpopuliConfig(
            name=name,
            version=datasets.Version("1.3.0"),
            )
        for name in _LANGUAGES + _LANGUAGES_V2 + ["10k", "100k", "400k"]
    ]
    # DEFAULT_CONFIG_NAME = "400k"
    DEFAULT_WRITER_BATCH_SIZE = 256  # SET THIS TO A LOWER VALUE IF IT USES TOO MUCH RAM SPACE

    def _info(self):
        try:
            import torch
            import torchaudio
        except ImportError as e:
            raise ValueError(
                f"{str(e)}.\n" +
                "Loading voxpopuli requires `torchaudio` to be installed."
                "You can install torchaudio with `pip install torchaudio`."
            )
        global torchaudio

        features = datasets.Features(
            {
                "path": datasets.Value("string"),
                "language": datasets.ClassLabel(names=_LANGUAGES),
                "year": datasets.Value("int16"),
                "audio": datasets.Audio(sampling_rate=16_000),
                "segment_id": datasets.Value("int16"),
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _read_metadata_unlabelled(self, metadata_path):
        #  from https://github.com/facebookresearch/voxpopuli/blob/main/voxpopuli/get_unlabelled_data.py#L34
        def predicate(id_):
            is_plenary = id_.find("PLENARY") > -1
            if self.config.name == "10k":  # in {"10k", "10k_sd"}
                return is_plenary and 20190101 <= int(id_[:8]) < 20200801
            elif self.config.name == "100k":
                return is_plenary
            elif self.config.name in _LANGUAGES:
                return is_plenary and id_.endswith(self.config.name)
            elif self.config.name in _LANGUAGES_V2:
                return id_.endswith(self.config.name.split("_")[0])
            return True

        metadata = defaultdict(list)

        with open(metadata_path, encoding="utf-8") as csv_file:
            csv_reader = csv.reader(csv_file, delimiter="\t")
            for i, row in tqdm(enumerate(csv_reader)):
                if i == 0:
                    continue
                event_id, segment_id, start, end = row
                _, lang = event_id.rsplit("_", 1)[-2:]
                if lang in self.config.languages and predicate(event_id):
                    metadata[event_id].append((float(start), float(end)))

        return metadata

    def _read_metadata_asr(self, metadata_paths):
        pass

    def _split_generators(self, dl_manager):
        metadata_path = dl_manager.download_and_extract(_UNLABELLED_META_URL)

        urls = [_DATA_URL.format(lang=language, year=year) for language in self.config.languages for year in self.config.years]
        dl_manager.download_config.num_proc = len(urls)
        data_dirs = dl_manager.download_and_extract(urls)

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "data_dirs": data_dirs,
                    "metadata_path": metadata_path,
                }
            ),
        ]

    def _generate_examples(self, data_dirs, metadata_path):
        metadata = self._read_metadata_unlabelled(metadata_path)

        for data_dir in data_dirs:
            for file in glob.glob(f"{data_dir}/**/*.ogg", recursive=True):
                path_components = file.split(os.sep)
                language, year, audio_filename = path_components[-3:]
                audio_id, _ = os.path.splitext(audio_filename)
                if audio_id not in metadata:
                    continue
                timestamps = metadata[audio_id]

                waveform, sr = torchaudio.load(file)
                duration = waveform.size(1)

                # split audio on the fly and yield segments as arrays - they will be converted to bytes by Audio feature
                for segment_id, (start, stop) in enumerate(timestamps):
                    segment = waveform[:, int(start * sr): min(int(stop * sr), duration)]

                    yield f"{audio_filename}_{segment_id}", {
                        "path": file,
                        "language": language,
                        "year": year,
                        "audio": {
                            "array": segment[0],  # segment is a 2-dim array
                            "sampling_rate": 16_000
                        },
                        "segment_id": segment_id,
                    }