neel-alex commited on
Commit
0e4d757
1 Parent(s): 90d7b47

First commit, test upload

Browse files
Files changed (1) hide show
  1. raft.py +162 -0
raft.py ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 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
+ """RAFT AI papers, test set."""
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
+ # You can copy an official description
37
+ _DESCRIPTION = """\
38
+ This dataset contains a corpus of AI papers. The first task is to determine\
39
+ whether or not a datapoint is an AI safety paper. The second task is to\
40
+ determine what type of paper it is.
41
+ """
42
+
43
+ # TODO: Add a link to an official homepage for the dataset here
44
+ _HOMEPAGE = ""
45
+
46
+ # TODO: Add the licence for the dataset here if you can find it
47
+ _LICENSE = ""
48
+
49
+ # TODO: Add link to the official dataset URLs here
50
+ # The HuggingFace dataset library don't host the datasets but only point to the original files
51
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
52
+ _URLs = {
53
+ 'train': "https://raw.githubusercontent.com/neel-alex/raft/master/AISafety_train.csv",
54
+ 'test': "https://raw.githubusercontent.com/neel-alex/raft/master/AISafety_test_unlabeled.csv"
55
+ }
56
+
57
+
58
+ class RaftAisafetyTest(datasets.GeneratorBasedBuilder):
59
+ """Dataset of AI papers for safety-related classification."""
60
+
61
+ VERSION = datasets.Version("1.1.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="safety_or_not", version=VERSION,
76
+ description="Decide whether the papers focus on AI safety methods."),
77
+ datasets.BuilderConfig(name="safety_type", version=VERSION,
78
+ description="If a paper has AI safety methods, determine if it is meta"
79
+ "safety or technical safety."),
80
+ ]
81
+
82
+ DEFAULT_CONFIG_NAME = "safety_or_not" # It's not mandatory to have a default configuration. Just use one if it make sense.
83
+
84
+ def _info(self):
85
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
86
+ if self.config.name == "safety_or_not": # This is the name of the configuration selected in BUILDER_CONFIGS above
87
+ features = datasets.Features(
88
+ {
89
+ "title": datasets.Value("string"),
90
+ "publication": datasets.Value("string"),
91
+ "abstract": datasets.Value("string"),
92
+ "answer": datasets.Value("string"),
93
+ }
94
+ )
95
+ else: # This is an example to show how to have different features for "first_domain" and "second_domain"
96
+ features = datasets.Features(
97
+ {
98
+ "title": datasets.Value("string"),
99
+ "publication": datasets.Value("string"),
100
+ "abstract": datasets.Value("string"),
101
+ "answer": datasets.Value("string"),
102
+ }
103
+ )
104
+ return datasets.DatasetInfo(
105
+ # This is the description that will appear on the datasets page.
106
+ description=_DESCRIPTION,
107
+ # This defines the different columns of the dataset and their types
108
+ features=features, # Here we define them above because they are different between the two configurations
109
+ # If there's a common (input, target) tuple from the features,
110
+ # specify them here. They'll be used if as_supervised=True in
111
+ # builder.as_dataset.
112
+ supervised_keys=None,
113
+ # Homepage of the dataset for documentation
114
+ homepage=_HOMEPAGE,
115
+ # License for the dataset if available
116
+ license=_LICENSE,
117
+ # Citation for the dataset
118
+ citation=_CITATION,
119
+ )
120
+
121
+ def _split_generators(self, dl_manager):
122
+ """Returns SplitGenerators."""
123
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
124
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
125
+
126
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
127
+ # 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.
128
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
129
+ data_dir = dl_manager.download_and_extract(_URLs)
130
+ return [
131
+ datasets.SplitGenerator(name=datasets.Split.TRAIN,
132
+ gen_kwargs={"filepath": data_dir['train'],
133
+ "split": "train"}),
134
+ datasets.SplitGenerator(name=datasets.Split.TEST,
135
+ gen_kwargs={"filepath": data_dir['test'],
136
+ "split": "test"})
137
+ ]
138
+
139
+ def _generate_examples(
140
+ self, filepath, split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
141
+ ):
142
+ """ Yields examples as (key, example) tuples. """
143
+ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
144
+ # The `key` is here for legacy reason (tfds) and is not important in itself.
145
+
146
+ with open(filepath, encoding="utf-8") as f:
147
+ csv_reader = csv.reader(
148
+ f, quotechar='"', delimiter=",", quoting=csv.QUOTE_ALL, skipinitialspace=True
149
+ )
150
+ for id_, row in enumerate(csv_reader):
151
+ if id_ == 0: # First row is column names
152
+ continue
153
+ if split == "train":
154
+ title, publication, abstract, category, binary = row
155
+ answer = category if self.config.name == "safety_type" else binary
156
+ elif split == "test":
157
+ title, publication, abstract = row
158
+ answer = ""
159
+ yield id_, {"title": title,
160
+ "publication": publication,
161
+ "abstract": abstract,
162
+ "answer": answer}