Francisco Castillo commited on
Commit
b94a097
1 Parent(s): 74bf671

First try to loading script

Browse files
Files changed (1) hide show
  1. reviews_with_drift.py +161 -0
reviews_with_drift.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
15
+ # Lint as: python3
16
+ """IMDb movie revies dataset mixed with Trip Advisor Hotel Reviews to simulate drift accross time."""
17
+
18
+
19
+ import csv
20
+ import json
21
+ import os
22
+
23
+ import datasets
24
+ from datasets.tasks import TextClassification
25
+
26
+
27
+
28
+ # TODO: Add BibTeX citation to our BLOG
29
+ # Find for instance the citation on arxiv or on the dataset repo/website
30
+ _CITATION = ""
31
+ # _CITATION = """\
32
+ # @InProceedings{huggingface:dataset,
33
+ # title = {A great new dataset},
34
+ # author={huggingface, Inc.
35
+ # },
36
+ # year={2020}
37
+ # }
38
+ # """
39
+
40
+ # TODO: Add description of the dataset here
41
+ # You can copy an official description
42
+ _DESCRIPTION = """\
43
+ This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
44
+ """
45
+
46
+ # TODO: Add a link to an official homepage for the dataset here
47
+ _HOMEPAGE = ""
48
+
49
+ # TODO: Add the licence for the dataset here if you can find it
50
+ _LICENSE = ""
51
+
52
+ # TODO: Add link to the official dataset URLs here
53
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
54
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
55
+ _URL = "https://huggingface.co/datasets/arize-ai/reviews_with_drift/"
56
+ _URLS = {
57
+ "training": _URL + "training.csv",
58
+ "validation": _URL + "validation.csv",
59
+ "production": _URL + "production.csv",
60
+ }
61
+
62
+
63
+ # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
64
+ class NewDataset(datasets.GeneratorBasedBuilder):
65
+ """TODO: Short description of my dataset."""
66
+
67
+ VERSION = datasets.Version("1.0")
68
+
69
+ # This is an example of a dataset with multiple configurations.
70
+ # If you don't want/need to define several sub-sets in your dataset,
71
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
72
+
73
+ # If you need to make complex sub-parts in the datasets with configurable options
74
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
75
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
76
+
77
+ # You will be able to load one or the other configurations in the following list with
78
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
79
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
80
+ BUILDER_CONFIGS = [
81
+ datasets.BuilderConfig(name="training", version=VERSION, description="Training set"),
82
+ datasets.BuilderConfig(name="validation", version=VERSION, description="Validation set"),
83
+ datasets.BuilderConfig(name="production", version=VERSION, description="Production set"),
84
+ ]
85
+
86
+ DEFAULT_CONFIG_NAME = "training" # It's not mandatory to have a default configuration. Just use one if it make sense.
87
+
88
+ def _info(self):
89
+ # This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
90
+ features = datasets.Features(
91
+ # These are the features of your dataset like images, labels ...
92
+ {
93
+ "prediction_ts": datasets.Value("float"),
94
+ "age":datasets.Value("int"),
95
+ "gender":datasets.Value("string"),
96
+ "context":datasets.Value("string"),
97
+ "text":datasets.Value("string"),
98
+ "label":datasets.features.ClassLabel(names=["Negative", "Positive"]),
99
+ }
100
+ )
101
+
102
+ return datasets.DatasetInfo(
103
+ # This is the description that will appear on the datasets page.
104
+ description=_DESCRIPTION,
105
+ # This defines the different columns of the dataset and their types
106
+ features=features, # Here we define them above because they are different between the two configurations
107
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
108
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
109
+ supervised_keys=("text", "label"),
110
+ # Homepage of the dataset for documentation
111
+ # License for the dataset if available
112
+ license=_LICENSE,
113
+ # Citation for the dataset
114
+ citation=_CITATION,
115
+ task_templates=[TextClassification(text_column="text", label_column="label")],
116
+ )
117
+
118
+ def _split_generators(self, dl_manager):
119
+ # This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
120
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
121
+
122
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
123
+ # 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.
124
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
125
+ urls = _URLS[self.config.name]
126
+ data_dir = dl_manager.download_and_extract(urls)
127
+ return [
128
+ datasets.SplitGenerator(
129
+ name=datasets.Split.TRAIN,
130
+ # These kwargs will be passed to _generate_examples
131
+ gen_kwargs={
132
+ "filepath": os.path.join(data_dir, "training.csv"),
133
+ "split": "train",
134
+ },
135
+ ),
136
+ datasets.SplitGenerator(
137
+ name=datasets.Split.TEST,
138
+ # These kwargs will be passed to _generate_examples
139
+ gen_kwargs={
140
+ "filepath": os.path.join(data_dir, "validation.csv"),
141
+ "split": "validation"
142
+ },
143
+ ),
144
+ datasets.SplitGenerator(
145
+ name=datasets.Split.VALIDATION,
146
+ # These kwargs will be passed to _generate_examples
147
+ gen_kwargs={
148
+ "filepath": os.path.join(data_dir, "production.csv"),
149
+ "split": "production",
150
+ },
151
+ ),
152
+ ]
153
+
154
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
155
+ def _generate_examples(self, filepath, split):
156
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
157
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
158
+ with open(filepath, encoding="utf-8") as f:
159
+ for key, row in enumerate(f):
160
+ data = json.loads(row)
161
+ yield key, {"text":data["text"]}