the_pile_github / the_pile_github.py
andstor's picture
Add validation and test data + restructure train files
2a4b729
raw
history blame
12.2 kB
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""GitHub subset of The Pile."""
import os
import re
import pandas as pd
import datasets
_CITATION = """\
@misc{gao2020pile,
title={The Pile: An 800GB Dataset of Diverse Text for Language Modeling},
author={Leo Gao and Stella Biderman and Sid Black and Laurence Golding and Travis Hoppe and Charles Foster and Jason Phang and Horace He and Anish Thite and Noa Nabeshima and Shawn Presser and Connor Leahy},
year={2020},
eprint={2101.00027},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
"""
_DESCRIPTION = """\
The Pile is a 825 GiB diverse, open source language modelling data set that consists of 22 smaller, high-quality
datasets combined together.
"""
_HOMEPAGE = "https://pile.eleuther.ai/"
# TODO: Add the license for the dataset here if you can find it
_LICENSE = ""
# Add link to the official dataset URLs here
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
_URLS = {
"train": {
"batchfile": [f"train/data/Batchfile/part.{part}.parquet" for part in range(3)],
"c#": [f"train/data/C#/part.{part}.parquet" for part in range(10)],
"c++": [f"train/data/C++/part.{part}.parquet" for part in range(15)],
"css": [f"train/data/CSS/part.{part}.parquet" for part in range(3)],
"erlang": [f"train/data/Erlang/part.{part}.parquet" for part in range(1)],
"go": [f"train/data/Go/part.{part}.parquet" for part in range(15)],
"haskell": [f"train/data/Haskell/part.{part}.parquet" for part in range(2)],
"html": [f"train/data/HTML/part.{part}.parquet" for part in range(15)],
"java": [f"train/data/Java/part.{part}.parquet" for part in range(15)],
"javascript": [f"train/data/JavaScript/part.{part}.parquet" for part in range(15)],
"jupyter_notebook": [f"train/data/Jupyter Notebook/part.{part}.parquet" for part in range(2)],
"lua": [f"train/data/Lua/part.{part}.parquet" for part in range(15)],
"markdown": [f"train/data/Markdown/part.{part}.parquet" for part in range(15)],
"matlab": [f"train/data/Matlab/part.{part}.parquet" for part in range(1)],
"objective-c": [f"train/data/Objective-C/part.{part}.parquet" for part in range(5)],
"perl": [f"train/data/Perl/part.{part}.parquet" for part in range(3)],
"php": [f"train/data/PHP/part.{part}.parquet" for part in range(8)],
"powershell": [f"train/data/PowerShell/part.{part}.parquet" for part in range(15)],
"python": [f"train/data/Python/part.{part}.parquet" for part in range(10)],
"r": [f"train/data/R/part.{part}.parquet" for part in range(2)],
"ruby": [f"train/data/Ruby/part.{part}.parquet" for part in range(4)],
"rust": [f"train/data/Rust/part.{part}.parquet" for part in range(2)],
"scala": [f"train/data/Scala/part.{part}.parquet" for part in range(3)],
"shell": [f"train/data/Shell/part.{part}.parquet" for part in range(6)],
"sql": [f"train/data/SQL/part.{part}.parquet" for part in range(4)],
"swift": [f"train/data/Swift/part.{part}.parquet" for part in range(2)],
"tex": [f"train/data/TeX/part.{part}.parquet" for part in range(3)],
"typescript": [f"train/data/TypeScript/part.{part}.parquet" for part in range(6)],
"none": [f"train/data/None/part.{part}.parquet" for part in range(1)],
},
"dev": "data/validation.parquet",
"test": "data/test.parquet",
}
# Add all existing urls to the dict
_URLS["train"]["all"] = [url for urls in _URLS["train"].values() for url in urls]
# Name of the dataset usually match the script name with CamelCase instead of snake_case
class SmartContracts(datasets.GeneratorBasedBuilder):
"""Smart Contracts Dataset."""
VERSION = datasets.Version("1.0.0")
# You will be able to load one or the other configurations in the following list with
# data = datasets.load_dataset('my_dataset', 'all')
# data = datasets.load_dataset('my_dataset', 'plain_text')
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="all", version=VERSION, description="All programing languages"),
datasets.BuilderConfig(name="batchfile", version=VERSION, description="Batchfile programming language"),
datasets.BuilderConfig(name="c#", version=VERSION, description="C# programming language"),
datasets.BuilderConfig(name="c++", version=VERSION, description="C++ programming language"),
datasets.BuilderConfig(name="css", version=VERSION, description="CSS programming language"),
datasets.BuilderConfig(name="erlang", version=VERSION, description="Erlang programming language"),
datasets.BuilderConfig(name="go", version=VERSION, description="Go programming language"),
datasets.BuilderConfig(name="haskell", version=VERSION, description="Haskell programming language"),
datasets.BuilderConfig(name="html", version=VERSION, description="HTML programming language"),
datasets.BuilderConfig(name="java", version=VERSION, description="Java programming language"),
datasets.BuilderConfig(name="javascript", version=VERSION, description="JavaScript programming language"),
datasets.BuilderConfig(name="jupyter_notebook", version=VERSION, description="Jupyter Notebook programming language"),
datasets.BuilderConfig(name="lua", version=VERSION, description="Lua programming language"),
datasets.BuilderConfig(name="markdown", version=VERSION, description="Markdown programming language"),
datasets.BuilderConfig(name="matlab", version=VERSION, description="Matlab programming language"),
datasets.BuilderConfig(name="none", version=VERSION, description="None programming language"),
datasets.BuilderConfig(name="objective-c", version=VERSION, description="Objective-C programming language"),
datasets.BuilderConfig(name="perl", version=VERSION, description="Perl programming language"),
datasets.BuilderConfig(name="php", version=VERSION, description="PHP programming language"),
datasets.BuilderConfig(name="powershell", version=VERSION, description="PowerShell programming language"),
datasets.BuilderConfig(name="python", version=VERSION, description="Python programming language"),
datasets.BuilderConfig(name="r", version=VERSION, description="R programming language"),
datasets.BuilderConfig(name="ruby", version=VERSION, description="Ruby programming language"),
datasets.BuilderConfig(name="rust", version=VERSION, description="Rust programming language"),
datasets.BuilderConfig(name="scala", version=VERSION, description="Scala programming language"),
datasets.BuilderConfig(name="shell", version=VERSION, description="Shell programming language"),
datasets.BuilderConfig(name="sql", version=VERSION, description="SQL programming language"),
datasets.BuilderConfig(name="swift", version=VERSION, description="Swift programming language"),
datasets.BuilderConfig(name="tex", version=VERSION, description="TeX programming language"),
datasets.BuilderConfig(name="typescript", version=VERSION, description="TypeScript programming language"),
]
DEFAULT_CONFIG_NAME = "all" # It's not mandatory to have a default configuration. Just use one if it make sense.
def _info(self):
# This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
features = datasets.Features(
{
"text": datasets.Value("string"),
"meta": datasets.Sequence(feature={'language': datasets.Value('string')}),
}
)
return datasets.DatasetInfo(
# This is the description that will appear on the datasets page.
description=_DESCRIPTION,
# This defines the different columns of the dataset and their types
features=features, # Here we define them above because they are different between the two configurations
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
# supervised_keys=("sentence", "label"),
# Homepage of the dataset for documentation
homepage=_HOMEPAGE,
# License for the dataset if available
license=_LICENSE,
# Citation for the dataset
citation=_CITATION,
)
def _split_generators(self, dl_manager):
# This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
train_urls = _URLS["train"][self.config.name]
train_files = dl_manager.download_and_extract(train_urls)
dev_files = dl_manager.download_and_extract(_URLS["dev"])
test_files = dl_manager.download_and_extract(_URLS["test"])
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"split_key": "train", "files": train_files}),
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"split_key": "validation", "files": dev_files}),
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"split_key": "test", "files": test_files}),
]
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
def _generate_examples(self, split_key, files):
"""Yields examples."""
# This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
#data = pd.read_parquet(filepath)
key = 0
for path in files:
data = pd.read_parquet(path)
for row in data.itertuples():
if split_key == "train":
yield key, {
"text": row.text,
"meta": row.meta,
}
elif split_key in ["validation", "test"]:
if self.config.name == "all":
yield key, {
"text": row.text,
"meta": row.meta,
}
else:
language = row.meta[0]["language"].lower().replace(" ", "_") # e.g. "Jupyter Notebook" -> "jupyter_notebook"
if language == self.config.name:
yield key, {
"text": row.text,
"meta": row.meta,
}
else:
continue
key += 1