Sampson2022
commited on
Commit
•
9299753
1
Parent(s):
fd63b96
Create demo2.py
Browse files
demo2.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
# TODO: Address all TODOs and remove all explanatory comments
|
15 |
+
"""TODO: Add a description here."""
|
16 |
+
|
17 |
+
|
18 |
+
import csv
|
19 |
+
import json
|
20 |
+
import os
|
21 |
+
|
22 |
+
import datasets
|
23 |
+
|
24 |
+
|
25 |
+
# TODO: Add BibTeX citation
|
26 |
+
# Find for instance the citation on arxiv or on the dataset repo/website
|
27 |
+
_CITATION = """
|
28 |
+
@InProceedings{huggingface:dataset,
|
29 |
+
title = {A great new dataset},
|
30 |
+
author={huggingface, Inc.
|
31 |
+
},
|
32 |
+
year={2020}
|
33 |
+
}
|
34 |
+
"""
|
35 |
+
|
36 |
+
# TODO: Add description of the dataset here
|
37 |
+
# You can copy an official description
|
38 |
+
_DESCRIPTION = """
|
39 |
+
This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
|
40 |
+
"""
|
41 |
+
|
42 |
+
# TODO: Add a link to an official homepage for the dataset here
|
43 |
+
_HOMEPAGE = ""
|
44 |
+
|
45 |
+
# TODO: Add the licence for the dataset here if you can find it
|
46 |
+
_LICENSE = ""
|
47 |
+
|
48 |
+
# TODO: Add link to the official dataset URLs here
|
49 |
+
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
|
50 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
51 |
+
_URLS = {
|
52 |
+
"train": "https://huggingface.co/datasets/Sampson2022/demo/raw/main/train.csv",
|
53 |
+
"test": "https://huggingface.co/datasets/Sampson2022/demo/raw/main/test.csv",
|
54 |
+
}
|
55 |
+
|
56 |
+
|
57 |
+
# TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
|
58 |
+
class DemoDataset(datasets.GeneratorBasedBuilder):
|
59 |
+
"""TODO: Short description of my dataset."""
|
60 |
+
|
61 |
+
VERSION = datasets.Version("1.0.0")
|
62 |
+
|
63 |
+
# This is an example of a dataset with multiple configurations.
|
64 |
+
# If you don't want/need to define several sub-sets in your dataset,
|
65 |
+
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
|
66 |
+
|
67 |
+
# If you need to make complex sub-parts in the datasets with configurable options
|
68 |
+
# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
|
69 |
+
# BUILDER_CONFIG_CLASS = MyBuilderConfig
|
70 |
+
|
71 |
+
# You will be able to load one or the other configurations in the following list with
|
72 |
+
# data = datasets.load_dataset('my_dataset', 'first_domain')
|
73 |
+
# data = datasets.load_dataset('my_dataset', 'second_domain')
|
74 |
+
BUILDER_CONFIGS = [
|
75 |
+
datasets.BuilderConfig(name="plain_text", version=VERSION, description="Plain text")
|
76 |
+
]
|
77 |
+
|
78 |
+
def _info(self):
|
79 |
+
features = datasets.Features(
|
80 |
+
{
|
81 |
+
"index": datasets.Value("string"),
|
82 |
+
"text": datasets.Value("string"),
|
83 |
+
"label": datasets.Value("string")
|
84 |
+
}
|
85 |
+
)
|
86 |
+
|
87 |
+
return datasets.DatasetInfo(
|
88 |
+
description=_DESCRIPTION,
|
89 |
+
# This defines the different columns of the dataset and their types
|
90 |
+
features=features, # Here we define them above because they are different between the two configurations
|
91 |
+
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
|
92 |
+
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
|
93 |
+
# supervised_keys=("sentence", "label"),
|
94 |
+
# Homepage of the dataset for documentation
|
95 |
+
homepage=_HOMEPAGE,
|
96 |
+
# License for the dataset if available
|
97 |
+
license=_LICENSE,
|
98 |
+
# Citation for the dataset
|
99 |
+
citation=_CITATION,
|
100 |
+
)
|
101 |
+
|
102 |
+
def _split_generators(self, dl_manager):
|
103 |
+
download_files = dl_manager.download_and_extract(_URLS)
|
104 |
+
return [
|
105 |
+
datasets.SplitGenerator(
|
106 |
+
name=datasets.Split.TRAIN,
|
107 |
+
gen_kwargs={
|
108 |
+
"filepath": download_files['train']
|
109 |
+
},
|
110 |
+
),
|
111 |
+
datasets.SplitGenerator(
|
112 |
+
name=datasets.Split.TEST,
|
113 |
+
# These kwargs will be passed to _generate_examples
|
114 |
+
gen_kwargs={
|
115 |
+
"filepath": download_files['test']
|
116 |
+
},
|
117 |
+
),
|
118 |
+
]
|
119 |
+
|
120 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
121 |
+
def _generate_examples(self, filepath, split):
|
122 |
+
key = 0
|
123 |
+
with open(filepath, encoding="utf-8") as f:
|
124 |
+
reader = csv.DictReader(f)
|
125 |
+
for key, row in enumerate(reader):
|
126 |
+
yield key, {
|
127 |
+
"index": row["index"],
|
128 |
+
"text": row["text"],
|
129 |
+
"label": row["label"]
|
130 |
+
}
|
131 |
+
key += 1
|