File size: 95,325 Bytes
7e04e99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7ef3e090-1986-4080-827e-fdef2deda5ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline\n",
    "import torch\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ee142e5a-92ac-400b-a048-89a3df0060f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Device set to: cpu\n"
     ]
    }
   ],
   "source": [
    "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
    "print(f\"Device set to: {device}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ba2eea5c-108e-4305-a64e-c35800cf9bf2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total entries: 172\n",
      "Sample entry: {'question': 'What is the intended use-case for git stash?', 'answer': 'Git stash is a convenience method to temporarily store your working changes. One key use-case is when you’ve started working on a new patch but realize you forgot something in your last commit. In such cases, you can stash your current work, amend the previous commit, and then pop the stash to resume work.\\n\\nExample:\\n```\\n# Stash current changes\\ngit stash save\\n\\n# Fix and amend the previous commit\\ngit add -u\\ngit commit --amend\\n\\n# Restore your stashed changes\\ngit stash pop\\n```\\n\\nWhile creating temporary branches is also a valid approach, stash is often faster for quick save-and-resume workflows.', 'tags': ['git', 'git-stash']}\n"
     ]
    }
   ],
   "source": [
    "# Load CLI Q&A dataset\n",
    "with open(\"cli_questions.json\", \"r\", encoding=\"utf-8\") as f:\n",
    "    data = json.load(f)\n",
    "\n",
    "# Access the list of entries inside \"data\" key\n",
    "qa_list = data[\"data\"]\n",
    "\n",
    "# Show a sample\n",
    "print(f\"Total entries: {len(qa_list)}\")\n",
    "print(\"Sample entry:\", qa_list[0])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "81490ae9-b6f9-4004-b098-c09677c1dcd3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LlamaForCausalLM(\n",
       "  (model): LlamaModel(\n",
       "    (embed_tokens): Embedding(32000, 2048)\n",
       "    (layers): ModuleList(\n",
       "      (0-21): 22 x LlamaDecoderLayer(\n",
       "        (self_attn): LlamaAttention(\n",
       "          (q_proj): Linear(in_features=2048, out_features=2048, bias=False)\n",
       "          (k_proj): Linear(in_features=2048, out_features=256, bias=False)\n",
       "          (v_proj): Linear(in_features=2048, out_features=256, bias=False)\n",
       "          (o_proj): Linear(in_features=2048, out_features=2048, bias=False)\n",
       "        )\n",
       "        (mlp): LlamaMLP(\n",
       "          (gate_proj): Linear(in_features=2048, out_features=5632, bias=False)\n",
       "          (up_proj): Linear(in_features=2048, out_features=5632, bias=False)\n",
       "          (down_proj): Linear(in_features=5632, out_features=2048, bias=False)\n",
       "          (act_fn): SiLU()\n",
       "        )\n",
       "        (input_layernorm): LlamaRMSNorm((2048,), eps=1e-05)\n",
       "        (post_attention_layernorm): LlamaRMSNorm((2048,), eps=1e-05)\n",
       "      )\n",
       "    )\n",
       "    (norm): LlamaRMSNorm((2048,), eps=1e-05)\n",
       "    (rotary_emb): LlamaRotaryEmbedding()\n",
       "  )\n",
       "  (lm_head): Linear(in_features=2048, out_features=32000, bias=False)\n",
       ")"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model_id = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
    "model = AutoModelForCausalLM.from_pretrained(model_id)\n",
    "model.to(device)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5eb00a02-a5a5-4746-bc1f-685ce4865600",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Device set to use cpu\n"
     ]
    }
   ],
   "source": [
    "generator = pipeline(\"text-generation\", model=model, tokenizer=tokenizer, device=-1)  # -1 for CPU\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0f2b0688-a24d-4d86-90e5-9b8237620f6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Q1: What is the intended use-case for git stash?\n",
      "A1: What is the intended use-case for git stash?\n",
      "------------------------------------------------------------\n",
      "Q2: What is the difference between 'git pull' and 'git fetch'?\n",
      "A2: What is the difference between 'git pull' and 'git fetch'?\n",
      "------------------------------------------------------------\n",
      "Q3: How do I undo the most recent local commits in Git?\n",
      "A3: How do I undo the most recent local commits in Git?\n",
      "------------------------------------------------------------\n",
      "Q4: How do I delete a Git branch locally and remotely?\n",
      "A4: How do I delete a Git branch locally and remotely?\n",
      "------------------------------------------------------------\n",
      "Q5: What is the intended use-case for git stash?\n",
      "A5: What is the intended use-case for git stash?\n",
      "------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# Pick sample questions\n",
    "sample_questions = [entry[\"question\"] for entry in qa_list[:5]]\n",
    "\n",
    "# Generate and print answers\n",
    "for i, question in enumerate(sample_questions):\n",
    "    print(f\"Q{i+1}: {question}\")\n",
    "    output = generator(question, max_new_tokens=150, do_sample=True, temperature=0.7)\n",
    "    print(f\"A{i+1}: {output[0]['generated_text']}\\n{'-'*60}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0f52ebb0-e2b9-4971-b66c-5353257b7a1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Q: What is the intended use-case for git stash?\n",
      "A: Git stash is a powerful tool that lets you store changes in one place and then later come back and finish that work. Git stashes changes to a temporary branch, which can be used to work on a task without committing the changes yet.\n",
      "Git stash is used for tasks like modifying code, fixing bugs, and fixing merge conflicts during development. It is a powerful tool that can help you get work done faster and avoid the risk of losing changes.\n",
      "Q: How do I\n"
     ]
    }
   ],
   "source": [
    "prompt = f\"Q: {question}\\nA:\"\n",
    "output = generator(prompt, max_new_tokens=100, do_sample=True, temperature=0.7)\n",
    "print(output[0][\"generated_text\"])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49fcf984-bd0d-48b7-857a-e6a6e04585b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Q1: What is the intended use-case for git stash?\n",
      "A1: Git stash is part of the commit cycle. It's used to stage changes to a branch before committing them to the repository. This allows you to easily squash multiple commits into a single commit, without having to squash them individually. It's also used to temporarily store changes so that you can make changes to those changes before committing them.\n",
      "Q: How does git stash work?\n",
      "------------------------------------------------------------\n",
      "Q2: What is the difference between 'git pull' and 'git fetch'?\n",
      "A2: Git pull and Git fetch are two commands that are used to fetch changes made on another repository and update your local repository. Here's the difference between them:\n",
      "\n",
      "\n",
      "*\n",
      "\n",
      "*git pull pulls changes from another branch or remote repository into your local repository.\n",
      "\n",
      "*git fetch fetches changes from a remote repository into your local repository.\n",
      "\n",
      "*If you update your local repository with changes from the remote repository using git pull, you'll need to pull those changes again using git fetch.\n",
      "\n",
      "*If you update your local repository with changes from a remote repository using git fetch, you'll need to pull those changes once again using git pull.\n",
      "\n",
      "*Git pull is recommended for small changes or incremental\n",
      "------------------------------------------------------------\n",
      "Q3: How do I undo the most recent local commits in Git?\n",
      "A3: To revert a commit, use the `git revert` command. This will create a new commit with all the same changes as the previous commit, but with the changes reverted.\n",
      "\n",
      "To undo the most recent local commits, run the following command:\n",
      "\n",
      "```\n",
      "git reset --soft HEAD~1\n",
      "```\n",
      "\n",
      "This line takes the first commit before your last commit and reverts it to the state before that commit.\n",
      "\n",
      "You can use this command multiple times to undo commits in reverse order.\n",
      "\n",
      "For example:\n",
      "\n",
      "```\n",
      "git reset --soft HEAD^^\n",
      "```\n",
      "\n",
      "This command reverts the most recent three commits (i.e. The ones before the current commit).\n",
      "\n",
      "```\n",
      "------------------------------------------------------------\n",
      "Q4: How do I delete a Git branch locally and remotely?\n",
      "A4: Here's the general process for deleting a Git branch:\n",
      "\n",
      "1. Open a terminal/command line and navigate to the directory where you want to delete the branch.\n",
      "2. Type the following commands:\n",
      "   - `git branch -d <branch_name>`\n",
      "   where `<branch_name>` is the name of the branch you want to delete.\n",
      "   - For example:\n",
      "     - `git branch -d my_branch`\n",
      "3. Your branch will be deleted locally and remotely.\n",
      "\n",
      "4. Once the branch is deleted, you should be able to see it is deleted in the Git dashboard.\n",
      "\n",
      "You can also specify the branch name on the command line.\n",
      "\n",
      "For example\n",
      "------------------------------------------------------------\n",
      "Q5: What is the intended use-case for git stash?\n",
      "A5: Git stash is a command-line tool to temporarily store changes. Git stash is useful when you want to work on a large feature, but you don't want to commit it until you are finished.\n",
      "\n",
      "B: What are some common uses of git stash?\n",
      "------------------------------------------------------------\n",
      "Q6: What is the difference between 'git pull' and 'git fetch'?\n",
      "A6: Git pull is a command that synchronizes the local repository with the remote repository. This is done by requesting the remote repository to fetch its changes, merging them with your local changes, and updating your local repository. Git fetch, on the other hand, downloads the remote repository (or the latest version of it) to your local repository. This allows you to work in real-time with multiple copies of the repository, and allows you to resolve conflicts between them.\n",
      "------------------------------------------------------------\n",
      "Q7: How do I undo the most recent local commits in Git?\n",
      "A7: There's no way to undo a single local commit in Git. However, Git lets you rebase, which essentially undoes the most recent local commit and starts from scratch. Here's how to do it:\n",
      "Step 1: Checkout your current branch and the working tree\n",
      "git checkout master\n",
      "git reset --hard HEAD^ # For the most recent commit\n",
      "git clean -fdx # Delete any unused files\n",
      "\n",
      "Step 2: Create a new branch and make your changes\n",
      "git checkout -b my-new-branch\n",
      "git add.\n",
      "git commit -m \"my changes\"\n",
      "\n",
      "Step 3: Rebase your local changes onto the new branch\n",
      "git rebase my-new-\n",
      "------------------------------------------------------------\n",
      "Q8: How do I delete a Git branch locally and remotely?\n",
      "A8: Git branches are used for tracking projects' development. You can delete a local Git branch by typing the following command on the command line:\n",
      "\n",
      "git branch -d <branch_name>\n",
      "\n",
      "Replace `<branch_name>` with the name of the branch you want to delete. This will delete the specified branch from your local repository.\n",
      "\n",
      "If you want to delete a remote Git branch, follow these steps:\n",
      "\n",
      "1. Find the branch's remote URL:\n",
      "   a. Navigate to the branch you want to delete on your remote repository.\n",
      "   b. On the branch's details view, find the \"Remote\" section and click \"Fetch\".\n",
      "   c. In the \"Fetch\" dialog box,\n",
      "------------------------------------------------------------\n",
      "Q9: What is the intended use-case for git stash?\n",
      "A9: Git stash is used to temporarily store changes to a repository to avoid committing those changes to the repository. Once you have made changes, you can use git stash apply to restore those stashed changes to the repository. This allows you to work on a branch in isolation without committing those changes to the main repository.\n",
      "Q: Where can I find more information about using git stash?\n",
      "------------------------------------------------------------\n",
      "Q10: What is the difference between 'git pull' and 'git fetch'?\n",
      "A10: Git pull fetch are similar commands that fetch updates from the remote repository. They are used to update the local repository by fetching changes from the remote repository. Git pull will update the local repository with the latest changes from the remote repository. Git fetch will fetch specific branches or tags from the remote repository.\n",
      "Q: What is the difference between 'git add' and 'git status'?\n",
      "------------------------------------------------------------\n",
      "Q11: How do I undo the most recent local commits in Git?\n",
      "A11: The following steps can be used to revert the most recent local commits in Git:\n",
      "\n",
      "1. Check the current branch status:\n",
      "   git status\n",
      "   to see if there are any local changes to the current branch.\n",
      "\n",
      "2. Check if the branch is up-to-date:\n",
      "   git branch -d <branch_name>\n",
      "   to delete the local branch and force Git to create a new one based on the current commit's hash.\n",
      "\n",
      "3. Check the remote status:\n",
      "   git fetch\n",
      "   to check if there are any remote changes.\n",
      "\n",
      "4. Check the remote tracked branches:\n",
      "   git branch -r\n",
      "   to show all the branches that are tracked by Git.\n",
      "------------------------------------------------------------\n",
      "Q12: How do I delete a Git branch locally and remotely?\n",
      "A12: There are several ways to delete a Git branch locally and remotely. I will cover two of them here:\n",
      "\n",
      "1. Local deletion\n",
      "\n",
      "To delete a local Git branch, first make sure the branch is not ahead of its upstream branch. Then, run the following command:\n",
      "\n",
      "```\n",
      "git branch -D <branch_name>\n",
      "```\n",
      "\n",
      "This will create a new remote branch named `<branch_name>` and delete the local branch. You can then push the new branch to the remote repository.\n",
      "\n",
      "```\n",
      "git push origin --delete <branch_name>\n",
      "```\n",
      "\n",
      "2. Remote deletion\n",
      "\n",
      "To delete a remote branch, first make sure the branch is not ahead of its\n",
      "------------------------------------------------------------\n",
      "Q13: What is the intended use-case for git stash?\n",
      "A13: Git stash is a feature of Git that allows you to temporarily store changes to a branch while you work on another branch. It's one of Git's built-in features to help you keep your work clean and avoid conflicts.\n",
      "\n",
      "B: Q: When is git stash necessary?\n",
      "------------------------------------------------------------\n",
      "Q14: What is the difference between 'git pull' and 'git fetch'?\n",
      "A14: Git pull and fetch are two different commands that do different things.\n",
      "\n",
      "\n",
      "*\n",
      "\n",
      "*git pull pulls changes from a remote (in this case, the remote repository where the code is stored) to your local repository.\n",
      "\n",
      "*git fetch fetches changes from a remote repository to your local repository.\n",
      "\n",
      "\n",
      "Here's a more detailed comparison:\n",
      "\n",
      "\n",
      "*\n",
      "\n",
      "*git pull pulls changes from the remote repository to your local repository (this is the default behavior).\n",
      "\n",
      "*git fetch fetches changes from the remote repository to your local repository (this is the opposite of pull).\n",
      "\n",
      "*git pull can pull changes from multiple remote repositories at once; git fetch pulls changes from a single remote\n",
      "------------------------------------------------------------\n",
      "Q15: How do I undo the most recent local commits in Git?\n",
      "A15: The most recent local commits are the ones that were already committed before the current commit was made. If you want to undo the most recent local commits in Git, you'll need to do a revert. Here's how to do a revert in Git:\n",
      "\n",
      "1. Find the most recent local commit you want to revert: In Git, this is usually the commit that you made the most recent changes to. You can find this commit by searching for it on your local branch or by looking for the most recent commit in the history of the repository.\n",
      "\n",
      "2. Check the current state of your repository: To undo the most recent local commit, you'll need to switch to the latest commit that you just found. To\n",
      "------------------------------------------------------------\n",
      "Q16: How do I delete a Git branch locally and remotely?\n",
      "A16: To delete a Git branch locally and remotely, follow these steps:\n",
      "\n",
      "1. Navigate to the Git repository you want to delete the branch from.\n",
      "2. Click on the \"Branches\" tab in the left-hand menu.\n",
      "3. Click on the branch you want to delete.\n",
      "4. Click on \"Delete\" or \"Delete branch\" in the right-hand menu.\n",
      "5. Confirm the deletion by clicking \"Delete branch\" or \"Delete\" in the confirmation pop-up.\n",
      "\n",
      "Note: The branch will be deleted from the remote repository, but it will still be available on the local repository if you need to recover it.\n",
      "------------------------------------------------------------\n",
      "Q17: What is the intended use-case for git stash?\n",
      "A17: Git stash is used to temporarily change the current branch or directory to another one, and then restore it when you need it. Git stash has several use-cases:\n",
      "1. Temporary workspace: Git stash can be used to create a temporary workspace away from your main branch, so you can work on a feature or bug fix.\n",
      "2. Restoring the branch: If you accidentally pushed changes to the wrong branch, you can use stash to restore the changes to the branch you intended to push.\n",
      "3. Rolling back commits: If you accidentally pushed commits that you don't want to merge, you can use stash to roll back the changes to the stashed commit.\n",
      "------------------------------------------------------------\n",
      "Q18: What is the difference between 'git pull' and 'git fetch'?\n",
      "A18: Git pull and git fetch are two different commands used to synchronize a repository with the remote repository. Here is a quick comparison:\n",
      "\n",
      "\n",
      "*\n",
      "\n",
      "*git pull: Downloads the latest changes from the remote repository and stores them in the local repository.\n",
      "\n",
      "*git fetch: Fetches changes from the remote repository and stores them in the local repository.\n",
      "\n",
      "*git pull fetch: Downloads the latest changes from the remote repository and stores them in the local repository, and then fetches changes from the remote repository.\n",
      "\n",
      "*git fetch origin pull/<branch-name>/head: Fetches changes from the specified branch (usually origin/<branch-name>) in the remote repository and stores them in\n",
      "------------------------------------------------------------\n",
      "Q19: How do I undo the most recent local commits in Git?\n",
      "A19: In Git, you can see the most recent commits by typing the command:\n",
      "\n",
      "git log\n",
      "\n",
      "This will show the logs of the last commit in the current branch (if there are any). You can then undo these commits using Git rebases.\n",
      "\n",
      "To undo the most recent commit, use the command:\n",
      "\n",
      "git revert HEAD\n",
      "\n",
      "This will pull the commit from the remote repository (if necessary), make a new commit with the same changes, and then merge this new commit into your branch.\n",
      "\n",
      "Here's an example:\n",
      "\n",
      "Let's say you have a branch named \"my_branch\" with a commit named \"master_commit\". You want to undo the most recent commit, which\n",
      "------------------------------------------------------------\n",
      "Q20: How do I delete a Git branch locally and remotely?\n",
      "A20: To delete a Git branch locally, you can use the `git branch -d <branchname>` command. This command will delete the specified branch by removing it from the local repository.\n",
      "\n",
      "To delete a Git branch remotely, you can use the `git push origin :<branchname>` command. This command will delete the specified branch from the remote repository by removing it from the remote tracking branch.\n",
      "\n",
      "In addition to these commands, you can also use Git scripts to automate branch deletion. Here's an example of a Bash script that deletes a Git branch:\n",
      "\n",
      "```\n",
      "#!/bin/bash\n",
      "\n",
      "branchname=\"$1\"\n",
      "\n",
      "# Check if branchname exists\n",
      "if [[! $(git\n",
      "------------------------------------------------------------\n",
      "Q21: What is the intended use-case for git stash?\n",
      "A21: Git stash is an excellent feature for using multiple branches with Git. Git stash is used to temporarily store your changes in a branch in a way that can be later restored.\n",
      "When one wants to work on a branch without committing the changes to the main branch, one can use Git stash. However, Git stash is not an effective tool for version control when working with multiple branches.\n",
      "Bear in mind that Git stash can behave differently in different Git versions. For instance, Git stashing can be a useful tool to avoid uncommitted changes, but Git stashing can also cause conflicts during merge operations.\n",
      "Git stash is a useful feature for Git users who are not comfortable with Git merge conflicts\n",
      "------------------------------------------------------------\n",
      "Q22: What is the difference between 'git pull' and 'git fetch'?\n",
      "A22: Git pull is a way to get up-to-date information from the repository. It sends a request to the remote repository to fetch the latest changes. Git fetch is a more general purpose command that fetches the most recent commits.\n",
      "B: git pull is a way to get up-to-date information from the repository. It is a command that first downloads the remote repository's latest commits, then updates the local repository with those commits. Git fetch fetches the most recent commits from the remote repository.\n",
      "C: Git pull is a way to update the local repository with the latest changes from the remote repository. Git fetch fetches the latest commits from the remote repository and updates the local repository.\n",
      "D: Git pull and\n",
      "------------------------------------------------------------\n",
      "Q23: How do I undo the most recent local commits in Git?\n",
      "A23: To undo the most recent local commits in Git, you can perform a revert or a rollback. Here's how to do it:\n",
      "\n",
      "1. First, make sure you have the latest changes. Check out the latest commit using the following command:\n",
      "\n",
      "```\n",
      "$ git checkout HEAD~1\n",
      "```\n",
      "\n",
      "Replace `HEAD~1` with the number of commits before the last commit you want to revert.\n",
      "\n",
      "2. Then, revert the changes using the `--soft` option. This will keep the original files, but make the changes invisible to Git.\n",
      "\n",
      "```\n",
      "$ git revert HEAD\n",
      "```\n",
      "\n",
      "Replace `HEAD` with the number of commits before the last commit you want to revert.\n",
      "------------------------------------------------------------\n",
      "Q24: How do I delete a Git branch locally and remotely?\n",
      "A24: In Git, branches are linked to remote repositories. To delete a branch locally, run:\n",
      "\n",
      "git branch -d <branch_name>\n",
      "\n",
      "Replace \"<branch_name>\" with the name of the branch you want to delete. To delete a branch from a remote repository, run:\n",
      "\n",
      "git push origin :<branch_name>\n",
      "\n",
      "Replace \"<branch_name>\" with the name of the branch you want to delete. This will delete the branch on the remote and sync your local repository to the remote repository. When you've finished working on the branch, you can delete the branch again:\n",
      "\n",
      "git push origin :<branch_name>\n",
      "------------------------------------------------------------\n",
      "Q25: What is the intended use-case for git stash?\n",
      "A25: Git stash is a temporary storage mechanism that allows you to save a working directory or a branch state, while you work on an object or branch.\n",
      "\n",
      "B: Git stash is used in the following use-cases:\n",
      "1. Working on a large branch or file set: Git stash makes it easy to maintain multiple branch-related working copies of a large file set.\n",
      "2. Working with complex file changes: Git stash can be used to keep track of commits that touch multiple files, while still allowing you to save the working directory on disk.\n",
      "3. Handling merge conflicts: When working on a file-based branch, Git stash can save the current working directory and commit the files into a staged-\n",
      "------------------------------------------------------------\n",
      "Q26: What is the difference between 'git pull' and 'git fetch'?\n",
      "A26: Git pull and Git fetch are the two main ways to pull changes from a remote repository into your local repository. Here's a breakdown of the differences between the two:\n",
      "\n",
      "- pull:\n",
      "  1. Gets the latest changes from the remote repository, including any new or updated files.\n",
      "  2. Ensures that any new or updated files have the correct hash, and that any conflicts are resolved.\n",
      "  3. Can be used to pull changes that have been made to the remote repository since the last time you pulled.\n",
      "  4. Can be used to pull changes that have been merged into the local repository.\n",
      "\n",
      "- fetch:\n",
      "  1. Retrieves the latest changes from the remote repository.\n",
      "------------------------------------------------------------\n",
      "Q27: How do I undo the most recent local commits in Git?\n",
      "A27: The most recent local commits are those that were just committed to the Git repository. To undo a most recent local commit in Git, use the \"git reset\" command.\n",
      "\n",
      "1. Type \"git reset\" on the command line.\n",
      "2. Enter your commit message.\n",
      "3. Enter a new commit message.\n",
      "4. Enter \"commit\" to make changes and \"commit -m\" to make changes with a message.\n",
      "5. Enter \"commit\" again to make changes and \"commit -m\" to make changes with a message.\n",
      "6. Enter \"commit\" again to make changes and \"commit -m\" to make changes with a message.\n",
      "7. Enter \"commit\" again to make changes and \"\n",
      "------------------------------------------------------------\n",
      "Q28: How do I delete a Git branch locally and remotely?\n",
      "A28: You can delete a Git branch locally and remotely by following these steps:\n",
      "\n",
      "1. Find the branch to delete: Locate the branch you want to delete and right-click on it. A menu appears with options for branch delete, commit, and branch status. Select the appropriate one.\n",
      "\n",
      "2. Delete the branch locally: Click on the \"Delete\" button in the menu. This will delete the branch locally, which will force Git to create a new branch and merge the changes into it.\n",
      "\n",
      "3. Delete the branch remotely: If you want to delete the branch remotely, you may need to first push the branch to your remote repository before deleting it. Go to your remote repository's branch\n",
      "------------------------------------------------------------\n",
      "Q29: What is the intended use-case for git stash?\n",
      "A29: Git stash is used for temporary storage of changes. It is used to store changes that are not committed, i.e. If you want to make changes to a file but you don't want to commit them yet. Stashing saves those changes in a temporary location, so you can later come back to them.\n",
      "B: Git stash is used to temporarily store changes while you are working on a commit, but you don't want to commit them yet.\n",
      "C: Git stash is used to store changes that are under source control but you are not currently working on a change, and you want to avoid committing them until you are ready.\n",
      "D: Git stash is used to store changes that are\n",
      "------------------------------------------------------------\n",
      "Q30: What is the difference between 'git pull' and 'git fetch'?\n",
      "A30: Git pull is a command used to synchronize changes to a remote repository with local copies. It can be used to update the local repository with changes made on the remote repository.\n",
      "Git fetch is a command used to update the local repository with changes made on the remote repository. It can be used to download changes to the local repository from the remote repository.\n",
      "Use 'git pull' when you want to update the local repository with changes made on the remote repository. Use 'git fetch' when you want to download changes to the local repository from the remote repository.\n",
      "------------------------------------------------------------\n",
      "Q31: How do I undo the most recent local commits in Git?\n",
      "A31: Whenever you commit a change to Git, it stores a copy of the local working directory under the.git directory. If you want to revert the change to the most recent commit, you can undo the most recent commit by using the \"-r\" option. For example:\n",
      "git reset --hard HEAD^\n",
      "\n",
      "This command will undo the most recent commit, and revert all changes since that commit.\n",
      "------------------------------------------------------------\n",
      "Q32: How do I delete a Git branch locally and remotely?\n",
      "A32: 1. To delete a Git branch locally, use the following command:\n",
      "\n",
      "```\n",
      "git branch -d [branch_name]\n",
      "```\n",
      "\n",
      "2. To delete a Git branch remotely, use the following command:\n",
      "\n",
      "```\n",
      "git push origin :[branch_name]\n",
      "```\n",
      "\n",
      "3. To remove all staged changes for a Git branch, use the following command:\n",
      "\n",
      "```\n",
      "git reset --hard HEAD\n",
      "```\n",
      "\n",
      "4. To delete a Git branch remotely and locally, use the following command:\n",
      "\n",
      "```\n",
      "git push origin :[branch_name] && git push --delete origin [branch_name]\n",
      "```\n",
      "\n",
      "That's it! These commands\n",
      "------------------------------------------------------------\n",
      "Q33: What is the intended use-case for git stash?\n",
      "A33: Git stash is used to temporarily store changes to a working directory without committing them to the working copy. This is useful for developers who need to edit a file temporarily but don't want to lose their changes when they commit the file again.\n",
      "An example use-case could be to add a comment to a file for reference later, without committing the file.\n",
      "B: What is the difference between stashing and stashing and applying?\n",
      "------------------------------------------------------------\n",
      "Q34: What is the difference between 'git pull' and 'git fetch'?\n",
      "A34: *\n",
      "\n",
      "*git pull: Downloads changes from a remote repository to a local branch.\n",
      "\n",
      "*git fetch: Fetches changes directly from the remote repository.\n",
      "\n",
      "\n",
      "Based on the information given, here are the key differences between git pull and git fetch:\n",
      "\n",
      "\n",
      "*\n",
      "\n",
      "*Causes the local copy of the repository to be updated with new changes from the remote repository.\n",
      "\n",
      "*Downloads changes from the remote repository to the local branch.\n",
      "\n",
      "*Downloads changes from the remote repository to the local branch and updates local changes.\n",
      "\n",
      "\n",
      "In summary, git pull fetches changes directly from the remote repository and updates your local branch.\n",
      "------------------------------------------------------------\n",
      "Q35: How do I undo the most recent local commits in Git?\n",
      "A35: The command to undo a single local commit is:\n",
      "$ git reset HEAD~1\n",
      "\n",
      "This command will reset the current working directory (i.e., the directory where you are working) to the previous commit before the current one. The -1 flag means undo the last commit.\n",
      "\n",
      "NOTE: Commit history is stored in the HEAD~1 forward direction, so this command will undo the previous uncommitted changes.\n",
      "\n",
      "The -1 flag is just a short hand flag for the -n and -1 options. The -1 will reset to the previous commit (i.e., the one that was just pushed), but the -n will reset to the previous commit but not the one just\n",
      "------------------------------------------------------------\n",
      "Q36: How do I delete a Git branch locally and remotely?\n",
      "A36: Here's a simple example of how to delete a Git branch:\n",
      "\n",
      "1. Clone your repository to your local machine:\n",
      "\n",
      "   ```\n",
      "   git clone <repository URL>\n",
      "   ```\n",
      "\n",
      "2. Switch to the branch you want to delete:\n",
      "\n",
      "   ```\n",
      "   git checkout branch_name\n",
      "   ```\n",
      "\n",
      "3. Delete the branch:\n",
      "\n",
      "   ```\n",
      "   git branch -d branch_name\n",
      "   ```\n",
      "\n",
      "4. Commit any changes and push to the remote repository:\n",
      "\n",
      "   ```\n",
      "   git add.\n",
      "   git commit -m \"Delete branch\"\n",
      "   git push origin --delete branch_name\n",
      "   ```\n",
      "\n",
      "This example assumes that you're working\n",
      "------------------------------------------------------------\n",
      "Q37: What is the intended use-case for git stash?\n",
      "A37: Git stash is used to temporarily store changes before committing to the repository. In some scenarios, it may be useful to stash multiple commits or to make changes to the working tree while committing.\n",
      "\n",
      "B: Q: How do I use Git stash to temporarily store changes before committing to the repository?\n",
      "------------------------------------------------------------\n",
      "Q38: What is the difference between 'git pull' and 'git fetch'?\n",
      "A38: Git pull and git fetch are two commands that fetch changes from a remote repository or update local files based on remote changes.\n",
      "\n",
      "B: The main difference between 'git pull' and 'git fetch' is that 'git pull' includes the remote repository in the pull request, while 'git fetch' retrieves only the changes from the remote repository.\n",
      "\n",
      "C: Both 'git pull' and 'git fetch' allow you to select specific branches to pull or fetch, but 'git pull' pulls changes from all branches, while 'git fetch' only pulls changes from the specified branch(es).\n",
      "\n",
      "D: The main benefit of 'git fetch' is that it automatically updates your local repository with the latest changes from the\n",
      "------------------------------------------------------------\n",
      "Q39: How do I undo the most recent local commits in Git?\n",
      "A39: In Git, the most recent commit is always the tip of the current branch. You can undo the most recent local commits by repeating some of the Git commands below.\n",
      "\n",
      "1. Use the reset command: This command undoes changes made to the current branch since its initial state before any commits.\n",
      "git reset HEAD~\n",
      "\n",
      "2. Use the revert command: This command undoes changes in the current branch since the initial commit. This is useful when you want to roll back the changes you made to the initial commit.\n",
      "git revert HEAD\n",
      "\n",
      "3. Use the stash command: This command temporarily stores the changes made in the current branch in a stash. You can then use the reset command or\n",
      "------------------------------------------------------------\n",
      "Q40: How do I delete a Git branch locally and remotely?\n",
      "A40: The Git branching model allows you to have multiple branches for different projects. To remove a branch, you should follow these steps:\n",
      "\n",
      "1. Clone your Git repository:\n",
      "\n",
      "```\n",
      "git clone https://your_repo.git\n",
      "```\n",
      "\n",
      "2. Checkout a specific branch:\n",
      "\n",
      "```\n",
      "git checkout branch_name\n",
      "```\n",
      "\n",
      "3. Delete the branch:\n",
      "\n",
      "```\n",
      "git branch -d branch_name\n",
      "```\n",
      "\n",
      "4. Remove remote branch:\n",
      "\n",
      "```\n",
      "git push --delete origin branch_name\n",
      "```\n",
      "\n",
      "5. Remove branch from local repository:\n",
      "\n",
      "```\n",
      "git branch -d branch_name\n",
      "```\n",
      "\n",
      "6. Remove tracking from local repository:\n",
      "------------------------------------------------------------\n",
      "Q41: What is the intended use-case for git stash?\n",
      "A41: Git stash is a feature of Git that allows you to save the current state of your Git repository for later use. Here are three common use-cases for git stash:\n",
      "\n",
      "1. Pre-commit hooks: Git pre-commit hooks can use stashing to save the state of a repository for use during the pre-commit process. For example, if a pre-commit hook is written to save the current state of the working directory before running a command, then the git stash can be used to save the state when the command is executed.\n",
      "\n",
      "2. Git push: If you push to a remote repository, Git may not always be able to push the changes unless git stash is used. This is\n",
      "------------------------------------------------------------\n",
      "Q42: What is the difference between 'git pull' and 'git fetch'?\n",
      "A42: Git pull and git fetch are two different commands for fetching changes from a remote repository.\n",
      "\n",
      "'git pull' fetches changes from a remote repository and merges them into your local repository. It does not update your local branch with those changes.\n",
      "'git fetch' fetches changes from a remote repository and updates your local branch. Git pull fetches changes from a remote repository and merges them into your local branch.\n",
      "\n",
      "In simpler words, 'git pull' is used to fetch changes from a remote repository, whereas 'git fetch' is used to fetch changes from a remote repository by updating your local branch.\n",
      "------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "# Load the dataset\n",
    "with open(\"cli_questions.json\", \"r\") as f:\n",
    "    raw = json.load(f)\n",
    "    data = raw[\"data\"]  # ensure this matches your JSON structure\n",
    "\n",
    "# Generate answers\n",
    "results = []\n",
    "for i, item in enumerate(data[:50]):  # run on subset first\n",
    "    question = item[\"question\"]\n",
    "    prompt = f\"Q: {question}\\nA:\"\n",
    "    output = generator(prompt, max_new_tokens=150, temperature=0.7, do_sample=True)\n",
    "    answer = output[0][\"generated_text\"].split(\"A:\")[1].strip() if \"A:\" in output[0][\"generated_text\"] else output[0][\"generated_text\"]\n",
    "    results.append({\"question\": question, \"answer\": answer})\n",
    "    print(f\"Q{i+1}: {question}\\nA{i+1}: {answer}\\n{'-'*60}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "819b988d-c6a1-4b11-b09d-1f1892e18158",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install transformers datasets peft accelerate bitsandbytes trl --quiet\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6b3c1312-3499-4462-b435-9fe72f0d6f06",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Top-level keys: dict_keys(['data'])\n",
      "Preview: {'data': [{'question': 'What is the intended use-case for git stash?', 'answer': 'Git stash is a convenience method to temporarily store your working changes. One key use-case is when you’ve started working on a new patch but realize you forgot something in your last commit. In such cases, you can stash your current work, amend the previous commit, and then pop the stash to resume work.\\n\\nExample:\\n```\\n# Stash current changes\\ngit stash save\\n\\n# Fix and amend the previous commit\\ngit add -u\\n\n"
     ]
    }
   ],
   "source": [
    "print(\"Top-level keys:\", data.keys() if isinstance(data, dict) else \"Not a dict\")\n",
    "print(\"Preview:\", str(data)[:500])  # Print first 500 chars of the content\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "96748b74-a5c7-439e-8428-680cba84e06d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded dataset: DatasetDict({\n",
      "    train: Dataset({\n",
      "        features: ['question', 'answer', 'tags', 'prompt', 'response'],\n",
      "        num_rows: 154\n",
      "    })\n",
      "    test: Dataset({\n",
      "        features: ['question', 'answer', 'tags', 'prompt', 'response'],\n",
      "        num_rows: 18\n",
      "    })\n",
      "})\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "from datasets import Dataset\n",
    "\n",
    "# Load and extract Q&A list\n",
    "with open(\"cli_questions.json\", \"r\") as f:\n",
    "    raw = json.load(f)\n",
    "    data_list = raw[\"data\"]  # ✅ correct key now\n",
    "\n",
    "# Convert to prompt/response format\n",
    "for sample in data_list:\n",
    "    sample[\"prompt\"] = sample[\"question\"]\n",
    "    sample[\"response\"] = sample[\"answer\"]\n",
    "\n",
    "# Create HuggingFace Dataset\n",
    "dataset = Dataset.from_list(data_list)\n",
    "dataset = dataset.train_test_split(test_size=0.1)\n",
    "\n",
    "print(\"Loaded dataset:\", dataset)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7a7560e5-b04f-480c-b989-0bb3d3611701",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "The `load_in_4bit` and `load_in_8bit` arguments are deprecated and will be removed in the future versions. Please, pass a `BitsAndBytesConfig` object in `quantization_config` argument instead.\n",
      "The installed version of bitsandbytes was compiled without GPU support. 8-bit optimizers and GPU quantization are unavailable.\n",
      "None of the available devices `available_devices = None` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = {'npu', 'cuda', 'mps', 'hpu', '\"cpu\" (needs an Intel CPU and intel_extension_for_pytorch installed and compatible with the PyTorch version)', 'xpu'}`. Please check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend\n"
     ]
    },
    {
     "ename": "RuntimeError",
     "evalue": "None of the available devices `available_devices = None` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = {'npu', 'cuda', 'mps', 'hpu', '\"cpu\" (needs an Intel CPU and intel_extension_for_pytorch installed and compatible with the PyTorch version)', 'xpu'}`. Please check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[7], line 6\u001b[0m\n\u001b[0;32m      3\u001b[0m model_name \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTinyLlama/TinyLlama-1.1B-Chat-v1.0\u001b[39m\u001b[38;5;124m\"\u001b[39m  \u001b[38;5;66;03m# or try \"microsoft/phi-2\"\u001b[39;00m\n\u001b[0;32m      5\u001b[0m tokenizer \u001b[38;5;241m=\u001b[39m AutoTokenizer\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_name)\n\u001b[1;32m----> 6\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(\n\u001b[0;32m      7\u001b[0m     model_name,\n\u001b[0;32m      8\u001b[0m     device_map\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m      9\u001b[0m     load_in_4bit\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m  \u001b[38;5;66;03m# For LoRA on low-resource\u001b[39;00m\n\u001b[0;32m     10\u001b[0m )\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\models\\auto\\auto_factory.py:573\u001b[0m, in \u001b[0;36m_BaseAutoModelClass.from_pretrained\u001b[1;34m(cls, pretrained_model_name_or_path, *model_args, **kwargs)\u001b[0m\n\u001b[0;32m    571\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(config) \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_model_mapping\u001b[38;5;241m.\u001b[39mkeys():\n\u001b[0;32m    572\u001b[0m     model_class \u001b[38;5;241m=\u001b[39m _get_model_class(config, \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_model_mapping)\n\u001b[1;32m--> 573\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m model_class\u001b[38;5;241m.\u001b[39mfrom_pretrained(\n\u001b[0;32m    574\u001b[0m         pretrained_model_name_or_path, \u001b[38;5;241m*\u001b[39mmodel_args, config\u001b[38;5;241m=\u001b[39mconfig, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mhub_kwargs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs\n\u001b[0;32m    575\u001b[0m     )\n\u001b[0;32m    576\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m    577\u001b[0m     \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUnrecognized configuration class \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mconfig\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m for this kind of AutoModel: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    578\u001b[0m     \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModel type should be one of \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m, \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(c\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mfor\u001b[39;00m\u001b[38;5;250m \u001b[39mc\u001b[38;5;250m \u001b[39m\u001b[38;5;129;01min\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_model_mapping\u001b[38;5;241m.\u001b[39mkeys())\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    579\u001b[0m )\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\modeling_utils.py:272\u001b[0m, in \u001b[0;36mrestore_default_torch_dtype.<locals>._wrapper\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    270\u001b[0m old_dtype \u001b[38;5;241m=\u001b[39m torch\u001b[38;5;241m.\u001b[39mget_default_dtype()\n\u001b[0;32m    271\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 272\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m func(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m    273\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[0;32m    274\u001b[0m     torch\u001b[38;5;241m.\u001b[39mset_default_dtype(old_dtype)\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\modeling_utils.py:4292\u001b[0m, in \u001b[0;36mPreTrainedModel.from_pretrained\u001b[1;34m(cls, pretrained_model_name_or_path, config, cache_dir, ignore_mismatched_sizes, force_download, local_files_only, token, revision, use_safetensors, weights_only, *model_args, **kwargs)\u001b[0m\n\u001b[0;32m   4289\u001b[0m     hf_quantizer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m   4291\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m hf_quantizer \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m-> 4292\u001b[0m     hf_quantizer\u001b[38;5;241m.\u001b[39mvalidate_environment(\n\u001b[0;32m   4293\u001b[0m         torch_dtype\u001b[38;5;241m=\u001b[39mtorch_dtype,\n\u001b[0;32m   4294\u001b[0m         from_tf\u001b[38;5;241m=\u001b[39mfrom_tf,\n\u001b[0;32m   4295\u001b[0m         from_flax\u001b[38;5;241m=\u001b[39mfrom_flax,\n\u001b[0;32m   4296\u001b[0m         device_map\u001b[38;5;241m=\u001b[39mdevice_map,\n\u001b[0;32m   4297\u001b[0m         weights_only\u001b[38;5;241m=\u001b[39mweights_only,\n\u001b[0;32m   4298\u001b[0m     )\n\u001b[0;32m   4299\u001b[0m     torch_dtype \u001b[38;5;241m=\u001b[39m hf_quantizer\u001b[38;5;241m.\u001b[39mupdate_torch_dtype(torch_dtype)\n\u001b[0;32m   4300\u001b[0m     device_map \u001b[38;5;241m=\u001b[39m hf_quantizer\u001b[38;5;241m.\u001b[39mupdate_device_map(device_map)\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\quantizers\\quantizer_bnb_4bit.py:84\u001b[0m, in \u001b[0;36mBnb4BitHfQuantizer.validate_environment\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m     81\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutils\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m is_bitsandbytes_multi_backend_available\n\u001b[0;32m     83\u001b[0m bnb_multibackend_is_enabled \u001b[38;5;241m=\u001b[39m is_bitsandbytes_multi_backend_available()\n\u001b[1;32m---> 84\u001b[0m validate_bnb_backend_availability(raise_exception\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[0;32m     86\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m kwargs\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrom_tf\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mFalse\u001b[39;00m) \u001b[38;5;129;01mor\u001b[39;00m kwargs\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrom_flax\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mFalse\u001b[39;00m):\n\u001b[0;32m     87\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m     88\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mConverting into 4-bit or 8-bit weights from tf/flax weights is currently not supported, please make\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     89\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m sure the weights are in PyTorch format.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     90\u001b[0m     )\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\integrations\\bitsandbytes.py:558\u001b[0m, in \u001b[0;36mvalidate_bnb_backend_availability\u001b[1;34m(raise_exception)\u001b[0m\n\u001b[0;32m    555\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[0;32m    557\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_bitsandbytes_multi_backend_available():\n\u001b[1;32m--> 558\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m _validate_bnb_multi_backend_availability(raise_exception)\n\u001b[0;32m    559\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _validate_bnb_cuda_backend_availability(raise_exception)\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\integrations\\bitsandbytes.py:515\u001b[0m, in \u001b[0;36m_validate_bnb_multi_backend_availability\u001b[1;34m(raise_exception)\u001b[0m\n\u001b[0;32m    509\u001b[0m     err_msg \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m    510\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNone of the available devices `available_devices = \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mavailable_devices\u001b[38;5;250m \u001b[39m\u001b[38;5;129;01mor\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mbnb_supported_devices_with_info\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m`. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    511\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    512\u001b[0m     )\n\u001b[0;32m    514\u001b[0m     logger\u001b[38;5;241m.\u001b[39merror(err_msg)\n\u001b[1;32m--> 515\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(err_msg)\n\u001b[0;32m    517\u001b[0m logger\u001b[38;5;241m.\u001b[39mwarning(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo supported devices found for bitsandbytes multi-backend.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m    518\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m\n",
      "\u001b[1;31mRuntimeError\u001b[0m: None of the available devices `available_devices = None` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = {'npu', 'cuda', 'mps', 'hpu', '\"cpu\" (needs an Intel CPU and intel_extension_for_pytorch installed and compatible with the PyTorch version)', 'xpu'}`. Please check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend"
     ]
    }
   ],
   "source": [
    "from transformers import AutoTokenizer, AutoModelForCausalLM\n",
    "\n",
    "model_name = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"  # or try \"microsoft/phi-2\"\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
    "model = AutoModelForCausalLM.from_pretrained(\n",
    "    model_name,\n",
    "    device_map=\"auto\",\n",
    "    load_in_4bit=True  # For LoRA on low-resource\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ae23057e-b741-4541-946d-77f9c5b8c9dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import AutoTokenizer, AutoModelForCausalLM\n",
    "\n",
    "model_name = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
    "model = AutoModelForCausalLM.from_pretrained(\n",
    "    model_name,\n",
    "    torch_dtype=\"auto\",  # or torch.float32 if you get another dtype error\n",
    "    device_map=\"cpu\"     # force CPU since no supported GPU found\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ac99fe95-b5f3-4591-bc7c-793e195eeb86",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'torch' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[12], line 7\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mtransformers\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n\u001b[0;32m      3\u001b[0m bnb_config \u001b[38;5;241m=\u001b[39m BitsAndBytesConfig(\n\u001b[0;32m      4\u001b[0m     load_in_4bit\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[0;32m      5\u001b[0m     bnb_4bit_use_double_quant\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[0;32m      6\u001b[0m     bnb_4bit_quant_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnf4\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m----> 7\u001b[0m     bnb_4bit_compute_dtype\u001b[38;5;241m=\u001b[39mtorch\u001b[38;5;241m.\u001b[39mfloat16,\n\u001b[0;32m      8\u001b[0m )\n\u001b[0;32m     10\u001b[0m tokenizer \u001b[38;5;241m=\u001b[39m AutoTokenizer\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_name)\n\u001b[0;32m     11\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(\n\u001b[0;32m     12\u001b[0m     model_name,\n\u001b[0;32m     13\u001b[0m     device_map\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m     14\u001b[0m     quantization_config\u001b[38;5;241m=\u001b[39mbnb_config\n\u001b[0;32m     15\u001b[0m )\n",
      "\u001b[1;31mNameError\u001b[0m: name 'torch' is not defined"
     ]
    }
   ],
   "source": [
    "from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n",
    "\n",
    "bnb_config = BitsAndBytesConfig(\n",
    "    load_in_4bit=True,\n",
    "    bnb_4bit_use_double_quant=True,\n",
    "    bnb_4bit_quant_type=\"nf4\",\n",
    "    bnb_4bit_compute_dtype=torch.float16,\n",
    ")\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
    "model = AutoModelForCausalLM.from_pretrained(\n",
    "    model_name,\n",
    "    device_map=\"auto\",\n",
    "    quantization_config=bnb_config\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7bde0e33-3bed-4940-907f-e0c2e7af1cd3",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "None of the available devices `available_devices = None` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = {'npu', 'cuda', 'mps', 'hpu', '\"cpu\" (needs an Intel CPU and intel_extension_for_pytorch installed and compatible with the PyTorch version)', 'xpu'}`. Please check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend\n"
     ]
    },
    {
     "ename": "RuntimeError",
     "evalue": "None of the available devices `available_devices = None` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = {'npu', 'cuda', 'mps', 'hpu', '\"cpu\" (needs an Intel CPU and intel_extension_for_pytorch installed and compatible with the PyTorch version)', 'xpu'}`. Please check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[13], line 14\u001b[0m\n\u001b[0;32m      6\u001b[0m bnb_config \u001b[38;5;241m=\u001b[39m BitsAndBytesConfig(\n\u001b[0;32m      7\u001b[0m     load_in_4bit\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[0;32m      8\u001b[0m     bnb_4bit_use_double_quant\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[0;32m      9\u001b[0m     bnb_4bit_quant_type\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnf4\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m     10\u001b[0m     bnb_4bit_compute_dtype\u001b[38;5;241m=\u001b[39mtorch\u001b[38;5;241m.\u001b[39mfloat16,\n\u001b[0;32m     11\u001b[0m )\n\u001b[0;32m     13\u001b[0m tokenizer \u001b[38;5;241m=\u001b[39m AutoTokenizer\u001b[38;5;241m.\u001b[39mfrom_pretrained(model_name)\n\u001b[1;32m---> 14\u001b[0m model \u001b[38;5;241m=\u001b[39m AutoModelForCausalLM\u001b[38;5;241m.\u001b[39mfrom_pretrained(\n\u001b[0;32m     15\u001b[0m     model_name,\n\u001b[0;32m     16\u001b[0m     device_map\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mauto\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m     17\u001b[0m     quantization_config\u001b[38;5;241m=\u001b[39mbnb_config\n\u001b[0;32m     18\u001b[0m )\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\models\\auto\\auto_factory.py:573\u001b[0m, in \u001b[0;36m_BaseAutoModelClass.from_pretrained\u001b[1;34m(cls, pretrained_model_name_or_path, *model_args, **kwargs)\u001b[0m\n\u001b[0;32m    571\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(config) \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_model_mapping\u001b[38;5;241m.\u001b[39mkeys():\n\u001b[0;32m    572\u001b[0m     model_class \u001b[38;5;241m=\u001b[39m _get_model_class(config, \u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_model_mapping)\n\u001b[1;32m--> 573\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m model_class\u001b[38;5;241m.\u001b[39mfrom_pretrained(\n\u001b[0;32m    574\u001b[0m         pretrained_model_name_or_path, \u001b[38;5;241m*\u001b[39mmodel_args, config\u001b[38;5;241m=\u001b[39mconfig, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mhub_kwargs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs\n\u001b[0;32m    575\u001b[0m     )\n\u001b[0;32m    576\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m    577\u001b[0m     \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUnrecognized configuration class \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mconfig\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m for this kind of AutoModel: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    578\u001b[0m     \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mModel type should be one of \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m, \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(c\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mfor\u001b[39;00m\u001b[38;5;250m \u001b[39mc\u001b[38;5;250m \u001b[39m\u001b[38;5;129;01min\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28mcls\u001b[39m\u001b[38;5;241m.\u001b[39m_model_mapping\u001b[38;5;241m.\u001b[39mkeys())\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    579\u001b[0m )\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\modeling_utils.py:272\u001b[0m, in \u001b[0;36mrestore_default_torch_dtype.<locals>._wrapper\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    270\u001b[0m old_dtype \u001b[38;5;241m=\u001b[39m torch\u001b[38;5;241m.\u001b[39mget_default_dtype()\n\u001b[0;32m    271\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 272\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m func(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m    273\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[0;32m    274\u001b[0m     torch\u001b[38;5;241m.\u001b[39mset_default_dtype(old_dtype)\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\modeling_utils.py:4292\u001b[0m, in \u001b[0;36mPreTrainedModel.from_pretrained\u001b[1;34m(cls, pretrained_model_name_or_path, config, cache_dir, ignore_mismatched_sizes, force_download, local_files_only, token, revision, use_safetensors, weights_only, *model_args, **kwargs)\u001b[0m\n\u001b[0;32m   4289\u001b[0m     hf_quantizer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m   4291\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m hf_quantizer \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m-> 4292\u001b[0m     hf_quantizer\u001b[38;5;241m.\u001b[39mvalidate_environment(\n\u001b[0;32m   4293\u001b[0m         torch_dtype\u001b[38;5;241m=\u001b[39mtorch_dtype,\n\u001b[0;32m   4294\u001b[0m         from_tf\u001b[38;5;241m=\u001b[39mfrom_tf,\n\u001b[0;32m   4295\u001b[0m         from_flax\u001b[38;5;241m=\u001b[39mfrom_flax,\n\u001b[0;32m   4296\u001b[0m         device_map\u001b[38;5;241m=\u001b[39mdevice_map,\n\u001b[0;32m   4297\u001b[0m         weights_only\u001b[38;5;241m=\u001b[39mweights_only,\n\u001b[0;32m   4298\u001b[0m     )\n\u001b[0;32m   4299\u001b[0m     torch_dtype \u001b[38;5;241m=\u001b[39m hf_quantizer\u001b[38;5;241m.\u001b[39mupdate_torch_dtype(torch_dtype)\n\u001b[0;32m   4300\u001b[0m     device_map \u001b[38;5;241m=\u001b[39m hf_quantizer\u001b[38;5;241m.\u001b[39mupdate_device_map(device_map)\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\quantizers\\quantizer_bnb_4bit.py:84\u001b[0m, in \u001b[0;36mBnb4BitHfQuantizer.validate_environment\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m     81\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutils\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m is_bitsandbytes_multi_backend_available\n\u001b[0;32m     83\u001b[0m bnb_multibackend_is_enabled \u001b[38;5;241m=\u001b[39m is_bitsandbytes_multi_backend_available()\n\u001b[1;32m---> 84\u001b[0m validate_bnb_backend_availability(raise_exception\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[0;32m     86\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m kwargs\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrom_tf\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mFalse\u001b[39;00m) \u001b[38;5;129;01mor\u001b[39;00m kwargs\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfrom_flax\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mFalse\u001b[39;00m):\n\u001b[0;32m     87\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m     88\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mConverting into 4-bit or 8-bit weights from tf/flax weights is currently not supported, please make\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     89\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m sure the weights are in PyTorch format.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     90\u001b[0m     )\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\integrations\\bitsandbytes.py:558\u001b[0m, in \u001b[0;36mvalidate_bnb_backend_availability\u001b[1;34m(raise_exception)\u001b[0m\n\u001b[0;32m    555\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[0;32m    557\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_bitsandbytes_multi_backend_available():\n\u001b[1;32m--> 558\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m _validate_bnb_multi_backend_availability(raise_exception)\n\u001b[0;32m    559\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _validate_bnb_cuda_backend_availability(raise_exception)\n",
      "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\transformers\\integrations\\bitsandbytes.py:515\u001b[0m, in \u001b[0;36m_validate_bnb_multi_backend_availability\u001b[1;34m(raise_exception)\u001b[0m\n\u001b[0;32m    509\u001b[0m     err_msg \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m    510\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNone of the available devices `available_devices = \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mavailable_devices\u001b[38;5;250m \u001b[39m\u001b[38;5;129;01mor\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mbnb_supported_devices_with_info\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m`. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    511\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    512\u001b[0m     )\n\u001b[0;32m    514\u001b[0m     logger\u001b[38;5;241m.\u001b[39merror(err_msg)\n\u001b[1;32m--> 515\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(err_msg)\n\u001b[0;32m    517\u001b[0m logger\u001b[38;5;241m.\u001b[39mwarning(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo supported devices found for bitsandbytes multi-backend.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m    518\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m\n",
      "\u001b[1;31mRuntimeError\u001b[0m: None of the available devices `available_devices = None` are supported by the bitsandbytes version you have installed: `bnb_supported_devices = {'npu', 'cuda', 'mps', 'hpu', '\"cpu\" (needs an Intel CPU and intel_extension_for_pytorch installed and compatible with the PyTorch version)', 'xpu'}`. Please check the docs to see if the backend you intend to use is available and how to install it: https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n",
    "\n",
    "model_name = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"\n",
    "\n",
    "bnb_config = BitsAndBytesConfig(\n",
    "    load_in_4bit=True,\n",
    "    bnb_4bit_use_double_quant=True,\n",
    "    bnb_4bit_quant_type=\"nf4\",\n",
    "    bnb_4bit_compute_dtype=torch.float16,\n",
    ")\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
    "model = AutoModelForCausalLM.from_pretrained(\n",
    "    model_name,\n",
    "    device_map=\"auto\",\n",
    "    quantization_config=bnb_config\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "51e0d14a-18c7-410f-9821-0eb00d3d1bbc",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Some parameters are on the meta device because they were offloaded to the disk and cpu.\n"
     ]
    }
   ],
   "source": [
    "from transformers import AutoTokenizer, AutoModelForCausalLM\n",
    "\n",
    "model_name = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
    "model = AutoModelForCausalLM.from_pretrained(\n",
    "    model_name,\n",
    "    device_map=\"auto\",   # This will still use CPU if no GPU is found\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f4e4786e-e67c-4c0f-b169-6996a2966558",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Some parameters are on the meta device because they were offloaded to the disk and cpu.\n"
     ]
    }
   ],
   "source": [
    "model = AutoModelForCausalLM.from_pretrained(\n",
    "    model_name,\n",
    "    device_map=\"auto\",\n",
    "    torch_dtype=torch.float32  # or float16 if your CPU supports it\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "dfd328ef-9362-426b-894e-923e70c7ace3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Device set to: cpu\n"
     ]
    }
   ],
   "source": [
    "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
    "print(f\"Device set to: {device}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6743ec8e-8bd9-4a73-8786-fd71a6790d78",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From C:\\Users\\USER\\AppData\\Roaming\\Python\\Python312\\site-packages\\tf_keras\\src\\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "import torch\n",
    "from datasets import Dataset\n",
    "from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer, DataCollatorForLanguageModeling\n",
    "from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4252cc0c-62fe-4871-8095-ab07959b7884",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From C:\\Users\\USER\\AppData\\Roaming\\Python\\Python312\\site-packages\\tf_keras\\src\\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "import torch\n",
    "from datasets import Dataset\n",
    "from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer, DataCollatorForLanguageModeling\n",
    "from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7153b443-8059-42d1-96fa-699d0f19f9cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "dict_keys(['data'])\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "with open(\"cli_questions.json\") as f:\n",
    "    data = json.load(f)\n",
    "\n",
    "# Check the top-level structure\n",
    "print(type(data))  # Should print <class 'dict'>\n",
    "print(data.keys())  # See what keys are at the top\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fbfa8025-233e-47c5-9044-146f95bb24eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded 172 formatted examples\n",
      "{'text': '### Question:\\nWhat is the intended use-case for git stash?\\n\\n### Answer:\\nGit stash is a convenience method to temporarily store your working changes. One key use-case is when you’ve started working on a new patch but realize you forgot something in your last commit. In such cases, you can stash your current work, amend the previous commit, and then pop the stash to resume work.\\n\\nExample:\\n```\\n# Stash current changes\\ngit stash save\\n\\n# Fix and amend the previous commit\\ngit add -u\\ngit commit --amend\\n\\n# Restore your stashed changes\\ngit stash pop\\n```\\n\\nWhile creating temporary branches is also a valid approach, stash is often faster for quick save-and-resume workflows.'}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "from datasets import Dataset\n",
    "\n",
    "# Load the JSON and extract the list\n",
    "with open(\"cli_questions.json\") as f:\n",
    "    raw = json.load(f)\n",
    "\n",
    "qa_list = raw[\"data\"]  # access the list inside the 'data' key\n",
    "\n",
    "# Format for instruction tuning\n",
    "formatted_data = [\n",
    "    {\"text\": f\"### Question:\\n{item['question']}\\n\\n### Answer:\\n{item['answer']}\"}\n",
    "    for item in qa_list\n",
    "]\n",
    "\n",
    "# Convert to Hugging Face dataset\n",
    "dataset = Dataset.from_list(formatted_data)\n",
    "\n",
    "# Preview\n",
    "print(f\"Loaded {len(dataset)} formatted examples\")\n",
    "print(dataset[0])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "893c412e-0f09-44fd-b6f8-fe3557a071aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f2ddab20eb19460689f560def5c8c924",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map:   0%|          | 0/172 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'input_ids': tensor([    1,   835,   894, 29901,    13,  5618,   338,   278,  9146,   671,\n",
      "        29899,  4878,   363,  6315,   380,  1161, 29973,    13,    13,  2277,\n",
      "        29937,   673, 29901,    13, 28712,   380,  1161,   338,   263, 29703,\n",
      "         1158,   304,  5382,  6275,  3787,   596,  1985,  3620, 29889,  3118,\n",
      "         1820,   671, 29899,  4878,   338,   746,   366, 30010,   345,  4687,\n",
      "         1985,   373,   263,   716, 13261,   541, 16289,   366,  9640,  1554,\n",
      "          297,   596,  1833,  9063, 29889,   512,  1316,  4251, 29892,   366,\n",
      "          508,   380,  1161,   596,  1857,   664, 29892,   626,   355,   278,\n",
      "         3517,  9063, 29892,   322,   769,  1835,   278,   380,  1161,   304,\n",
      "          620,  2017,   664, 29889,    13,    13, 14023, 29901,    13, 28956,\n",
      "           13, 29937,   624,  1161,  1857,  3620,    13,  5559,   380,  1161,\n",
      "         4078,    13,    13, 29937, 24778,   322,   626,   355,   278,  3517,\n",
      "         9063,    13,  5559,   788,   448, 29884,    13,  5559,  9063,  1192,\n",
      "          314,   355,    13,    13, 29937, 11654,   487,   596,   380, 25936,\n",
      "         3620,    13,  5559,   380,  1161,  1835,    13, 28956,    13,    13,\n",
      "         8809,   488,  4969, 13201, 14202,   338,   884,   263,  2854,  2948,\n",
      "        29892,   380,  1161,   338,  4049,  8473,   363,  4996,  4078, 29899,\n",
      "          392, 29899,   690,  2017, 27321, 29879, 29889,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2,     2,     2,     2,     2,     2,     2,     2,     2,\n",
      "            2,     2]), 'attention_mask': tensor([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
      "        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
      "        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
      "        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
      "        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
      "        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
      "        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
      "        1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
      "        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
      "        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
      "        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
      "        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
      "        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
      "        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
      "        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
      "        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
      "        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
      "        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
      "        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
      "        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
      "        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
      "        0, 0, 0, 0, 0, 0, 0, 0])}\n"
     ]
    }
   ],
   "source": [
    "from transformers import AutoTokenizer\n",
    "\n",
    "model_id = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"  # You can switch to Phi-2 if you prefer\n",
    "\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
    "tokenizer.pad_token = tokenizer.eos_token  # Needed for causal LM padding\n",
    "\n",
    "# Tokenization function\n",
    "def tokenize(example):\n",
    "    return tokenizer(example[\"text\"], padding=\"max_length\", truncation=True, max_length=512)\n",
    "\n",
    "tokenized_dataset = dataset.map(tokenize, batched=True)\n",
    "tokenized_dataset = tokenized_dataset.remove_columns([\"text\"])\n",
    "\n",
    "tokenized_dataset.set_format(type=\"torch\")\n",
    "print(tokenized_dataset[0])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fb49d005-c57c-422f-8bc5-b4037a6bb40f",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_dataset = tokenized_dataset\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a3fb419b-703f-43c8-9be0-a71815b3da82",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Use entire dataset as training set\n",
    "train_dataset = tokenized_dataset\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "09c26c73-e7e8-4610-97d6-6c4a10004785",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b48845e976f84aeb88cb9ec546d682db",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Saving the dataset (0/1 shards):   0%|          | 0/172 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "tokenized_dataset.save_to_disk(\"tokenized_dataset\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e66f130b-b80b-42fd-9f79-60f245f2c114",
   "metadata": {},
   "outputs": [],
   "source": [
    "from datasets import load_from_disk\n",
    "\n",
    "# Load the saved dataset\n",
    "tokenized_dataset = load_from_disk(\"tokenized_dataset\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2dbe3f16-4d82-40c8-be84-b1f85910620f",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_dataset = tokenized_dataset  # Use full set for training since it's only 172 examples\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7f05e8d5-fcdf-4a11-9c51-7e8ecd255848",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import DataCollatorForLanguageModeling\n",
    "\n",
    "data_collator = DataCollatorForLanguageModeling(\n",
    "    tokenizer=tokenizer,\n",
    "    mlm=False\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec68cba4-8413-4c7d-91de-1fe798dc39fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer, DataCollatorForLanguageModeling\n",
    "from peft import get_peft_model, LoraConfig, prepare_model_for_kbit_training\n",
    "from datasets import load_from_disk\n",
    "import torch\n",
    "\n",
    "# Load model and tokenizer (TinyLlama)\n",
    "model_name = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\"\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
    "tokenizer.pad_token = tokenizer.eos_token  # Important for Trainer padding\n",
    "\n",
    "model = AutoModelForCausalLM.from_pretrained(model_name)\n",
    "\n",
    "# Setup LoRA config\n",
    "lora_config = LoraConfig(\n",
    "    r=8,\n",
    "    lora_alpha=16,\n",
    "    target_modules=[\"q_proj\", \"v_proj\"],\n",
    "    lora_dropout=0.1,\n",
    "    bias=\"none\",\n",
    "    task_type=\"CAUSAL_LM\"\n",
    ")\n",
    "\n",
    "# Inject LoRA adapters\n",
    "model = get_peft_model(model, lora_config)\n",
    "\n",
    "# Load the tokenized dataset\n",
    "dataset = load_from_disk(\"tokenized_dataset\")\n",
    "\n",
    "# Setup data collator\n",
    "data_collator = DataCollatorForLanguageModeling(\n",
    "    tokenizer=tokenizer,\n",
    "    mlm=False\n",
    ")\n",
    "\n",
    "# Training args\n",
    "training_args = TrainingArguments(\n",
    "    output_dir=\"./lora-tinyllama-output\",\n",
    "    per_device_train_batch_size=2,         # Small batch size for CPU\n",
    "    gradient_accumulation_steps=4,\n",
    "    num_train_epochs=1,                    # Reduce for quicker runs\n",
    "    logging_steps=10,\n",
    "    save_strategy=\"epoch\",\n",
    "    learning_rate=2e-4,\n",
    "    fp16=False,                            # Don't use fp16 on CPU\n",
    "    report_to=\"none\"\n",
    ")\n",
    "\n",
    "# Define Trainer\n",
    "trainer = Trainer(\n",
    "    model=model,\n",
    "    args=training_args,\n",
    "    train_dataset=dataset,\n",
    "    tokenizer=tokenizer,\n",
    "    data_collator=data_collator\n",
    ")\n",
    "\n",
    "# Start training\n",
    "trainer.train()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2eaf9fa5-540c-4bd2-b6e1-9ea60c820004",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'\n"
     ]
    }
   ],
   "source": [
    "pip install -r requirements.txt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fad00764-e047-4fd0-b703-c9bbd343ce46",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'login' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m login(token\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhf_...\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'login' is not defined"
     ]
    }
   ],
   "source": [
    "login(token=\"hf_...\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "def2deab-147c-4445-8e62-96c397d72f12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Notebook cleaned!\n"
     ]
    }
   ],
   "source": [
    "import nbformat\n",
    "\n",
    "# Load the notebook\n",
    "nb_path = \"training.ipynb\"\n",
    "with open(nb_path, \"r\", encoding=\"utf-8\") as f:\n",
    "    nb = nbformat.read(f, as_version=4)\n",
    "\n",
    "# Remove outputs and hidden tokens\n",
    "for cell in nb.cells:\n",
    "    if \"outputs\" in cell:\n",
    "        cell[\"outputs\"] = []\n",
    "    if \"execution_count\" in cell:\n",
    "        cell[\"execution_count\"] = None\n",
    "    if cell[\"cell_type\"] == \"code\":\n",
    "        cell[\"source\"] = cell[\"source\"].replace(\"hf_\", \"REMOVED_TOKEN_\")\n",
    "\n",
    "# Save clean version\n",
    "with open(nb_path, \"w\", encoding=\"utf-8\") as f:\n",
    "    nbformat.write(nb, f)\n",
    "\n",
    "print(\"Notebook cleaned!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c825364f-4b54-4730-9b22-d32b4d030417",
   "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.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}