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llib.parse import urljoin
|
| 4 |
+
|
| 5 |
+
import datasets
|
| 6 |
+
from pyarrow import parquet as pq
|
| 7 |
+
|
| 8 |
+
from seacrowd.utils import schemas
|
| 9 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
| 10 |
+
from seacrowd.utils.constants import Tasks, Licenses
|
| 11 |
+
|
| 12 |
+
_CITATION = """\
|
| 13 |
+
@article{nguyen2023culturax,
|
| 14 |
+
author = {Thuat Nguyen and Chien Van Nguyen and Viet Dac Lai and Hieu Man and Nghia Trung Ngo and Franck Dernoncourt and Ryan A. Rossi and Thien Huu Nguyen},
|
| 15 |
+
title = {CulturaX: A Cleaned, Enormous, and Multilingual Dataset for Large Language Models in 167 Languages},
|
| 16 |
+
journal = {arXiv preprint arXiv:2309.09400},
|
| 17 |
+
year = {2023},
|
| 18 |
+
url = {https://arxiv.org/abs/2309.09400},
|
| 19 |
+
}
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
_DATASETNAME = "culturax"
|
| 23 |
+
_DESCRIPTION = """\
|
| 24 |
+
CulturaX is a comprehensive multilingual dataset comprising 6.3 trillion tokens across 167
|
| 25 |
+
languages, designed for large language model development. It incorporates an advanced
|
| 26 |
+
cleaning and deduplication process, including language identification and fuzzy
|
| 27 |
+
deduplication with MinHash, to ensure high-quality data for model training. The dataset,
|
| 28 |
+
which spans 16TB in parquet format and 27TB when unpacked, is a combination of the latest
|
| 29 |
+
mC4 and OSCAR corpora, emphasizing non-English languages to support multilingual model
|
| 30 |
+
training. For data cleaning validation, CulturaX employs a SentencePiece tokenizer and
|
| 31 |
+
KenLM language models, utilizing recent Wikipedia dumps for perplexity scoring.
|
| 32 |
+
Before using this dataloader, please accept the acknowledgement at https://huggingface.co/datasets/uonlp/CulturaX and use huggingface-cli login for authentication.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
_LOCAL=False
|
| 36 |
+
_LANGUAGES = ["ind", "jav", "khm", "lao", "tgl", "min", "mya", "sun", "tha", "vie", "zlm", "ceb", "war", "cbk", "bcl"] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
|
| 37 |
+
|
| 38 |
+
_HOMEPAGE = "https://huggingface.co/datasets/uonlp/CulturaX"
|
| 39 |
+
_LICENSE = f"""{Licenses.OTHERS.value} | \
|
| 40 |
+
The licence terms for CulturaX strictly follows those of mC4 and OSCAR. \
|
| 41 |
+
Please refer to both below licenses when using this dataset. \
|
| 42 |
+
- mC4 license: https://huggingface.co/datasets/allenai/c4#license \
|
| 43 |
+
- OSCAR license: https://huggingface.co/datasets/oscar-corpus/OSCAR-2301#licensing-information \
|
| 44 |
+
"""
|
| 45 |
+
_BASE_URL = "https://huggingface.co/datasets/uonlp/CulturaX/resolve/main/{lang}/"
|
| 46 |
+
|
| 47 |
+
_SUPPORTED_TASKS = [Tasks.SELF_SUPERVISED_PRETRAINING]
|
| 48 |
+
_SOURCE_VERSION = "1.0.0"
|
| 49 |
+
_SEACROWD_VERSION = "2024.06.20"
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class CulturaXDataset(datasets.GeneratorBasedBuilder):
|
| 53 |
+
"""CulturaX subset for SEA languages."""
|
| 54 |
+
|
| 55 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
| 56 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
| 57 |
+
|
| 58 |
+
SEACROWD_SCHEMA_NAME = "ssp"
|
| 59 |
+
SUBSETS = ["id", "jv", "km", "lo", "tl", "min", "my", "su", "th", "vi", "ms", "ceb", "war", "cbk", "bcl"]
|
| 60 |
+
|
| 61 |
+
BUILDER_CONFIGS = [
|
| 62 |
+
SEACrowdConfig(
|
| 63 |
+
name=f"{_DATASETNAME}_{subset}_source",
|
| 64 |
+
version=datasets.Version(_SOURCE_VERSION),
|
| 65 |
+
description=f"{_DATASETNAME} {subset} source schema",
|
| 66 |
+
schema="source",
|
| 67 |
+
subset_id=subset,
|
| 68 |
+
)
|
| 69 |
+
for subset in SUBSETS
|
| 70 |
+
] + [
|
| 71 |
+
SEACrowdConfig(
|
| 72 |
+
name=f"{_DATASETNAME}_{subset}_seacrowd_ssp",
|
| 73 |
+
version=datasets.Version(_SEACROWD_VERSION),
|
| 74 |
+
description=f"{_DATASETNAME} {subset} SEACrowd schema",
|
| 75 |
+
schema="seacrowd_ssp",
|
| 76 |
+
subset_id=subset,
|
| 77 |
+
)
|
| 78 |
+
for subset in SUBSETS
|
| 79 |
+
]
|
| 80 |
+
|
| 81 |
+
DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_jv_source"
|
| 82 |
+
|
| 83 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 84 |
+
if self.config.schema == "source":
|
| 85 |
+
features = datasets.Features(
|
| 86 |
+
{
|
| 87 |
+
"text": datasets.Value("string"),
|
| 88 |
+
"timestamp": datasets.Value("string"),
|
| 89 |
+
"url": datasets.Value("string"),
|
| 90 |
+
"source": datasets.Value("string"),
|
| 91 |
+
}
|
| 92 |
+
)
|
| 93 |
+
elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
|
| 94 |
+
features = schemas.ssp_features
|
| 95 |
+
|
| 96 |
+
return datasets.DatasetInfo(
|
| 97 |
+
description=_DESCRIPTION,
|
| 98 |
+
features=features,
|
| 99 |
+
homepage=_HOMEPAGE,
|
| 100 |
+
license=_LICENSE,
|
| 101 |
+
citation=_CITATION,
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
| 105 |
+
"""Returns SplitGenerators."""
|
| 106 |
+
base_path = _BASE_URL.format(lang=self.config.name.split("_")[1])
|
| 107 |
+
|
| 108 |
+
checksum_url = urljoin(base_path, "checksum.sha256")
|
| 109 |
+
checksum_path = Path(dl_manager.download(checksum_url))
|
| 110 |
+
with open(checksum_path, encoding="utf-8") as f:
|
| 111 |
+
filenames = [line.split()[1] for line in f if line]
|
| 112 |
+
data_urls = [urljoin(base_path, filename) for filename in filenames]
|
| 113 |
+
|
| 114 |
+
data_paths = list(map(Path, dl_manager.download([url for url in data_urls if url.endswith(".parquet")])))
|
| 115 |
+
|
| 116 |
+
return [
|
| 117 |
+
datasets.SplitGenerator(
|
| 118 |
+
name=datasets.Split.TRAIN,
|
| 119 |
+
gen_kwargs={
|
| 120 |
+
"filepaths": data_paths,
|
| 121 |
+
"split": "train",
|
| 122 |
+
},
|
| 123 |
+
)
|
| 124 |
+
]
|
| 125 |
+
|
| 126 |
+
def _generate_examples(self, filepaths: [Path], split: str) -> Tuple[int, Dict]:
|
| 127 |
+
"""Yields examples as (key, example) tuples.
|
| 128 |
+
|
| 129 |
+
Iterate over row groups in each filepaths, then yield each row as an example.
|
| 130 |
+
"""
|
| 131 |
+
key = 0
|
| 132 |
+
for filepath in filepaths:
|
| 133 |
+
with open(filepath, "rb") as f:
|
| 134 |
+
pf = pq.ParquetFile(f)
|
| 135 |
+
for row_group in range(pf.num_row_groups):
|
| 136 |
+
df = pf.read_row_group(row_group).to_pandas()
|
| 137 |
+
for row in df.itertuples():
|
| 138 |
+
if self.config.schema == "source":
|
| 139 |
+
yield key, {
|
| 140 |
+
"text": row.text,
|
| 141 |
+
"timestamp": row.timestamp,
|
| 142 |
+
"url": row.url,
|
| 143 |
+
"source": row.source,
|
| 144 |
+
}
|
| 145 |
+
elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
|
| 146 |
+
yield key, {
|
| 147 |
+
"id": str(key),
|
| 148 |
+
"text": row.text,
|
| 149 |
+
}
|
| 150 |
+
key += 1
|