File size: 4,236 Bytes
d037432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b52d294
d037432
b52d294
d037432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Huggingface Dataset version of Banc Trawsgrifiadau Bangor"""


import os
import datasets


# TODO: Add BibTeX citation
_CITATION = """\
}
"""
_DESCRIPTION = """Huggingface Dataset version of Banc Trawsgrifiadau Bangor"""
_HOMEPAGE = "https://git.techiaith.bangor.ac.uk/data-porth-technolegau-iaith/banc-trawsgrifiadau-bangor"
_LICENSE = "Creative Commons Zero v1.0 Universal"
_URL = "https://huggingface.co/datasets/prvInSpace/banc-trawsgrifiadau-bangor/resolve/main"

# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case


class BancTrawsgrifiadauBangor(datasets.GeneratorBasedBuilder):
    """Huggingface Dataset version of Banc Trawsgrifiadau Bangor"""

    VERSION = datasets.Version("1.0.0")

    def _info(self):
        features = datasets.Features(
            {
                "audio_filename": datasets.Value("string"),
                "audio_filesize": datasets.Value("int64"),
                "transcript": datasets.Value("string"),
                "duration": datasets.Value("duration[64]"),
                "audio": datasets.features.Audio(sampling_rate=16_000)
                # These are the features of your dataset like images, labels ...
            }
        )
        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            # Here we define them above because they are different between the two configurations
            features=features,
            # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
            # specify them. They'll be used if as_supervised=True in builder.as_dataset.
            # supervised_keys=("sentence", "label"),
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE,
            # License for the dataset if available
            license=_LICENSE,
            # Citation for the dataset
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):

        # Download the clips
        data_dir = dl_manager.download_and_extract(
            f"{_URL}/clips.zip")

        # Generate the splits
        return [
            datasets.SplitGenerator(
                name="clips",
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": dl_manager.download(f"{_URL}/clips.tsv"),
                    "path_to_clips": os.path.join(data_dir, "clips")
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": dl_manager.download(f"{_URL}/train.tsv"),
                    "path_to_clips": os.path.join(data_dir, "clips")
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": dl_manager.download(f"{_URL}/test.tsv"),
                    "path_to_clips": os.path.join(data_dir, "clips")
                },
            ),
        ]

    # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
    def _generate_examples(self, filepath, path_to_clips):
        print(path_to_clips)
        import csv
        # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
        # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
        with open(filepath, encoding="utf-8") as f:
            reader = csv.DictReader(f, delimiter="\t")
            for row in reader:
                path = f'{path_to_clips}/{row["audio_filename"]}'

                # Add the audio data
                with open(path, "rb") as file:
                    row['audio'] = {
                        'path': path, 'bytes': file.read()
                    }
                yield path, row