File size: 11,836 Bytes
49e56e4 |
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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "2703e56a-a405-4950-a8a0-428a76d1ecb5",
"metadata": {},
"outputs": [],
"source": [
"# Use the trained astroBERT model to make NER predictions"
]
},
{
"cell_type": "markdown",
"id": "21d28579-5d87-40ec-835d-56cfc797b608",
"metadata": {},
"source": [
"# Tutorial 2 - Using astroBERT to make NER Prediction\n",
"This tutorials shows you how to use astroBERT to make NER predictions. \n",
"For a list and description of the labels, see DEAL@WIESP 2022 (https://ui.adsabs.harvard.edu/WIESP/2022/LabelDefinitions)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "2324e04c-4abe-4ba3-85fa-c4c38c0ae83f",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2022-10-31 11:27:34.285610: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0\n"
]
}
],
"source": [
"from transformers import AutoModelForTokenClassification, AutoTokenizer"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "18c2688f-8686-411e-82f4-cc517073a5f7",
"metadata": {},
"outputs": [],
"source": [
"# load astroBERT for NER-DEAL\n",
"remote_model_path = 'adsabs/astroBERT'\n",
"# you need to load the astroBERT trained for NER-DEAL, which is on a seperate branch\n",
"revision = 'NER-DEAL'\n",
"\n",
"astroBERT_NER_DEAL = AutoModelForTokenClassification.from_pretrained(pretrained_model_name_or_path=remote_model_path,\n",
" revision=revision,\n",
" )\n",
"\n",
"astroBERT_tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path=remote_model_path,\n",
" add_special_tokens=True,\n",
" do_lower_case=False,\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f6be2bf3-7aea-41d2-b5cd-fdeb6dd615fa",
"metadata": {},
"outputs": [],
"source": [
"from transformers import TokenClassificationPipeline"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "4ffd986c-5150-4476-8660-bbad3f2a16b5",
"metadata": {},
"outputs": [],
"source": [
"# use the Hugginface Pipeline class\n",
"NER_pileline = TokenClassificationPipeline(model = astroBERT_NER_DEAL,\n",
" tokenizer = astroBERT_tokenizer,\n",
" task='astroBERT NER_DEAL',\n",
" aggregation_strategy='average',\n",
" ignore_labels=['O'],\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "da1efd38-38be-4f62-b371-835fe2a93b8a",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Asking to truncate to max_length but no maximum length is provided and the model has no predefined maximum length. Default to no truncation.\n"
]
}
],
"source": [
"# make predictions on text of your choice\n",
"text = 'The National Aeronautics and Space Administration (NASA /ˈnæsə/) is an independent agency of the US federal government responsible for the civil space program, aeronautics research, and space research.[note 1] NASA was established in 1958, succeeding the National Advisory Committee for Aeronautics (NACA), to give the U.S. space development effort a distinctly civilian orientation, emphasizing peaceful applications in space science.[7][8][9] Since its establishment, most American space exploration efforts have been led by NASA, including the Apollo Moon landing missions, the Skylab space station, and later the Space Shuttle. NASA supports the International Space Station and oversees the development of the Orion spacecraft, the Space Launch System, Commercial Crew vehicles, and the planned Lunar Gateway space station. The agency is also responsible for the Launch Services Program'\n",
"pred = NER_pileline(text) "
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "0377c060-55d6-43c1-8940-096570dbf209",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'entity_group': 'Organization',\n",
" 'score': 0.8618845,\n",
" 'word': 'National Aeronautics and Space Administration',\n",
" 'start': 4,\n",
" 'end': 49},\n",
" {'entity_group': 'Organization',\n",
" 'score': 0.5089845,\n",
" 'word': '( NASA / ˈnæsə / )',\n",
" 'start': 50,\n",
" 'end': 64},\n",
" {'entity_group': 'Location',\n",
" 'score': 0.84241116,\n",
" 'word': 'US',\n",
" 'start': 97,\n",
" 'end': 99},\n",
" {'entity_group': 'Organization',\n",
" 'score': 0.35269254,\n",
" 'word': 'aeronautics',\n",
" 'start': 160,\n",
" 'end': 171},\n",
" {'entity_group': 'Organization',\n",
" 'score': 0.5513096,\n",
" 'word': 'NASA',\n",
" 'start': 210,\n",
" 'end': 214},\n",
" {'entity_group': 'Organization',\n",
" 'score': 0.81638354,\n",
" 'word': 'National Advisory Committee for Aeronautics',\n",
" 'start': 255,\n",
" 'end': 298},\n",
" {'entity_group': 'Organization',\n",
" 'score': 0.60596126,\n",
" 'word': '( NACA )',\n",
" 'start': 299,\n",
" 'end': 305},\n",
" {'entity_group': 'Location',\n",
" 'score': 0.9698747,\n",
" 'word': 'U',\n",
" 'start': 319,\n",
" 'end': 320},\n",
" {'entity_group': 'Location',\n",
" 'score': 0.8322766,\n",
" 'word': 'S',\n",
" 'start': 321,\n",
" 'end': 322},\n",
" {'entity_group': 'Organization',\n",
" 'score': 0.5043399,\n",
" 'word': 'NASA',\n",
" 'start': 527,\n",
" 'end': 531},\n",
" {'entity_group': 'Mission',\n",
" 'score': 0.5467872,\n",
" 'word': 'Skylab',\n",
" 'start': 581,\n",
" 'end': 587},\n",
" {'entity_group': 'Mission',\n",
" 'score': 0.9082912,\n",
" 'word': 'Shuttle',\n",
" 'start': 623,\n",
" 'end': 630},\n",
" {'entity_group': 'Mission',\n",
" 'score': 0.20667519,\n",
" 'word': 'International',\n",
" 'start': 650,\n",
" 'end': 663},\n",
" {'entity_group': 'Organization',\n",
" 'score': 0.42570755,\n",
" 'word': 'Space Station',\n",
" 'start': 664,\n",
" 'end': 677},\n",
" {'entity_group': 'Mission',\n",
" 'score': 0.3754898,\n",
" 'word': 'Orion',\n",
" 'start': 714,\n",
" 'end': 719},\n",
" {'entity_group': 'Organization',\n",
" 'score': 0.27432305,\n",
" 'word': 'System',\n",
" 'start': 749,\n",
" 'end': 755},\n",
" {'entity_group': 'CelestialObject',\n",
" 'score': 0.19273384,\n",
" 'word': 'Lunar',\n",
" 'start': 799,\n",
" 'end': 804},\n",
" {'entity_group': 'Organization',\n",
" 'score': 0.32622194,\n",
" 'word': 'Gateway',\n",
" 'start': 805,\n",
" 'end': 812},\n",
" {'entity_group': 'Organization',\n",
" 'score': 0.4211573,\n",
" 'word': 'Services',\n",
" 'start': 874,\n",
" 'end': 882}]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# predictions include a \n",
"pred"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "523e8a7b-bbf2-49e4-bb47-be01ad6cadad",
"metadata": {},
"outputs": [],
"source": [
"# view the results using custom function\n",
"def format_pred_for_print(pred, paragraph):\n",
" '''\n",
" returns a pretty string with the predictions in paragraph highlighted.\n",
" pred: prediction output from a pipeline\n",
" paragraph: the original text the predictions were made on\n",
" '''\n",
" \n",
" RED_START = '\\x1b[31m'\n",
" RED_END = '\\x1b[0m'\n",
" \n",
" formatted_string=''\n",
" end=0\n",
" \n",
" for entry in pred:\n",
" start = entry['start']\n",
" # add what's in between\n",
" formatted_string += paragraph[end:start]\n",
" # add the entry\n",
" end = entry['end']\n",
" label = entry['entity_group']\n",
" score = ' {:.2f}'.format(entry['score'])\n",
" formatted_string+= RED_START+'['+paragraph[start:end]+' ('+label+score+')]'+RED_END\n",
" \n",
" formatted_string+= paragraph[end:]\n",
" return(formatted_string)\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "74b9e2bd-817a-4347-a4ea-8f57e20baa3c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The \u001b[31m[National Aeronautics and Space Administration (Organization 0.86)]\u001b[0m \u001b[31m[(NASA /ˈnæsə/) (Organization 0.51)]\u001b[0m is an independent agency of the \u001b[31m[US (Location 0.84)]\u001b[0m federal government responsible for the civil space program, \u001b[31m[aeronautics (Organization 0.35)]\u001b[0m research, and space research.[note 1] \u001b[31m[NASA (Organization 0.55)]\u001b[0m was established in 1958, succeeding the \u001b[31m[National Advisory Committee for Aeronautics (Organization 0.82)]\u001b[0m \u001b[31m[(NACA) (Organization 0.61)]\u001b[0m, to give the \u001b[31m[U (Location 0.97)]\u001b[0m.\u001b[31m[S (Location 0.83)]\u001b[0m. space development effort a distinctly civilian orientation, emphasizing peaceful applications in space science.[7][8][9] Since its establishment, most American space exploration efforts have been led by \u001b[31m[NASA (Organization 0.50)]\u001b[0m, including the Apollo Moon landing missions, the \u001b[31m[Skylab (Mission 0.55)]\u001b[0m space station, and later the Space \u001b[31m[Shuttle (Mission 0.91)]\u001b[0m. NASA supports the \u001b[31m[International (Mission 0.21)]\u001b[0m \u001b[31m[Space Station (Organization 0.43)]\u001b[0m and oversees the development of the \u001b[31m[Orion (Mission 0.38)]\u001b[0m spacecraft, the Space Launch \u001b[31m[System (Organization 0.27)]\u001b[0m, Commercial Crew vehicles, and the planned \u001b[31m[Lunar (CelestialObject 0.19)]\u001b[0m \u001b[31m[Gateway (Organization 0.33)]\u001b[0m space station. The agency is also responsible for the Launch \u001b[31m[Services (Organization 0.42)]\u001b[0m Program\n"
]
}
],
"source": [
"print(format_pred_for_print(pred, text))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3f576b7d-d778-4cda-8b4c-5f05a3a245b2",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|