Datasets:

Modalities:
Audio
Text
Size:
< 1K
ArXiv:
DOI:
Libraries:
Datasets
File size: 3,796 Bytes
1a16f09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""HuggingFace loading script for the JamALT dataset."""


import csv
from dataclasses import dataclass
import json
import os
from pathlib import Path
from typing import Optional

import datasets


# TODO: Add BibTeX citation
_CITATION = """\
"""

# TODO: Add description of the dataset here
_DESCRIPTION = """\
"""

# TODO: Add a link to an official homepage for the dataset here
_HOMEPAGE = ""

# TODO: Add the licence for the dataset here
_LICENSE = ""

_METADATA_FILENAME = "metadata.csv"


_LANGUAGE_NAME_TO_CODE = {
    "English": "en",
    "French": "fr",
    "German": "de",
    "Spanish": "es",
}


@dataclass
class JamAltBuilderConfig(datasets.BuilderConfig):
    language: Optional[str] = None
    with_audio: bool = False
    decode_audio: bool = True
    sampling_rate: Optional[int] = None
    mono: bool = True


# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
class JamAltDataset(datasets.GeneratorBasedBuilder):
    """TODO: Short description of my dataset."""

    VERSION = datasets.Version("0.0.0")
    BUILDER_CONFIG_CLASS = JamAltBuilderConfig
    BUILDER_CONFIGS = [JamAltBuilderConfig("default")]
    DEFAULT_CONFIG_NAME = "default"

    def _info(self):
        feat_dict = {
            "name": datasets.Value("string"),
            "text": datasets.Value("string"),
            "language": datasets.Value("string"),
        }
        if self.config.with_audio:
            feat_dict["audio"] = datasets.Audio(
                decode=self.config.decode_audio,
                sampling_rate=self.config.sampling_rate,
                mono=self.config.mono,
            )

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(feat_dict),
            supervised_keys=("audio", "text") if "audio" in feat_dict else None,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        metadata_path = dl_manager.download(_METADATA_FILENAME)

        audio_paths, text_paths, metadata = [], [], []
        with open(metadata_path, encoding="utf-8") as f:
            for row in csv.DictReader(f):
                if (
                    self.config.language is None
                    or _LANGUAGE_NAME_TO_CODE[row["Language"]] == self.config.language
                ):
                    audio_paths.append("audio/" + row["Filepath"])
                    text_paths.append(
                        "lyrics/" + os.path.splitext(row["Filepath"])[0] + ".txt"
                    )
                    metadata.append(row)

        text_paths = dl_manager.download(text_paths)
        audio_paths = (
            dl_manager.download(audio_paths) if self.config.with_audio else None
        )

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs=dict(
                    text_paths=text_paths,
                    audio_paths=audio_paths,
                    metadata=metadata,
                ),
            ),
        ]

    def _generate_examples(self, text_paths, audio_paths, metadata):
        if audio_paths is None:
            audio_paths = [None] * len(text_paths)
        for text_path, audio_path, meta in zip(text_paths, audio_paths, metadata):
            name = os.path.splitext(os.path.basename(text_path))[0]
            with open(text_path, encoding="utf-8") as text_f:
                record = {
                    "name": name,
                    "text": text_f.read(),
                    "language": _LANGUAGE_NAME_TO_CODE[meta["Language"]],
                }
            if audio_path is not None:
                record["audio"] = audio_path
            yield name, record