derek-thomas commited on
Commit
bb9389b
·
verified ·
1 Parent(s): 2c8fe90

Upload baseline.ipynb with huggingface_hub

Browse files
Files changed (1) hide show
  1. baseline.ipynb +292 -0
baseline.ipynb ADDED
@@ -0,0 +1,292 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "883e3354-1538-4f98-bf42-67552215bba3",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Setup"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "code",
13
+ "execution_count": 34,
14
+ "id": "b45bd52f-03e9-419f-8110-1013ff45fb1b",
15
+ "metadata": {
16
+ "tags": []
17
+ },
18
+ "outputs": [],
19
+ "source": [
20
+ "from huggingface_hub import InferenceClient, login"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": 35,
26
+ "id": "dc9f0411-8bf2-4a20-a6ea-331a2a486b8e",
27
+ "metadata": {
28
+ "tags": []
29
+ },
30
+ "outputs": [
31
+ {
32
+ "data": {
33
+ "application/vnd.jupyter.widget-view+json": {
34
+ "model_id": "fcb8e82880df4053899633bfe3c2220f",
35
+ "version_major": 2,
36
+ "version_minor": 0
37
+ },
38
+ "text/plain": [
39
+ "VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
40
+ ]
41
+ },
42
+ "metadata": {},
43
+ "output_type": "display_data"
44
+ }
45
+ ],
46
+ "source": [
47
+ "login()"
48
+ ]
49
+ },
50
+ {
51
+ "cell_type": "code",
52
+ "execution_count": null,
53
+ "id": "6fc55725-216f-45dd-9c6d-dae77e16d606",
54
+ "metadata": {},
55
+ "outputs": [],
56
+ "source": [
57
+ "from huggingface_hub import HfApi\n",
58
+ "api = HfApi()\n",
59
+ "api.upload_file(\n",
60
+ " path_or_fileobj=\"baseline.ipynb\",\n",
61
+ " path_in_repo=\"baseline.ipynb\",\n",
62
+ " repo_id=\"arabic-translation-prompt-engineering/atpe-notebooks\",\n",
63
+ " repo_type=\"dataset\",\n",
64
+ ")"
65
+ ]
66
+ },
67
+ {
68
+ "cell_type": "markdown",
69
+ "id": "4c254d6f-f3a1-49c1-815e-36e41e75ca25",
70
+ "metadata": {},
71
+ "source": [
72
+ "<div class=\"alert alert-danger\" role=\"alert\" style=\"display: flex; align-items: center;\">\n",
73
+ " <div style=\"text-align: center; padding-right: 10px;\">\n",
74
+ " <i class=\"fa fa-exclamation-triangle fa-2x\"></i>\n",
75
+ " </div>\n",
76
+ " <div style=\"display: flex; align-items: center; margin-top: 4px;\"> <!-- Added margin-top to lower the text -->\n",
77
+ " <strong>Warning: You will need to point to a model/deployment that is running.</strong>\n",
78
+ " </div>\n",
79
+ "</div>\n"
80
+ ]
81
+ },
82
+ {
83
+ "cell_type": "code",
84
+ "execution_count": 36,
85
+ "id": "84e6cb89-30d3-4ef5-8063-07783798e045",
86
+ "metadata": {},
87
+ "outputs": [],
88
+ "source": [
89
+ "MODEL = \"CohereForAI/c4ai-command-r-plus\"\n",
90
+ "client = InferenceClient(MODEL)"
91
+ ]
92
+ },
93
+ {
94
+ "cell_type": "markdown",
95
+ "id": "f5fe63f8-dea2-4c61-b6ce-29f173e4c4eb",
96
+ "metadata": {},
97
+ "source": [
98
+ "# Translation"
99
+ ]
100
+ },
101
+ {
102
+ "cell_type": "markdown",
103
+ "id": "c5bee77b-da1c-43b3-ab14-d15e871f7502",
104
+ "metadata": {},
105
+ "source": [
106
+ "## Baseline\n",
107
+ "\n",
108
+ "For our baseline we will translate with a simple system prompt and instruction."
109
+ ]
110
+ },
111
+ {
112
+ "cell_type": "markdown",
113
+ "id": "6cea05d6-afb7-4829-b130-d4bcfe549acb",
114
+ "metadata": {},
115
+ "source": [
116
+ "### Analysis"
117
+ ]
118
+ },
119
+ {
120
+ "cell_type": "markdown",
121
+ "id": "a98b9b67-e68b-43b2-b8e9-0ed1cf85591f",
122
+ "metadata": {},
123
+ "source": [
124
+ "### System Prompt\n",
125
+ "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",
126
+ "\n",
127
+ "> 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."
128
+ ]
129
+ },
130
+ {
131
+ "cell_type": "code",
132
+ "execution_count": null,
133
+ "id": "032c86d2-868e-4fa6-b03e-58f1c41434cc",
134
+ "metadata": {
135
+ "tags": []
136
+ },
137
+ "outputs": [],
138
+ "source": [
139
+ "system_prompt = \"\"\"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.\"\"\""
140
+ ]
141
+ },
142
+ {
143
+ "cell_type": "markdown",
144
+ "id": "803ddeba-03de-4f13-95d1-5fb097058cf2",
145
+ "metadata": {},
146
+ "source": [
147
+ "### Prompt\n",
148
+ "> Translate this from english to arabic: {en_input}.\n",
149
+ ">\n",
150
+ "> Translation: \n",
151
+ "\n",
152
+ "Again we use a simple prompt to get a translation."
153
+ ]
154
+ },
155
+ {
156
+ "cell_type": "code",
157
+ "execution_count": 53,
158
+ "id": "b7f1722c-c484-4e22-a025-53f95943fc76",
159
+ "metadata": {},
160
+ "outputs": [],
161
+ "source": [
162
+ "def baseline_chat_completion(system_prompt, en_input):\n",
163
+ " \"\"\"\n",
164
+ " Generates a completion for a chat conversation using a specified system prompt and a user input.\n",
165
+ " \"\"\"\n",
166
+ " messages = [\n",
167
+ " {\"role\": \"system\", \"content\": system_prompt},\n",
168
+ " {\"role\": \"user\", \"content\": f\"Translate this from english to arabic: {en_input}.\\nTranslation: \"},\n",
169
+ " ]\n",
170
+ " return client.chat_completion(messages, max_tokens=10_000)"
171
+ ]
172
+ },
173
+ {
174
+ "cell_type": "code",
175
+ "execution_count": 50,
176
+ "id": "96a0ba0b-be47-4eb0-bbc2-c82b0ea1b72e",
177
+ "metadata": {
178
+ "tags": []
179
+ },
180
+ "outputs": [
181
+ {
182
+ "data": {
183
+ "text/plain": [
184
+ "120"
185
+ ]
186
+ },
187
+ "execution_count": 50,
188
+ "metadata": {},
189
+ "output_type": "execute_result"
190
+ }
191
+ ],
192
+ "source": [
193
+ "en_input = \"Float like a butterfly sting like a bee – his hands can’t hit what his eyes can’t see.\"\n",
194
+ "response = baseline_chat_completion(system_prompt, \"Float like a butterfly sting like a bee – his hands can’t hit what his eyes can’t see.\")"
195
+ ]
196
+ },
197
+ {
198
+ "cell_type": "markdown",
199
+ "id": "2bca574c-461d-4822-b0dd-b12a3b9846b3",
200
+ "metadata": {},
201
+ "source": [
202
+ "### Token Cost\n",
203
+ "Here we can see that the cost is quite cheap, only 92 tokens!"
204
+ ]
205
+ },
206
+ {
207
+ "cell_type": "code",
208
+ "execution_count": 52,
209
+ "id": "4e305b1e-56e0-44da-8c17-496cbcc35fad",
210
+ "metadata": {
211
+ "tags": []
212
+ },
213
+ "outputs": [
214
+ {
215
+ "data": {
216
+ "text/plain": [
217
+ "120"
218
+ ]
219
+ },
220
+ "execution_count": 52,
221
+ "metadata": {},
222
+ "output_type": "execute_result"
223
+ }
224
+ ],
225
+ "source": [
226
+ "response.usage.prompt_tokens"
227
+ ]
228
+ },
229
+ {
230
+ "cell_type": "code",
231
+ "execution_count": 51,
232
+ "id": "ef24fe6b-d801-4f3e-95ad-cb7f67247bc3",
233
+ "metadata": {
234
+ "tags": []
235
+ },
236
+ "outputs": [
237
+ {
238
+ "data": {
239
+ "text/plain": [
240
+ "'يرفرف مثل الفراشة ويلسع كالنحلة - يديه لا يمكن أن تصيب ما لا تستطيع عينيه رؤيته.'"
241
+ ]
242
+ },
243
+ "execution_count": 51,
244
+ "metadata": {},
245
+ "output_type": "execute_result"
246
+ }
247
+ ],
248
+ "source": [
249
+ "response.choices[0].message.content"
250
+ ]
251
+ },
252
+ {
253
+ "cell_type": "markdown",
254
+ "id": "3a9cdf02-d590-4bcf-a7a8-e6b7817ba715",
255
+ "metadata": {},
256
+ "source": [
257
+ "## Purpose Driven Translation\n",
258
+ "\n",
259
+ "[](https://arxiv.org/pdf/2308.01391)"
260
+ ]
261
+ },
262
+ {
263
+ "cell_type": "code",
264
+ "execution_count": null,
265
+ "id": "12e57d07-9e86-426b-be42-e932699d1fe2",
266
+ "metadata": {},
267
+ "outputs": [],
268
+ "source": []
269
+ }
270
+ ],
271
+ "metadata": {
272
+ "kernelspec": {
273
+ "display_name": "Python 3 (ipykernel)",
274
+ "language": "python",
275
+ "name": "python3"
276
+ },
277
+ "language_info": {
278
+ "codemirror_mode": {
279
+ "name": "ipython",
280
+ "version": 3
281
+ },
282
+ "file_extension": ".py",
283
+ "mimetype": "text/x-python",
284
+ "name": "python",
285
+ "nbconvert_exporter": "python",
286
+ "pygments_lexer": "ipython3",
287
+ "version": "3.10.9"
288
+ }
289
+ },
290
+ "nbformat": 4,
291
+ "nbformat_minor": 5
292
+ }