File size: 3,318 Bytes
adbedc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os

import datasets

from .configs import CONFIGS_MAP

_BASE_URL = (
    "https://huggingface.co/datasets/formospeech/AudioSCAN-XTTSv2-female/resolve/main/"
)
_AUDIO_URL = _BASE_URL + "audio.tar.gz"


class AudioSCANConfig(datasets.BuilderConfig):
    """BuilderConfig for CommonVoice."""

    def __init__(self, **kwargs):
        super(AudioSCANConfig, self).__init__(
            version=datasets.Version("0.1.0", ""), **kwargs
        )


class LibrispeechASR(datasets.GeneratorBasedBuilder):
    DEFAULT_WRITER_BATCH_SIZE = 256
    DEFAULT_CONFIG_NAME = "addprim_jump"
    BUILDER_CONFIGS = []
    for config_name in CONFIGS_MAP.keys():
        BUILDER_CONFIGS.append(
            AudioSCANConfig(
                name=config_name,
                description=None,
            )
        )

    def _info(self):
        return datasets.DatasetInfo(
            description="",
            features=datasets.Features(
                {
                    "audio": datasets.Audio(sampling_rate=16_000),
                    "out": datasets.Value("string"),
                    "in": datasets.Value("string"),
                }
            ),
            license=None,
            supervised_keys=("audio", "out"),
            homepage=None,
            citation=None,
            task_templates=None,
        )

    def _split_generators(self, dl_manager):
        archive_path = dl_manager.download(_AUDIO_URL)
        local_extracted_archive = (
            dl_manager.extract(archive_path) if not dl_manager.is_streaming else {}
        )

        split_meta_urls = {
            split_name: _BASE_URL + split_meta_path
            for split_name, split_meta_path in CONFIGS_MAP[self.config.name].items()
        }

        split_meta_paths = dl_manager.download_and_extract(split_meta_urls)
        splits = []
        for split_name in CONFIGS_MAP[self.config.name].keys():
            split = datasets.SplitGenerator(
                name=split_name,
                gen_kwargs={
                    "meta_path": split_meta_paths[split_name],
                    "local_extracted_archive": local_extracted_archive,
                    "files": dl_manager.iter_archive(archive_path),
                },
            )
            splits.append(split)

        return splits

    def _generate_examples(self, meta_path, local_extracted_archive, files):
        with open(meta_path, "r") as f:
            metadata = json.loads("[" + ",".join(f.readlines()) + "]")

            audio_data = {}
            for path, file in files:
                filename = os.path.basename(path)
                audio_data[filename] = file.read()
            key = 0
            for item in metadata:
                path = (
                    os.path.join(local_extracted_archive, item["audio_path"])
                    if local_extracted_archive
                    else item["audio_path"]
                )
                yield (
                    key,
                    {
                        "in": item["in"],
                        "out": item["out"],
                        "audio": {
                            "path": path,
                            "bytes": audio_data[item["audio_path"]],
                        },
                    },
                )
                key += 1