Hieu Nguyen commited on
Commit
edd7e16
·
1 Parent(s): 63605dc

First version of deep research dataset

Browse files
Files changed (1) hide show
  1. deep-research.py +175 -0
deep-research.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 json
19
+ import os
20
+
21
+ import datasets
22
+
23
+
24
+ # TODO: Add BibTeX citation
25
+ # Find for instance the citation on arxiv or on the dataset repo/website
26
+ _CITATION = """\
27
+ @InProceedings{huggingface:dataset,
28
+ title = {A great new dataset},
29
+ author={huggingface, Inc.
30
+ },
31
+ year={2020}
32
+ }
33
+ """
34
+
35
+ # TODO: Add description of the dataset here
36
+ # You can copy an official description
37
+ _DESCRIPTION = """\
38
+ This dataset is for research at DeepUSC.
39
+ """
40
+
41
+ # TODO: Add a link to an official homepage for the dataset here
42
+ _HOMEPAGE = "https://deep.usc.edu/"
43
+
44
+ # TODO: Add the licence for the dataset here if you can find it
45
+ _LICENSE = ""
46
+
47
+ # TODO: Add link to the official dataset URLs here
48
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
49
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
50
+ _URLS = {
51
+ "train": "https://rajpurkar.github.io/SQuAD-explorer/dataset/train-v1.1.json",
52
+ "dev": "https://rajpurkar.github.io/SQuAD-explorer/dataset/dev-v1.1.json"
53
+ }
54
+
55
+
56
+ # TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
57
+ class NewDataset(datasets.GeneratorBasedBuilder):
58
+ """TODO: Short description of my dataset."""
59
+
60
+ VERSION = datasets.Version("1.1.0")
61
+
62
+ # This is an example of a dataset with multiple configurations.
63
+ # If you don't want/need to define several sub-sets in your dataset,
64
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
65
+
66
+ # If you need to make complex sub-parts in the datasets with configurable options
67
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
68
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
69
+
70
+ # You will be able to load one or the other configurations in the following list with
71
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
72
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
73
+ BUILDER_CONFIGS = [
74
+ datasets.BuilderConfig(name="train", version=VERSION, description="This part of the dataset covers the train set"),
75
+ datasets.BuilderConfig(name="dev", version=VERSION, description="This part of the dataset covers the dev set"),
76
+ ]
77
+
78
+ DEFAULT_CONFIG_NAME = "train" # It's not mandatory to have a default configuration. Just use one if it make sense.
79
+
80
+ def _info(self):
81
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
82
+ # if self.config.name == "train": # This is the name of the configuration selected in BUILDER_CONFIGS above
83
+ features = datasets.Features(
84
+ {
85
+ "data": datasets.Sequence(
86
+ {
87
+ "title": datasets.Value("string"),
88
+ "paragraphs": datasets.Sequence(
89
+ {
90
+ "context": datasets.Value("string"),
91
+ "qas": datasets.Sequence(
92
+ {
93
+ "answers": datasets.Sequence(
94
+ {
95
+ "answer_start": datasets.Value("int8"),
96
+ "text": datasets.Value("string")
97
+ },
98
+ ),
99
+ "question": datasets.Value("string"),
100
+ "id": datasets.Value("string")
101
+ }
102
+ )
103
+ }
104
+ )
105
+ }
106
+ ),
107
+ "version": datasets.Value("string")
108
+ # These are the features of your dataset like images, labels ...
109
+ }
110
+ )
111
+ return datasets.DatasetInfo(
112
+ # This is the description that will appear on the datasets page.
113
+ description=_DESCRIPTION,
114
+ # This defines the different columns of the dataset and their types
115
+ features=features, # Here we define them above because they are different between the two configurations
116
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
117
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
118
+ # supervised_keys=("sentence", "label"),
119
+ # Homepage of the dataset for documentation
120
+ homepage=_HOMEPAGE,
121
+ # License for the dataset if available
122
+ license=_LICENSE,
123
+ # Citation for the dataset
124
+ citation=_CITATION,
125
+ )
126
+
127
+ def _split_generators(self, dl_manager):
128
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
129
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
130
+
131
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
132
+ # 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.
133
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
134
+ # urls = _URLS[self.config.name]
135
+ data_dir = dl_manager.download_and_extract(_URLS)
136
+ return [
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.TRAIN,
139
+ # These kwargs will be passed to _generate_examples
140
+ gen_kwargs={"filepath": data_dir["train"]}
141
+ ),
142
+ datasets.SplitGenerator(
143
+ name=datasets.Split.VALIDATION,
144
+ # These kwargs will be passed to _generate_examples
145
+ gen_kwargs={"filepath": data_dir["dev"]}
146
+ )
147
+ ]
148
+
149
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
150
+ def _generate_examples(self, filepath, split):
151
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
152
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
153
+ with open(filepath, encoding="utf-8") as f:
154
+ for key, row in enumerate(f):
155
+ data = json.loads(row)
156
+ for article in data["data"]:
157
+ title = article.get("title", "")
158
+ for paragraph in article["paragraphs"]:
159
+ context = paragraph["context"] # do not strip leading blank spaces GH-2585
160
+ for qa in paragraph["qas"]:
161
+ answer_starts = [answer["answer_start"] for answer in qa["answers"]]
162
+ answers = [answer["text"] for answer in qa["answers"]]
163
+ # Features currently used are "context", "question", and "answers".
164
+ # Others are extracted here for the ease of future expansions.
165
+ yield key, {
166
+ "title": title,
167
+ "context": context,
168
+ "question": qa["question"],
169
+ "id": qa["id"],
170
+ "answers": {
171
+ "answer_start": answer_starts,
172
+ "text": answers,
173
+ },
174
+ }
175
+ key += 1