File size: 47,146 Bytes
cfb4a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66148ed
 
 
 
 
 
 
 
 
 
 
 
cfb4a98
 
 
 
 
 
 
66148ed
 
cfb4a98
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66148ed
cfb4a98
 
66148ed
 
cfb4a98
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
 
 
 
 
 
66148ed
 
 
 
 
 
 
cfb4a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
66148ed
cfb4a98
 
66148ed
 
cfb4a98
 
 
66148ed
cfb4a98
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
66148ed
 
cfb4a98
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66148ed
 
cfb4a98
66148ed
cfb4a98
 
 
66148ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfb4a98
66148ed
 
cfb4a98
66148ed
 
 
 
 
 
 
 
 
cfb4a98
66148ed
cfb4a98
66148ed
cfb4a98
 
 
 
 
66148ed
 
 
 
cfb4a98
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
66148ed
 
cfb4a98
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
 
 
 
66148ed
 
 
 
 
cfb4a98
 
 
 
66148ed
 
 
cfb4a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66148ed
cfb4a98
66148ed
 
 
 
 
 
 
cfb4a98
 
 
 
 
 
 
 
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66148ed
cfb4a98
 
66148ed
 
cfb4a98
 
 
66148ed
cfb4a98
 
 
 
66148ed
cfb4a98
 
66148ed
 
 
cfb4a98
 
66148ed
cfb4a98
 
 
 
 
66148ed
cfb4a98
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
66148ed
 
cfb4a98
 
 
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
 
 
 
66148ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfb4a98
 
 
 
 
 
 
 
 
 
 
66148ed
cfb4a98
 
66148ed
 
cfb4a98
 
 
66148ed
cfb4a98
 
 
 
66148ed
 
 
 
 
cfb4a98
 
 
66148ed
 
cfb4a98
 
 
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
66148ed
cfb4a98
 
 
 
66148ed
cfb4a98
 
 
 
 
 
 
 
 
 
 
66148ed
 
 
 
 
 
 
 
 
cfb4a98
 
 
 
 
 
 
 
 
66148ed
 
 
 
 
 
 
 
 
 
cfb4a98
66148ed
cfb4a98
66148ed
cfb4a98
66148ed
 
 
 
cfb4a98
 
 
 
66148ed
 
cfb4a98
 
 
 
 
 
 
66148ed
cfb4a98
 
66148ed
cfb4a98
 
 
 
66148ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfb4a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "883e3354-1538-4f98-bf42-67552215bba3",
   "metadata": {
    "id": "883e3354-1538-4f98-bf42-67552215bba3"
   },
   "source": [
    "# Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddb3dfde-39cc-4ad9-917e-48413add2d9b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "%pip install -U -q transformers huggingface-hub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b45bd52f-03e9-419f-8110-1013ff45fb1b",
   "metadata": {
    "id": "b45bd52f-03e9-419f-8110-1013ff45fb1b",
    "tags": []
   },
   "outputs": [],
   "source": [
    "from huggingface_hub import InferenceClient, login\n",
    "from transformers import AutoTokenizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dc9f0411-8bf2-4a20-a6ea-331a2a486b8e",
   "metadata": {
    "colab": {
     "referenced_widgets": [
      "515c96c357454fdc9a38ecc995ff1b3d"
     ]
    },
    "id": "dc9f0411-8bf2-4a20-a6ea-331a2a486b8e",
    "outputId": "cb57d6e8-7d61-49ca-ec60-05594a7d5842",
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "da0ae7fafffb4005a5325a53896feb82",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "login()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c254d6f-f3a1-49c1-815e-36e41e75ca25",
   "metadata": {
    "id": "4c254d6f-f3a1-49c1-815e-36e41e75ca25"
   },
   "source": [
    "<div class=\"alert alert-danger\" role=\"alert\" style=\"display: flex; align-items: center;\">\n",
    "    <div style=\"text-align: center; padding-right: 10px;\">\n",
    "        <i class=\"fa fa-exclamation-triangle fa-2x\"></i>\n",
    "    </div>\n",
    "    <div style=\"display: flex; align-items: center; margin-top: 4px;\"> <!-- Added margin-top to lower the text -->\n",
    "        <strong>Warning: You will need to point to a model/deployment that is running.</strong>\n",
    "    </div>\n",
    "</div>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "84e6cb89-30d3-4ef5-8063-07783798e045",
   "metadata": {
    "id": "84e6cb89-30d3-4ef5-8063-07783798e045",
    "tags": []
   },
   "outputs": [],
   "source": [
    "MODEL = \"CohereForAI/c4ai-command-r-plus\"\n",
    "tokenizer = AutoTokenizer.from_pretrained(MODEL, use_fast=True)\n",
    "client = InferenceClient(MODEL)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5fe63f8-dea2-4c61-b6ce-29f173e4c4eb",
   "metadata": {
    "id": "f5fe63f8-dea2-4c61-b6ce-29f173e4c4eb"
   },
   "source": [
    "# Translation\n",
    "Our goal is to explore translation between English and Arabic and how prompt engineering can impact it. There has been [some work](https://arxiv.org/pdf/2308.01391), but we didn't find as much as we were hoping, especially for open source models.\n",
    "\n",
    "We have created a dataset [arabic-translation-prompt-engineering/TpDwD](https://huggingface.co/datasets/arabic-translation-prompt-engineering/TpDwD) across 6 domains and want to compare each method by having human rankers. We also have human translations to ground these rankings.\n",
    "\n",
    "We will evaluate the following methods:\n",
    "- Baseline\n",
    "- Manual Purpose Driven\n",
    "- Automatic Purpose Driven\n",
    "- Automatic Motivation Driven"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5bee77b-da1c-43b3-ab14-d15e871f7502",
   "metadata": {
    "id": "c5bee77b-da1c-43b3-ab14-d15e871f7502"
   },
   "source": [
    "## Baseline\n",
    "\n",
    "For our baseline we will translate with a simple system prompt and instruction."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a98b9b67-e68b-43b2-b8e9-0ed1cf85591f",
   "metadata": {
    "id": "a98b9b67-e68b-43b2-b8e9-0ed1cf85591f"
   },
   "source": [
    "### System Prompt\n",
    "This is a pretty basic system prompt. We give a role, and an assumed understanding. We also push for goals like \"highly motivated and detail-oriented\".\n",
    "\n",
    "> You are a skilled translator with extensive experience in English to Arabic translations. You possess a deep understanding of the linguistic, cultural, and contextual nuances essential for accurate and effective translation between these languages. Highly motivated and detail-oriented, you are committed to delivering translations that maintain the integrity and intent of the original text. Your role is crucial in ensuring clear and precise communication in our multilingual system."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "032c86d2-868e-4fa6-b03e-58f1c41434cc",
   "metadata": {
    "id": "032c86d2-868e-4fa6-b03e-58f1c41434cc",
    "tags": []
   },
   "outputs": [],
   "source": [
    "baseline_system_prompt = \"\"\"You are a skilled translator with extensive experience in English and Arabic translations. You possess a deep understanding of the linguistic, cultural, and contextual nuances essential for accurate and effective translation between these languages. Highly motivated and detail-oriented, you are committed to delivering translations that maintain the integrity and intent of the original text. Your role is crucial in ensuring clear and precise communication in our multilingual system.\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "803ddeba-03de-4f13-95d1-5fb097058cf2",
   "metadata": {
    "id": "803ddeba-03de-4f13-95d1-5fb097058cf2"
   },
   "source": [
    "### Instruction\n",
    "> Translate this from english to arabic: {translation_input}.\n",
    ">\n",
    "> Translation:\n",
    "\n",
    "We will use a simple instruction to get a translation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b7f1722c-c484-4e22-a025-53f95943fc76",
   "metadata": {
    "id": "b7f1722c-c484-4e22-a025-53f95943fc76",
    "tags": []
   },
   "outputs": [],
   "source": [
    "def baseline_chat_completion(translation_input):\n",
    "    \"\"\"\n",
    "    Generates a completion for a chat conversation using a specified system prompt and a user input.\n",
    "    \"\"\"\n",
    "    messages = [\n",
    "        {\"role\": \"system\", \"content\": baseline_system_prompt},\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": f\"Translate this from english to arabic: {translation_input}.\\nTranslation: \",\n",
    "        },\n",
    "    ]\n",
    "    return client.chat_completion(messages, max_tokens=10_000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "96a0ba0b-be47-4eb0-bbc2-c82b0ea1b72e",
   "metadata": {
    "id": "96a0ba0b-be47-4eb0-bbc2-c82b0ea1b72e",
    "tags": []
   },
   "outputs": [],
   "source": [
    "translation_input = \"Float like a butterfly sting like a bee – his hands can’t hit what his eyes can’t see.\"\n",
    "response = baseline_chat_completion(\n",
    "    translation_input,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bca574c-461d-4822-b0dd-b12a3b9846b3",
   "metadata": {
    "id": "2bca574c-461d-4822-b0dd-b12a3b9846b3"
   },
   "source": [
    "### Token Cost\n",
    "Here we can see that the cost is quite cheap, only 96 tokens!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2afc890f-5d8d-4df8-b19a-25888211cf18",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Baseline Total Prompt tokens: 96'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f\"Baseline Total Prompt tokens: {response.usage.prompt_tokens - len(tokenizer(translation_input, return_tensors='pt')['input_ids'][0])}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ef24fe6b-d801-4f3e-95ad-cb7f67247bc3",
   "metadata": {
    "id": "ef24fe6b-d801-4f3e-95ad-cb7f67247bc3",
    "outputId": "0d7417e1-b648-40aa-eba7-5f1b0dc962b3",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "يسبح في الحلبة كالفراشة ويلسع كالنحلة - لا يمكن ليديه أن تصيبا ما لا تستطيع عيناه رؤيته\n"
     ]
    }
   ],
   "source": [
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a9cdf02-d590-4bcf-a7a8-e6b7817ba715",
   "metadata": {
    "id": "3a9cdf02-d590-4bcf-a7a8-e6b7817ba715"
   },
   "source": [
    "## Manual Purpose Driven Translation\n",
    "\n",
    "[Optimizing Machine Translation through Prompt Engineering](https://arxiv.org/pdf/2308.01391) has done some good exploratory work in examining how prompt engineering can impact translation. They were working between Japanese and English and showed that translations influenced by prompts tailored to **specific purposes** and **target audiences** generally adhered more closely to the translation specifications, suggesting that such prompted translations could be more culturally and contextually appropriate than standard machine translations.\n",
    "\n",
    "### Prompt\n",
    "One of the approaches in the paper was to include the purpose and target audience specification. This was motivated by the author’s experience as a professional translator, leading to the conclusion that these two parameters are essential even in everyday translation work. You can find the prompt below adapted for Arabic to English:\n",
    "\n",
    "> Translate the following English [source text] into Arabic. Please fulfill the following conditions when translating.    \n",
    "> Purpose of the translation: `<Manual description>`  \n",
    "> Target audience: `<Manual description>`  \n",
    "> [source text] `{translation_input}`  \n",
    "> [translated text]\n",
    "\n",
    "You can see that we need to provide the Purpose and the Target Audience for each translation. This makes sense as we will be able to steer our model appropriately, but the drawback is that we need to do this for each subject. In the real world this likely won't scale and is rather tedious.\n",
    "\n",
    "Lets go ahead and create these for each of our datasets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4714f6a2-fd0b-48ee-80bc-860f40ee2baa",
   "metadata": {
    "id": "4714f6a2-fd0b-48ee-80bc-860f40ee2baa",
    "tags": []
   },
   "outputs": [],
   "source": [
    "dataset_to_purpose_target = {\n",
    "    \"ELRC-24ss\": {\n",
    "        \"purpose\": \"Enhancing understanding and knowledge about COVID-19 and health-related topics.\",\n",
    "        \"audience\": \"Individuals seeking reliable and comprehensible information about COVID-19 and related health topics.\",\n",
    "    },\n",
    "    \"GNOME-25ss\": {\n",
    "        \"purpose\": \"Facilitating localization and translation of GNOME software.\",\n",
    "        \"audience\": \"Translators and developers working on GNOME projects.\"\n",
    "    },\n",
    "    \"HPLT-25ss\": {\n",
    "        \"purpose\": \"Providing multilingual data for high-performance language technologies.\",\n",
    "        \"audience\": \"Researchers and developers working on multilingual NLP applications.\"\n",
    "    },\n",
    "    \"OpenSubtitles-25ss\": {\n",
    "        \"purpose\": \"Creating parallel corpora from movie and TV subtitles.\",\n",
    "        \"audience\": \"Researchers and developers in NLP and machine translation. And Movies and TV Shows translators\"\n",
    "    },\n",
    "    \"TED2020-25ss\": {\n",
    "        \"purpose\": \"Generating multilingual sentence embeddings using TED transcripts.\",\n",
    "        \"audience\": \"Researchers and developers working on multilingual sentence embeddings.\"\n",
    "    },\n",
    "    \"UNPC-24ss\": {\n",
    "        \"purpose\": \"Offering a parallel corpus of United Nations documents for linguistic research.\",\n",
    "        \"audience\": \"Researchers and linguists studying multilingual and legal texts.\"\n",
    "    }\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d0c3418e-0b87-458f-8517-1ca3e59ab57a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Define the translation tool function\n",
    "purpose_driven_translation_tools = [\n",
    "    {\n",
    "        \"type\": \"function\",\n",
    "        \"function\": {\n",
    "            \"name\": \"purpose_driven_translation\",\n",
    "            \"description\": \"Translate given the purpose and the target audience.\",\n",
    "            \"parameters\": {\n",
    "                \"type\": \"object\",\n",
    "                \"properties\": {\n",
    "                    \"translation\": {\n",
    "                        \"type\": \"string\",\n",
    "                        \"description\": \"The translated \\\"source_text\\\".\",\n",
    "                    },\n",
    "                },\n",
    "                \"required\": [\"translation\"],\n",
    "            },\n",
    "        },\n",
    "    }\n",
    "]\n",
    "\n",
    "# Create the purpose-driven chat completion function using function calling\n",
    "def purpose_driven_chat_completion(translation_input, dataset):\n",
    "    \"\"\"\n",
    "    Generates a completion for a chat conversation using a specified system prompt and a user input,\n",
    "    incorporating function calling to retrieve translation context.\n",
    "    \"\"\"\n",
    "    \n",
    "    # Prepare the prompt\n",
    "    prompt = f\"\"\"Translate the English \"source text\" into Arabic. Please fulfill the \"Purpose of the translation\" and tailor it to the \"target audience\". Respond in a json format with just the translation as the key.\n",
    "{{\n",
    "    \"Purpose of the translation\": \"{dataset_to_purpose_target[dataset]['purpose']}\"\n",
    "    \"Target audience\": \"{dataset_to_purpose_target[dataset]['audience']}\"\n",
    "    \"source text\" `{translation_input}`\n",
    "}} \n",
    "Translation json: \"\"\"\n",
    "\n",
    "    # Initial messages, including the function call to get context\n",
    "    messages = [\n",
    "        {\"role\": \"system\", \"content\": baseline_system_prompt},\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": prompt,\n",
    "        },\n",
    "    ]\n",
    "\n",
    "    \n",
    "    # Call the chat completion API with the function tools and specific tool choice\n",
    "    return client.chat_completion(messages, max_tokens=10_000, tools=purpose_driven_translation_tools, tool_choice='purpose_driven_translation')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4115515d-cbcd-405a-b2e0-a805880a40c4",
   "metadata": {
    "id": "4115515d-cbcd-405a-b2e0-a805880a40c4",
    "tags": []
   },
   "outputs": [],
   "source": [
    "translation_input = \"We have observed that when groups of stakeholders work to define … visions, this leads to debate over whether to emphasize ecosystem health or human well-being … Whether the priority is ecosystems or people greatly influences stakeholders' assessment of desirable ecological and social states.\"\n",
    "response = purpose_driven_chat_completion(translation_input, \"ELRC-24ss\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "51cc3241-ef6d-43e8-8740-defb6f542918",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Manual Purpose Driven Total Prompt tokens: 350'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f\"Manual Purpose Driven Total Prompt tokens: {response.usage.prompt_tokens - len(tokenizer(translation_input, return_tensors='pt')['input_ids'][0])}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1f1c6dd0-11bf-4b88-9029-8bce1e7bcb1c",
   "metadata": {
    "id": "1f1c6dd0-11bf-4b88-9029-8bce1e7bcb1c",
    "outputId": "a12fc058-249f-4493-eb3e-f021c12651dc",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'translation': 'لاحظنا أنه عندما تعمل مجموعات أصحاب المصلحة على تحديد ... '\n",
      "                'الرؤى، فإن هذا يؤدي إلى نقاش حول ما إذا كان ينبغي التركيز على '\n",
      "                'صحة النظام البيئي أو رفاهية الإنسان ... إن مسألة ما إذا كان '\n",
      "                'الأولوية للنظم البيئية أو الناس تؤثر بشكل كبير على تقييم '\n",
      "                'أصحاب المصلحة للحالات الاجتماعية والبيئية المرغوبة.'}\n"
     ]
    }
   ],
   "source": [
    "from pprint import pprint\n",
    "description_json = response.choices[0].message.tool_calls[0].function.arguments\n",
    "pprint(description_json)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "898a909e-88a2-4efb-99ed-64ceee317037",
   "metadata": {
    "id": "898a909e-88a2-4efb-99ed-64ceee317037"
   },
   "source": [
    "## Automatic Purpose Driven Structured Generation Translation\n",
    "\n",
    "Manual Purpose Driven Translation is a great step in the right direction, but its challenging to scale. Instead of having the user submit these purposes and target audiences, what if we use a model to do that? The easiest way to get this input in a format that is convenient is going to be by using [structured generation](https://huggingface.co/blog/evaluation-structured-outputs) to get a json. We can easily do this in InferenceClient easily just by using [tools](https://huggingface.co/docs/huggingface_hub/en/package_reference/inference_client#huggingface_hub.InferenceClient.chat_completion.tools)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8aca3847-7f3d-43d3-8e80-0c7e2282073b",
   "metadata": {
    "id": "8aca3847-7f3d-43d3-8e80-0c7e2282073b"
   },
   "source": [
    "## Instruction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f5deb21-18fb-4c5b-9045-c7fe5e751c05",
   "metadata": {
    "id": "2f5deb21-18fb-4c5b-9045-c7fe5e751c05"
   },
   "source": [
    "Its usually helpful if we tell the LLM what we want to create when we prompt it. \n",
    "\n",
    "> ```I want to translate the following source_text from English into Arabic. But first I want to create a json that includes the following:\n",
    "{\"subject\": \"\", \"assumptions relating to content\": \"\", \"purpose\": \"\", \"target audience\": \"\"}.\n",
    "Can you fill this out and be specific to how this can help you translate in the next step? No need to translate yet!\n",
    "{\n",
    "    \"source_text\": {translation_input}\n",
    "}```\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55f02988-1c63-4f03-a5a6-ca86c83b3c17",
   "metadata": {
    "id": "55f02988-1c63-4f03-a5a6-ca86c83b3c17"
   },
   "source": [
    "## Tool Definition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "affe3668-aa37-47b5-be37-a0bd5dabab56",
   "metadata": {
    "id": "affe3668-aa37-47b5-be37-a0bd5dabab56",
    "tags": []
   },
   "outputs": [],
   "source": [
    "automatic_purpose_driven_translation_tools = [\n",
    "    {\n",
    "        \"type\": \"function\",\n",
    "        \"function\": {\n",
    "            \"name\": \"get_translation_audience_purpose\",\n",
    "            \"description\": \"Get the background of a text to assist in translation\",\n",
    "            \"parameters\": {\n",
    "                \"type\": \"object\",\n",
    "                \"properties\": {\n",
    "                    \"subject\": {\n",
    "                        \"type\": \"string\",\n",
    "                        \"description\": \"The topic or central theme that the text revolves around.\",\n",
    "                    },\n",
    "                    \"assumptions relating to the content\": {\n",
    "                        \"type\": \"string\",\n",
    "                        \"description\": \"Write out any assumptions relating to the text.\",\n",
    "                    },\n",
    "                    \"purpose\": {\n",
    "                        \"type\": \"string\",\n",
    "                        \"description\": \"Why the text was written\",\n",
    "                    },\n",
    "                    \"audience\": {\n",
    "                        \"type\": \"string\",\n",
    "                        \"description\": \"The inferred audience that the text is written for.\",\n",
    "                    },\n",
    "                },\n",
    "                \"required\": [\n",
    "                    \"subject\",\n",
    "                    \"assumptions relating to the content\",\n",
    "                    \"purpose\",\n",
    "                    \"audience\",\n",
    "                ],\n",
    "            },\n",
    "        },\n",
    "    }\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b6436ce6-03af-4206-a283-0c2ecd17bd88",
   "metadata": {
    "id": "b6436ce6-03af-4206-a283-0c2ecd17bd88",
    "tags": []
   },
   "outputs": [],
   "source": [
    "def tool_call_chat_completion(translation_input):\n",
    "    \"\"\"\n",
    "    Generates a completion for a chat conversation using a specified system prompt and a user input.\n",
    "    \"\"\"\n",
    "\n",
    "    prompt = f\"\"\"I want to translate the following source_text from English into Arabic. But first I want to create a json that includes the following:\n",
    "{{\"subject\": \"\", \"assumptions relating to content\": \"\", \"purpose\": \"\", \"target audience\": \"\"}}.\n",
    "Can you fill this out and be specific to how this can help you translate in the next step? No need to translate yet!\n",
    "{{\n",
    "    \"source_text\": {translation_input}\n",
    "}}\n",
    "\"\"\"\n",
    "    messages = [\n",
    "        {\"role\": \"system\", \"content\": baseline_system_prompt},\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": prompt,\n",
    "        },\n",
    "    ]\n",
    "    return client.chat_completion(messages, max_tokens=10_000, tools=automatic_purpose_driven_translation_tools, tool_choice='get_translation_audience_purpose')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a731b2c0-54a3-4b8e-83f6-1663c759cf79",
   "metadata": {
    "id": "a731b2c0-54a3-4b8e-83f6-1663c759cf79",
    "tags": []
   },
   "outputs": [],
   "source": [
    "translation_input = \"We have observed that when groups of stakeholders work to define … visions, this leads to debate over whether to emphasize ecosystem health or human well-being … Whether the priority is ecosystems or people greatly influences stakeholders' assessment of desirable ecological and social states.\"\n",
    "response = tool_call_chat_completion(translation_input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f87110a2-a40b-4d65-a12d-728dbdda8fbe",
   "metadata": {
    "id": "07e0f133-f6e1-4bc6-969c-da9d36bfba2f",
    "outputId": "82e2506a-7336-4909-b8e4-fc52f671c511",
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Function Calling Prompt tokens: 406'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f\"Function Calling Prompt tokens: {response.usage.prompt_tokens - len(tokenizer(translation_input, return_tensors='pt')['input_ids'][0])}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "02be1827-7137-463f-a026-0b26dec6f552",
   "metadata": {
    "id": "02be1827-7137-463f-a026-0b26dec6f552",
    "outputId": "60cd7be7-b6ac-412b-e0bc-3b8d3088f2b2",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'assumptions relating to the content': 'The source text assumes that there is '\n",
      "                                        'a debate between ecological health '\n",
      "                                        'and human well-being, and that '\n",
      "                                        'stakeholders have different '\n",
      "                                        'priorities that influence their '\n",
      "                                        'assessment of desirable ecological '\n",
      "                                        'and social outcomes.',\n",
      " 'audience': 'Individuals interested in environmental policy, ecology, '\n",
      "             'sustainability, and/or stakeholder engagement.',\n",
      " 'purpose': 'To communicate observations about the varying priorities of '\n",
      "            'different stakeholder groups and how these priorities impact '\n",
      "            'their definition of vision, particularly in the context of '\n",
      "            'ecosystem health versus human well-being.',\n",
      " 'subject': 'Stakeholder priorities and their impact on defining visions '\n",
      "            'related to ecological and social outcomes.'}\n"
     ]
    }
   ],
   "source": [
    "from pprint import pprint\n",
    "description_json = response.choices[0].message.tool_calls[0].function.arguments\n",
    "pprint(description_json)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7575bd09-2d20-49ae-bb10-162a0e469f16",
   "metadata": {
    "id": "7575bd09-2d20-49ae-bb10-162a0e469f16",
    "tags": []
   },
   "outputs": [],
   "source": [
    "def automatic_purpose_driven_chat_completion(translation_input, description_json):\n",
    "    \"\"\"\n",
    "    Generates a completion for a chat conversation using a specified system prompt and a user input.\n",
    "    \"\"\"\n",
    "\n",
    "    prompt = f\"\"\"Given the following description translate source_text from English to Arabic\n",
    "{{\n",
    "    \"description\": {description_json},\n",
    "    \"translation\": {translation_input}\n",
    "}}\n",
    "Translation:\n",
    "\"\"\"\n",
    "    messages = [\n",
    "        {\"role\": \"system\", \"content\": baseline_system_prompt},\n",
    "        {\"role\": \"user\", \"content\": prompt},\n",
    "    ]\n",
    "    return client.chat_completion(messages, max_tokens=10_000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "32bccca0-c866-4006-84d1-d5b783b73689",
   "metadata": {
    "id": "32bccca0-c866-4006-84d1-d5b783b73689",
    "tags": []
   },
   "outputs": [],
   "source": [
    "response = automatic_purpose_driven_chat_completion(translation_input, description_json)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b0867efb-39ea-4f9a-b073-2a84261f3821",
   "metadata": {
    "id": "b0867efb-39ea-4f9a-b073-2a84261f3821",
    "outputId": "485ece36-c47e-4d1d-cf50-bbe1a9960776",
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Automatic Purpose Driven Total Prompt tokens: 235'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f\"Automatic Purpose Driven Total Prompt tokens: {response.usage.prompt_tokens - len(tokenizer(translation_input, return_tensors='pt')['input_ids'][0])}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "462ca84c-9ffd-4924-880d-e06b724caf02",
   "metadata": {
    "id": "462ca84c-9ffd-4924-880d-e06b724caf02",
    "outputId": "bf026405-eb15-4a28-8357-d99e174967e7",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"description\": {\n",
      "        \"الافتراضات المتعلقة بالمحتوى\": \"يفترض النص المصدري وجود نقاش بين الصحة البيئية ورفاهية الإنسان، وأن أصحاب المصلحة لديهم أولويات مختلفة تؤثر على تقييمهم للنتائج البيئية والاجتماعية المرجوة.\",\n",
      "        \"الجمهور\": \"الأفراد المهتمون بالسياسة البيئية، أو علم البيئة، أو الاستدامة، و/أو مشاركة أصحاب المصلحة.\",\n",
      "        \"الغرض\": \"إيصال الملاحظات حول الأولويات المتنوعة لمجموعات أصحاب المصلحة المختلفة، وكيف تؤثر هذه الأولويات على تعريفهم للرؤى، خاصة في سياق صحة الأنظمة البيئية مقابل رفاهية الإنسان.\",\n",
      "        \"الموضوع\": \"أولويات أصحاب المصلحة وتأثيرها على تحديد الرؤى المتعلقة بالنتائج البيئية والاجتماعية.\"\n",
      "    },\n",
      "    \"الترجمة\": \"لاحظنا أنه عندما تعمل مجموعات أصحاب المصلحة على تحديد ... الرؤى، فإن هذا يؤدي إلى نقاش حول ما إذا كان ينبغي التأكيد على صحة النظام البيئي أو رفاهية الإنسان ... سواء كانت الأولوية للنظم البيئية أو للبشر يؤثر بشكل كبير على تقييم أصحاب المصلحة للحالات البيئية والاجتماعية المرغوبة.\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28a0e518-358e-44f0-97a4-ea76a5563743",
   "metadata": {},
   "source": [
    "### Helper Function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "7fbf9e01-23d9-41b0-a1e3-fd7a99c55bd0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def automatic_purpose_driven_chat(translation_input):\n",
    "    response = tool_call_chat_completion(translation_input)\n",
    "    description_json = response.choices[0].message.tool_calls[0].function.arguments\n",
    "    return automatic_purpose_driven_chat_completion(translation_input, description_json)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "8c5dd9e6-44c1-4ac7-92ef-c2e594b7b91d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'الافتراضات المتعلقة بالمحتوى: لا توجد افتراضات محددة.\\n\\nالجمهور المستهدف: جمهور عام لا يحتاج إلى معرفة تقنية محددة.\\n\\nالغرض: نقل رسالة بسيطة لاختبار الترجمة.\\n\\nالموضوع: اختبار الترجمة\\n\\nالترجمة: هذا اختبار'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "automatic_purpose_driven_chat(\"This is a test\").choices[0].message.content"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4abf1aeb-ee4e-4b8f-97c3-e6b1664ac8b8",
   "metadata": {
    "id": "1ec3b20b-8393-4fda-a51d-cf67984cc166"
   },
   "source": [
    "## Dataset Creation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4b2e3951-6704-43d5-a69b-7587f26e6491",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e983537088ae4305ada7aff87127eaa2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map:   0%|          | 0/24 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0595b2ee75a44cb48f4fef0fbcb75752",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading the dataset shards:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2e42a9acaf544f6a9dbd7cd3fcb3d381",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/1 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6252b1b7f45a4b779ac77748a997256f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading data:   0%|          | 0.00/5.38k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "10d2c9d09da54646a6ea06ff09622ad0",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Generating train split:   0%|          | 0/25 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ee28014824554610903cb642534c6cfe",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map:   0%|          | 0/25 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c26ea784ed514f6382898cdd82e629c0",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading the dataset shards:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6470a8eba6cc45e1b266f160e07ac5f9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/1 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "3e4d136bda254528993b54d6c3b5395a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "README.md:   0%|          | 0.00/599 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "23978739fcbf40ffb24f78c4e99088bc",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading data:   0%|          | 0.00/13.8k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d4bcf66919fb4444b5c2e4f489a57dc2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Generating train split:   0%|          | 0/25 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a3f4915f12a047daabdcbf50eb133131",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map:   0%|          | 0/25 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4c628855a75447fe812638f60d81e259",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading the dataset shards:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "3ec49e43954d43e4bab6581afb5c3b95",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/1 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "be12c10f2f4846a49ad71670bfbf7df3",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "README.md:   0%|          | 0.00/1.17k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "85b48f1ca1414aed9d24c09b7ee64ae8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading data:   0%|          | 0.00/5.74k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "66f25ed30a7a47eba1f62c59a5ef6b1a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Generating train split:   0%|          | 0/25 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0b1856a42f844ab286c628a5ab48ba6c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map:   0%|          | 0/25 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "06b2892ed4da4846baee7b178c42fa40",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading the dataset shards:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "92fb9cf2d4bf4b2abf9412d9238bc769",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/1 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "36914c848e654dee81683a379721b8c9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "README.md:   0%|          | 0.00/1.74k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6b23b1c80d1349c68fea02d8bb4baee1",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading data:   0%|          | 0.00/10.9k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0b91c12b7b6c4d88a32504e52be379b3",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Generating train split:   0%|          | 0/25 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "188ee5373fbc4b2c845ae823b073daca",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map:   0%|          | 0/25 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "91b7e4137f50487d927dabc859d9f19f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading the dataset shards:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d2b2d76b482f4fe0a7dcf1ff4cfc1168",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/1 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4fff6b9680244b85a572cf6373781cec",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "README.md:   0%|          | 0.00/2.34k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "21baa00c99e2485d8af5edaf095dfc46",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading data:   0%|          | 0.00/16.5k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "610e87cdb9a44e91b8eb1c0dbe4af60f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Generating train split:   0%|          | 0/24 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d7fe56bef4fd490fa18ad574cc72ba77",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map:   0%|          | 0/24 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "14d8c2e7c9144f62ab6d5ba3dae7e96f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading the dataset shards:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "eb21d60a8bf940e797a432469b74d262",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/1 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0cc785b4868143f99ffe04a0f9239692",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "README.md:   0%|          | 0.00/2.91k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from datasets import load_dataset\n",
    "\n",
    "subsets = ['ELRC-24ss', 'GNOME-25ss', 'HPLT-25ss', 'OpenSubtitles-25ss', 'TED2020-25ss', 'UNPC-24ss']\n",
    "\n",
    "# Iterate over each subset\n",
    "for subset in subsets:\n",
    "    # Load the dataset for the specific subset\n",
    "    dataset = load_dataset(\"arabic-translation-prompt-engineering/TpDwD\", subset)\n",
    "\n",
    "    # Rename the columns\n",
    "    dataset = dataset.rename_column(\"ar_text\", \"human_translation\")\n",
    "    dataset = dataset.rename_column(\"en_text\", \"source_text\")\n",
    "\n",
    "    # Apply functions to add new columns\n",
    "    dataset = dataset.map(lambda example: {\n",
    "        \"baseline_translation\": baseline_chat_completion(example['source_text']).choices[0].message.content,\n",
    "        \"purpose_driven_translation\": purpose_driven_chat_completion(example['source_text'], subset).choices[0].message.tool_calls[0].function.arguments['translation'],\n",
    "        \"automatic_purpose_driven_translation\": automatic_purpose_driven_chat(example['source_text']).choices[0].message.content\n",
    "    })\n",
    "    \n",
    "    # Push the processed dataset to the Hub\n",
    "    dataset.push_to_hub(f\"arabic-translation-prompt-engineering/TpDwD_translated\",subset)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ec3b20b-8393-4fda-a51d-cf67984cc166",
   "metadata": {
    "id": "1ec3b20b-8393-4fda-a51d-cf67984cc166"
   },
   "source": [
    "# Push to the hub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6fc55725-216f-45dd-9c6d-dae77e16d606",
   "metadata": {
    "id": "6fc55725-216f-45dd-9c6d-dae77e16d606",
    "outputId": "d3b9b994-e3af-4f05-88b4-3b29778d1dc7",
    "tags": []
   },
   "outputs": [],
   "source": [
    "from huggingface_hub import HfApi\n",
    "\n",
    "api = HfApi()\n",
    "api.upload_file(\n",
    "    path_or_fileobj=\"translate-prompts.ipynb\",\n",
    "    path_in_repo=\"translate-prompts.ipynb\",\n",
    "    repo_id=\"arabic-translation-prompt-engineering/atpe-notebooks\",\n",
    "    repo_type=\"model\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f21f4cf-1872-4934-bb91-8aef311ed729",
   "metadata": {
    "id": "6f21f4cf-1872-4934-bb91-8aef311ed729"
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "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.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}