File size: 2,566 Bytes
b9d7915
 
 
 
 
 
 
4e594cb
 
b9d7915
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from glob import glob
from pathlib import Path

import datasets
from datasets import Features, Value, Audio

os.system('pip install music-tag')

from music_tag import load_file


_DESCRIPTION = """
This dataset contains speech generated for anecdotes from the [baneks dataset](https://huggingface.co/datasets/zeio/baneks)
"""

_HOMEPAGE = "https://huggingface.co/datasets/zeio/baneks-speech"

_LICENSE = "Apache License Version 2.0"

_URL = "https://huggingface.co/datasets/zeio/baneks-speech/resolve/main/speech/{first:06d}-{last:06d}.tar.xz"


_N_TOTAL = 46_554
_N_BATCH = 1_000


class BaneksSpeech(datasets.GeneratorBasedBuilder):
    """Speech generated for anecdotes from the baneks dataset"""

    VERSION = datasets.Version("10.10.2023")

    def _info(self):
        return datasets.DatasetInfo(
            description = _DESCRIPTION,
            features = Features(
                {
                    "text": Value("string"),
                    "audio": Audio(sampling_rate = 22_050),
                    "artist": Value("string"),
                    "id": Value("int32"),
                    "source": Value("string")
                }
            ),
            homepage = _HOMEPAGE,
            license = _LICENSE
        )

    def _split_generators(self, dl_manager):
        offset = 0

        paths = []

        while offset < _N_TOTAL:
            url = _URL.format(first = offset + 1, last = min(offset := offset + _N_BATCH, _N_TOTAL))

            paths.append(dl_manager.download_and_extract(url))

            break

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

    def _generate_examples(self, paths: list[str]):
        for path in paths:
            for file in sorted(glob(os.path.join(path, "*"))):
                components = Path(file).stem.split('.', maxsplit = 1)

                assert len(components) == 2, f'Incorrect file name: {file}'

                id_ = int(components[0])
                source = components[1]

                meta = load_file(file)

                # artist = meta['artist']
                # text = meta['lyrics']

                # print(id_, source, artist, text)

                yield f'{id_:08d}-{source}', {
                    'text': meta['lyrics'],
                    'audio': file,
                    'artist': meta['artist'],
                    'id': id_,
                    'source': source
                }