|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "ea4e3e72-09e2-484c-af14-4819276a29e4", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## Build and Test your model locally" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": 14, |
| 14 | + "id": "577c63e0-ce42-4755-bedd-3e6b259899ff", |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "# you might have to install transfomers and torch\n", |
| 19 | + "import torch\n", |
| 20 | + "from transformers import pipeline\n" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": 15, |
| 26 | + "id": "7d9cceeb-8e6f-40ba-869d-ffca7c33c9fc", |
| 27 | + "metadata": { |
| 28 | + "scrolled": true |
| 29 | + }, |
| 30 | + "outputs": [ |
| 31 | + { |
| 32 | + "name": "stderr", |
| 33 | + "output_type": "stream", |
| 34 | + "text": [ |
| 35 | + "Some weights of the model checkpoint at bert-base-uncased were not used when initializing BertForMaskedLM: ['bert.pooler.dense.bias', 'cls.seq_relationship.weight', 'cls.seq_relationship.bias', 'bert.pooler.dense.weight']\n", |
| 36 | + "- 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", |
| 37 | + "- 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" |
| 38 | + ] |
| 39 | + } |
| 40 | + ], |
| 41 | + "source": [ |
| 42 | + "my_model = pipeline('fill-mask', model='bert-base-uncased')" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": 16, |
| 48 | + "id": "17c0d0a5-37b3-48bb-852c-5bae4642c854", |
| 49 | + "metadata": {}, |
| 50 | + "outputs": [ |
| 51 | + { |
| 52 | + "data": { |
| 53 | + "text/plain": [ |
| 54 | + "[{'score': 0.5164636969566345,\n", |
| 55 | + " 'token': 3910,\n", |
| 56 | + " 'token_str': 'founder',\n", |
| 57 | + " 'sequence': 'steve jobs is the founder of apple'},\n", |
| 58 | + " {'score': 0.36049818992614746,\n", |
| 59 | + " 'token': 5766,\n", |
| 60 | + " 'token_str': 'ceo',\n", |
| 61 | + " 'sequence': 'steve jobs is the ceo of apple'},\n", |
| 62 | + " {'score': 0.049299854785203934,\n", |
| 63 | + " 'token': 2343,\n", |
| 64 | + " 'token_str': 'president',\n", |
| 65 | + " 'sequence': 'steve jobs is the president of apple'},\n", |
| 66 | + " {'score': 0.02111203595995903,\n", |
| 67 | + " 'token': 8543,\n", |
| 68 | + " 'token_str': 'creator',\n", |
| 69 | + " 'sequence': 'steve jobs is the creator of apple'},\n", |
| 70 | + " {'score': 0.008550191298127174,\n", |
| 71 | + " 'token': 2269,\n", |
| 72 | + " 'token_str': 'father',\n", |
| 73 | + " 'sequence': 'steve jobs is the father of apple'}]" |
| 74 | + ] |
| 75 | + }, |
| 76 | + "execution_count": 16, |
| 77 | + "metadata": {}, |
| 78 | + "output_type": "execute_result" |
| 79 | + } |
| 80 | + ], |
| 81 | + "source": [ |
| 82 | + "my_model('steve jobs is the [MASK] of apple')" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "code", |
| 87 | + "execution_count": 17, |
| 88 | + "id": "1032fb2e-dac6-48be-a3ba-94b99f5a3249", |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [ |
| 91 | + { |
| 92 | + "data": { |
| 93 | + "text/plain": [ |
| 94 | + "[{'score': 0.31370100378990173,\n", |
| 95 | + " 'token': 2269,\n", |
| 96 | + " 'token_str': 'father',\n", |
| 97 | + " 'sequence': 'elon musk is the father of tesla'},\n", |
| 98 | + " {'score': 0.21307483315467834,\n", |
| 99 | + " 'token': 5766,\n", |
| 100 | + " 'token_str': 'ceo',\n", |
| 101 | + " 'sequence': 'elon musk is the ceo of tesla'},\n", |
| 102 | + " {'score': 0.14913086593151093,\n", |
| 103 | + " 'token': 2365,\n", |
| 104 | + " 'token_str': 'son',\n", |
| 105 | + " 'sequence': 'elon musk is the son of tesla'},\n", |
| 106 | + " {'score': 0.051770761609077454,\n", |
| 107 | + " 'token': 2343,\n", |
| 108 | + " 'token_str': 'president',\n", |
| 109 | + " 'sequence': 'elon musk is the president of tesla'},\n", |
| 110 | + " {'score': 0.050566092133522034,\n", |
| 111 | + " 'token': 3910,\n", |
| 112 | + " 'token_str': 'founder',\n", |
| 113 | + " 'sequence': 'elon musk is the founder of tesla'}]" |
| 114 | + ] |
| 115 | + }, |
| 116 | + "execution_count": 17, |
| 117 | + "metadata": {}, |
| 118 | + "output_type": "execute_result" |
| 119 | + } |
| 120 | + ], |
| 121 | + "source": [ |
| 122 | + "my_model('elon musk is the [MASK] of tesla')" |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "markdown", |
| 127 | + "id": "c9b9eef0-460e-4437-ac70-e396394ad263", |
| 128 | + "metadata": {}, |
| 129 | + "source": [ |
| 130 | + "## Deploy the model to SlashML" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "code", |
| 135 | + "execution_count": 18, |
| 136 | + "id": "0785f21b-2d2d-42e9-83fb-e88d77ac2c3c", |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [], |
| 139 | + "source": [ |
| 140 | + "from slashml import ModelDeployment\n", |
| 141 | + "import time" |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "code", |
| 146 | + "execution_count": 19, |
| 147 | + "id": "015c1d53-309f-45ae-b052-bd44d4fcfd43", |
| 148 | + "metadata": {}, |
| 149 | + "outputs": [], |
| 150 | + "source": [ |
| 151 | + "# Replace `API_KEY` with your SlasML API token.\n", |
| 152 | + "client = ModelDeployment(api_key=\"68b50f46b97f8c0f98fe419df240a22af3da392f\")\n", |
| 153 | + "# client = ModelDeployment(api_key=\"e7e33ba9a41c1426a425496319a14cab2b3f794f\")" |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "cell_type": "code", |
| 158 | + "execution_count": 20, |
| 159 | + "id": "9916a6d5-bcbc-4ab5-9c39-8c8a630436b8", |
| 160 | + "metadata": {}, |
| 161 | + "outputs": [], |
| 162 | + "source": [ |
| 163 | + "# deploy model\n", |
| 164 | + "response = client.deploy(model_name='my_model_3', model=my_model)" |
| 165 | + ] |
| 166 | + }, |
| 167 | + { |
| 168 | + "cell_type": "code", |
| 169 | + "execution_count": 21, |
| 170 | + "id": "fa7c3e50-d512-41e9-9d38-c0f842b9b5f4", |
| 171 | + "metadata": {}, |
| 172 | + "outputs": [ |
| 173 | + { |
| 174 | + "data": { |
| 175 | + "text/plain": [ |
| 176 | + "'163fc6ad-fbab-4f17-b06a-860eba1f5496'" |
| 177 | + ] |
| 178 | + }, |
| 179 | + "execution_count": 21, |
| 180 | + "metadata": {}, |
| 181 | + "output_type": "execute_result" |
| 182 | + } |
| 183 | + ], |
| 184 | + "source": [ |
| 185 | + "response.id" |
| 186 | + ] |
| 187 | + }, |
| 188 | + { |
| 189 | + "cell_type": "code", |
| 190 | + "execution_count": 25, |
| 191 | + "id": "1843adac-3341-49db-83b7-88e0b7f1eb20", |
| 192 | + "metadata": {}, |
| 193 | + "outputs": [ |
| 194 | + { |
| 195 | + "data": { |
| 196 | + "text/plain": [ |
| 197 | + "{'id': '163fc6ad-fbab-4f17-b06a-860eba1f5496',\n", |
| 198 | + " 'created': '2023-09-23T22:24:23.737712Z',\n", |
| 199 | + " 'status': 'READY',\n", |
| 200 | + " 'name': 'my_model_3'}" |
| 201 | + ] |
| 202 | + }, |
| 203 | + "execution_count": 25, |
| 204 | + "metadata": {}, |
| 205 | + "output_type": "execute_result" |
| 206 | + } |
| 207 | + ], |
| 208 | + "source": [ |
| 209 | + "client.status(model_version_id=response.id)" |
| 210 | + ] |
| 211 | + }, |
| 212 | + { |
| 213 | + "cell_type": "code", |
| 214 | + "execution_count": 24, |
| 215 | + "id": "1263caa7-3147-4229-90b6-84bae4b0e479", |
| 216 | + "metadata": {}, |
| 217 | + "outputs": [ |
| 218 | + { |
| 219 | + "name": "stdout", |
| 220 | + "output_type": "stream", |
| 221 | + "text": [ |
| 222 | + "model succeeded\n" |
| 223 | + ] |
| 224 | + } |
| 225 | + ], |
| 226 | + "source": [ |
| 227 | + "status = client.status(model_version_id=response.id)\n", |
| 228 | + "\n", |
| 229 | + "while status.status != 'READY':\n", |
| 230 | + " print(f'status: {status.status}')\n", |
| 231 | + " print('trying again in 5 seconds')\n", |
| 232 | + " time.sleep(5)\n", |
| 233 | + " status = model.status(model_version_id=response.id)\n", |
| 234 | + "\n", |
| 235 | + " if status.status in ['FAILED', \"ERROR\"]:\n", |
| 236 | + " raise Exception(f'Model deployment {status}')\n", |
| 237 | + "\n", |
| 238 | + "print(f'model succeeded')" |
| 239 | + ] |
| 240 | + }, |
| 241 | + { |
| 242 | + "cell_type": "markdown", |
| 243 | + "id": "f387ba63-28ab-4d4a-9d4f-ec86d4483441", |
| 244 | + "metadata": {}, |
| 245 | + "source": [ |
| 246 | + "## Run predictions on the hosted model" |
| 247 | + ] |
| 248 | + }, |
| 249 | + { |
| 250 | + "cell_type": "code", |
| 251 | + "execution_count": 26, |
| 252 | + "id": "9ca7d301-6b38-46c1-aad8-1a9909b1001b", |
| 253 | + "metadata": {}, |
| 254 | + "outputs": [ |
| 255 | + { |
| 256 | + "name": "stdout", |
| 257 | + "output_type": "stream", |
| 258 | + "text": [ |
| 259 | + "{'id': '163fc6ad-fbab-4f17-b06a-860eba1f5496', 'model_input': 'Steve jobs is the [MASK] of Apple.', 'model_response': {'predictions': [{'score': 0.49993884563446045, 'sequence': 'steve jobs is the founder of apple.', 'token': 3910, 'token_str': 'founder'}, {'score': 0.39265578985214233, 'sequence': 'steve jobs is the ceo of apple.', 'token': 5766, 'token_str': 'ceo'}, {'score': 0.04603331536054611, 'sequence': 'steve jobs is the president of apple.', 'token': 2343, 'token_str': 'president'}, {'score': 0.01692850887775421, 'sequence': 'steve jobs is the creator of apple.', 'token': 8543, 'token_str': 'creator'}, {'score': 0.008718223311007023, 'sequence': 'steve jobs is the chairman of apple.', 'token': 3472, 'token_str': 'chairman'}]}}\n" |
| 260 | + ] |
| 261 | + } |
| 262 | + ], |
| 263 | + "source": [ |
| 264 | + "input_text = 'Steve jobs is the [MASK] of Apple.'\n", |
| 265 | + "prediction = client.predict(model_version_id=response.id, model_input=input_text)\n", |
| 266 | + "print(prediction)" |
| 267 | + ] |
| 268 | + }, |
| 269 | + { |
| 270 | + "cell_type": "code", |
| 271 | + "execution_count": 27, |
| 272 | + "id": "08b95a31-10d1-457f-abdf-c4310b0d02ac", |
| 273 | + "metadata": {}, |
| 274 | + "outputs": [ |
| 275 | + { |
| 276 | + "data": { |
| 277 | + "text/plain": [ |
| 278 | + "[{'score': 0.49993884563446045,\n", |
| 279 | + " 'sequence': 'steve jobs is the founder of apple.',\n", |
| 280 | + " 'token': 3910,\n", |
| 281 | + " 'token_str': 'founder'},\n", |
| 282 | + " {'score': 0.39265578985214233,\n", |
| 283 | + " 'sequence': 'steve jobs is the ceo of apple.',\n", |
| 284 | + " 'token': 5766,\n", |
| 285 | + " 'token_str': 'ceo'},\n", |
| 286 | + " {'score': 0.04603331536054611,\n", |
| 287 | + " 'sequence': 'steve jobs is the president of apple.',\n", |
| 288 | + " 'token': 2343,\n", |
| 289 | + " 'token_str': 'president'},\n", |
| 290 | + " {'score': 0.01692850887775421,\n", |
| 291 | + " 'sequence': 'steve jobs is the creator of apple.',\n", |
| 292 | + " 'token': 8543,\n", |
| 293 | + " 'token_str': 'creator'},\n", |
| 294 | + " {'score': 0.008718223311007023,\n", |
| 295 | + " 'sequence': 'steve jobs is the chairman of apple.',\n", |
| 296 | + " 'token': 3472,\n", |
| 297 | + " 'token_str': 'chairman'}]" |
| 298 | + ] |
| 299 | + }, |
| 300 | + "execution_count": 27, |
| 301 | + "metadata": {}, |
| 302 | + "output_type": "execute_result" |
| 303 | + } |
| 304 | + ], |
| 305 | + "source": [ |
| 306 | + "prediction['model_response']['predictions']" |
| 307 | + ] |
| 308 | + }, |
| 309 | + { |
| 310 | + "cell_type": "code", |
| 311 | + "execution_count": null, |
| 312 | + "id": "d1dd161a-8244-4f78-a852-3f25b5435b32", |
| 313 | + "metadata": {}, |
| 314 | + "outputs": [], |
| 315 | + "source": [ |
| 316 | + "input_text = 'elon jobs is the [MASK] of Apple.'\n", |
| 317 | + "prediction = client.predict(model_version_id=response.id, model_input=input_text)\n", |
| 318 | + "print(prediction)" |
| 319 | + ] |
| 320 | + }, |
| 321 | + { |
| 322 | + "cell_type": "code", |
| 323 | + "execution_count": null, |
| 324 | + "id": "5c31e48e-9dbc-407b-b2ac-3008a91be93d", |
| 325 | + "metadata": {}, |
| 326 | + "outputs": [], |
| 327 | + "source": [ |
| 328 | + "prediction['model_response']['predictions']" |
| 329 | + ] |
| 330 | + } |
| 331 | + ], |
| 332 | + "metadata": { |
| 333 | + "kernelspec": { |
| 334 | + "display_name": "Python 3 (ipykernel)", |
| 335 | + "language": "python", |
| 336 | + "name": "python3" |
| 337 | + }, |
| 338 | + "language_info": { |
| 339 | + "codemirror_mode": { |
| 340 | + "name": "ipython", |
| 341 | + "version": 3 |
| 342 | + }, |
| 343 | + "file_extension": ".py", |
| 344 | + "mimetype": "text/x-python", |
| 345 | + "name": "python", |
| 346 | + "nbconvert_exporter": "python", |
| 347 | + "pygments_lexer": "ipython3", |
| 348 | + "version": "3.9.1" |
| 349 | + } |
| 350 | + }, |
| 351 | + "nbformat": 4, |
| 352 | + "nbformat_minor": 5 |
| 353 | +} |
0 commit comments