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14 |
+
"name": "python"
|
15 |
+
}
|
16 |
+
},
|
17 |
+
"cells": [
|
18 |
+
{
|
19 |
+
"cell_type": "code",
|
20 |
+
"source": [],
|
21 |
+
"metadata": {
|
22 |
+
"id": "ArOANWk0chIw"
|
23 |
+
},
|
24 |
+
"execution_count": null,
|
25 |
+
"outputs": []
|
26 |
+
},
|
27 |
+
{
|
28 |
+
"cell_type": "code",
|
29 |
+
"source": [],
|
30 |
+
"metadata": {
|
31 |
+
"id": "p-xhqHURchLv"
|
32 |
+
},
|
33 |
+
"execution_count": null,
|
34 |
+
"outputs": []
|
35 |
+
},
|
36 |
+
{
|
37 |
+
"cell_type": "markdown",
|
38 |
+
"source": [
|
39 |
+
"## السطور ثابته"
|
40 |
+
],
|
41 |
+
"metadata": {
|
42 |
+
"id": "c9HVIB17pXwL"
|
43 |
+
}
|
44 |
+
},
|
45 |
+
{
|
46 |
+
"cell_type": "code",
|
47 |
+
"execution_count": null,
|
48 |
+
"metadata": {
|
49 |
+
"id": "Bbl112scce3k"
|
50 |
+
},
|
51 |
+
"outputs": [],
|
52 |
+
"source": [
|
53 |
+
"import requests\n",
|
54 |
+
"\n",
|
55 |
+
"API_TOKEN = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n",
|
56 |
+
"headers = {\"Authorization\": f\"Bearer {token_access}\"}"
|
57 |
+
]
|
58 |
+
},
|
59 |
+
{
|
60 |
+
"cell_type": "code",
|
61 |
+
"source": [],
|
62 |
+
"metadata": {
|
63 |
+
"id": "6wjPCSG0chO-"
|
64 |
+
},
|
65 |
+
"execution_count": null,
|
66 |
+
"outputs": []
|
67 |
+
},
|
68 |
+
{
|
69 |
+
"cell_type": "markdown",
|
70 |
+
"source": [
|
71 |
+
"## مابين الاقواس ضع ماقبل التساوى"
|
72 |
+
],
|
73 |
+
"metadata": {
|
74 |
+
"id": "QxFoALn9p6eb"
|
75 |
+
}
|
76 |
+
},
|
77 |
+
{
|
78 |
+
"cell_type": "code",
|
79 |
+
"source": [
|
80 |
+
"data = query xxxxxxxxxxxxxx\n",
|
81 |
+
"print(data)"
|
82 |
+
],
|
83 |
+
"metadata": {
|
84 |
+
"id": "n56U28k3pecM"
|
85 |
+
},
|
86 |
+
"execution_count": null,
|
87 |
+
"outputs": []
|
88 |
+
},
|
89 |
+
{
|
90 |
+
"cell_type": "code",
|
91 |
+
"source": [],
|
92 |
+
"metadata": {
|
93 |
+
"id": "IxvX-v6Ipefo"
|
94 |
+
},
|
95 |
+
"execution_count": null,
|
96 |
+
"outputs": []
|
97 |
+
},
|
98 |
+
{
|
99 |
+
"cell_type": "code",
|
100 |
+
"source": [],
|
101 |
+
"metadata": {
|
102 |
+
"id": "c-ERtDCRpejC"
|
103 |
+
},
|
104 |
+
"execution_count": null,
|
105 |
+
"outputs": []
|
106 |
+
},
|
107 |
+
{
|
108 |
+
"cell_type": "code",
|
109 |
+
"source": [
|
110 |
+
"import requests\n",
|
111 |
+
"API_TOKEN = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n",
|
112 |
+
"API_URL = \"https://api-inference.huggingface.co/models/openai-community/gpt2\"\n",
|
113 |
+
"headers = {\"Authorization\": f\"Bearer {API_TOKEN}\"}\n",
|
114 |
+
"\n",
|
115 |
+
"def query(payload):\n",
|
116 |
+
"\tresponse = requests.post(API_URL, headers=headers, json=payload)\n",
|
117 |
+
"\treturn response.json()\n",
|
118 |
+
"\n",
|
119 |
+
"output = query({\n",
|
120 |
+
"\t\"inputs\": \"Can you please let us know more details about your \",\n",
|
121 |
+
"})\n",
|
122 |
+
"print(data)"
|
123 |
+
],
|
124 |
+
"metadata": {
|
125 |
+
"id": "45_HIQeMchRv"
|
126 |
+
},
|
127 |
+
"execution_count": null,
|
128 |
+
"outputs": []
|
129 |
+
},
|
130 |
+
{
|
131 |
+
"cell_type": "code",
|
132 |
+
"source": [],
|
133 |
+
"metadata": {
|
134 |
+
"id": "fwrz9-ujfn6K"
|
135 |
+
},
|
136 |
+
"execution_count": null,
|
137 |
+
"outputs": []
|
138 |
+
},
|
139 |
+
{
|
140 |
+
"cell_type": "markdown",
|
141 |
+
"source": [
|
142 |
+
"## gpt2"
|
143 |
+
],
|
144 |
+
"metadata": {
|
145 |
+
"id": "Fwg_UkgQo0DM"
|
146 |
+
}
|
147 |
+
},
|
148 |
+
{
|
149 |
+
"cell_type": "code",
|
150 |
+
"source": [
|
151 |
+
"import requests\n",
|
152 |
+
"\n",
|
153 |
+
"API_TOKEN = \"xxxxxxxxxxxxxxxxxxxxx\"\n",
|
154 |
+
"API_URL = \"https://api-inference.huggingface.co/models/gpt2\"\n",
|
155 |
+
"headers = {\"Authorization\": f\"Bearer {token_access}\"}\n",
|
156 |
+
"\n",
|
157 |
+
"def query(payload):\n",
|
158 |
+
" response = requests.post(API_URL, headers=headers, json={\"inputs\": payload})\n",
|
159 |
+
" return response.json()\n",
|
160 |
+
"\n",
|
161 |
+
"data = query(\"Can you please let us know more details about your \")\n",
|
162 |
+
"print(data)\n",
|
163 |
+
"\n"
|
164 |
+
],
|
165 |
+
"metadata": {
|
166 |
+
"id": "_JL9cZ8Efn9R"
|
167 |
+
},
|
168 |
+
"execution_count": null,
|
169 |
+
"outputs": []
|
170 |
+
},
|
171 |
+
{
|
172 |
+
"cell_type": "code",
|
173 |
+
"source": [],
|
174 |
+
"metadata": {
|
175 |
+
"id": "D-rp_2jRc13v"
|
176 |
+
},
|
177 |
+
"execution_count": null,
|
178 |
+
"outputs": []
|
179 |
+
},
|
180 |
+
{
|
181 |
+
"cell_type": "code",
|
182 |
+
"source": [
|
183 |
+
"!ENDPOINT = https://api-inference.huggingface.co/models/<MODEL_ID>"
|
184 |
+
],
|
185 |
+
"metadata": {
|
186 |
+
"id": "M5xb4b70c16Z"
|
187 |
+
},
|
188 |
+
"execution_count": null,
|
189 |
+
"outputs": []
|
190 |
+
},
|
191 |
+
{
|
192 |
+
"cell_type": "code",
|
193 |
+
"source": [],
|
194 |
+
"metadata": {
|
195 |
+
"id": "6ZGC66_8dDCn"
|
196 |
+
},
|
197 |
+
"execution_count": null,
|
198 |
+
"outputs": []
|
199 |
+
},
|
200 |
+
{
|
201 |
+
"cell_type": "code",
|
202 |
+
"source": [
|
203 |
+
"import requests\n",
|
204 |
+
"\n",
|
205 |
+
"API_TOKEN = \"xxxxxxxxxxxxxxxxxxxxxxxxxx\"\n",
|
206 |
+
"headers = {\"Authorization\": f\"Bearer {token_access}\"}\n",
|
207 |
+
"API_URL = \"https://api-inference.huggingface.co/models/deepset/roberta-base-squad2\"\n",
|
208 |
+
"def query(payload):\n",
|
209 |
+
" response = requests.post(API_URL, headers=headers, json=payload)\n",
|
210 |
+
" return response.json()\n",
|
211 |
+
"data = query(\n",
|
212 |
+
" {\n",
|
213 |
+
" \"inputs\": {\n",
|
214 |
+
" \"question\": \"What's my name?\",\n",
|
215 |
+
" \"context\": \"My name is Clara and I live in Berkeley.\",\n",
|
216 |
+
" }\n",
|
217 |
+
" }\n",
|
218 |
+
")\n",
|
219 |
+
"print(data)"
|
220 |
+
],
|
221 |
+
"metadata": {
|
222 |
+
"id": "_Hp2T82FdDGv"
|
223 |
+
},
|
224 |
+
"execution_count": null,
|
225 |
+
"outputs": []
|
226 |
+
},
|
227 |
+
{
|
228 |
+
"cell_type": "code",
|
229 |
+
"source": [],
|
230 |
+
"metadata": {
|
231 |
+
"id": "GWHLt-mFg8aq"
|
232 |
+
},
|
233 |
+
"execution_count": null,
|
234 |
+
"outputs": []
|
235 |
+
},
|
236 |
+
{
|
237 |
+
"cell_type": "code",
|
238 |
+
"source": [],
|
239 |
+
"metadata": {
|
240 |
+
"id": "vkXj3J2og8dr"
|
241 |
+
},
|
242 |
+
"execution_count": null,
|
243 |
+
"outputs": []
|
244 |
+
},
|
245 |
+
{
|
246 |
+
"cell_type": "code",
|
247 |
+
"source": [
|
248 |
+
"import requests\n",
|
249 |
+
"\n",
|
250 |
+
"API_TOKEN = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n",
|
251 |
+
"headers = {\"Authorization\": f\"Bearer {token_access}\"}\n",
|
252 |
+
"API_URL = \"https://api-inference.huggingface.co/models/google/tapas-base-finetuned-wtq\"\n",
|
253 |
+
"def query(payload):\n",
|
254 |
+
" response = requests.post(API_URL, headers=headers, json=payload)\n",
|
255 |
+
" return response.json()\n",
|
256 |
+
"data = query(\n",
|
257 |
+
" {\n",
|
258 |
+
" \"inputs\": {\n",
|
259 |
+
" \"query\": \"How many stars does the transformers repository have?\",\n",
|
260 |
+
" \"table\": {\n",
|
261 |
+
" \"Repository\": [\"Transformers\", \"Datasets\", \"Tokenizers\"],\n",
|
262 |
+
" \"Stars\": [\"36542\", \"4512\", \"3934\"],\n",
|
263 |
+
" \"Contributors\": [\"651\", \"77\", \"34\"],\n",
|
264 |
+
" \"Programming language\": [\n",
|
265 |
+
" \"Python\",\n",
|
266 |
+
" \"Python\",\n",
|
267 |
+
" \"Rust, Python and NodeJS\",\n",
|
268 |
+
" ],\n",
|
269 |
+
" },\n",
|
270 |
+
" }\n",
|
271 |
+
" }\n",
|
272 |
+
")\n",
|
273 |
+
"print(data)"
|
274 |
+
],
|
275 |
+
"metadata": {
|
276 |
+
"id": "gLTGSz1Pdt1I"
|
277 |
+
},
|
278 |
+
"execution_count": null,
|
279 |
+
"outputs": []
|
280 |
+
},
|
281 |
+
{
|
282 |
+
"cell_type": "code",
|
283 |
+
"source": [
|
284 |
+
"import requests\n",
|
285 |
+
"\n",
|
286 |
+
"API_TOKEN = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n",
|
287 |
+
"headers = {\"Authorization\": f\"Bearer {token_access}\"}\n",
|
288 |
+
"API_URL = \"https://api-inference.huggingface.co/models/bert-base-uncased\"\n",
|
289 |
+
"def query(payload):\n",
|
290 |
+
" response = requests.post(API_URL, headers=headers, json=payload)\n",
|
291 |
+
" return response.json()\n",
|
292 |
+
"data = query({\"inputs\": \"The answer to the universe is [MASK].\"})\n",
|
293 |
+
"print(data)"
|
294 |
+
],
|
295 |
+
"metadata": {
|
296 |
+
"id": "PLfVr4yUfI23"
|
297 |
+
},
|
298 |
+
"execution_count": null,
|
299 |
+
"outputs": []
|
300 |
+
},
|
301 |
+
{
|
302 |
+
"cell_type": "code",
|
303 |
+
"source": [],
|
304 |
+
"metadata": {
|
305 |
+
"id": "3RmnuvQVgY9w"
|
306 |
+
},
|
307 |
+
"execution_count": null,
|
308 |
+
"outputs": []
|
309 |
+
},
|
310 |
+
{
|
311 |
+
"cell_type": "code",
|
312 |
+
"source": [],
|
313 |
+
"metadata": {
|
314 |
+
"id": "wAgMVYvNgZAy"
|
315 |
+
},
|
316 |
+
"execution_count": null,
|
317 |
+
"outputs": []
|
318 |
+
},
|
319 |
+
{
|
320 |
+
"cell_type": "code",
|
321 |
+
"source": [],
|
322 |
+
"metadata": {
|
323 |
+
"id": "NsqFnUa7gZDo"
|
324 |
+
},
|
325 |
+
"execution_count": null,
|
326 |
+
"outputs": []
|
327 |
+
},
|
328 |
+
{
|
329 |
+
"cell_type": "code",
|
330 |
+
"source": [
|
331 |
+
"!huggingface-cli login\n",
|
332 |
+
"import requests\n",
|
333 |
+
"def query(payload, model_id, api_token):\n",
|
334 |
+
"\theaders = {\"Authorization\": f\"Bearer {api_token}\"}\n",
|
335 |
+
"\tAPI_URL = f\"https://api-inference.huggingface.co/models/{model_id}\"\n",
|
336 |
+
"\tresponse = requests.post(API_URL, headers=headers, json=payload)\n",
|
337 |
+
"\treturn response.json()\n",
|
338 |
+
"\n",
|
339 |
+
"model_id = \"distilbert-base-uncased\"\n",
|
340 |
+
"api_token = \"xxxxxxxxxxxxxxxxxxxxxxxxx\" # get yours at hf.co/settings/tokens\n",
|
341 |
+
"data = query(\"The goal of life is [MASK].\", model_id, api_token)"
|
342 |
+
],
|
343 |
+
"metadata": {
|
344 |
+
"id": "GDJt8dM-ghL4"
|
345 |
+
},
|
346 |
+
"execution_count": null,
|
347 |
+
"outputs": []
|
348 |
+
},
|
349 |
+
{
|
350 |
+
"cell_type": "code",
|
351 |
+
"source": [],
|
352 |
+
"metadata": {
|
353 |
+
"id": "mDJ2Oo6Fg9uZ"
|
354 |
+
},
|
355 |
+
"execution_count": null,
|
356 |
+
"outputs": []
|
357 |
+
},
|
358 |
+
{
|
359 |
+
"cell_type": "code",
|
360 |
+
"source": [],
|
361 |
+
"metadata": {
|
362 |
+
"id": "91d0M9vrrruG"
|
363 |
+
},
|
364 |
+
"execution_count": null,
|
365 |
+
"outputs": []
|
366 |
+
},
|
367 |
+
{
|
368 |
+
"cell_type": "code",
|
369 |
+
"source": [
|
370 |
+
"import json\n",
|
371 |
+
"import requests\n",
|
372 |
+
"import time\n",
|
373 |
+
"\n",
|
374 |
+
"token_access = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n",
|
375 |
+
"headers = {\"Authorization\": f\"Bearer {token_access}\"}\n",
|
376 |
+
"\n",
|
377 |
+
"API_URL = \"https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-es\"\n",
|
378 |
+
"\n",
|
379 |
+
"def query(payload):\n",
|
380 |
+
"\n",
|
381 |
+
" data = json.dumps(payload)\n",
|
382 |
+
"\n",
|
383 |
+
" time.sleep(1)\n",
|
384 |
+
"\n",
|
385 |
+
" while True:\n",
|
386 |
+
"\n",
|
387 |
+
" try:\n",
|
388 |
+
"\n",
|
389 |
+
" response = requests.request(\"POST\", API_URL, headers=headers, data=data)\n",
|
390 |
+
" break\n",
|
391 |
+
"\n",
|
392 |
+
" except Exception:\n",
|
393 |
+
"\n",
|
394 |
+
" continue\n",
|
395 |
+
"\n",
|
396 |
+
" return json.loads(response.content.decode(\"utf-8\"))\n",
|
397 |
+
"\n",
|
398 |
+
"data = query(\n",
|
399 |
+
" {\n",
|
400 |
+
" \"inputs\": \"Hello I'm in Alexander's tutorial\",\n",
|
401 |
+
" }\n",
|
402 |
+
")\n",
|
403 |
+
"\n",
|
404 |
+
"print(data)"
|
405 |
+
],
|
406 |
+
"metadata": {
|
407 |
+
"id": "vxQsCDuXi2xo"
|
408 |
+
},
|
409 |
+
"execution_count": null,
|
410 |
+
"outputs": []
|
411 |
+
},
|
412 |
+
{
|
413 |
+
"cell_type": "code",
|
414 |
+
"source": [],
|
415 |
+
"metadata": {
|
416 |
+
"id": "Ghdjipdwj-oL"
|
417 |
+
},
|
418 |
+
"execution_count": null,
|
419 |
+
"outputs": []
|
420 |
+
},
|
421 |
+
{
|
422 |
+
"cell_type": "code",
|
423 |
+
"source": [
|
424 |
+
"import json\n",
|
425 |
+
"import requests\n",
|
426 |
+
"import time\n",
|
427 |
+
"\n",
|
428 |
+
"token_access = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n",
|
429 |
+
"headers = {\"Authorization\": f\"Bearer {token_access}\"}\n",
|
430 |
+
"\n",
|
431 |
+
"API_URL = \"https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-ar\"\n",
|
432 |
+
"\n",
|
433 |
+
"def query(payload):\n",
|
434 |
+
"\n",
|
435 |
+
" data = json.dumps(payload)\n",
|
436 |
+
"\n",
|
437 |
+
" time.sleep(1)\n",
|
438 |
+
"\n",
|
439 |
+
" while True:\n",
|
440 |
+
"\n",
|
441 |
+
" try:\n",
|
442 |
+
"\n",
|
443 |
+
" response = requests.request(\"POST\", API_URL, headers=headers, data=data)\n",
|
444 |
+
" break\n",
|
445 |
+
"\n",
|
446 |
+
" except Exception:\n",
|
447 |
+
"\n",
|
448 |
+
" continue\n",
|
449 |
+
"\n",
|
450 |
+
" return json.loads(response.content.decode(\"utf-8\"))\n",
|
451 |
+
"\n",
|
452 |
+
"data = query(\n",
|
453 |
+
" {\n",
|
454 |
+
" \"inputs\": \"Hello I'm in Alexander's tutorial\",\n",
|
455 |
+
" }\n",
|
456 |
+
")\n",
|
457 |
+
"\n",
|
458 |
+
"print(data)"
|
459 |
+
],
|
460 |
+
"metadata": {
|
461 |
+
"id": "-rokVqVVjUwA"
|
462 |
+
},
|
463 |
+
"execution_count": null,
|
464 |
+
"outputs": []
|
465 |
+
},
|
466 |
+
{
|
467 |
+
"cell_type": "code",
|
468 |
+
"source": [],
|
469 |
+
"metadata": {
|
470 |
+
"id": "lwWmpQGok1mx"
|
471 |
+
},
|
472 |
+
"execution_count": null,
|
473 |
+
"outputs": []
|
474 |
+
},
|
475 |
+
{
|
476 |
+
"cell_type": "code",
|
477 |
+
"source": [],
|
478 |
+
"metadata": {
|
479 |
+
"id": "noWjicykmQiS"
|
480 |
+
},
|
481 |
+
"execution_count": null,
|
482 |
+
"outputs": []
|
483 |
+
},
|
484 |
+
{
|
485 |
+
"cell_type": "markdown",
|
486 |
+
"source": [
|
487 |
+
"# كود ترجمة ناجح"
|
488 |
+
],
|
489 |
+
"metadata": {
|
490 |
+
"id": "J8mJxZ4CmRA7"
|
491 |
+
}
|
492 |
+
},
|
493 |
+
{
|
494 |
+
"cell_type": "code",
|
495 |
+
"source": [
|
496 |
+
"import requests\n",
|
497 |
+
"token_access = \"xxxxxxxxxxxx\"\n",
|
498 |
+
"headers = {\"Authorization\": f\"Bearer {token_access}\"}\n",
|
499 |
+
"\n",
|
500 |
+
"def query(payload):\n",
|
501 |
+
"\tresponse = requests.post(API_URL, headers=headers, json=payload)\n",
|
502 |
+
"\treturn response.json()\n",
|
503 |
+
"\n",
|
504 |
+
"output = query({\n",
|
505 |
+
"\t\"inputs\": \"how are you\",\n",
|
506 |
+
"})\n",
|
507 |
+
"print(output)"
|
508 |
+
],
|
509 |
+
"metadata": {
|
510 |
+
"id": "ewy25jAGl_69"
|
511 |
+
},
|
512 |
+
"execution_count": null,
|
513 |
+
"outputs": []
|
514 |
+
},
|
515 |
+
{
|
516 |
+
"cell_type": "code",
|
517 |
+
"source": [
|
518 |
+
"import requests\n",
|
519 |
+
"\n",
|
520 |
+
"\n",
|
521 |
+
"token_access = \"xxxxxxxxxxxxxxxxxxxxxxxxxx\"\n",
|
522 |
+
"headers = {\"Authorization\": f\"Bearer {token_access}\"}\n",
|
523 |
+
"API_URL = \"https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-es\"\n",
|
524 |
+
"\n",
|
525 |
+
"def query(payload):\n",
|
526 |
+
"\tresponse = requests.post(API_URL, headers=headers, json=payload)\n",
|
527 |
+
"\treturn response.json()\n",
|
528 |
+
"\n",
|
529 |
+
"output = query({\n",
|
530 |
+
"\t\"inputs\": \"good name\",\n",
|
531 |
+
"})\n",
|
532 |
+
"print(output)"
|
533 |
+
],
|
534 |
+
"metadata": {
|
535 |
+
"id": "Jc1hjA_PlAkA"
|
536 |
+
},
|
537 |
+
"execution_count": null,
|
538 |
+
"outputs": []
|
539 |
+
},
|
540 |
+
{
|
541 |
+
"cell_type": "code",
|
542 |
+
"source": [
|
543 |
+
"API_URL = \"https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-ar\""
|
544 |
+
],
|
545 |
+
"metadata": {
|
546 |
+
"id": "Kq5cOSHJl2ML"
|
547 |
+
},
|
548 |
+
"execution_count": null,
|
549 |
+
"outputs": []
|
550 |
+
},
|
551 |
+
{
|
552 |
+
"cell_type": "code",
|
553 |
+
"source": [],
|
554 |
+
"metadata": {
|
555 |
+
"id": "UCE20OzxnrhL"
|
556 |
+
},
|
557 |
+
"execution_count": null,
|
558 |
+
"outputs": []
|
559 |
+
},
|
560 |
+
{
|
561 |
+
"cell_type": "markdown",
|
562 |
+
"source": [
|
563 |
+
"ara to eng"
|
564 |
+
],
|
565 |
+
"metadata": {
|
566 |
+
"id": "Tt4voITXns_8"
|
567 |
+
}
|
568 |
+
},
|
569 |
+
{
|
570 |
+
"cell_type": "code",
|
571 |
+
"source": [
|
572 |
+
"import requests\n",
|
573 |
+
"\n",
|
574 |
+
"\n",
|
575 |
+
"token_access = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n",
|
576 |
+
"headers = {\"Authorization\": f\"Bearer {token_access}\"}\n",
|
577 |
+
"API_URL = \"https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-ar-en\"\n",
|
578 |
+
"\n",
|
579 |
+
"def query(payload):\n",
|
580 |
+
"\tresponse = requests.post(API_URL, headers=headers, json=payload)\n",
|
581 |
+
"\treturn response.json()\n",
|
582 |
+
"\n",
|
583 |
+
"output = query({\n",
|
584 |
+
"\t\"inputs\": \"الشمس مشرقة وجميلة\",\n",
|
585 |
+
"})\n",
|
586 |
+
"print(output)"
|
587 |
+
],
|
588 |
+
"metadata": {
|
589 |
+
"id": "xNHCm034l2PK"
|
590 |
+
},
|
591 |
+
"execution_count": null,
|
592 |
+
"outputs": []
|
593 |
+
},
|
594 |
+
{
|
595 |
+
"cell_type": "code",
|
596 |
+
"source": [],
|
597 |
+
"metadata": {
|
598 |
+
"id": "4FfBIqgnl2SM"
|
599 |
+
},
|
600 |
+
"execution_count": null,
|
601 |
+
"outputs": []
|
602 |
+
},
|
603 |
+
{
|
604 |
+
"cell_type": "markdown",
|
605 |
+
"source": [
|
606 |
+
"en to ar"
|
607 |
+
],
|
608 |
+
"metadata": {
|
609 |
+
"id": "i0Y56yMKoLzD"
|
610 |
+
}
|
611 |
+
},
|
612 |
+
{
|
613 |
+
"cell_type": "code",
|
614 |
+
"source": [
|
615 |
+
"import requests\n",
|
616 |
+
"\n",
|
617 |
+
"\n",
|
618 |
+
"token_access = \"xxxxxxxxxxxxxxxxxxxxx\"\n",
|
619 |
+
"headers = {\"Authorization\": f\"Bearer {token_access}\"}\n",
|
620 |
+
"API_URL = \"https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-ar\"\n",
|
621 |
+
"\n",
|
622 |
+
"def query(payload):\n",
|
623 |
+
"\tresponse = requests.post(API_URL, headers=headers, json=payload)\n",
|
624 |
+
"\treturn response.json()\n",
|
625 |
+
"\n",
|
626 |
+
"output = query({\n",
|
627 |
+
"\t\"inputs\": \"good world\",\n",
|
628 |
+
"})\n",
|
629 |
+
"print(output)"
|
630 |
+
],
|
631 |
+
"metadata": {
|
632 |
+
"id": "oP4_ErDsn2pK"
|
633 |
+
},
|
634 |
+
"execution_count": null,
|
635 |
+
"outputs": []
|
636 |
+
},
|
637 |
+
{
|
638 |
+
"cell_type": "code",
|
639 |
+
"source": [
|
640 |
+
"import requests\n",
|
641 |
+
"\n",
|
642 |
+
"API_TOKEN = \"xxxxxxxxxxxxxxxxxxxxxxxxx\"\n",
|
643 |
+
"headers = {\"Authorization\": f\"Bearer {token_access}\"}"
|
644 |
+
],
|
645 |
+
"metadata": {
|
646 |
+
"id": "_oyzBbjisIzF"
|
647 |
+
},
|
648 |
+
"execution_count": null,
|
649 |
+
"outputs": []
|
650 |
+
},
|
651 |
+
{
|
652 |
+
"cell_type": "code",
|
653 |
+
"source": [
|
654 |
+
"import json\n",
|
655 |
+
"import requests\n",
|
656 |
+
"import time\n",
|
657 |
+
"\n",
|
658 |
+
"token_access = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n",
|
659 |
+
"headers = {\"Authorization\": f\"Bearer {token_access}\"}\n",
|
660 |
+
"\n",
|
661 |
+
"API_URL = \"https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-es\"\n",
|
662 |
+
"\n",
|
663 |
+
"def query(payload):\n",
|
664 |
+
"\n",
|
665 |
+
" data = json.dumps(payload)\n",
|
666 |
+
"\n",
|
667 |
+
" time.sleep(1)\n",
|
668 |
+
"\n",
|
669 |
+
" while True:\n",
|
670 |
+
"\n",
|
671 |
+
" try:\n",
|
672 |
+
"\n",
|
673 |
+
" response = requests.request(\"POST\", API_URL, headers=headers, data=data)\n",
|
674 |
+
" break\n",
|
675 |
+
"\n",
|
676 |
+
" except Exception:\n",
|
677 |
+
"\n",
|
678 |
+
" continue\n",
|
679 |
+
"\n",
|
680 |
+
" return json.loads(response.content.decode(\"utf-8\"))\n",
|
681 |
+
"\n",
|
682 |
+
"data = query(\n",
|
683 |
+
" {\n",
|
684 |
+
" \"inputs\": \"Hello I'm in Alexander's tutorial\",\n",
|
685 |
+
" }\n",
|
686 |
+
")\n",
|
687 |
+
"\n",
|
688 |
+
"print(data)\n"
|
689 |
+
],
|
690 |
+
"metadata": {
|
691 |
+
"id": "snxyeZE3l2VE"
|
692 |
+
},
|
693 |
+
"execution_count": null,
|
694 |
+
"outputs": []
|
695 |
+
}
|
696 |
+
]
|
697 |
+
}
|