Datasets:
File size: 8,809 Bytes
914d2f5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import os, json\n",
"from transformers import pipeline\n",
"from tqdm import tqdm"
]
},
{
"cell_type": "markdown",
"source": [
"## Load the dataset"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"from datasets import load_dataset\n",
"\n",
"dataset = load_dataset(\"generative-newsai/news-unmasked\")\n",
"\n",
"# Train and test split\n",
"test_dataset = dataset[\"test\"]\n"
],
"metadata": {
"collapsed": false,
"pycharm": {
"is_executing": true
}
}
},
{
"cell_type": "markdown",
"source": [
"## Check first 5 rows of the Test dataset"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'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"
]
}
],
"source": [
"# Check first 5 rows of the test dataset\n",
"sample_data = test_dataset[:5]\n",
"print(sample_data)"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## Load the model"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 5,
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"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",
"- 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",
"- 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"
]
}
],
"source": [
"model_name = \"mlcorelib/deberta-base-uncased\"\n",
"unmasker = pipeline('fill-mask', model=model_name)"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## Unmask the sentences"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 6,
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|ββββββββββ| 12247/12247 [05:47<00:00, 35.20it/s]\n"
]
}
],
"source": [
"all_masked_words = []\n",
"for each_dict in tqdm(test_dataset):\n",
" sentence = each_dict['headline'] # Get the sentence from the dictionary\n",
" image_id = each_dict['image_id'] # Get the image_id from the dictionary\n",
" if \"[MASK]\" in sentence: # See if it has a [MASK] in headline\n",
" result = unmasker(sentence) # Unmask the sentence\n",
"\n",
" # Make a list of indices where [MASK] is present in the sentence\n",
" # If there are more than one [MASK] in the sentence, then add them as separate entries in the result list\n",
" indices = [i for i, x in enumerate(sentence.split()) if x == \"[MASK]\"]\n",
" if len(indices) > 1:\n",
" masked_word_idx_list = []\n",
" for i, each_result in enumerate(result):\n",
" # Get the top scoring word\n",
" top_word = each_result[0]['token_str']\n",
" all_masked_words.append([image_id, indices[i], top_word])\n",
" else:\n",
" all_masked_words.append([image_id, indices[0], result[0]['token_str']])\n",
"\n",
"final_masked_words = [l[0] for l in all_masked_words]"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## Print first 5 rows of the masked words list"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 13,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['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"
]
}
],
"source": [
"print(final_masked_words[:5])"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## Save the results as a dataframe and print first 5 rows of the dataframe"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 15,
"outputs": [
{
"data": {
"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",
"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>"
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Save the results in a dataframe with column name id,token_index,token\n",
"df = pd.DataFrame(all_masked_words, columns=['id', 'token_index', 'token'])\n",
"df.head()"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"## Save the dataframe as a csv file"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 16,
"outputs": [],
"source": [
"df.to_csv('sample_result.csv', index=False)"
],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|