File size: 4,518 Bytes
e18e4bf
05a39e1
e18e4bf
 
 
 
 
 
 
 
 
 
 
 
05a39e1
e18e4bf
 
05a39e1
 
 
 
e18e4bf
05a39e1
 
 
 
 
3e96494
05a39e1
242cd59
ae4e3b0
242cd59
 
 
e18e4bf
05a39e1
 
 
 
e18e4bf
 
92dcd94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e18e4bf
242cd59
 
 
05a39e1
 
 
 
 
fbc4954
 
 
 
 
3653c5c
05a39e1
 
 
fbc4954
05a39e1
242cd59
 
05a39e1
 
 
 
 
 
 
67e6cc8
e18e4bf
05a39e1
 
 
 
 
 
 
 
 
 
 
67e6cc8
05a39e1
 
 
 
 
 
 
67e6cc8
05a39e1
296c5a5
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
# coding=utf-8
# Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Common Voice Dataset"""


import csv
import os
import json

import datasets
from datasets.utils.py_utils import size_str
from tqdm import tqdm


# TODO: change "streaming" to "main" after merge!
_BASE_URL = "https://huggingface.co/datasets/leviethoang/VBVLSP/resolve/main/"

_AUDIO_URL = {
        "train": "https://husteduvn-my.sharepoint.com/:u:/g/personal/hoang_lv194767_sis_hust_edu_vn/EYhNns0j8GJEgZvb-G2aRS4Bt7AEdQMrGxYtyO2xjc6Img?e=3PkypA&download=1",
        "test": "https://husteduvn-my.sharepoint.com/:u:/g/personal/hoang_lv194767_sis_hust_edu_vn/Ea0uw5DdlxRKpjay1pm6LIoBI6cU4cxHbpTmhWCCRtvMXw?e=yfN5NR&download=1",
        "validation": "https://husteduvn-my.sharepoint.com/:u:/g/personal/hoang_lv194767_sis_hust_edu_vn/EerG7YTpS8dNgpG5vsnpsm0BBKZYYifqcW4kRX3VzHHO5w?e=uvo7Is&download=1"
} 

_TRANSCRIPT_URL = _BASE_URL + "transcript/{split}.tsv"


class CommonVoice(datasets.GeneratorBasedBuilder):
    DEFAULT_WRITER_BATCH_SIZE = 1000

    def _info(self):
        description = ("""

                       """
        )
        features = datasets.Features(
            {
                "file_path": datasets.Value("string"),
                "audio": datasets.features.Audio(sampling_rate=48_000),
                "script": datasets.Value("string"),
            }
        )

        return datasets.DatasetInfo(
            description=description,
            features=features,
            supervised_keys=None,
            version=self.config.version,
        )

    def _split_generators(self, dl_manager):
        splits = ("train", "test", "validation")
        archive_paths = dl_manager.download(_AUDIO_URL)
        local_extracted_archive_paths = dl_manager.extract(archive_paths)

        meta_urls = {split: _TRANSCRIPT_URL.format(split=split) for split in splits}
        meta_paths = dl_manager.download_and_extract(meta_urls)

        split_generators = []
        split_names = {
            "train": datasets.Split.TRAIN,
            "dev": datasets.Split.VALIDATION,
            "test": datasets.Split.TEST,
        }

        for split in splits:
            split_generators.append(
                datasets.SplitGenerator(
                    name=split_names.get(split, split),
                    gen_kwargs={
                        "local_extracted_archive_path": local_extracted_archive_paths.get(split),
                        "archive": dl_manager.iter_archive(archive_paths.get(split)),
                        "meta_path": meta_paths[split],
                    },
                ),
            )

        return split_generators

    def _generate_examples(self, local_extracted_archive_path, archive, meta_path):
        data_fields = list(self._info().features.keys())
        metadata = {}
        with open(meta_path, encoding="utf-8") as f:
            reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
            for row in tqdm(reader, desc="Reading metadata..."):
                # if data is incomplete, fill with empty values
                for field in data_fields:
                    if field not in row:
                        row[field] = ""
                metadata[row["file_path"]] = row

        
        for filename, file in archive:
            _, filename = os.path.split(filename)
            if filename in metadata:
                result = dict(metadata[filename])
                # set the audio feature and the path to the extracted file
                path = os.path.join(local_extracted_archive_path, filename) if local_extracted_archive_path else filename
                result["audio"] = {"file_path": path, "bytes": file.read()}
                # set path to None if the audio file doesn't exist locally (i.e. in streaming mode)
                result["file_path"] = path if local_extracted_archive_path else filename

                yield path, result