Copper commited on
Commit
914d2f5
β€’
1 Parent(s): c11ab41

Helper notebook

Browse files
Files changed (1) hide show
  1. .extras/helper_notebook.ipynb +255 -0
.extras/helper_notebook.ipynb ADDED
@@ -0,0 +1,255 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {
7
+ "collapsed": true
8
+ },
9
+ "outputs": [],
10
+ "source": [
11
+ "import pandas as pd\n",
12
+ "import os, json\n",
13
+ "from transformers import pipeline\n",
14
+ "from tqdm import tqdm"
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "markdown",
19
+ "source": [
20
+ "## Load the dataset"
21
+ ],
22
+ "metadata": {
23
+ "collapsed": false
24
+ }
25
+ },
26
+ {
27
+ "cell_type": "code",
28
+ "execution_count": null,
29
+ "outputs": [],
30
+ "source": [
31
+ "from datasets import load_dataset\n",
32
+ "\n",
33
+ "dataset = load_dataset(\"generative-newsai/news-unmasked\")\n",
34
+ "\n",
35
+ "# Train and test split\n",
36
+ "test_dataset = dataset[\"test\"]\n"
37
+ ],
38
+ "metadata": {
39
+ "collapsed": false,
40
+ "pycharm": {
41
+ "is_executing": true
42
+ }
43
+ }
44
+ },
45
+ {
46
+ "cell_type": "markdown",
47
+ "source": [
48
+ "## Check first 5 rows of the Test dataset"
49
+ ],
50
+ "metadata": {
51
+ "collapsed": false
52
+ }
53
+ },
54
+ {
55
+ "cell_type": "code",
56
+ "execution_count": 3,
57
+ "outputs": [
58
+ {
59
+ "name": "stdout",
60
+ "output_type": "stream",
61
+ "text": [
62
+ "{'image': [<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1050x550 at 0x13796D490>, <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1050x550 at 0x137977700>, <PIL.PngImagePlugin.PngImageFile image mode=RGB size=789x412 at 0x137977910>, <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1050x549 at 0x1379771C0>, <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1050x550 at 0x137977640>], 'section': ['Television', 'Sports', 'Television', 'Technology', 'Fashion & Style'], 'headline': [\"What 's on TV Monday : ' The Voice ' and ' Gentefied '\", 'Phillies - Blue Jays Games Postponed After 2 Staff Members Test [MASK]', \"Joe Biden 's Run Has [MASK] Night Looking for a Fight\", 'Pinterest Posts [MASK] Loss , but Falls Short of Wall St. Estimates', 'Yolanda Foster : Watching Her Daughter Gigi Hadid From the Front Row'], 'image_id': ['00008455-a932-5f2c-b5ce-86584d8345b0', '00040f12-c19e-54db-9513-d4a3d9ce30f1', '0006d6e6-a16f-5d69-a307-0e7e1b659075', '000755c6-df96-502a-a3e4-8f78f0919a8c', '001b8cc1-c623-571c-9e2d-86e1f3f7c20c']}\n"
63
+ ]
64
+ }
65
+ ],
66
+ "source": [
67
+ "# Check first 5 rows of the test dataset\n",
68
+ "sample_data = test_dataset[:5]\n",
69
+ "print(sample_data)"
70
+ ],
71
+ "metadata": {
72
+ "collapsed": false
73
+ }
74
+ },
75
+ {
76
+ "cell_type": "markdown",
77
+ "source": [
78
+ "## Load the model"
79
+ ],
80
+ "metadata": {
81
+ "collapsed": false
82
+ }
83
+ },
84
+ {
85
+ "cell_type": "code",
86
+ "execution_count": 5,
87
+ "outputs": [
88
+ {
89
+ "name": "stderr",
90
+ "output_type": "stream",
91
+ "text": [
92
+ "Some weights of the model checkpoint at mlcorelib/deberta-base-uncased were not used when initializing BertForMaskedLM: ['cls.seq_relationship.bias', 'cls.seq_relationship.weight']\n",
93
+ "- This IS expected if you are initializing BertForMaskedLM from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
94
+ "- This IS NOT expected if you are initializing BertForMaskedLM from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
95
+ ]
96
+ }
97
+ ],
98
+ "source": [
99
+ "model_name = \"mlcorelib/deberta-base-uncased\"\n",
100
+ "unmasker = pipeline('fill-mask', model=model_name)"
101
+ ],
102
+ "metadata": {
103
+ "collapsed": false
104
+ }
105
+ },
106
+ {
107
+ "cell_type": "markdown",
108
+ "source": [
109
+ "## Unmask the sentences"
110
+ ],
111
+ "metadata": {
112
+ "collapsed": false
113
+ }
114
+ },
115
+ {
116
+ "cell_type": "code",
117
+ "execution_count": 6,
118
+ "outputs": [
119
+ {
120
+ "name": "stderr",
121
+ "output_type": "stream",
122
+ "text": [
123
+ "100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 12247/12247 [05:47<00:00, 35.20it/s]\n"
124
+ ]
125
+ }
126
+ ],
127
+ "source": [
128
+ "all_masked_words = []\n",
129
+ "for each_dict in tqdm(test_dataset):\n",
130
+ " sentence = each_dict['headline'] # Get the sentence from the dictionary\n",
131
+ " image_id = each_dict['image_id'] # Get the image_id from the dictionary\n",
132
+ " if \"[MASK]\" in sentence: # See if it has a [MASK] in headline\n",
133
+ " result = unmasker(sentence) # Unmask the sentence\n",
134
+ "\n",
135
+ " # Make a list of indices where [MASK] is present in the sentence\n",
136
+ " # If there are more than one [MASK] in the sentence, then add them as separate entries in the result list\n",
137
+ " indices = [i for i, x in enumerate(sentence.split()) if x == \"[MASK]\"]\n",
138
+ " if len(indices) > 1:\n",
139
+ " masked_word_idx_list = []\n",
140
+ " for i, each_result in enumerate(result):\n",
141
+ " # Get the top scoring word\n",
142
+ " top_word = each_result[0]['token_str']\n",
143
+ " all_masked_words.append([image_id, indices[i], top_word])\n",
144
+ " else:\n",
145
+ " all_masked_words.append([image_id, indices[0], result[0]['token_str']])\n",
146
+ "\n",
147
+ "final_masked_words = [l[0] for l in all_masked_words]"
148
+ ],
149
+ "metadata": {
150
+ "collapsed": false
151
+ }
152
+ },
153
+ {
154
+ "cell_type": "markdown",
155
+ "source": [
156
+ "## Print first 5 rows of the masked words list"
157
+ ],
158
+ "metadata": {
159
+ "collapsed": false
160
+ }
161
+ },
162
+ {
163
+ "cell_type": "code",
164
+ "execution_count": 13,
165
+ "outputs": [
166
+ {
167
+ "name": "stdout",
168
+ "output_type": "stream",
169
+ "text": [
170
+ "['00040f12-c19e-54db-9513-d4a3d9ce30f1', '0006d6e6-a16f-5d69-a307-0e7e1b659075', '000755c6-df96-502a-a3e4-8f78f0919a8c', '0038ee8b-3f57-5838-a201-509d4bcd1c06', '0048f974-e081-54ee-b0c4-e30b5f66c763']\n"
171
+ ]
172
+ }
173
+ ],
174
+ "source": [
175
+ "print(final_masked_words[:5])"
176
+ ],
177
+ "metadata": {
178
+ "collapsed": false
179
+ }
180
+ },
181
+ {
182
+ "cell_type": "markdown",
183
+ "source": [
184
+ "## Save the results as a dataframe and print first 5 rows of the dataframe"
185
+ ],
186
+ "metadata": {
187
+ "collapsed": false
188
+ }
189
+ },
190
+ {
191
+ "cell_type": "code",
192
+ "execution_count": 15,
193
+ "outputs": [
194
+ {
195
+ "data": {
196
+ "text/plain": " id token_index token\n0 00040f12-c19e-54db-9513-d4a3d9ce30f1 11 .\n1 0006d6e6-a16f-5d69-a307-0e7e1b659075 5 the\n2 000755c6-df96-502a-a3e4-8f78f0919a8c 2 a\n3 0038ee8b-3f57-5838-a201-509d4bcd1c06 0 regular\n4 0048f974-e081-54ee-b0c4-e30b5f66c763 6 a",
197
+ "text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>id</th>\n <th>token_index</th>\n <th>token</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>00040f12-c19e-54db-9513-d4a3d9ce30f1</td>\n <td>11</td>\n <td>.</td>\n </tr>\n <tr>\n <th>1</th>\n <td>0006d6e6-a16f-5d69-a307-0e7e1b659075</td>\n <td>5</td>\n <td>the</td>\n </tr>\n <tr>\n <th>2</th>\n <td>000755c6-df96-502a-a3e4-8f78f0919a8c</td>\n <td>2</td>\n <td>a</td>\n </tr>\n <tr>\n <th>3</th>\n <td>0038ee8b-3f57-5838-a201-509d4bcd1c06</td>\n <td>0</td>\n <td>regular</td>\n </tr>\n <tr>\n <th>4</th>\n <td>0048f974-e081-54ee-b0c4-e30b5f66c763</td>\n <td>6</td>\n <td>a</td>\n </tr>\n </tbody>\n</table>\n</div>"
198
+ },
199
+ "execution_count": 15,
200
+ "metadata": {},
201
+ "output_type": "execute_result"
202
+ }
203
+ ],
204
+ "source": [
205
+ "# Save the results in a dataframe with column name id,token_index,token\n",
206
+ "df = pd.DataFrame(all_masked_words, columns=['id', 'token_index', 'token'])\n",
207
+ "df.head()"
208
+ ],
209
+ "metadata": {
210
+ "collapsed": false
211
+ }
212
+ },
213
+ {
214
+ "cell_type": "markdown",
215
+ "source": [
216
+ "## Save the dataframe as a csv file"
217
+ ],
218
+ "metadata": {
219
+ "collapsed": false
220
+ }
221
+ },
222
+ {
223
+ "cell_type": "code",
224
+ "execution_count": 16,
225
+ "outputs": [],
226
+ "source": [
227
+ "df.to_csv('sample_result.csv', index=False)"
228
+ ],
229
+ "metadata": {
230
+ "collapsed": false
231
+ }
232
+ }
233
+ ],
234
+ "metadata": {
235
+ "kernelspec": {
236
+ "display_name": "Python 3",
237
+ "language": "python",
238
+ "name": "python3"
239
+ },
240
+ "language_info": {
241
+ "codemirror_mode": {
242
+ "name": "ipython",
243
+ "version": 2
244
+ },
245
+ "file_extension": ".py",
246
+ "mimetype": "text/x-python",
247
+ "name": "python",
248
+ "nbconvert_exporter": "python",
249
+ "pygments_lexer": "ipython2",
250
+ "version": "2.7.6"
251
+ }
252
+ },
253
+ "nbformat": 4,
254
+ "nbformat_minor": 0
255
+ }