Datasets:

ArXiv:
License:
holylovenia commited on
Commit
be4a70b
1 Parent(s): d54d3ff

Upload aya_dataset.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. aya_dataset.py +188 -0
aya_dataset.py ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2024 The HuggingFace Datasets Authors and the current dataset script contributor.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ """
17
+ The Aya Dataset is a multilingual instruction fine-tuning dataset curated by an open-science community via Aya Annotation Platform from Cohere For AI. The dataset contains a total of 204k human-annotated prompt-completion pairs along with the demographics data of the annotators. This dataset can be used to train, finetune, and evaluate multilingual LLMs.
18
+ """
19
+
20
+ from pathlib import Path
21
+ from typing import List
22
+
23
+ import datasets
24
+ import pandas as pd
25
+
26
+ from seacrowd.utils import schemas
27
+ from seacrowd.utils.configs import SEACrowdConfig
28
+ from seacrowd.utils.constants import Licenses, Tasks
29
+
30
+ _CITATION = """\
31
+ @misc{singh2024aya,
32
+ title={Aya Dataset: An Open-Access Collection for Multilingual Instruction Tuning},
33
+ author={Shivalika Singh and Freddie Vargus and Daniel Dsouza and Börje F. Karlsson and Abinaya Mahendiran and Wei-Yin Ko and Herumb Shandilya and Jay Patel and Deividas Mataciunas and Laura OMahony and Mike Zhang and Ramith Hettiarachchi and Joseph Wilson and Marina Machado and Luisa Souza Moura and Dominik Krzemiński and Hakimeh Fadaei and Irem Ergün and Ifeoma Okoh and Aisha Alaagib and Oshan Mudannayake and Zaid Alyafeai and Vu Minh Chien and Sebastian Ruder and Surya Guthikonda and Emad A. Alghamdi and Sebastian Gehrmann and Niklas Muennighoff and Max Bartolo and Julia Kreutzer and Ahmet Üstün and Marzieh Fadaee and Sara Hooker},
34
+ year={2024},
35
+ eprint={2402.06619},
36
+ archivePrefix={arXiv},
37
+ primaryClass={cs.CL}
38
+ }
39
+ """
40
+
41
+ _DATASETNAME = "aya_dataset"
42
+
43
+ _DESCRIPTION = """\
44
+ The Aya Dataset is a multilingual instruction fine-tuning dataset curated by an open-science community via Aya Annotation Platform from Cohere For AI. The dataset contains a total of 204k human-annotated prompt-completion pairs along with the demographics data of the annotators. This dataset can be used to train, finetune, and evaluate multilingual LLMs.
45
+ """
46
+
47
+ _HOMEPAGE = "https://huggingface.co/datasets/CohereForAI/aya_dataset"
48
+
49
+ _LANGUAGES = ["ceb", "ind", "jav", "mya", "tam", "fil", "sun", "tha", "vie", "zsm"]
50
+
51
+ _LICENSE = Licenses.APACHE_2_0.value
52
+
53
+ _LOCAL = False
54
+
55
+ # This can be an arbitrarily nested dict/list of URLs (see below in `_split_generators` method)
56
+ _URLS = {
57
+ "train": "https://huggingface.co/datasets/CohereForAI/aya_dataset/resolve/main/data/train-00000-of-00001.parquet", # test split does not contain SEA languages
58
+ }
59
+
60
+ _SUPPORTED_TASKS = [Tasks.INSTRUCTION_TUNING]
61
+
62
+ _SOURCE_VERSION = "1.0.0"
63
+
64
+ _SEACROWD_VERSION = "2024.06.20"
65
+
66
+ _SEACROWD_SCHEMA = "seacrowd_t2t"
67
+
68
+
69
+ def _aya_config_constructor(lang: str, schema: str, version: str) -> SEACrowdConfig:
70
+ return SEACrowdConfig(
71
+ name=f"{_DATASETNAME}_{lang}_{schema}",
72
+ version=version,
73
+ description=f"Aya Dataset {schema} schema",
74
+ schema=schema,
75
+ subset_id=f"Aya {lang}",
76
+ )
77
+
78
+
79
+ class AyaDataset(datasets.GeneratorBasedBuilder):
80
+ """
81
+ The Aya Dataset is a multilingual instruction fine-tuning dataset curated by an open-science community via Aya Annotation Platform from Cohere For AI. The dataset contains a total of 204k human-annotated prompt-completion pairs along with the demographics data of the annotators. This dataset can be used to train, finetune, and evaluate multilingual LLMs.
82
+
83
+ """
84
+
85
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
86
+ SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
87
+
88
+ def _populate_configs():
89
+ configs = [_aya_config_constructor(lang, "source", _SOURCE_VERSION) for lang in _LANGUAGES] + [_aya_config_constructor(lang, _SEACROWD_SCHEMA, _SEACROWD_VERSION) for lang in _LANGUAGES]
90
+
91
+ all_lang_source_config = SEACrowdConfig(
92
+ name=f"{_DATASETNAME}_source",
93
+ version=_SOURCE_VERSION,
94
+ description="Aya Dataset source schema",
95
+ schema="source",
96
+ subset_id="Aya",
97
+ )
98
+
99
+ all_lang_t2t_config = SEACrowdConfig(
100
+ name=f"{_DATASETNAME}_{_SEACROWD_SCHEMA}",
101
+ version=_SEACROWD_VERSION,
102
+ description=f"Aya Dataset {_SEACROWD_SCHEMA} schema",
103
+ schema=_SEACROWD_SCHEMA,
104
+ subset_id="Aya",
105
+ )
106
+
107
+ configs.append(all_lang_source_config)
108
+ configs.append(all_lang_t2t_config)
109
+ return configs
110
+
111
+ BUILDER_CONFIGS = _populate_configs()
112
+
113
+ DEFAULT_CONFIG_NAME = "aya_dataset_source"
114
+
115
+ def _info(self) -> datasets.DatasetInfo:
116
+
117
+ if self.config.schema == "source":
118
+ features = datasets.Features(
119
+ {
120
+ "inputs": datasets.Value("string"),
121
+ "targets": datasets.Value("string"),
122
+ "language": datasets.Value("string"),
123
+ "language_code": datasets.Value("string"),
124
+ "annotation_type": datasets.Value("string"),
125
+ "user_id": datasets.Value("string"),
126
+ }
127
+ )
128
+
129
+ elif self.config.schema == "seacrowd_t2t":
130
+ features = schemas.text2text_features
131
+
132
+ return datasets.DatasetInfo(
133
+ description=_DESCRIPTION,
134
+ features=features,
135
+ homepage=_HOMEPAGE,
136
+ license=_LICENSE,
137
+ citation=_CITATION,
138
+ )
139
+
140
+ def get_lang_filter(self, config_name: str):
141
+ # aya_dataset_{lang}_{schema}
142
+ tokens = config_name.split("_")
143
+ if len(tokens) == 0 or len(tokens[2]) != 3:
144
+ return None
145
+ return tokens[2]
146
+
147
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
148
+ """Returns SplitGenerators."""
149
+
150
+ url = _URLS["train"]
151
+ data_dir = dl_manager.download_and_extract(url)
152
+
153
+ return [
154
+ datasets.SplitGenerator(
155
+ name=datasets.Split.TRAIN,
156
+ gen_kwargs={
157
+ "data_path": Path(data_dir),
158
+ "split": "train",
159
+ },
160
+ ),
161
+ ]
162
+
163
+ def _generate_examples(self, data_path: Path, split: str):
164
+ """Yields examples as (key, example) tuples."""
165
+
166
+ df = pd.read_parquet(data_path)
167
+
168
+ lang_filter = self.get_lang_filter(self.config.name)
169
+ if lang_filter is not None:
170
+ df = df[df["language_code"] == lang_filter]
171
+ else:
172
+ df = df[df["language_code"].isin(_LANGUAGES)]
173
+
174
+ if self.config.schema == "source":
175
+ for idx, row in df.iterrows():
176
+ data = row.to_dict()
177
+ yield idx, data
178
+
179
+ elif self.config.schema == "seacrowd_t2t":
180
+ for idx, row in df.iterrows():
181
+ sample = {
182
+ "id": str(idx),
183
+ "text_1": row["inputs"],
184
+ "text_2": row["targets"],
185
+ "text_1_name": "inputs",
186
+ "text_2_name": "targets",
187
+ }
188
+ yield idx, sample