File size: 40,728 Bytes
c9686b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "003973a6",
   "metadata": {},
   "source": [
    "## Save model to S3 storage"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ae08384",
   "metadata": {},
   "source": [
    "### Import Caikit-NLP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d758b7e4-c8c1-4c7f-82a2-2cbf5e580a96",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cloning into 'caikit-nlp'...\n",
      "remote: Enumerating objects: 5954, done.\u001b[K\n",
      "remote: Counting objects: 100% (643/643), done.\u001b[K\n",
      "remote: Compressing objects: 100% (222/222), done.\u001b[K\n",
      "remote: Total 5954 (delta 444), reused 546 (delta 403), pack-reused 5311\u001b[K\n",
      "Receiving objects: 100% (5954/5954), 2.56 MiB | 27.03 MiB/s, done.\n",
      "Resolving deltas: 100% (4361/4361), done.\n"
     ]
    }
   ],
   "source": [
    "!git clone https://github.com/caikit/caikit-nlp.git"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2bce9855-3ea3-48ed-b3f4-5927fc047d6a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Processing ./caikit-nlp\n",
      "  Installing build dependencies ... \u001b[?25ldone\n",
      "\u001b[?25h\u001b[33m  WARNING: Missing build requirements in pyproject.toml for file:///opt/app-root/src/notebooks/caikit-nlp.\u001b[0m\u001b[33m\n",
      "\u001b[0m\u001b[33m  WARNING: The project does not specify a build backend, and pip cannot fall back to setuptools without 'wheel'.\u001b[0m\u001b[33m\n",
      "\u001b[0m  Getting requirements to build wheel ... \u001b[?25ldone\n",
      "\u001b[?25h  Installing backend dependencies ... \u001b[?25ldone\n",
      "\u001b[?25h  Preparing metadata (pyproject.toml) ... \u001b[?25ldone\n",
      "\u001b[?25hRequirement already satisfied: numpy>=1.22.4 in /opt/app-root/lib/python3.9/site-packages (from caikit-nlp==0.4.9.dev2+gc12cb82) (1.24.4)\n",
      "Collecting caikit-tgis-backend<0.2.0,>=0.1.27\n",
      "  Downloading caikit_tgis_backend-0.1.31-py3-none-any.whl (29 kB)\n",
      "Collecting huggingface-hub\n",
      "  Downloading huggingface_hub-0.22.2-py3-none-any.whl (388 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m388.9/388.9 kB\u001b[0m \u001b[31m45.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting transformers>=4.32.0\n",
      "  Downloading transformers-4.40.1-py3-none-any.whl (9.0 MB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m9.0/9.0 MB\u001b[0m \u001b[31m221.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
      "\u001b[?25hCollecting datasets>=2.4.0\n",
      "  Downloading datasets-2.19.0-py3-none-any.whl (542 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m542.0/542.0 kB\u001b[0m \u001b[31m331.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: torch>=2.0.1 in /opt/app-root/lib/python3.9/site-packages (from caikit-nlp==0.4.9.dev2+gc12cb82) (2.0.1+cu118)\n",
      "Requirement already satisfied: pandas>=1.5.0 in /opt/app-root/lib/python3.9/site-packages (from caikit-nlp==0.4.9.dev2+gc12cb82) (1.5.3)\n",
      "Requirement already satisfied: tqdm>=4.65.0 in /opt/app-root/lib/python3.9/site-packages (from caikit-nlp==0.4.9.dev2+gc12cb82) (4.66.2)\n",
      "Requirement already satisfied: scipy>=1.8.1 in /opt/app-root/lib/python3.9/site-packages (from caikit-nlp==0.4.9.dev2+gc12cb82) (1.11.4)\n",
      "Collecting tokenizers>=0.13.3\n",
      "  Downloading tokenizers-0.19.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.6 MB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.6/3.6 MB\u001b[0m \u001b[31m320.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting accelerate>=0.22.0\n",
      "  Downloading accelerate-0.29.3-py3-none-any.whl (297 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m297.6/297.6 kB\u001b[0m \u001b[31m305.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: scikit-learn>=1.1 in /opt/app-root/lib/python3.9/site-packages (from caikit-nlp==0.4.9.dev2+gc12cb82) (1.3.2)\n",
      "Collecting caikit[runtime-grpc,runtime-http]<0.27.0,>=0.26.17\n",
      "  Downloading caikit-0.26.20-py3-none-any.whl (419 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m419.9/419.9 kB\u001b[0m \u001b[31m304.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting peft==0.6.0\n",
      "  Downloading peft-0.6.0-py3-none-any.whl (134 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m134.9/134.9 kB\u001b[0m \u001b[31m158.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting sentence-transformers<2.4.0,>=2.3.1\n",
      "  Downloading sentence_transformers-2.3.1-py3-none-any.whl (132 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m132.8/132.8 kB\u001b[0m \u001b[31m258.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: pyyaml in /opt/app-root/lib/python3.9/site-packages (from peft==0.6.0->caikit-nlp==0.4.9.dev2+gc12cb82) (6.0.1)\n",
      "Requirement already satisfied: psutil in /opt/app-root/lib/python3.9/site-packages (from peft==0.6.0->caikit-nlp==0.4.9.dev2+gc12cb82) (5.9.8)\n",
      "Requirement already satisfied: packaging>=20.0 in /opt/app-root/lib/python3.9/site-packages (from peft==0.6.0->caikit-nlp==0.4.9.dev2+gc12cb82) (23.2)\n",
      "Collecting safetensors\n",
      "  Downloading safetensors-0.4.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m334.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: grpcio<2.0,>=1.35.0 in /opt/app-root/lib/python3.9/site-packages (from caikit-tgis-backend<0.2.0,>=0.1.27->caikit-nlp==0.4.9.dev2+gc12cb82) (1.62.0)\n",
      "Requirement already satisfied: requests<3,>=2.28.2 in /opt/app-root/lib/python3.9/site-packages (from caikit-tgis-backend<0.2.0,>=0.1.27->caikit-nlp==0.4.9.dev2+gc12cb82) (2.31.0)\n",
      "Collecting alchemy-logging<2.0.0,>=1.3.2\n",
      "  Downloading alchemy_logging-1.3.2-py3-none-any.whl (18 kB)\n",
      "Collecting semver<4.0,>=2.13.0\n",
      "  Downloading semver-3.0.2-py3-none-any.whl (17 kB)\n",
      "Collecting alchemy-config<2.0.0,>=1.1.1\n",
      "  Downloading alchemy_config-1.1.3-py3-none-any.whl (7.3 kB)\n",
      "Requirement already satisfied: werkzeug<4.0.0,>=2.3.7 in /opt/app-root/lib/python3.9/site-packages (from caikit[runtime-grpc,runtime-http]<0.27.0,>=0.26.17->caikit-nlp==0.4.9.dev2+gc12cb82) (3.0.1)\n",
      "Collecting anytree<3.0,>=2.7.0\n",
      "  Downloading anytree-2.12.1-py3-none-any.whl (44 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m44.9/44.9 kB\u001b[0m \u001b[31m227.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting py-to-proto!=0.2.1,<0.6.0,>=0.5.0\n",
      "  Downloading py_to_proto-0.5.2-py39-none-any.whl (32 kB)\n",
      "Requirement already satisfied: importlib-metadata<8.0.0,>=6.8.0 in /opt/app-root/lib/python3.9/site-packages (from caikit[runtime-grpc,runtime-http]<0.27.0,>=0.26.17->caikit-nlp==0.4.9.dev2+gc12cb82) (7.0.1)\n",
      "Collecting munch<5.0,>=2.5.0\n",
      "  Downloading munch-4.0.0-py2.py3-none-any.whl (9.9 kB)\n",
      "Requirement already satisfied: protobuf<6,>=3.19.0 in /opt/app-root/lib/python3.9/site-packages (from caikit[runtime-grpc,runtime-http]<0.27.0,>=0.26.17->caikit-nlp==0.4.9.dev2+gc12cb82) (3.20.2)\n",
      "Requirement already satisfied: six<2.0.0,>=1.16.0 in /opt/app-root/lib/python3.9/site-packages (from caikit[runtime-grpc,runtime-http]<0.27.0,>=0.26.17->caikit-nlp==0.4.9.dev2+gc12cb82) (1.16.0)\n",
      "Collecting ijson<3.3.0,>=3.1.4\n",
      "  Downloading ijson-3.2.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (111 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m111.1/111.1 kB\u001b[0m \u001b[31m291.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting docstring-parser<0.17.0,>=0.14.1\n",
      "  Downloading docstring_parser-0.16-py3-none-any.whl (36 kB)\n",
      "Collecting py-grpc-prometheus<0.9,>=0.7.0\n",
      "  Downloading py_grpc_prometheus-0.8.0-py3-none-any.whl (12 kB)\n",
      "Collecting grpcio-health-checking<2.0,>=1.35.0\n",
      "  Downloading grpcio_health_checking-1.62.2-py3-none-any.whl (18 kB)\n",
      "Collecting grpcio-reflection<2.0,>=1.35.0\n",
      "  Downloading grpcio_reflection-1.62.2-py3-none-any.whl (22 kB)\n",
      "Requirement already satisfied: prometheus-client<1.0,>=0.12.0 in /opt/app-root/lib/python3.9/site-packages (from caikit[runtime-grpc,runtime-http]<0.27.0,>=0.26.17->caikit-nlp==0.4.9.dev2+gc12cb82) (0.20.0)\n",
      "Collecting sse-starlette<3,>=1.6.1\n",
      "  Downloading sse_starlette-2.1.0-py3-none-any.whl (9.2 kB)\n",
      "Collecting fastapi[all]<1,>=0.100\n",
      "  Downloading fastapi-0.110.2-py3-none-any.whl (91 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m91.9/91.9 kB\u001b[0m \u001b[31m290.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: pyarrow>=12.0.0 in /opt/app-root/lib/python3.9/site-packages (from datasets>=2.4.0->caikit-nlp==0.4.9.dev2+gc12cb82) (15.0.0)\n",
      "Requirement already satisfied: filelock in /opt/app-root/lib/python3.9/site-packages (from datasets>=2.4.0->caikit-nlp==0.4.9.dev2+gc12cb82) (3.13.1)\n",
      "Collecting xxhash\n",
      "  Downloading xxhash-3.4.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (193 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m193.8/193.8 kB\u001b[0m \u001b[31m304.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting multiprocess\n",
      "  Downloading multiprocess-0.70.16-py39-none-any.whl (133 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m133.4/133.4 kB\u001b[0m \u001b[31m270.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting pyarrow-hotfix\n",
      "  Downloading pyarrow_hotfix-0.6-py3-none-any.whl (7.9 kB)\n",
      "Requirement already satisfied: fsspec[http]<=2024.3.1,>=2023.1.0 in /opt/app-root/lib/python3.9/site-packages (from datasets>=2.4.0->caikit-nlp==0.4.9.dev2+gc12cb82) (2024.2.0)\n",
      "Requirement already satisfied: dill<0.3.9,>=0.3.0 in /opt/app-root/lib/python3.9/site-packages (from datasets>=2.4.0->caikit-nlp==0.4.9.dev2+gc12cb82) (0.3.8)\n",
      "Requirement already satisfied: aiohttp in /opt/app-root/lib/python3.9/site-packages (from datasets>=2.4.0->caikit-nlp==0.4.9.dev2+gc12cb82) (3.9.3)\n",
      "Requirement already satisfied: typing-extensions>=3.7.4.3 in /opt/app-root/lib/python3.9/site-packages (from huggingface-hub->caikit-nlp==0.4.9.dev2+gc12cb82) (4.10.0)\n",
      "Requirement already satisfied: python-dateutil>=2.8.1 in /opt/app-root/lib/python3.9/site-packages (from pandas>=1.5.0->caikit-nlp==0.4.9.dev2+gc12cb82) (2.9.0)\n",
      "Requirement already satisfied: pytz>=2020.1 in /opt/app-root/lib/python3.9/site-packages (from pandas>=1.5.0->caikit-nlp==0.4.9.dev2+gc12cb82) (2024.1)\n",
      "Requirement already satisfied: joblib>=1.1.1 in /opt/app-root/lib/python3.9/site-packages (from scikit-learn>=1.1->caikit-nlp==0.4.9.dev2+gc12cb82) (1.3.2)\n",
      "Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/app-root/lib/python3.9/site-packages (from scikit-learn>=1.1->caikit-nlp==0.4.9.dev2+gc12cb82) (3.3.0)\n",
      "Requirement already satisfied: Pillow in /opt/app-root/lib/python3.9/site-packages (from sentence-transformers<2.4.0,>=2.3.1->caikit-nlp==0.4.9.dev2+gc12cb82) (10.2.0)\n",
      "Collecting sentencepiece\n",
      "  Downloading sentencepiece-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m322.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting nltk\n",
      "  Downloading nltk-3.8.1-py3-none-any.whl (1.5 MB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.5/1.5 MB\u001b[0m \u001b[31m334.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: triton==2.0.0 in /opt/app-root/lib/python3.9/site-packages (from torch>=2.0.1->caikit-nlp==0.4.9.dev2+gc12cb82) (2.0.0)\n",
      "Requirement already satisfied: networkx in /opt/app-root/lib/python3.9/site-packages (from torch>=2.0.1->caikit-nlp==0.4.9.dev2+gc12cb82) (3.2.1)\n",
      "Requirement already satisfied: jinja2 in /opt/app-root/lib/python3.9/site-packages (from torch>=2.0.1->caikit-nlp==0.4.9.dev2+gc12cb82) (3.1.3)\n",
      "Requirement already satisfied: sympy in /opt/app-root/lib/python3.9/site-packages (from torch>=2.0.1->caikit-nlp==0.4.9.dev2+gc12cb82) (1.12)\n",
      "Requirement already satisfied: cmake in /opt/app-root/lib/python3.9/site-packages (from triton==2.0.0->torch>=2.0.1->caikit-nlp==0.4.9.dev2+gc12cb82) (3.28.3)\n",
      "Requirement already satisfied: lit in /opt/app-root/lib/python3.9/site-packages (from triton==2.0.0->torch>=2.0.1->caikit-nlp==0.4.9.dev2+gc12cb82) (17.0.6)\n",
      "Collecting regex!=2019.12.17\n",
      "  Downloading regex-2024.4.16-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (773 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m773.4/773.4 kB\u001b[0m \u001b[31m342.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,!=2.1.0,<3.0.0,>=1.7.4 in /opt/app-root/lib/python3.9/site-packages (from fastapi[all]<1,>=0.100->caikit[runtime-grpc,runtime-http]<0.27.0,>=0.26.17->caikit-nlp==0.4.9.dev2+gc12cb82) (1.10.14)\n",
      "Collecting starlette<0.38.0,>=0.37.2\n",
      "  Downloading starlette-0.37.2-py3-none-any.whl (71 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m71.9/71.9 kB\u001b[0m \u001b[31m259.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting httpx>=0.23.0\n",
      "  Downloading httpx-0.27.0-py3-none-any.whl (75 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m278.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting uvicorn[standard]>=0.12.0\n",
      "  Downloading uvicorn-0.29.0-py3-none-any.whl (60 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m60.8/60.8 kB\u001b[0m \u001b[31m212.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting itsdangerous>=1.1.0\n",
      "  Downloading itsdangerous-2.2.0-py3-none-any.whl (16 kB)\n",
      "Collecting email-validator>=2.0.0\n",
      "  Downloading email_validator-2.1.1-py3-none-any.whl (30 kB)\n",
      "Collecting python-multipart>=0.0.7\n",
      "  Downloading python_multipart-0.0.9-py3-none-any.whl (22 kB)\n",
      "Collecting pydantic-extra-types>=2.0.0\n",
      "  Downloading pydantic_extra_types-2.7.0-py3-none-any.whl (25 kB)\n",
      "Collecting pydantic-settings>=2.0.0\n",
      "  Downloading pydantic_settings-2.2.1-py3-none-any.whl (13 kB)\n",
      "Collecting orjson>=3.2.1\n",
      "  Downloading orjson-3.10.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (140 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m140.9/140.9 kB\u001b[0m \u001b[31m280.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: ujson!=4.0.2,!=4.1.0,!=4.2.0,!=4.3.0,!=5.0.0,!=5.1.0,>=4.0.1 in /opt/app-root/lib/python3.9/site-packages (from fastapi[all]<1,>=0.100->caikit[runtime-grpc,runtime-http]<0.27.0,>=0.26.17->caikit-nlp==0.4.9.dev2+gc12cb82) (5.9.0)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets>=2.4.0->caikit-nlp==0.4.9.dev2+gc12cb82) (1.4.1)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets>=2.4.0->caikit-nlp==0.4.9.dev2+gc12cb82) (6.0.5)\n",
      "Requirement already satisfied: attrs>=17.3.0 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets>=2.4.0->caikit-nlp==0.4.9.dev2+gc12cb82) (23.2.0)\n",
      "Requirement already satisfied: async-timeout<5.0,>=4.0 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets>=2.4.0->caikit-nlp==0.4.9.dev2+gc12cb82) (4.0.3)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets>=2.4.0->caikit-nlp==0.4.9.dev2+gc12cb82) (1.3.1)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in /opt/app-root/lib/python3.9/site-packages (from aiohttp->datasets>=2.4.0->caikit-nlp==0.4.9.dev2+gc12cb82) (1.9.4)\n",
      "Collecting grpcio<2.0,>=1.35.0\n",
      "  Downloading grpcio-1.62.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.6 MB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.6/5.6 MB\u001b[0m \u001b[31m248.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting protobuf<6,>=3.19.0\n",
      "  Downloading protobuf-5.26.1-cp37-abi3-manylinux2014_x86_64.whl (302 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m302.8/302.8 kB\u001b[0m \u001b[31m258.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: zipp>=0.5 in /opt/app-root/lib/python3.9/site-packages (from importlib-metadata<8.0.0,>=6.8.0->caikit[runtime-grpc,runtime-http]<0.27.0,>=0.26.17->caikit-nlp==0.4.9.dev2+gc12cb82) (3.17.0)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /opt/app-root/lib/python3.9/site-packages (from jinja2->torch>=2.0.1->caikit-nlp==0.4.9.dev2+gc12cb82) (2.1.5)\n",
      "Requirement already satisfied: setuptools>=39.0.1 in /opt/app-root/lib/python3.9/site-packages (from py-grpc-prometheus<0.9,>=0.7.0->caikit[runtime-grpc,runtime-http]<0.27.0,>=0.26.17->caikit-nlp==0.4.9.dev2+gc12cb82) (68.1.2)\n",
      "  Downloading protobuf-4.25.3-cp37-abi3-manylinux2014_x86_64.whl (294 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m294.6/294.6 kB\u001b[0m \u001b[31m271.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: certifi>=2017.4.17 in /opt/app-root/lib/python3.9/site-packages (from requests<3,>=2.28.2->caikit-tgis-backend<0.2.0,>=0.1.27->caikit-nlp==0.4.9.dev2+gc12cb82) (2024.2.2)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/app-root/lib/python3.9/site-packages (from requests<3,>=2.28.2->caikit-tgis-backend<0.2.0,>=0.1.27->caikit-nlp==0.4.9.dev2+gc12cb82) (1.26.18)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /opt/app-root/lib/python3.9/site-packages (from requests<3,>=2.28.2->caikit-tgis-backend<0.2.0,>=0.1.27->caikit-nlp==0.4.9.dev2+gc12cb82) (3.6)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/app-root/lib/python3.9/site-packages (from requests<3,>=2.28.2->caikit-tgis-backend<0.2.0,>=0.1.27->caikit-nlp==0.4.9.dev2+gc12cb82) (3.3.2)\n",
      "Requirement already satisfied: anyio in /opt/app-root/lib/python3.9/site-packages (from sse-starlette<3,>=1.6.1->caikit[runtime-grpc,runtime-http]<0.27.0,>=0.26.17->caikit-nlp==0.4.9.dev2+gc12cb82) (4.3.0)\n",
      "Requirement already satisfied: click in /opt/app-root/lib/python3.9/site-packages (from nltk->sentence-transformers<2.4.0,>=2.3.1->caikit-nlp==0.4.9.dev2+gc12cb82) (8.1.7)\n",
      "Requirement already satisfied: mpmath>=0.19 in /opt/app-root/lib/python3.9/site-packages (from sympy->torch>=2.0.1->caikit-nlp==0.4.9.dev2+gc12cb82) (1.3.0)\n",
      "Requirement already satisfied: dnspython>=2.0.0 in /opt/app-root/lib/python3.9/site-packages (from email-validator>=2.0.0->fastapi[all]<1,>=0.100->caikit[runtime-grpc,runtime-http]<0.27.0,>=0.26.17->caikit-nlp==0.4.9.dev2+gc12cb82) (2.6.1)\n",
      "Collecting httpcore==1.*\n",
      "  Downloading httpcore-1.0.5-py3-none-any.whl (77 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m257.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: sniffio in /opt/app-root/lib/python3.9/site-packages (from httpx>=0.23.0->fastapi[all]<1,>=0.100->caikit[runtime-grpc,runtime-http]<0.27.0,>=0.26.17->caikit-nlp==0.4.9.dev2+gc12cb82) (1.3.1)\n",
      "Collecting h11<0.15,>=0.13\n",
      "  Downloading h11-0.14.0-py3-none-any.whl (58 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m244.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,!=2.1.0,<3.0.0,>=1.7.4\n",
      "  Downloading pydantic-2.7.1-py3-none-any.whl (409 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m409.3/409.3 kB\u001b[0m \u001b[31m274.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting annotated-types>=0.4.0\n",
      "  Downloading annotated_types-0.6.0-py3-none-any.whl (12 kB)\n",
      "Collecting pydantic-core==2.18.2\n",
      "  Downloading pydantic_core-2.18.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.1/2.1 MB\u001b[0m \u001b[31m218.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting python-dotenv>=0.21.0\n",
      "  Downloading python_dotenv-1.0.1-py3-none-any.whl (19 kB)\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in /opt/app-root/lib/python3.9/site-packages (from anyio->sse-starlette<3,>=1.6.1->caikit[runtime-grpc,runtime-http]<0.27.0,>=0.26.17->caikit-nlp==0.4.9.dev2+gc12cb82) (1.2.0)\n",
      "Collecting watchfiles>=0.13\n",
      "  Downloading watchfiles-0.21.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m319.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting websockets>=10.4\n",
      "  Downloading websockets-12.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (130 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m130.0/130.0 kB\u001b[0m \u001b[31m283.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting httptools>=0.5.0\n",
      "  Downloading httptools-0.6.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (345 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m345.2/345.2 kB\u001b[0m \u001b[31m287.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting uvloop!=0.15.0,!=0.15.1,>=0.14.0\n",
      "  Downloading uvloop-0.19.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.5 MB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.5/3.5 MB\u001b[0m \u001b[31m232.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hBuilding wheels for collected packages: caikit-nlp\n",
      "  Building wheel for caikit-nlp (pyproject.toml) ... \u001b[?25ldone\n",
      "\u001b[?25h  Created wheel for caikit-nlp: filename=caikit_nlp-0.4.9.dev2+gc12cb82-py3-none-any.whl size=105362 sha256=b53f539b8612cc617877ec0e663ee13a542a6753f3994459594083a1f9b9d17a\n",
      "  Stored in directory: /tmp/pip-ephem-wheel-cache-s8xp8dl9/wheels/c9/17/3b/9156973c813daca1cc62f7a708d527eef028b44cdbe9d52ea6\n",
      "Successfully built caikit-nlp\n",
      "Installing collected packages: sentencepiece, ijson, alchemy-logging, xxhash, websockets, uvloop, semver, safetensors, regex, python-multipart, python-dotenv, pydantic-core, pyarrow-hotfix, protobuf, orjson, munch, multiprocess, itsdangerous, httptools, h11, grpcio, email-validator, docstring-parser, anytree, annotated-types, alchemy-config, watchfiles, uvicorn, starlette, pydantic, py-to-proto, py-grpc-prometheus, nltk, huggingface-hub, httpcore, grpcio-reflection, grpcio-health-checking, tokenizers, sse-starlette, pydantic-settings, pydantic-extra-types, httpx, fastapi, caikit, transformers, datasets, caikit-tgis-backend, accelerate, sentence-transformers, peft, caikit-nlp\n",
      "  Attempting uninstall: protobuf\n",
      "    Found existing installation: protobuf 3.20.2\n",
      "    Uninstalling protobuf-3.20.2:\n",
      "      Successfully uninstalled protobuf-3.20.2\n",
      "  Attempting uninstall: grpcio\n",
      "    Found existing installation: grpcio 1.62.0\n",
      "    Uninstalling grpcio-1.62.0:\n",
      "      Successfully uninstalled grpcio-1.62.0\n",
      "  Attempting uninstall: docstring-parser\n",
      "    Found existing installation: docstring_parser 0.8.1\n",
      "    Uninstalling docstring_parser-0.8.1:\n",
      "      Successfully uninstalled docstring_parser-0.8.1\n",
      "  Attempting uninstall: pydantic\n",
      "    Found existing installation: pydantic 1.10.14\n",
      "    Uninstalling pydantic-1.10.14:\n",
      "      Successfully uninstalled pydantic-1.10.14\n",
      "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
      "onnxconverter-common 1.14.0 requires protobuf==3.20.2, but you have protobuf 4.25.3 which is incompatible.\n",
      "mysql-connector-python 8.0.33 requires protobuf<=3.20.3,>=3.11.0, but you have protobuf 4.25.3 which is incompatible.\n",
      "kfp 1.8.22 requires protobuf<4,>=3.13.0, but you have protobuf 4.25.3 which is incompatible.\n",
      "kfp 1.8.22 requires pydantic<2,>=1.8.2, but you have pydantic 2.7.1 which is incompatible.\n",
      "kfp-pipeline-spec 0.1.16 requires protobuf<4,>=3.13.0, but you have protobuf 4.25.3 which is incompatible.\n",
      "codeflare-torchx 0.6.0.dev2 requires docstring-parser==0.8.1, but you have docstring-parser 0.16 which is incompatible.\n",
      "codeflare-sdk 0.14.1 requires pydantic<2, but you have pydantic 2.7.1 which is incompatible.\u001b[0m\u001b[31m\n",
      "\u001b[0mSuccessfully installed accelerate-0.29.3 alchemy-config-1.1.3 alchemy-logging-1.3.2 annotated-types-0.6.0 anytree-2.12.1 caikit-0.26.20 caikit-nlp-0.4.9.dev2+gc12cb82 caikit-tgis-backend-0.1.31 datasets-2.19.0 docstring-parser-0.16 email-validator-2.1.1 fastapi-0.110.2 grpcio-1.62.2 grpcio-health-checking-1.62.2 grpcio-reflection-1.62.2 h11-0.14.0 httpcore-1.0.5 httptools-0.6.1 httpx-0.27.0 huggingface-hub-0.22.2 ijson-3.2.3 itsdangerous-2.2.0 multiprocess-0.70.16 munch-4.0.0 nltk-3.8.1 orjson-3.10.1 peft-0.6.0 protobuf-4.25.3 py-grpc-prometheus-0.8.0 py-to-proto-0.5.2 pyarrow-hotfix-0.6 pydantic-2.7.1 pydantic-core-2.18.2 pydantic-extra-types-2.7.0 pydantic-settings-2.2.1 python-dotenv-1.0.1 python-multipart-0.0.9 regex-2024.4.16 safetensors-0.4.3 semver-3.0.2 sentence-transformers-2.3.1 sentencepiece-0.2.0 sse-starlette-2.1.0 starlette-0.37.2 tokenizers-0.19.1 transformers-4.40.1 uvicorn-0.29.0 uvloop-0.19.0 watchfiles-0.21.0 websockets-12.0 xxhash-3.4.1\n",
      "\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.2.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.0\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "!pip install ./caikit-nlp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "579be6f8-fda5-451b-ac20-12f4c808b618",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<function register_backend_type at 0x7f4aa20b7820> is still in the BETA phase and subject to change!\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import time\n",
    "import boto3\n",
    "import botocore\n",
    "\n",
    "from transformers import AutoTokenizer, AutoModelForCausalLM\n",
    "from peft import PeftModel, PeftConfig\n",
    "import caikit_nlp"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fbf41b9e",
   "metadata": {},
   "source": [
    "### Load model from HuggingFace Hub and save locally"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e9f7e5b0-5265-49f3-b0f7-cba6b6bf6e5b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('../models/opt-350m_DETOXIFY_CAUSAL_LM/tokenizer_config.json',\n",
       " '../models/opt-350m_DETOXIFY_CAUSAL_LM/special_tokens_map.json',\n",
       " '../models/opt-350m_DETOXIFY_CAUSAL_LM/vocab.json',\n",
       " '../models/opt-350m_DETOXIFY_CAUSAL_LM/merges.txt',\n",
       " '../models/opt-350m_DETOXIFY_CAUSAL_LM/added_tokens.json',\n",
       " '../models/opt-350m_DETOXIFY_CAUSAL_LM/tokenizer.json')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output_dir = \"../models/opt-350m_DETOXIFY_CAUSAL_LM\"\n",
    "\n",
    "model = AutoModelForCausalLM.from_pretrained(\"facebook/opt-350m\")\n",
    "tokenizer = AutoTokenizer.from_pretrained(\"facebook/opt-350m\")\n",
    "peft_model = PeftModel.from_pretrained(model, \"exyou/opt-350m_DETOXIFY_CAUSAL_LM\")\n",
    "peft_model = peft_model.merge_and_unload()\n",
    "\n",
    "peft_model.save_pretrained(output_dir)\n",
    "tokenizer.save_pretrained(output_dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "108e6ecf",
   "metadata": {},
   "source": [
    "### Convert model to Caikit format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "2b270421-e167-4fc4-9765-db7247357308",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time to convert model: 9.06516 seconds\n"
     ]
    }
   ],
   "source": [
    "input_dir = \"../models/opt-350m_DETOXIFY_CAUSAL_LM\"\n",
    "\n",
    "output_dir = \"../models/opt-350m_DETOXIFY_CAUSAL_LM-caikit\"\n",
    "\n",
    "start_time = time.time()\n",
    "model = caikit_nlp.text_generation.TextGeneration.bootstrap(input_dir)\n",
    "model.save(model_path=output_dir)\n",
    "end_time = time.time()\n",
    "print(f\"Time to convert model: {end_time - start_time:.5f} seconds\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8de1dad",
   "metadata": {},
   "source": [
    "### Upload Caikit model to S3 storage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5a5351e0-4ea3-4579-9d58-693e78b25f4d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "aws_access_key_id = os.environ.get('AWS_ACCESS_KEY_ID')\n",
    "aws_secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY')\n",
    "endpoint_url = os.environ.get('AWS_S3_ENDPOINT')\n",
    "region_name = os.environ.get('AWS_DEFAULT_REGION')\n",
    "bucket_name = os.environ.get('AWS_S3_BUCKET')\n",
    "\n",
    "session = boto3.session.Session(aws_access_key_id=aws_access_key_id,\n",
    "                                aws_secret_access_key=aws_secret_access_key)\n",
    "\n",
    "s3_resource = session.resource(\n",
    "    's3',\n",
    "    config=botocore.client.Config(signature_version='s3v4'),\n",
    "    endpoint_url=endpoint_url,\n",
    "    region_name=region_name)\n",
    "\n",
    "bucket = s3_resource.Bucket(bucket_name)\n",
    "\n",
    "\n",
    "def upload_directory_to_s3(local_directory, s3_prefix):\n",
    "    for root, dirs, files in os.walk(local_directory):\n",
    "        for filename in files:\n",
    "            file_path = os.path.join(root, filename)\n",
    "            relative_path = os.path.relpath(file_path, local_directory)\n",
    "            s3_key = os.path.join(s3_prefix, relative_path)\n",
    "            print(f\"{file_path} -> {s3_key}\")\n",
    "            bucket.upload_file(file_path, s3_key)\n",
    "\n",
    "def list_objects(prefix):\n",
    "    filter = bucket.objects.filter(Prefix=prefix)\n",
    "    for obj in filter.all():\n",
    "        print(obj.key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "09403e96-7970-4456-a480-faf153fe8bb7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../models/opt-350m_DETOXIFY_CAUSAL_LM/model.safetensors -> models/opt-350m_DETOXIFY_CAUSAL_LM/model.safetensors\n",
      "../models/opt-350m_DETOXIFY_CAUSAL_LM/merges.txt -> models/opt-350m_DETOXIFY_CAUSAL_LM/merges.txt\n",
      "../models/opt-350m_DETOXIFY_CAUSAL_LM/config.json -> models/opt-350m_DETOXIFY_CAUSAL_LM/config.json\n",
      "../models/opt-350m_DETOXIFY_CAUSAL_LM/tokenizer.json -> models/opt-350m_DETOXIFY_CAUSAL_LM/tokenizer.json\n",
      "../models/opt-350m_DETOXIFY_CAUSAL_LM/special_tokens_map.json -> models/opt-350m_DETOXIFY_CAUSAL_LM/special_tokens_map.json\n",
      "../models/opt-350m_DETOXIFY_CAUSAL_LM/tokenizer_config.json -> models/opt-350m_DETOXIFY_CAUSAL_LM/tokenizer_config.json\n",
      "../models/opt-350m_DETOXIFY_CAUSAL_LM/vocab.json -> models/opt-350m_DETOXIFY_CAUSAL_LM/vocab.json\n",
      "../models/opt-350m_DETOXIFY_CAUSAL_LM/generation_config.json -> models/opt-350m_DETOXIFY_CAUSAL_LM/generation_config.json\n",
      "../models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/config.yml -> models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/config.yml\n",
      "../models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/model.safetensors -> models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/model.safetensors\n",
      "../models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/merges.txt -> models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/merges.txt\n",
      "../models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/config.json -> models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/config.json\n",
      "../models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/tokenizer.json -> models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/tokenizer.json\n",
      "../models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/special_tokens_map.json -> models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/special_tokens_map.json\n",
      "../models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/tokenizer_config.json -> models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/tokenizer_config.json\n",
      "../models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/vocab.json -> models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/vocab.json\n",
      "../models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/generation_config.json -> models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/generation_config.json\n"
     ]
    }
   ],
   "source": [
    "upload_directory_to_s3(\"../models\", \"models\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "6c478800-c961-49d2-a7cf-bc291c6eb175",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/config.json\n",
      "models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/generation_config.json\n",
      "models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/merges.txt\n",
      "models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/model.safetensors\n",
      "models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/special_tokens_map.json\n",
      "models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/tokenizer.json\n",
      "models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/tokenizer_config.json\n",
      "models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/artifacts/vocab.json\n",
      "models/opt-350m_DETOXIFY_CAUSAL_LM-caikit/config.yml\n",
      "models/opt-350m_DETOXIFY_CAUSAL_LM/config.json\n",
      "models/opt-350m_DETOXIFY_CAUSAL_LM/generation_config.json\n",
      "models/opt-350m_DETOXIFY_CAUSAL_LM/merges.txt\n",
      "models/opt-350m_DETOXIFY_CAUSAL_LM/model.safetensors\n",
      "models/opt-350m_DETOXIFY_CAUSAL_LM/special_tokens_map.json\n",
      "models/opt-350m_DETOXIFY_CAUSAL_LM/tokenizer.json\n",
      "models/opt-350m_DETOXIFY_CAUSAL_LM/tokenizer_config.json\n",
      "models/opt-350m_DETOXIFY_CAUSAL_LM/vocab.json\n"
     ]
    }
   ],
   "source": [
    "# sanity check\n",
    "list_objects(\"models\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9",
   "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.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}