Upload baseline.ipynb
Browse filesUpdated the dataset_to_purpose_target
- baseline.ipynb +782 -693
baseline.ipynb
CHANGED
@@ -1,697 +1,786 @@
|
|
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 |
-
"data": {
|
646 |
-
"text/plain": [
|
647 |
-
"CommitInfo(commit_url='https://huggingface.co/arabic-translation-prompt-engineering/atpe-notebooks/commit/d112cc49a99a72106b75c3eae9be453264fb488d', commit_message='Upload baseline.ipynb with huggingface_hub', commit_description='', oid='d112cc49a99a72106b75c3eae9be453264fb488d', pr_url=None, pr_revision=None, pr_num=None)"
|
648 |
-
]
|
649 |
-
},
|
650 |
-
"execution_count": 23,
|
651 |
-
"metadata": {},
|
652 |
-
"output_type": "execute_result"
|
653 |
}
|
654 |
-
],
|
655 |
-
"source": [
|
656 |
-
"from huggingface_hub import HfApi\n",
|
657 |
-
"\n",
|
658 |
-
"api = HfApi()\n",
|
659 |
-
"api.upload_file(\n",
|
660 |
-
" path_or_fileobj=\"baseline.ipynb\",\n",
|
661 |
-
" path_in_repo=\"baseline.ipynb\",\n",
|
662 |
-
" repo_id=\"arabic-translation-prompt-engineering/atpe-notebooks\",\n",
|
663 |
-
" repo_type=\"model\",\n",
|
664 |
-
")"
|
665 |
-
]
|
666 |
-
},
|
667 |
-
{
|
668 |
-
"cell_type": "code",
|
669 |
-
"execution_count": null,
|
670 |
-
"id": "6f21f4cf-1872-4934-bb91-8aef311ed729",
|
671 |
-
"metadata": {},
|
672 |
-
"outputs": [],
|
673 |
-
"source": []
|
674 |
-
}
|
675 |
-
],
|
676 |
-
"metadata": {
|
677 |
-
"kernelspec": {
|
678 |
-
"display_name": "Python 3 (ipykernel)",
|
679 |
-
"language": "python",
|
680 |
-
"name": "python3"
|
681 |
},
|
682 |
-
"
|
683 |
-
|
684 |
-
|
685 |
-
"version": 3
|
686 |
-
},
|
687 |
-
"file_extension": ".py",
|
688 |
-
"mimetype": "text/x-python",
|
689 |
-
"name": "python",
|
690 |
-
"nbconvert_exporter": "python",
|
691 |
-
"pygments_lexer": "ipython3",
|
692 |
-
"version": "3.10.9"
|
693 |
-
}
|
694 |
-
},
|
695 |
-
"nbformat": 4,
|
696 |
-
"nbformat_minor": 5
|
697 |
-
}
|
|
|
1 |
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"id": "883e3354-1538-4f98-bf42-67552215bba3",
|
6 |
+
"metadata": {
|
7 |
+
"id": "883e3354-1538-4f98-bf42-67552215bba3"
|
8 |
+
},
|
9 |
+
"source": [
|
10 |
+
"# Setup"
|
11 |
+
]
|
12 |
+
},
|
13 |
+
{
|
14 |
+
"cell_type": "code",
|
15 |
+
"execution_count": null,
|
16 |
+
"id": "b45bd52f-03e9-419f-8110-1013ff45fb1b",
|
17 |
+
"metadata": {
|
18 |
+
"tags": [],
|
19 |
+
"id": "b45bd52f-03e9-419f-8110-1013ff45fb1b"
|
20 |
+
},
|
21 |
+
"outputs": [],
|
22 |
+
"source": [
|
23 |
+
"from huggingface_hub import InferenceClient, login"
|
24 |
+
]
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"cell_type": "code",
|
28 |
+
"execution_count": null,
|
29 |
+
"id": "dc9f0411-8bf2-4a20-a6ea-331a2a486b8e",
|
30 |
+
"metadata": {
|
31 |
+
"tags": [],
|
32 |
+
"id": "dc9f0411-8bf2-4a20-a6ea-331a2a486b8e",
|
33 |
+
"outputId": "cb57d6e8-7d61-49ca-ec60-05594a7d5842",
|
34 |
+
"colab": {
|
35 |
+
"referenced_widgets": [
|
36 |
+
"515c96c357454fdc9a38ecc995ff1b3d"
|
37 |
+
]
|
38 |
+
}
|
39 |
+
},
|
40 |
+
"outputs": [
|
41 |
+
{
|
42 |
+
"data": {
|
43 |
+
"application/vnd.jupyter.widget-view+json": {
|
44 |
+
"model_id": "515c96c357454fdc9a38ecc995ff1b3d",
|
45 |
+
"version_major": 2,
|
46 |
+
"version_minor": 0
|
47 |
+
},
|
48 |
+
"text/plain": [
|
49 |
+
"VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
|
50 |
+
]
|
51 |
+
},
|
52 |
+
"metadata": {},
|
53 |
+
"output_type": "display_data"
|
54 |
+
}
|
55 |
+
],
|
56 |
+
"source": [
|
57 |
+
"login()"
|
58 |
+
]
|
59 |
+
},
|
60 |
+
{
|
61 |
+
"cell_type": "markdown",
|
62 |
+
"id": "4c254d6f-f3a1-49c1-815e-36e41e75ca25",
|
63 |
+
"metadata": {
|
64 |
+
"id": "4c254d6f-f3a1-49c1-815e-36e41e75ca25"
|
65 |
+
},
|
66 |
+
"source": [
|
67 |
+
"<div class=\"alert alert-danger\" role=\"alert\" style=\"display: flex; align-items: center;\">\n",
|
68 |
+
" <div style=\"text-align: center; padding-right: 10px;\">\n",
|
69 |
+
" <i class=\"fa fa-exclamation-triangle fa-2x\"></i>\n",
|
70 |
+
" </div>\n",
|
71 |
+
" <div style=\"display: flex; align-items: center; margin-top: 4px;\"> <!-- Added margin-top to lower the text -->\n",
|
72 |
+
" <strong>Warning: You will need to point to a model/deployment that is running.</strong>\n",
|
73 |
+
" </div>\n",
|
74 |
+
"</div>\n"
|
75 |
+
]
|
76 |
+
},
|
77 |
+
{
|
78 |
+
"cell_type": "code",
|
79 |
+
"execution_count": null,
|
80 |
+
"id": "84e6cb89-30d3-4ef5-8063-07783798e045",
|
81 |
+
"metadata": {
|
82 |
+
"id": "84e6cb89-30d3-4ef5-8063-07783798e045"
|
83 |
+
},
|
84 |
+
"outputs": [],
|
85 |
+
"source": [
|
86 |
+
"MODEL = \"CohereForAI/c4ai-command-r-plus\"\n",
|
87 |
+
"client = InferenceClient(MODEL)"
|
88 |
+
]
|
89 |
+
},
|
90 |
+
{
|
91 |
+
"cell_type": "markdown",
|
92 |
+
"id": "f5fe63f8-dea2-4c61-b6ce-29f173e4c4eb",
|
93 |
+
"metadata": {
|
94 |
+
"id": "f5fe63f8-dea2-4c61-b6ce-29f173e4c4eb"
|
95 |
+
},
|
96 |
+
"source": [
|
97 |
+
"# Translation\n",
|
98 |
+
"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",
|
99 |
+
"\n",
|
100 |
+
"We have created a dataset across 6 domains and want to compare each method by having human rankers. We also have human translations to ground these rankings."
|
101 |
+
]
|
102 |
+
},
|
103 |
+
{
|
104 |
+
"cell_type": "markdown",
|
105 |
+
"id": "c5bee77b-da1c-43b3-ab14-d15e871f7502",
|
106 |
+
"metadata": {
|
107 |
+
"id": "c5bee77b-da1c-43b3-ab14-d15e871f7502"
|
108 |
+
},
|
109 |
+
"source": [
|
110 |
+
"## Baseline\n",
|
111 |
+
"\n",
|
112 |
+
"For our baseline we will translate with a simple system prompt and instruction."
|
113 |
+
]
|
114 |
+
},
|
115 |
+
{
|
116 |
+
"cell_type": "markdown",
|
117 |
+
"id": "a98b9b67-e68b-43b2-b8e9-0ed1cf85591f",
|
118 |
+
"metadata": {
|
119 |
+
"id": "a98b9b67-e68b-43b2-b8e9-0ed1cf85591f"
|
120 |
+
},
|
121 |
+
"source": [
|
122 |
+
"### System Prompt\n",
|
123 |
+
"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",
|
124 |
+
"\n",
|
125 |
+
"> 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."
|
126 |
+
]
|
127 |
+
},
|
128 |
+
{
|
129 |
+
"cell_type": "code",
|
130 |
+
"execution_count": null,
|
131 |
+
"id": "032c86d2-868e-4fa6-b03e-58f1c41434cc",
|
132 |
+
"metadata": {
|
133 |
+
"tags": [],
|
134 |
+
"id": "032c86d2-868e-4fa6-b03e-58f1c41434cc"
|
135 |
+
},
|
136 |
+
"outputs": [],
|
137 |
+
"source": [
|
138 |
+
"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.\"\"\""
|
139 |
+
]
|
140 |
+
},
|
141 |
+
{
|
142 |
+
"cell_type": "markdown",
|
143 |
+
"id": "803ddeba-03de-4f13-95d1-5fb097058cf2",
|
144 |
+
"metadata": {
|
145 |
+
"id": "803ddeba-03de-4f13-95d1-5fb097058cf2"
|
146 |
+
},
|
147 |
+
"source": [
|
148 |
+
"### Instruction\n",
|
149 |
+
"> Translate this from arabic to english: {translation_input}.\n",
|
150 |
+
">\n",
|
151 |
+
"> Translation:\n",
|
152 |
+
"\n",
|
153 |
+
"We will use a simple instruction to get a translation."
|
154 |
+
]
|
155 |
+
},
|
156 |
+
{
|
157 |
+
"cell_type": "code",
|
158 |
+
"execution_count": null,
|
159 |
+
"id": "b7f1722c-c484-4e22-a025-53f95943fc76",
|
160 |
+
"metadata": {
|
161 |
+
"id": "b7f1722c-c484-4e22-a025-53f95943fc76"
|
162 |
+
},
|
163 |
+
"outputs": [],
|
164 |
+
"source": [
|
165 |
+
"def baseline_chat_completion(system_prompt, translation_input):\n",
|
166 |
+
" \"\"\"\n",
|
167 |
+
" Generates a completion for a chat conversation using a specified system prompt and a user input.\n",
|
168 |
+
" \"\"\"\n",
|
169 |
+
" messages = [\n",
|
170 |
+
" {\"role\": \"system\", \"content\": system_prompt},\n",
|
171 |
+
" {\n",
|
172 |
+
" \"role\": \"user\",\n",
|
173 |
+
" \"content\": f\"Translate this from english to arabic: {translation_input}.\\nTranslation: \",\n",
|
174 |
+
" },\n",
|
175 |
+
" ]\n",
|
176 |
+
" return client.chat_completion(messages, max_tokens=10_000)"
|
177 |
+
]
|
178 |
+
},
|
179 |
+
{
|
180 |
+
"cell_type": "code",
|
181 |
+
"execution_count": null,
|
182 |
+
"id": "96a0ba0b-be47-4eb0-bbc2-c82b0ea1b72e",
|
183 |
+
"metadata": {
|
184 |
+
"tags": [],
|
185 |
+
"id": "96a0ba0b-be47-4eb0-bbc2-c82b0ea1b72e"
|
186 |
+
},
|
187 |
+
"outputs": [],
|
188 |
+
"source": [
|
189 |
+
"translation_input = \"Float like a butterfly sting like a bee – his hands can’t hit what his eyes can’t see.\"\n",
|
190 |
+
"response = baseline_chat_completion(\n",
|
191 |
+
" system_prompt,\n",
|
192 |
+
" translation_input,\n",
|
193 |
+
")"
|
194 |
+
]
|
195 |
+
},
|
196 |
+
{
|
197 |
+
"cell_type": "markdown",
|
198 |
+
"id": "2bca574c-461d-4822-b0dd-b12a3b9846b3",
|
199 |
+
"metadata": {
|
200 |
+
"id": "2bca574c-461d-4822-b0dd-b12a3b9846b3"
|
201 |
+
},
|
202 |
+
"source": [
|
203 |
+
"### Token Cost\n",
|
204 |
+
"Here we can see that the cost is quite cheap, only 92 tokens!"
|
205 |
+
]
|
206 |
+
},
|
207 |
+
{
|
208 |
+
"cell_type": "code",
|
209 |
+
"execution_count": null,
|
210 |
+
"id": "4e305b1e-56e0-44da-8c17-496cbcc35fad",
|
211 |
+
"metadata": {
|
212 |
+
"tags": [],
|
213 |
+
"id": "4e305b1e-56e0-44da-8c17-496cbcc35fad",
|
214 |
+
"outputId": "a97a8a75-e2e6-4ac3-e7ac-db674a1f46c9"
|
215 |
+
},
|
216 |
+
"outputs": [
|
217 |
+
{
|
218 |
+
"data": {
|
219 |
+
"text/plain": [
|
220 |
+
"120"
|
221 |
+
]
|
222 |
+
},
|
223 |
+
"execution_count": 7,
|
224 |
+
"metadata": {},
|
225 |
+
"output_type": "execute_result"
|
226 |
+
}
|
227 |
+
],
|
228 |
+
"source": [
|
229 |
+
"response.usage.prompt_tokens"
|
230 |
+
]
|
231 |
+
},
|
232 |
+
{
|
233 |
+
"cell_type": "code",
|
234 |
+
"execution_count": null,
|
235 |
+
"id": "ef24fe6b-d801-4f3e-95ad-cb7f67247bc3",
|
236 |
+
"metadata": {
|
237 |
+
"tags": [],
|
238 |
+
"id": "ef24fe6b-d801-4f3e-95ad-cb7f67247bc3",
|
239 |
+
"outputId": "0d7417e1-b648-40aa-eba7-5f1b0dc962b3"
|
240 |
+
},
|
241 |
+
"outputs": [
|
242 |
+
{
|
243 |
+
"name": "stdout",
|
244 |
+
"output_type": "stream",
|
245 |
+
"text": [
|
246 |
+
"يطير كالفراشة ويلسع كالنحلة - لا يمكن ليديه أن تصيبا ما لا تستطيع عيناه رؤيته.\n"
|
247 |
+
]
|
248 |
+
}
|
249 |
+
],
|
250 |
+
"source": [
|
251 |
+
"print(response.choices[0].message.content)"
|
252 |
+
]
|
253 |
+
},
|
254 |
+
{
|
255 |
+
"cell_type": "markdown",
|
256 |
+
"id": "3a9cdf02-d590-4bcf-a7a8-e6b7817ba715",
|
257 |
+
"metadata": {
|
258 |
+
"id": "3a9cdf02-d590-4bcf-a7a8-e6b7817ba715"
|
259 |
+
},
|
260 |
+
"source": [
|
261 |
+
"## Manual Purpose Driven Translation\n",
|
262 |
+
"\n",
|
263 |
+
"[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",
|
264 |
+
"\n",
|
265 |
+
"### Prompt\n",
|
266 |
+
"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",
|
267 |
+
"\n",
|
268 |
+
"> Translate the following English [source text] into Arabic. Please fulfill the following conditions when translating. \n",
|
269 |
+
"> Purpose of the translation: `<Manual description>` \n",
|
270 |
+
"> Target audience: `<Manual description>` \n",
|
271 |
+
"> [source text] `{translation_input}` \n",
|
272 |
+
"> [translated text]\n",
|
273 |
+
"\n",
|
274 |
+
"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",
|
275 |
+
"\n",
|
276 |
+
"Lets go ahead and create these for each of our datasets."
|
277 |
+
]
|
278 |
+
},
|
279 |
+
{
|
280 |
+
"cell_type": "code",
|
281 |
+
"execution_count": null,
|
282 |
+
"id": "4714f6a2-fd0b-48ee-80bc-860f40ee2baa",
|
283 |
+
"metadata": {
|
284 |
+
"tags": [],
|
285 |
+
"id": "4714f6a2-fd0b-48ee-80bc-860f40ee2baa"
|
286 |
+
},
|
287 |
+
"outputs": [],
|
288 |
+
"source": [
|
289 |
+
"dataset_to_purpose_target = {\n",
|
290 |
+
" \"ELRC-24ss\": {\n",
|
291 |
+
" \"purpose\": \"Enhancing understanding and knowledge about COVID-19 and health-related topics.\",\n",
|
292 |
+
" \"audience\": \"Individuals seeking reliable and comprehensible information about COVID-19 and related health topics.\",\n",
|
293 |
+
" },\n",
|
294 |
+
" \"GNOME-25ss\": {\n",
|
295 |
+
" \"purpose\": \"Facilitating localization and translation of GNOME software.\",\n",
|
296 |
+
" \"audience\": \"Translators and developers working on GNOME projects.\"\n",
|
297 |
+
" },\n",
|
298 |
+
" \"HPLT-25ss\": {\n",
|
299 |
+
" \"purpose\": \"Providing multilingual data for high-performance language technologies.\",\n",
|
300 |
+
" \"audience\": \"Researchers and developers working on multilingual NLP applications.\"\n",
|
301 |
+
" },\n",
|
302 |
+
" \"OpenSubtitles-25ss\": {\n",
|
303 |
+
" \"purpose\": \"Creating parallel corpora from movie and TV subtitles.\",\n",
|
304 |
+
" \"audience\": \"Researchers and developers in NLP and machine translation. And Movies and TV Shows translators\"\n",
|
305 |
+
" },\n",
|
306 |
+
" \"TED2020-25ss\": {\n",
|
307 |
+
" \"purpose\": \"Generating multilingual sentence embeddings using TED transcripts.\",\n",
|
308 |
+
" \"audience\": \"Researchers and developers working on multilingual sentence embeddings.\"\n",
|
309 |
+
" },\n",
|
310 |
+
" \"UNPC-24ss\": {\n",
|
311 |
+
" \"purpose\": \"Offering a parallel corpus of United Nations documents for linguistic research.\",\n",
|
312 |
+
" \"audience\": \"Researchers and linguists studying multilingual and legal texts.\"\n",
|
313 |
+
" }\n",
|
314 |
+
"}\n"
|
315 |
+
]
|
316 |
+
},
|
317 |
+
{
|
318 |
+
"cell_type": "code",
|
319 |
+
"execution_count": null,
|
320 |
+
"id": "f865e9f8-7c63-4e72-b539-0d5916eda44f",
|
321 |
+
"metadata": {
|
322 |
+
"id": "f865e9f8-7c63-4e72-b539-0d5916eda44f"
|
323 |
+
},
|
324 |
+
"outputs": [],
|
325 |
+
"source": [
|
326 |
+
"def purpose_driven_chat_completion(system_prompt, translation_input, dataset):\n",
|
327 |
+
" \"\"\"\n",
|
328 |
+
" Generates a completion for a chat conversation using a specified system prompt and a user input.\n",
|
329 |
+
" \"\"\"\n",
|
330 |
+
"\n",
|
331 |
+
" prompt = f\"\"\"Translate the following English [source text] into Arabic. Please fulfill the following conditions when translating.\n",
|
332 |
+
"Purpose of the translation: {dataset_to_purpose_target[dataset]['purpose']}\n",
|
333 |
+
"Target audience: {dataset_to_purpose_target[dataset]['audience']}\n",
|
334 |
+
"[source text] `{translation_input}`\n",
|
335 |
+
"[translated text] \"\"\"\n",
|
336 |
+
"\n",
|
337 |
+
" messages = [\n",
|
338 |
+
" {\"role\": \"system\", \"content\": system_prompt},\n",
|
339 |
+
" {\n",
|
340 |
+
" \"role\": \"user\",\n",
|
341 |
+
" \"content\": prompt,\n",
|
342 |
+
" },\n",
|
343 |
+
" ]\n",
|
344 |
+
" return client.chat_completion(messages, max_tokens=10_000)"
|
345 |
+
]
|
346 |
+
},
|
347 |
+
{
|
348 |
+
"cell_type": "code",
|
349 |
+
"execution_count": null,
|
350 |
+
"id": "4115515d-cbcd-405a-b2e0-a805880a40c4",
|
351 |
+
"metadata": {
|
352 |
+
"tags": [],
|
353 |
+
"id": "4115515d-cbcd-405a-b2e0-a805880a40c4"
|
354 |
+
},
|
355 |
+
"outputs": [],
|
356 |
+
"source": [
|
357 |
+
"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",
|
358 |
+
"response = purpose_driven_chat_completion(system_prompt, translation_input, \"ELRC-24ss\")"
|
359 |
+
]
|
360 |
+
},
|
361 |
+
{
|
362 |
+
"cell_type": "code",
|
363 |
+
"execution_count": null,
|
364 |
+
"id": "6296d255-d11d-4df7-aa0e-1226ef3d963a",
|
365 |
+
"metadata": {
|
366 |
+
"tags": [],
|
367 |
+
"id": "6296d255-d11d-4df7-aa0e-1226ef3d963a",
|
368 |
+
"outputId": "45282f39-18ef-4dc0-f708-1113ca1769ce"
|
369 |
+
},
|
370 |
+
"outputs": [
|
371 |
+
{
|
372 |
+
"data": {
|
373 |
+
"text/plain": [
|
374 |
+
"208"
|
375 |
+
]
|
376 |
+
},
|
377 |
+
"execution_count": 12,
|
378 |
+
"metadata": {},
|
379 |
+
"output_type": "execute_result"
|
380 |
+
}
|
381 |
+
],
|
382 |
+
"source": [
|
383 |
+
"response.usage.prompt_tokens"
|
384 |
+
]
|
385 |
+
},
|
386 |
+
{
|
387 |
+
"cell_type": "code",
|
388 |
+
"execution_count": null,
|
389 |
+
"id": "1f1c6dd0-11bf-4b88-9029-8bce1e7bcb1c",
|
390 |
+
"metadata": {
|
391 |
+
"tags": [],
|
392 |
+
"id": "1f1c6dd0-11bf-4b88-9029-8bce1e7bcb1c",
|
393 |
+
"outputId": "a12fc058-249f-4493-eb3e-f021c12651dc"
|
394 |
+
},
|
395 |
+
"outputs": [
|
396 |
+
{
|
397 |
+
"name": "stdout",
|
398 |
+
"output_type": "stream",
|
399 |
+
"text": [
|
400 |
+
"\"لقد لاحظنا أنه عندما تعمل مجموعات أصحاب المصلحة على تحديد ... الرؤى، فإن هذا يؤدي إلى نقاش حول ما إذا كان ينبغي التركيز على صحة النظام البيئي أو رفاهية الإنسان ... إن معرفة ما إذا كانت الأولوية للنظم البيئية أو للبشر تؤثر بشكل كبير على تقييم أصحاب المصلحة للحالات البيئية والاجتماعية المرغوبة.\"\n"
|
401 |
+
]
|
402 |
+
}
|
403 |
+
],
|
404 |
+
"source": [
|
405 |
+
"print(response.choices[0].message.content)"
|
406 |
+
]
|
407 |
+
},
|
408 |
+
{
|
409 |
+
"cell_type": "markdown",
|
410 |
+
"id": "898a909e-88a2-4efb-99ed-64ceee317037",
|
411 |
+
"metadata": {
|
412 |
+
"id": "898a909e-88a2-4efb-99ed-64ceee317037"
|
413 |
+
},
|
414 |
+
"source": [
|
415 |
+
"## Automatic Purpose Driven Structured Generation Translation\n",
|
416 |
+
"\n",
|
417 |
+
"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)"
|
418 |
+
]
|
419 |
+
},
|
420 |
+
{
|
421 |
+
"cell_type": "markdown",
|
422 |
+
"id": "8aca3847-7f3d-43d3-8e80-0c7e2282073b",
|
423 |
+
"metadata": {
|
424 |
+
"id": "8aca3847-7f3d-43d3-8e80-0c7e2282073b"
|
425 |
+
},
|
426 |
+
"source": [
|
427 |
+
"## Instruction"
|
428 |
+
]
|
429 |
+
},
|
430 |
+
{
|
431 |
+
"cell_type": "markdown",
|
432 |
+
"id": "2f5deb21-18fb-4c5b-9045-c7fe5e751c05",
|
433 |
+
"metadata": {
|
434 |
+
"id": "2f5deb21-18fb-4c5b-9045-c7fe5e751c05"
|
435 |
+
},
|
436 |
+
"source": [
|
437 |
+
"Its usually helpful if we tell the LLM what we want to create when we prompt it. I have found using the tags like `<source text>` can be really useful to denote what you are specifying.\n",
|
438 |
+
"\n",
|
439 |
+
"> 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",
|
440 |
+
"{\"intent\": \"\", \"subject\": \"\", \"assumptions relating to content\": \"\", \"purpose\": \"\", \"target audience\": \"\"}. \n",
|
441 |
+
"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",
|
442 |
+
"\\<source text\\> \n",
|
443 |
+
"\\</source text\\> "
|
444 |
+
]
|
445 |
+
},
|
446 |
+
{
|
447 |
+
"cell_type": "markdown",
|
448 |
+
"id": "55f02988-1c63-4f03-a5a6-ca86c83b3c17",
|
449 |
+
"metadata": {
|
450 |
+
"id": "55f02988-1c63-4f03-a5a6-ca86c83b3c17"
|
451 |
+
},
|
452 |
+
"source": [
|
453 |
+
"## Tool Definition"
|
454 |
+
]
|
455 |
+
},
|
456 |
+
{
|
457 |
+
"cell_type": "code",
|
458 |
+
"execution_count": null,
|
459 |
+
"id": "affe3668-aa37-47b5-be37-a0bd5dabab56",
|
460 |
+
"metadata": {
|
461 |
+
"tags": [],
|
462 |
+
"id": "affe3668-aa37-47b5-be37-a0bd5dabab56"
|
463 |
+
},
|
464 |
+
"outputs": [],
|
465 |
+
"source": [
|
466 |
+
"translation_tools = [\n",
|
467 |
+
" {\n",
|
468 |
+
" \"type\": \"function\",\n",
|
469 |
+
" \"function\": {\n",
|
470 |
+
" \"name\": \"get_translation_audience_purpose\",\n",
|
471 |
+
" \"description\": \"Get the background of a text to assist in translation\",\n",
|
472 |
+
" \"parameters\": {\n",
|
473 |
+
" \"type\": \"object\",\n",
|
474 |
+
" \"properties\": {\n",
|
475 |
+
" \"subject\": {\n",
|
476 |
+
" \"type\": \"string\",\n",
|
477 |
+
" \"description\": \"The topic or central theme that the text revolves around.\",\n",
|
478 |
+
" },\n",
|
479 |
+
" \"assumptions relating to the content\": {\n",
|
480 |
+
" \"type\": \"string\",\n",
|
481 |
+
" \"description\": \"Write out any assumptions relating to the text.\",\n",
|
482 |
+
" },\n",
|
483 |
+
" \"purpose\": {\n",
|
484 |
+
" \"type\": \"string\",\n",
|
485 |
+
" \"description\": \"Why the text was written\",\n",
|
486 |
+
" },\n",
|
487 |
+
" \"audience\": {\n",
|
488 |
+
" \"type\": \"string\",\n",
|
489 |
+
" \"description\": \"The inferred audience that the text is written for.\",\n",
|
490 |
+
" },\n",
|
491 |
+
" },\n",
|
492 |
+
" \"required\": [\n",
|
493 |
+
" \"subject\",\n",
|
494 |
+
" \"assumptions relating to the content\",\n",
|
495 |
+
" \"purpose\",\n",
|
496 |
+
" \"audience\",\n",
|
497 |
+
" ],\n",
|
498 |
+
" },\n",
|
499 |
+
" },\n",
|
500 |
+
" }\n",
|
501 |
+
"]"
|
502 |
+
]
|
503 |
+
},
|
504 |
+
{
|
505 |
+
"cell_type": "code",
|
506 |
+
"execution_count": null,
|
507 |
+
"id": "b6436ce6-03af-4206-a283-0c2ecd17bd88",
|
508 |
+
"metadata": {
|
509 |
+
"id": "b6436ce6-03af-4206-a283-0c2ecd17bd88"
|
510 |
+
},
|
511 |
+
"outputs": [],
|
512 |
+
"source": [
|
513 |
+
"def tool_call_chat_completion(system_prompt, translation_input, tools):\n",
|
514 |
+
" \"\"\"\n",
|
515 |
+
" Generates a completion for a chat conversation using a specified system prompt and a user input.\n",
|
516 |
+
" \"\"\"\n",
|
517 |
+
"\n",
|
518 |
+
" 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",
|
519 |
+
"{{\"subject\": \"\", \"assumptions relating to content\": \"\", \"purpose\": \"\", \"target audience\": \"\"}}.\n",
|
520 |
+
"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",
|
521 |
+
"<source text>\n",
|
522 |
+
"{translation_input}\n",
|
523 |
+
"</source text>\n",
|
524 |
+
"\"\"\"\n",
|
525 |
+
" messages = [\n",
|
526 |
+
" {\"role\": \"system\", \"content\": system_prompt},\n",
|
527 |
+
" {\n",
|
528 |
+
" \"role\": \"user\",\n",
|
529 |
+
" \"content\": prompt,\n",
|
530 |
+
" },\n",
|
531 |
+
" ]\n",
|
532 |
+
" return client.chat_completion(messages, max_tokens=10_000, tools=tools, tool_choice='get_translation_audience_purpose')"
|
533 |
+
]
|
534 |
+
},
|
535 |
+
{
|
536 |
+
"cell_type": "code",
|
537 |
+
"execution_count": null,
|
538 |
+
"id": "a731b2c0-54a3-4b8e-83f6-1663c759cf79",
|
539 |
+
"metadata": {
|
540 |
+
"tags": [],
|
541 |
+
"id": "a731b2c0-54a3-4b8e-83f6-1663c759cf79"
|
542 |
+
},
|
543 |
+
"outputs": [],
|
544 |
+
"source": [
|
545 |
+
"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",
|
546 |
+
"response = tool_call_chat_completion(system_prompt, translation_input, translation_tools)"
|
547 |
+
]
|
548 |
+
},
|
549 |
+
{
|
550 |
+
"cell_type": "code",
|
551 |
+
"execution_count": null,
|
552 |
+
"id": "07e0f133-f6e1-4bc6-969c-da9d36bfba2f",
|
553 |
+
"metadata": {
|
554 |
+
"tags": [],
|
555 |
+
"id": "07e0f133-f6e1-4bc6-969c-da9d36bfba2f",
|
556 |
+
"outputId": "82e2506a-7336-4909-b8e4-fc52f671c511"
|
557 |
+
},
|
558 |
+
"outputs": [
|
559 |
+
{
|
560 |
+
"data": {
|
561 |
+
"text/plain": [
|
562 |
+
"454"
|
563 |
+
]
|
564 |
+
},
|
565 |
+
"execution_count": 17,
|
566 |
+
"metadata": {},
|
567 |
+
"output_type": "execute_result"
|
568 |
+
}
|
569 |
+
],
|
570 |
+
"source": [
|
571 |
+
"function_call_tokens = response.usage.prompt_tokens\n",
|
572 |
+
"function_call_tokens"
|
573 |
+
]
|
574 |
+
},
|
575 |
+
{
|
576 |
+
"cell_type": "code",
|
577 |
+
"execution_count": null,
|
578 |
+
"id": "02be1827-7137-463f-a026-0b26dec6f552",
|
579 |
+
"metadata": {
|
580 |
+
"tags": [],
|
581 |
+
"id": "02be1827-7137-463f-a026-0b26dec6f552",
|
582 |
+
"outputId": "60cd7be7-b6ac-412b-e0bc-3b8d3088f2b2"
|
583 |
+
},
|
584 |
+
"outputs": [
|
585 |
+
{
|
586 |
+
"name": "stdout",
|
587 |
+
"output_type": "stream",
|
588 |
+
"text": [
|
589 |
+
"{'assumptions relating to the content': 'The text assumes that the readers '\n",
|
590 |
+
" 'understand the concept of stakeholder '\n",
|
591 |
+
" 'groups and their potential differing '\n",
|
592 |
+
" 'priorities regarding environmental '\n",
|
593 |
+
" 'and social issues.',\n",
|
594 |
+
" 'audience': 'The target audience for this text is likely individuals or '\n",
|
595 |
+
" 'organizations involved in sustainability, ecology, or social '\n",
|
596 |
+
" 'welfare fields, as well as stakeholders with an interest in '\n",
|
597 |
+
" 'these areas.',\n",
|
598 |
+
" 'purpose': 'The purpose of this text is to highlight the observations made '\n",
|
599 |
+
" 'about the debates that arise when stakeholder groups define their '\n",
|
600 |
+
" 'visions, specifically regarding the priority between ecosystem '\n",
|
601 |
+
" 'health and human well-being, and how this priority influences '\n",
|
602 |
+
" 'their assessment of desired ecological and social outcomes.',\n",
|
603 |
+
" 'subject': 'Debates on Prioritizing Ecosystem vs. Human Well-being by '\n",
|
604 |
+
" 'Stakeholder Groups'}\n"
|
605 |
+
]
|
606 |
+
}
|
607 |
+
],
|
608 |
+
"source": [
|
609 |
+
"from pprint import pprint\n",
|
610 |
+
"description_json = response.choices[0].message.tool_calls[0].function.arguments\n",
|
611 |
+
"pprint(description_json)"
|
612 |
+
]
|
613 |
+
},
|
614 |
+
{
|
615 |
+
"cell_type": "code",
|
616 |
+
"execution_count": null,
|
617 |
+
"id": "7575bd09-2d20-49ae-bb10-162a0e469f16",
|
618 |
+
"metadata": {
|
619 |
+
"id": "7575bd09-2d20-49ae-bb10-162a0e469f16"
|
620 |
+
},
|
621 |
+
"outputs": [],
|
622 |
+
"source": [
|
623 |
+
"def automatic_purpose_driven_chat_completion(system_prompt, translation_input, description_json):\n",
|
624 |
+
" \"\"\"\n",
|
625 |
+
" Generates a completion for a chat conversation using a specified system prompt and a user input.\n",
|
626 |
+
" \"\"\"\n",
|
627 |
+
"\n",
|
628 |
+
" prompt = f\"\"\"Given the following descriptive json translate <source text> from English to Arabic\n",
|
629 |
+
"{description_json}\n",
|
630 |
+
"<source text>\n",
|
631 |
+
"{translation_input}\n",
|
632 |
+
"</source text>\n",
|
633 |
+
"Translation:\n",
|
634 |
+
"\"\"\"\n",
|
635 |
+
" messages = [\n",
|
636 |
+
" {\"role\": \"system\", \"content\": system_prompt},\n",
|
637 |
+
" {\n",
|
638 |
+
" \"role\": \"user\",\n",
|
639 |
+
" \"content\": prompt,\n",
|
640 |
+
" },\n",
|
641 |
+
" ]\n",
|
642 |
+
" return client.chat_completion(messages, max_tokens=10_000)"
|
643 |
+
]
|
644 |
+
},
|
645 |
+
{
|
646 |
+
"cell_type": "code",
|
647 |
+
"execution_count": null,
|
648 |
+
"id": "32bccca0-c866-4006-84d1-d5b783b73689",
|
649 |
+
"metadata": {
|
650 |
+
"tags": [],
|
651 |
+
"id": "32bccca0-c866-4006-84d1-d5b783b73689"
|
652 |
+
},
|
653 |
+
"outputs": [],
|
654 |
+
"source": [
|
655 |
+
"response = automatic_purpose_driven_chat_completion(system_prompt, translation_input, description_json)"
|
656 |
+
]
|
657 |
+
},
|
658 |
+
{
|
659 |
+
"cell_type": "code",
|
660 |
+
"execution_count": null,
|
661 |
+
"id": "b0867efb-39ea-4f9a-b073-2a84261f3821",
|
662 |
+
"metadata": {
|
663 |
+
"tags": [],
|
664 |
+
"id": "b0867efb-39ea-4f9a-b073-2a84261f3821",
|
665 |
+
"outputId": "485ece36-c47e-4d1d-cf50-bbe1a9960776"
|
666 |
+
},
|
667 |
+
"outputs": [
|
668 |
+
{
|
669 |
+
"data": {
|
670 |
+
"text/plain": [
|
671 |
+
"311"
|
672 |
+
]
|
673 |
+
},
|
674 |
+
"execution_count": 21,
|
675 |
+
"metadata": {},
|
676 |
+
"output_type": "execute_result"
|
677 |
+
}
|
678 |
+
],
|
679 |
+
"source": [
|
680 |
+
"automatic_purpose_driven_tokens = response.usage.prompt_tokens\n",
|
681 |
+
"automatic_purpose_driven_tokens"
|
682 |
+
]
|
683 |
+
},
|
684 |
+
{
|
685 |
+
"cell_type": "code",
|
686 |
+
"execution_count": null,
|
687 |
+
"id": "462ca84c-9ffd-4924-880d-e06b724caf02",
|
688 |
+
"metadata": {
|
689 |
+
"tags": [],
|
690 |
+
"id": "462ca84c-9ffd-4924-880d-e06b724caf02",
|
691 |
+
"outputId": "bf026405-eb15-4a28-8357-d99e174967e7"
|
692 |
+
},
|
693 |
+
"outputs": [
|
694 |
+
{
|
695 |
+
"name": "stdout",
|
696 |
+
"output_type": "stream",
|
697 |
+
"text": [
|
698 |
+
"Here is the translated text from English to Arabic:\n",
|
699 |
+
"\n",
|
700 |
+
"{\"افتراضات متعلقة بالمحتوى\": \"يفترض النص أن القراء يفهمون مفهوم مجموعات أصحاب المصلحة وأولوياتهم المختلفة المحتملة فيما يتعلق بالقضايا البيئية والاجتماعية.\", \"الجمهور\": \"الجمهور المستهدف لهذا النص هو على الأرجح أفراد أو منظمات تعمل في مجالات الاستدامة أو الإيكولوجيا أو الرعاية الاجتماعية، بالإضافة إلى أصحاب المصلحة المهتمين بهذه المجالات.\", \"الغرض\": \"الغرض من هذا النص هو تسليط الضوء على الملاحظات التي أبديت حول النقاشات التي تنشأ عندما تحدد مجموعات أصحاب المصلحة رؤيتها، خاصة فيما يتعلق بالأولوية بين صحة النظام البيئي ورفاهية الإنسان، وكيف تؤثر هذه الأولوية على تقييمها للنتائج الإيكولوجية والاجتماعية المرجوة.\", \"الموضوع\": \"النقاشات حول أولويات النظام البيئي مقابل رفاهية الإنسان من قبل مجموعات أصحاب المصلحة\": \"لقد لاحظنا أنه عندما تعمل مجموعات أصحاب المصلحة على تحديد الرؤى، فإن ذلك يؤدي إلى نقاش حول ما إذا كان ينبغي التركيز على صحة النظام البيئي أو رفاهية الإنسان... إن أولوية النظم البيئية أو البشر تؤثر بشكل كبير على تقييم أصحاب المصلحة للوضع الاجتماعي والإيكولوجي المرغوب.\"}\n"
|
701 |
+
]
|
702 |
+
}
|
703 |
+
],
|
704 |
+
"source": [
|
705 |
+
"print(response.choices[0].message.content)"
|
706 |
+
]
|
707 |
+
},
|
708 |
+
{
|
709 |
+
"cell_type": "markdown",
|
710 |
+
"id": "1ec3b20b-8393-4fda-a51d-cf67984cc166",
|
711 |
+
"metadata": {
|
712 |
+
"id": "1ec3b20b-8393-4fda-a51d-cf67984cc166"
|
713 |
+
},
|
714 |
+
"source": [
|
715 |
+
"# Push to the hub"
|
716 |
+
]
|
717 |
+
},
|
718 |
+
{
|
719 |
+
"cell_type": "code",
|
720 |
+
"execution_count": null,
|
721 |
+
"id": "6fc55725-216f-45dd-9c6d-dae77e16d606",
|
722 |
+
"metadata": {
|
723 |
+
"tags": [],
|
724 |
+
"id": "6fc55725-216f-45dd-9c6d-dae77e16d606",
|
725 |
+
"outputId": "d3b9b994-e3af-4f05-88b4-3b29778d1dc7"
|
726 |
+
},
|
727 |
+
"outputs": [
|
728 |
+
{
|
729 |
+
"data": {
|
730 |
+
"text/plain": [
|
731 |
+
"CommitInfo(commit_url='https://huggingface.co/arabic-translation-prompt-engineering/atpe-notebooks/commit/d112cc49a99a72106b75c3eae9be453264fb488d', commit_message='Upload baseline.ipynb with huggingface_hub', commit_description='', oid='d112cc49a99a72106b75c3eae9be453264fb488d', pr_url=None, pr_revision=None, pr_num=None)"
|
732 |
+
]
|
733 |
+
},
|
734 |
+
"execution_count": 23,
|
735 |
+
"metadata": {},
|
736 |
+
"output_type": "execute_result"
|
737 |
+
}
|
738 |
+
],
|
739 |
+
"source": [
|
740 |
+
"from huggingface_hub import HfApi\n",
|
741 |
+
"\n",
|
742 |
+
"api = HfApi()\n",
|
743 |
+
"api.upload_file(\n",
|
744 |
+
" path_or_fileobj=\"baseline.ipynb\",\n",
|
745 |
+
" path_in_repo=\"baseline.ipynb\",\n",
|
746 |
+
" repo_id=\"arabic-translation-prompt-engineering/atpe-notebooks\",\n",
|
747 |
+
" repo_type=\"model\",\n",
|
748 |
+
")"
|
749 |
+
]
|
750 |
+
},
|
751 |
+
{
|
752 |
+
"cell_type": "code",
|
753 |
+
"execution_count": null,
|
754 |
+
"id": "6f21f4cf-1872-4934-bb91-8aef311ed729",
|
755 |
+
"metadata": {
|
756 |
+
"id": "6f21f4cf-1872-4934-bb91-8aef311ed729"
|
757 |
+
},
|
758 |
+
"outputs": [],
|
759 |
+
"source": []
|
760 |
}
|
761 |
+
],
|
762 |
+
"metadata": {
|
763 |
+
"kernelspec": {
|
764 |
+
"display_name": "Python 3 (ipykernel)",
|
765 |
+
"language": "python",
|
766 |
+
"name": "python3"
|
767 |
+
},
|
768 |
+
"language_info": {
|
769 |
+
"codemirror_mode": {
|
770 |
+
"name": "ipython",
|
771 |
+
"version": 3
|
772 |
+
},
|
773 |
+
"file_extension": ".py",
|
774 |
+
"mimetype": "text/x-python",
|
775 |
+
"name": "python",
|
776 |
+
"nbconvert_exporter": "python",
|
777 |
+
"pygments_lexer": "ipython3",
|
778 |
+
"version": "3.10.9"
|
779 |
+
},
|
780 |
+
"colab": {
|
781 |
+
"provenance": []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
782 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
783 |
},
|
784 |
+
"nbformat": 4,
|
785 |
+
"nbformat_minor": 5
|
786 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|