Benjamin Steenhoek commited on
Commit
1ca4486
Β·
1 Parent(s): 5f58e8e

Upload dataset creation/split script

Browse files
Files changed (1) hide show
  1. create_dataset.ipynb +181 -0
create_dataset.ipynb ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 4,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "data": {
10
+ "text/plain": [
11
+ "True"
12
+ ]
13
+ },
14
+ "execution_count": 4,
15
+ "metadata": {},
16
+ "output_type": "execute_result"
17
+ }
18
+ ],
19
+ "source": [
20
+ "import dotenv\n",
21
+ "dotenv.load_dotenv()"
22
+ ]
23
+ },
24
+ {
25
+ "cell_type": "code",
26
+ "execution_count": 15,
27
+ "metadata": {},
28
+ "outputs": [
29
+ {
30
+ "name": "stdout",
31
+ "output_type": "stream",
32
+ "text": [
33
+ "DatasetDict({\n",
34
+ " train: Dataset({\n",
35
+ " features: ['func', 'target', 'cwe', 'project', 'commit_id', 'hash', 'size', 'message'],\n",
36
+ " num_rows: 264393\n",
37
+ " })\n",
38
+ " validation: Dataset({\n",
39
+ " features: ['func', 'target', 'cwe', 'project', 'commit_id', 'hash', 'size', 'message'],\n",
40
+ " num_rows: 33049\n",
41
+ " })\n",
42
+ " test: Dataset({\n",
43
+ " features: ['func', 'target', 'cwe', 'project', 'commit_id', 'hash', 'size', 'message'],\n",
44
+ " num_rows: 33050\n",
45
+ " })\n",
46
+ "})\n",
47
+ "{'func': 'static boolean ReadICCProfile(j_decompress_ptr jpeg_info)\\n{\\n char\\n magick[12];\\n\\n ErrorManager\\n *error_manager;\\n\\n ExceptionInfo\\n *exception;\\n\\n Image\\n *image;\\n\\n MagickBooleanType\\n status;\\n\\n register ssize_t\\n i;\\n\\n register unsigned char\\n *p;\\n\\n size_t\\n length;\\n\\n StringInfo\\n *icc_profile,\\n *profile;\\n\\n /*\\n Read color profile.\\n */\\n length=(size_t) ((size_t) GetCharacter(jpeg_info) << 8);\\n length+=(size_t) GetCharacter(jpeg_info);\\n length-=2;\\n if (length <= 14)\\n {\\n while (length-- > 0)\\n if (GetCharacter(jpeg_info) == EOF)\\n break;\\n return(TRUE);\\n }\\n for (i=0; i < 12; i++)\\n magick[i]=(char) GetCharacter(jpeg_info);\\n if (LocaleCompare(magick,ICC_PROFILE) != 0)\\n {\\n /*\\n Not a ICC profile, return.\\n */\\n for (i=0; i < (ssize_t) (length-12); i++)\\n if (GetCharacter(jpeg_info) == EOF)\\n break;\\n return(TRUE);\\n }\\n (void) GetCharacter(jpeg_info); /* id */\\n (void) GetCharacter(jpeg_info); /* markers */\\n length-=14;\\n error_manager=(ErrorManager *) jpeg_info->client_data;\\n exception=error_manager->exception;\\n image=error_manager->image;\\n profile=BlobToStringInfo((const void *) NULL,length);\\n if (profile == (StringInfo *) NULL)\\n {\\n (void) ThrowMagickException(exception,GetMagickModule(),\\n ResourceLimitError,\"MemoryAllocationFailed\",\"`%s\\'\",image->filename);\\n return(FALSE);\\n }\\n error_manager->profile=profile;\\n p=GetStringInfoDatum(profile);\\n for (i=0; i < (ssize_t) length; i++)\\n {\\n int\\n c;\\n\\n c=GetCharacter(jpeg_info);\\n if (c == EOF)\\n break;\\n *p++=(unsigned char) c;\\n }\\n if (i != (ssize_t) length)\\n {\\n profile=DestroyStringInfo(profile);\\n (void) ThrowMagickException(exception,GetMagickModule(),\\n CorruptImageError,\"InsufficientImageDataInFile\",\"`%s\\'\",\\n image->filename);\\n return(FALSE);\\n }\\n error_manager->profile=NULL;\\n icc_profile=(StringInfo *) GetImageProfile(image,\"icc\");\\n if (icc_profile != (StringInfo *) NULL)\\n {\\n ConcatenateStringInfo(icc_profile,profile);\\n profile=DestroyStringInfo(profile);\\n }\\n else\\n {\\n status=SetImageProfile(image,\"icc\",profile,exception);\\n profile=DestroyStringInfo(profile);\\n if (status == MagickFalse)\\n {\\n (void) ThrowMagickException(exception,GetMagickModule(),\\n ResourceLimitError,\"MemoryAllocationFailed\",\"`%s\\'\",image->filename);\\n return(FALSE);\\n }\\n }\\n if (image->debug != MagickFalse)\\n (void) LogMagickEvent(CoderEvent,GetMagickModule(),\\n \"Profile: ICC, %.20g bytes\",(double) length);\\n return(TRUE);\\n}', 'target': 1, 'cwe': ['CWE-416'], 'project': 'ImageMagick', 'commit_id': '39f226a9c137f547e12afde972eeba7551124493', 'hash': 1.623740923374004e+38, 'size': 111, 'message': 'https://github.com/ImageMagick/ImageMagick/issues/1641'}\n"
48
+ ]
49
+ }
50
+ ],
51
+ "source": [
52
+ "from datasets import load_dataset, DatasetDict\n",
53
+ "import os\n",
54
+ "\n",
55
+ "dataset = load_dataset(\"json\", data_files=\"diversevul_20230702.jsonl\")\n",
56
+ "\n",
57
+ "# Split into train/valid/test\n",
58
+ "train_valid = dataset[\"train\"].train_test_split(test_size=0.2, seed=0, train_indices_cache_file_name=\"train.indices\")\n",
59
+ "train_data, valid_data = train_valid[\"train\"], train_valid[\"test\"]\n",
60
+ "valid_test = valid_data.train_test_split(test_size=0.5, seed=0, train_indices_cache_file_name=\"valid.indices\", test_indices_cache_file_name=\"test.indices\")\n",
61
+ "valid_data, test_data = valid_test[\"train\"], valid_test[\"test\"]\n",
62
+ "dataset = DatasetDict({\n",
63
+ " \"train\": train_data,\n",
64
+ " \"validation\": valid_data,\n",
65
+ " \"test\": test_data,\n",
66
+ "})\n",
67
+ "\n",
68
+ "print(dataset)\n",
69
+ "print(dataset[\"train\"][0])"
70
+ ]
71
+ },
72
+ {
73
+ "cell_type": "code",
74
+ "execution_count": null,
75
+ "metadata": {},
76
+ "outputs": [],
77
+ "source": [
78
+ "dataset.save_to_disk(\"diversevul\")"
79
+ ]
80
+ },
81
+ {
82
+ "cell_type": "code",
83
+ "execution_count": 6,
84
+ "metadata": {},
85
+ "outputs": [
86
+ {
87
+ "name": "stderr",
88
+ "output_type": "stream",
89
+ "text": [
90
+ "Pushing dataset shards to the dataset hub: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 2/2 [00:00<00:00, 543.87it/s]\n",
91
+ "\n",
92
+ "\u001b[A\n",
93
+ "\u001b[A\n",
94
+ "\u001b[A\n",
95
+ "\u001b[A\n",
96
+ "\u001b[A\n",
97
+ "\u001b[A\n",
98
+ "\u001b[A\n",
99
+ "\u001b[A\n",
100
+ "\u001b[A\n",
101
+ "\u001b[A\n",
102
+ "\u001b[A\n",
103
+ "\u001b[A\n",
104
+ "\u001b[A\n",
105
+ "\u001b[A\n",
106
+ "\u001b[A\n",
107
+ "\u001b[A\n",
108
+ "\u001b[A\n",
109
+ "\u001b[A\n",
110
+ "\u001b[A\n",
111
+ "\u001b[A\n",
112
+ "\u001b[A\n",
113
+ "\u001b[A\n",
114
+ "\u001b[A\n",
115
+ "Creating parquet from Arrow format: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 34/34 [00:03<00:00, 8.88ba/s]\n",
116
+ "Pushing dataset shards to the dataset hub: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1/1 [00:12<00:00, 12.34s/it]\n",
117
+ "\n",
118
+ "\u001b[A\n",
119
+ "\u001b[A\n",
120
+ "\u001b[A\n",
121
+ "\u001b[A\n",
122
+ "\u001b[A\n",
123
+ "\u001b[A\n",
124
+ "\u001b[A\n",
125
+ "\u001b[A\n",
126
+ "\u001b[A\n",
127
+ "\u001b[A\n",
128
+ "\u001b[A\n",
129
+ "\u001b[A\n",
130
+ "\u001b[A\n",
131
+ "\u001b[A\n",
132
+ "\u001b[A\n",
133
+ "\u001b[A\n",
134
+ "\u001b[A\n",
135
+ "\u001b[A\n",
136
+ "\u001b[A\n",
137
+ "\u001b[A\n",
138
+ "\u001b[A\n",
139
+ "\u001b[A\n",
140
+ "\u001b[A\n",
141
+ "\u001b[A\n",
142
+ "\u001b[A\n",
143
+ "\u001b[A\n",
144
+ "\u001b[A\n",
145
+ "\u001b[A\n",
146
+ "\u001b[A\n",
147
+ "\u001b[A\n",
148
+ "Creating parquet from Arrow format: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 34/34 [00:03<00:00, 9.98ba/s]\n",
149
+ "Pushing dataset shards to the dataset hub: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1/1 [00:11<00:00, 11.55s/it]\n",
150
+ "Downloading metadata: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 263/263 [00:00<00:00, 495kB/s]\n"
151
+ ]
152
+ }
153
+ ],
154
+ "source": [
155
+ "dataset.push_to_hub(\"benjis/diversevul\", token=os.getenv(\"HUGGINGFACEHUB_API_TOKEN\"))"
156
+ ]
157
+ }
158
+ ],
159
+ "metadata": {
160
+ "kernelspec": {
161
+ "display_name": "Python 3",
162
+ "language": "python",
163
+ "name": "python3"
164
+ },
165
+ "language_info": {
166
+ "codemirror_mode": {
167
+ "name": "ipython",
168
+ "version": 3
169
+ },
170
+ "file_extension": ".py",
171
+ "mimetype": "text/x-python",
172
+ "name": "python",
173
+ "nbconvert_exporter": "python",
174
+ "pygments_lexer": "ipython3",
175
+ "version": "3.10.12"
176
+ },
177
+ "orig_nbformat": 4
178
+ },
179
+ "nbformat": 4,
180
+ "nbformat_minor": 2
181
+ }