cw1521 commited on
Commit
8a60a24
1 Parent(s): 56a86ce

Upload build_dataset.py

Browse files
Files changed (1) hide show
  1. build_dataset.py +148 -0
build_dataset.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
16
+ import json
17
+ import os
18
+
19
+ import datasets
20
+
21
+
22
+
23
+ _CITATION = """\
24
+ @InProceedings{huggingface:dataset,
25
+ title = {Ember2018},
26
+ author=Christian Williams
27
+ },
28
+ year={2023}
29
+ }
30
+ """
31
+
32
+ _DESCRIPTION = """\
33
+ This dataset is from the EMBER 2018 Malware Analysis dataset
34
+ """
35
+ _HOMEPAGE = "https://github.com/elastic/ember"
36
+ _LICENSE = ""
37
+ _URLS = {
38
+ "text_classification": "https://huggingface.co/datasets/cw1521/ember2018-malware/blob/main/data/"
39
+ }
40
+
41
+
42
+ class EMBERConfig(datasets.GeneratorBasedBuilder):
43
+ VERSION = datasets.Version("1.1.0")
44
+ BUILDER_CONFIGS = [
45
+ datasets.BuilderConfig(
46
+ name="text_classification",
47
+ version=VERSION, description="This part of my dataset covers text classification"
48
+ )
49
+ ]
50
+
51
+ DEFAULT_CONFIG_NAME = "text_classification"
52
+
53
+ def _info(self):
54
+ if self.config.name == "text_classification":
55
+ features = datasets.Features(
56
+ {
57
+ "input": datasets.Value("string"),
58
+ "label": datasets.Value("string"),
59
+ "x": datasets.features.Sequence(
60
+ datasets.Value("float32")
61
+ ),
62
+ "y": datasets.Value("float32"),
63
+ "appeared": datasets.Value("string"),
64
+ "avclass": datasets.Value("string"),
65
+ "subset": datasets.Value("string"),
66
+ "sha256": datasets.Value("string")
67
+ }
68
+ )
69
+ else:
70
+ features = datasets.Features(
71
+ {
72
+ "input": datasets.Value("string"),
73
+ "label": datasets.Value("string"),
74
+ "x": datasets.features.Sequence(
75
+ datasets.Value("float32")
76
+ ),
77
+ "y": datasets.Value("float32"),
78
+ "appeared": datasets.Value("string"),
79
+ "avclass": datasets.Value("string"),
80
+ "subset": datasets.Value("string"),
81
+ "sha256": datasets.Value("string")
82
+ }
83
+ )
84
+ return datasets.DatasetInfo(
85
+ description=_DESCRIPTION,
86
+ features=features,
87
+ homepage=_HOMEPAGE,
88
+ license=_LICENSE,
89
+ citation=_CITATION,
90
+ )
91
+
92
+ def _split_generators(self, dl_manager):
93
+ urls = _URLS[self.config.name]
94
+ data_dir = dl_manager.download_and_extract(urls)
95
+ return [
96
+ datasets.SplitGenerator(
97
+ name=datasets.Split.TRAIN,
98
+ gen_kwargs={
99
+ "filepaths": os.path.join(data_dir, "ember2018_train_*.jsonl"),
100
+ "split": "train",
101
+ },
102
+ ),
103
+ # datasets.SplitGenerator(
104
+ # name=datasets.Split.VALIDATION,
105
+ # gen_kwargs={
106
+ # "filepaths": os.path.join(data_dir, "*_valid_*.jsonl"),
107
+ # "split": "valid",
108
+ # },
109
+ # ),
110
+ datasets.SplitGenerator(
111
+ name=datasets.Split.TEST,
112
+ gen_kwargs={
113
+ "filepaths": os.path.join(data_dir, "ember2018_test_*.jsonl"),
114
+ "split": "test"
115
+ },
116
+ )
117
+ ]
118
+
119
+
120
+ def _generate_examples(self, filepaths, split):
121
+ key = 0
122
+ for id, filepath in enumerate(filepaths[split]):
123
+ with open(filepath[id], encoding="utf-8") as f:
124
+ data_list = json.load(f)
125
+ for data in data_list:
126
+ key += 1
127
+ if self.config.name == "text_classification":
128
+ yield key, {
129
+ "input": data["input"],
130
+ "label": data["label"],
131
+ "x": data["x"],
132
+ "y": data["y"],
133
+ "appeared": data["appeared"],
134
+ "avclass": data["avclass"],
135
+ "subset": data["subset"],
136
+ "sha256": data["sha256"]
137
+ }
138
+ else:
139
+ yield key, {
140
+ "input": data["input"],
141
+ "label": data["label"],
142
+ "x": data["x"],
143
+ "y": data["y"],
144
+ "appeared": data["appeared"],
145
+ "avclass": data["avclass"],
146
+ "subset": data["subset"],
147
+ "sha256": data["sha256"]
148
+ }