{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "private_outputs": true, "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "source": [], "metadata": { "id": "ArOANWk0chIw" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "p-xhqHURchLv" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "## السطور ثابته" ], "metadata": { "id": "c9HVIB17pXwL" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Bbl112scce3k" }, "outputs": [], "source": [ "import requests\n", "\n", "API_TOKEN = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n", "headers = {\"Authorization\": f\"Bearer {token_access}\"}" ] }, { "cell_type": "code", "source": [], "metadata": { "id": "6wjPCSG0chO-" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "## مابين الاقواس ضع ماقبل التساوى" ], "metadata": { "id": "QxFoALn9p6eb" } }, { "cell_type": "code", "source": [ "data = query xxxxxxxxxxxxxx\n", "print(data)" ], "metadata": { "id": "n56U28k3pecM" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "IxvX-v6Ipefo" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "c-ERtDCRpejC" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import requests\n", "API_TOKEN = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n", "API_URL = \"https://api-inference.huggingface.co/models/openai-community/gpt2\"\n", "headers = {\"Authorization\": f\"Bearer {API_TOKEN}\"}\n", "\n", "def query(payload):\n", "\tresponse = requests.post(API_URL, headers=headers, json=payload)\n", "\treturn response.json()\n", "\n", "output = query({\n", "\t\"inputs\": \"Can you please let us know more details about your \",\n", "})\n", "print(data)" ], "metadata": { "id": "45_HIQeMchRv" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "fwrz9-ujfn6K" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "## gpt2" ], "metadata": { "id": "Fwg_UkgQo0DM" } }, { "cell_type": "code", "source": [ "import requests\n", "\n", "API_TOKEN = \"xxxxxxxxxxxxxxxxxxxxx\"\n", "API_URL = \"https://api-inference.huggingface.co/models/gpt2\"\n", "headers = {\"Authorization\": f\"Bearer {token_access}\"}\n", "\n", "def query(payload):\n", " response = requests.post(API_URL, headers=headers, json={\"inputs\": payload})\n", " return response.json()\n", "\n", "data = query(\"Can you please let us know more details about your \")\n", "print(data)\n", "\n" ], "metadata": { "id": "_JL9cZ8Efn9R" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "D-rp_2jRc13v" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "!ENDPOINT = https://api-inference.huggingface.co/models/" ], "metadata": { "id": "M5xb4b70c16Z" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "6ZGC66_8dDCn" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import requests\n", "\n", "API_TOKEN = \"xxxxxxxxxxxxxxxxxxxxxxxxxx\"\n", "headers = {\"Authorization\": f\"Bearer {token_access}\"}\n", "API_URL = \"https://api-inference.huggingface.co/models/deepset/roberta-base-squad2\"\n", "def query(payload):\n", " response = requests.post(API_URL, headers=headers, json=payload)\n", " return response.json()\n", "data = query(\n", " {\n", " \"inputs\": {\n", " \"question\": \"What's my name?\",\n", " \"context\": \"My name is Clara and I live in Berkeley.\",\n", " }\n", " }\n", ")\n", "print(data)" ], "metadata": { "id": "_Hp2T82FdDGv" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "GWHLt-mFg8aq" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "vkXj3J2og8dr" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import requests\n", "\n", "API_TOKEN = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n", "headers = {\"Authorization\": f\"Bearer {token_access}\"}\n", "API_URL = \"https://api-inference.huggingface.co/models/google/tapas-base-finetuned-wtq\"\n", "def query(payload):\n", " response = requests.post(API_URL, headers=headers, json=payload)\n", " return response.json()\n", "data = query(\n", " {\n", " \"inputs\": {\n", " \"query\": \"How many stars does the transformers repository have?\",\n", " \"table\": {\n", " \"Repository\": [\"Transformers\", \"Datasets\", \"Tokenizers\"],\n", " \"Stars\": [\"36542\", \"4512\", \"3934\"],\n", " \"Contributors\": [\"651\", \"77\", \"34\"],\n", " \"Programming language\": [\n", " \"Python\",\n", " \"Python\",\n", " \"Rust, Python and NodeJS\",\n", " ],\n", " },\n", " }\n", " }\n", ")\n", "print(data)" ], "metadata": { "id": "gLTGSz1Pdt1I" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import requests\n", "\n", "API_TOKEN = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n", "headers = {\"Authorization\": f\"Bearer {token_access}\"}\n", "API_URL = \"https://api-inference.huggingface.co/models/bert-base-uncased\"\n", "def query(payload):\n", " response = requests.post(API_URL, headers=headers, json=payload)\n", " return response.json()\n", "data = query({\"inputs\": \"The answer to the universe is [MASK].\"})\n", "print(data)" ], "metadata": { "id": "PLfVr4yUfI23" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "3RmnuvQVgY9w" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "wAgMVYvNgZAy" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "NsqFnUa7gZDo" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "!huggingface-cli login\n", "import requests\n", "def query(payload, model_id, api_token):\n", "\theaders = {\"Authorization\": f\"Bearer {api_token}\"}\n", "\tAPI_URL = f\"https://api-inference.huggingface.co/models/{model_id}\"\n", "\tresponse = requests.post(API_URL, headers=headers, json=payload)\n", "\treturn response.json()\n", "\n", "model_id = \"distilbert-base-uncased\"\n", "api_token = \"xxxxxxxxxxxxxxxxxxxxxxxxx\" # get yours at hf.co/settings/tokens\n", "data = query(\"The goal of life is [MASK].\", model_id, api_token)" ], "metadata": { "id": "GDJt8dM-ghL4" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "mDJ2Oo6Fg9uZ" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "91d0M9vrrruG" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import json\n", "import requests\n", "import time\n", "\n", "token_access = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n", "headers = {\"Authorization\": f\"Bearer {token_access}\"}\n", "\n", "API_URL = \"https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-es\"\n", "\n", "def query(payload):\n", "\n", " data = json.dumps(payload)\n", "\n", " time.sleep(1)\n", "\n", " while True:\n", "\n", " try:\n", "\n", " response = requests.request(\"POST\", API_URL, headers=headers, data=data)\n", " break\n", "\n", " except Exception:\n", "\n", " continue\n", "\n", " return json.loads(response.content.decode(\"utf-8\"))\n", "\n", "data = query(\n", " {\n", " \"inputs\": \"Hello I'm in Alexander's tutorial\",\n", " }\n", ")\n", "\n", "print(data)" ], "metadata": { "id": "vxQsCDuXi2xo" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "Ghdjipdwj-oL" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import json\n", "import requests\n", "import time\n", "\n", "token_access = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n", "headers = {\"Authorization\": f\"Bearer {token_access}\"}\n", "\n", "API_URL = \"https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-ar\"\n", "\n", "def query(payload):\n", "\n", " data = json.dumps(payload)\n", "\n", " time.sleep(1)\n", "\n", " while True:\n", "\n", " try:\n", "\n", " response = requests.request(\"POST\", API_URL, headers=headers, data=data)\n", " break\n", "\n", " except Exception:\n", "\n", " continue\n", "\n", " return json.loads(response.content.decode(\"utf-8\"))\n", "\n", "data = query(\n", " {\n", " \"inputs\": \"Hello I'm in Alexander's tutorial\",\n", " }\n", ")\n", "\n", "print(data)" ], "metadata": { "id": "-rokVqVVjUwA" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "lwWmpQGok1mx" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "noWjicykmQiS" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# كود ترجمة ناجح" ], "metadata": { "id": "J8mJxZ4CmRA7" } }, { "cell_type": "code", "source": [ "import requests\n", "token_access = \"xxxxxxxxxxxx\"\n", "headers = {\"Authorization\": f\"Bearer {token_access}\"}\n", "\n", "def query(payload):\n", "\tresponse = requests.post(API_URL, headers=headers, json=payload)\n", "\treturn response.json()\n", "\n", "output = query({\n", "\t\"inputs\": \"how are you\",\n", "})\n", "print(output)" ], "metadata": { "id": "ewy25jAGl_69" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import requests\n", "\n", "\n", "token_access = \"xxxxxxxxxxxxxxxxxxxxxxxxxx\"\n", "headers = {\"Authorization\": f\"Bearer {token_access}\"}\n", "API_URL = \"https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-es\"\n", "\n", "def query(payload):\n", "\tresponse = requests.post(API_URL, headers=headers, json=payload)\n", "\treturn response.json()\n", "\n", "output = query({\n", "\t\"inputs\": \"good name\",\n", "})\n", "print(output)" ], "metadata": { "id": "Jc1hjA_PlAkA" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "API_URL = \"https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-ar\"" ], "metadata": { "id": "Kq5cOSHJl2ML" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "UCE20OzxnrhL" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "ara to eng" ], "metadata": { "id": "Tt4voITXns_8" } }, { "cell_type": "code", "source": [ "import requests\n", "\n", "\n", "token_access = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n", "headers = {\"Authorization\": f\"Bearer {token_access}\"}\n", "API_URL = \"https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-ar-en\"\n", "\n", "def query(payload):\n", "\tresponse = requests.post(API_URL, headers=headers, json=payload)\n", "\treturn response.json()\n", "\n", "output = query({\n", "\t\"inputs\": \"الشمس مشرقة وجميلة\",\n", "})\n", "print(output)" ], "metadata": { "id": "xNHCm034l2PK" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "4FfBIqgnl2SM" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "en to ar" ], "metadata": { "id": "i0Y56yMKoLzD" } }, { "cell_type": "code", "source": [ "import requests\n", "\n", "\n", "token_access = \"xxxxxxxxxxxxxxxxxxxxx\"\n", "headers = {\"Authorization\": f\"Bearer {token_access}\"}\n", "API_URL = \"https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-ar\"\n", "\n", "def query(payload):\n", "\tresponse = requests.post(API_URL, headers=headers, json=payload)\n", "\treturn response.json()\n", "\n", "output = query({\n", "\t\"inputs\": \"good world\",\n", "})\n", "print(output)" ], "metadata": { "id": "oP4_ErDsn2pK" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import requests\n", "\n", "API_TOKEN = \"xxxxxxxxxxxxxxxxxxxxxxxxx\"\n", "headers = {\"Authorization\": f\"Bearer {token_access}\"}" ], "metadata": { "id": "_oyzBbjisIzF" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import json\n", "import requests\n", "import time\n", "\n", "token_access = \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n", "headers = {\"Authorization\": f\"Bearer {token_access}\"}\n", "\n", "API_URL = \"https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-es\"\n", "\n", "def query(payload):\n", "\n", " data = json.dumps(payload)\n", "\n", " time.sleep(1)\n", "\n", " while True:\n", "\n", " try:\n", "\n", " response = requests.request(\"POST\", API_URL, headers=headers, data=data)\n", " break\n", "\n", " except Exception:\n", "\n", " continue\n", "\n", " return json.loads(response.content.decode(\"utf-8\"))\n", "\n", "data = query(\n", " {\n", " \"inputs\": \"Hello I'm in Alexander's tutorial\",\n", " }\n", ")\n", "\n", "print(data)\n" ], "metadata": { "id": "snxyeZE3l2VE" }, "execution_count": null, "outputs": [] } ] }