From 3461061ce8218b3f14d52672fbc4cc779df5fe55 Mon Sep 17 00:00:00 2001 From: Saurabh Sawant Date: Wed, 8 Nov 2023 21:29:22 +0100 Subject: [PATCH 1/5] feat: implementing hugging face few shot learning --- ...Few_Shot_Learning_with_Inference_API.ipynb | 909 ++++++++++++++++++ 1 file changed, 909 insertions(+) create mode 100644 Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb diff --git a/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb b/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb new file mode 100644 index 0000000000..19be2e65e1 --- /dev/null +++ b/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb @@ -0,0 +1,909 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "bedfa998", + "metadata": { + "id": "G8G4XdnudkS6", + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Hugging" + ] + }, + { + "cell_type": "markdown", + "id": "adff8007-4a9b-425f-a5f1-892655a5de9b", + "metadata": { + "id": "cziuXC9hduys", + "papermill": {}, + "tags": [] + }, + "source": [ + "# Hugging Face - Few Shot Learning with Inference API\n", + "

Give Feedback | Bug report" + ] + }, + { + "cell_type": "markdown", + "id": "9776f972", + "metadata": { + "id": "CLPRc-MJd982", + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #huggingface #ml #few_shot_learning #prompt #inference_api #ai #text" + ] + }, + { + "cell_type": "markdown", + "id": "36ddb4e8", + "metadata": { + "id": "9TxAcynceEXm", + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Saurabh Arjun Sawant](https://www.linkedin.com/in/srsawant34/)" + ] + }, + { + "cell_type": "markdown", + "id": "d8b2e9ee-568e-43bd-a598-f55404b06ab6", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2023-11-08 (Created: 2023-11-08)" + ] + }, + { + "cell_type": "markdown", + "id": "naas-description", + "metadata": { + "papermill": {}, + "tags": [ + "description" + ] + }, + "source": [ + "**Description:** This notebook demonstrates how to utilize the inference endpoints of hugging face models. Additionally, it demonstrates how to use few shot learning for a specific task in a model." + ] + }, + { + "cell_type": "markdown", + "id": "df7f0816", + "metadata": { + "id": "I5qfxShzjrJK", + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "e4b53a36", + "metadata": { + "execution": { + "iopub.execute_input": "2022-11-02T22:06:30.070094Z", + "iopub.status.busy": "2022-11-02T22:06:30.069794Z", + "iopub.status.idle": "2022-11-02T22:06:30.072975Z", + "shell.execute_reply": "2022-11-02T22:06:30.072331Z", + "shell.execute_reply.started": "2022-11-02T22:06:30.070062Z" + }, + "papermill": {}, + "tags": [] + }, + "source": [ + "### Install Packages" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "72d1eac5", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "execution": { + "iopub.execute_input": "2023-11-08T18:45:00.863454Z", + "iopub.status.busy": "2023-11-08T18:45:00.863215Z", + "iopub.status.idle": "2023-11-08T18:45:04.806790Z", + "shell.execute_reply": "2023-11-08T18:45:04.806055Z", + "shell.execute_reply.started": "2023-11-08T18:45:00.863430Z" + }, + "id": "hWQbKgcncvLE", + "outputId": "218bfc13-27fd-4331-b3c3-304c7986a0b1", + "papermill": {}, + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33mWARNING: You are using pip version 22.0.3; however, version 23.3.1 is available.\n", + "You should consider upgrading via the '/opt/conda/bin/python3 -m pip install --upgrade pip' command.\u001b[0m\u001b[33m\n", + "\u001b[0m" + ] + } + ], + "source": [ + "!pip install -q datasets" + ] + }, + { + "cell_type": "markdown", + "id": "e3f7133c", + "metadata": { + "id": "qGxt0Sy1lQlL", + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import Libraries\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "99f89fd4", + "metadata": { + "execution": { + "iopub.execute_input": "2023-11-08T18:45:07.526596Z", + "iopub.status.busy": "2023-11-08T18:45:07.526358Z", + "iopub.status.idle": "2023-11-08T18:45:08.262790Z", + "shell.execute_reply": "2023-11-08T18:45:08.262065Z", + "shell.execute_reply.started": "2023-11-08T18:45:07.526571Z" + }, + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "import numpy as np\n", + "import requests\n", + "import json" + ] + }, + { + "cell_type": "markdown", + "id": "63d3834b", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Add the Model and API token\n", + "\n", + "We will use gpt-neo-1.3B model for our demonstration. " + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "5d02938f", + "metadata": { + "execution": { + "iopub.execute_input": "2023-11-08T19:11:34.006773Z", + "iopub.status.busy": "2023-11-08T19:11:34.006521Z", + "iopub.status.idle": "2023-11-08T19:11:34.010795Z", + "shell.execute_reply": "2023-11-08T19:11:34.010166Z", + "shell.execute_reply.started": "2023-11-08T19:11:34.006746Z" + }, + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "MODEL = \"EleutherAI/gpt-neo-1.3B\"\n", + "API_TOKEN = \"\"" + ] + }, + { + "cell_type": "markdown", + "id": "ec280229", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "38e868fc", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Define function to make API calls to Hugging Face endpoints" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "37c02550", + "metadata": { + "execution": { + "iopub.execute_input": "2023-11-08T18:47:21.815058Z", + "iopub.status.busy": "2023-11-08T18:47:21.814813Z", + "iopub.status.idle": "2023-11-08T18:47:21.821996Z", + "shell.execute_reply": "2023-11-08T18:47:21.821243Z", + "shell.execute_reply.started": "2023-11-08T18:47:21.815034Z" + }, + "papermill": {}, + "tags": [] + }, + "outputs": [], + "source": [ + "def query(\n", + " payload='', \n", + " model = 'EleutherAI/gpt-neo-1.3B', \n", + " parameters = {\n", + " 'max_new_tokens':5,\n", + " 'temperature': 0.5\n", + " }, \n", + " options = {\n", + " 'use_cache': False\n", + " }\n", + " ):\n", + " API_URL = f\"https://api-inference.huggingface.co/models/{model}\"\n", + " headers = {\"Authorization\": f\"Bearer {API_TOKEN}\"}\n", + " body = {\"inputs\":payload,'parameters':parameters,'options':options}\n", + " \n", + " try:\n", + " response = requests.request(\"POST\", API_URL, headers=headers, data= json.dumps(body))\n", + " return response.json()[0]['generated_text']\n", + " except:\n", + " return \"Error: \" + \" \".join(response.json()['error'])" + ] + }, + { + "cell_type": "markdown", + "id": "378de1e9", + "metadata": { + "execution": { + "iopub.execute_input": "2022-11-02T22:04:21.973105Z", + "iopub.status.busy": "2022-11-02T22:04:21.972816Z", + "iopub.status.idle": "2022-11-02T22:04:21.980111Z", + "shell.execute_reply": "2022-11-02T22:04:21.977454Z", + "shell.execute_reply.started": "2022-11-02T22:04:21.973076Z" + }, + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "f0724801-389c-4184-b3a1-a3491573e24e", + "metadata": {}, + "source": [ + " The model usually takes time to load in the hugging face server. For example, model gpt-neo-1.3B takes approximately 212 seconds" + ] + }, + { + "cell_type": "markdown", + "id": "fba0a8f9", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Zero-shot" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "acc20e20", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "execution": { + "iopub.execute_input": "2023-11-08T18:51:14.968874Z", + "iopub.status.busy": "2023-11-08T18:51:14.968639Z", + "iopub.status.idle": "2023-11-08T18:51:16.537316Z", + "shell.execute_reply": "2023-11-08T18:51:16.536640Z", + "shell.execute_reply.started": "2023-11-08T18:51:14.968851Z" + }, + "id": "9yETPBqgoWpO", + "outputId": "062820d9-a750-45ea-b263-ec617df962d6", + "papermill": {}, + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Sentence: I loved todays movie.\n", + "Sentiment: \n", + "\n", + "A:\n", + "\n" + ] + } + ], + "source": [ + "prompt = \"\"\"\n", + "Sentence: I loved todays movie.\n", + "Sentiment: \"\"\"\n", + "\n", + "response = query(payload=prompt, model=MODEL)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "id": "9f24f731", + "metadata": { + "id": "zaufheoZo4mf", + "papermill": {}, + "tags": [] + }, + "source": [ + "### One-shot" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "45e3ccf1", + "metadata": { + "execution": { + "iopub.execute_input": "2023-11-08T18:52:00.192577Z", + "iopub.status.busy": "2023-11-08T18:52:00.192341Z", + "iopub.status.idle": "2023-11-08T18:52:01.729565Z", + "shell.execute_reply": "2023-11-08T18:52:01.728932Z", + "shell.execute_reply.started": "2023-11-08T18:52:00.192552Z" + }, + "papermill": {}, + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Sentence: I loved todays movie.\n", + "Sentiment: positive\n", + "\n", + "#####\n", + "\n", + "Sentence: I didn't like the action.\n", + "Sentiment: negative\n", + "\n", + "#####\n" + ] + } + ], + "source": [ + "prompt = \"\"\"\n", + "Sentence: I loved todays movie.\n", + "Sentiment: positive\n", + "\n", + "#####\n", + "\n", + "Sentence: I didn't like the action.\n", + "Sentiment: \"\"\"\n", + "\n", + "response = query(payload=prompt, model=MODEL)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "id": "96831b89-e92f-4ddb-8703-0124c26c8613", + "metadata": {}, + "source": [ + "### Two-shot" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "166b6be0-c30a-4f70-93c4-b993558e741f", + "metadata": { + "execution": { + "iopub.execute_input": "2023-11-08T18:52:51.711071Z", + "iopub.status.busy": "2023-11-08T18:52:51.710766Z", + "iopub.status.idle": "2023-11-08T18:52:53.495876Z", + "shell.execute_reply": "2023-11-08T18:52:53.495096Z", + "shell.execute_reply.started": "2023-11-08T18:52:51.711037Z" + }, + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Sentence: I loved todays movie.\n", + "Sentiment: positive\n", + "\n", + "#####\n", + "\n", + "Sentence: I didn't like the action.\n", + "Sentiment: negative\n", + "\n", + "#####\n", + "\n", + "Sentence: Liked the direction and scene settings.\n", + "Sentiment: positive\n", + "\n", + "#####\n" + ] + } + ], + "source": [ + "prompt = \"\"\"\n", + "Sentence: I loved todays movie.\n", + "Sentiment: positive\n", + "\n", + "#####\n", + "\n", + "Sentence: I didn't like the action.\n", + "Sentiment: negative\n", + "\n", + "#####\n", + "\n", + "Sentence: Liked the direction and scene settings.\n", + "Sentiment: \"\"\"\n", + "\n", + "response = query(payload=prompt, model=MODEL)\n", + "print(response)" + ] + }, + { + "cell_type": "markdown", + "id": "af89831a-ccd8-4e78-b140-f69a94c6af12", + "metadata": {}, + "source": [ + "### Few-shot learning with custom dataset\n", + "\n", + "You can also use any custom dataset and generate prompts like above. For example, below we will use twitter-sentiment-analysis. More datasets in huggingface can be found here" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "835a322b-3967-42f3-a77d-720d1308a998", + "metadata": { + "execution": { + "iopub.execute_input": "2023-11-08T18:54:45.305804Z", + "iopub.status.busy": "2023-11-08T18:54:45.305495Z", + "iopub.status.idle": "2023-11-08T18:55:10.645700Z", + "shell.execute_reply": "2023-11-08T18:55:10.645085Z", + "shell.execute_reply.started": "2023-11-08T18:54:45.305773Z" + } + }, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "06595a18e15a4366b66b302f78c67a90", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Downloading builder script: 0%| | 0.00/4.38k [00:00 Date: Wed, 8 Nov 2023 21:31:20 +0100 Subject: [PATCH 2/5] feat: implementing hugging face few shot learning --- ...Few_Shot_Learning_with_Inference_API.ipynb | 308 ++---------------- 1 file changed, 21 insertions(+), 287 deletions(-) diff --git a/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb b/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb index 19be2e65e1..aa725e0723 100644 --- a/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb +++ b/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb @@ -105,18 +105,18 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "id": "72d1eac5", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "execution": { - "iopub.execute_input": "2023-11-08T18:45:00.863454Z", - "iopub.status.busy": "2023-11-08T18:45:00.863215Z", - "iopub.status.idle": "2023-11-08T18:45:04.806790Z", - "shell.execute_reply": "2023-11-08T18:45:04.806055Z", - "shell.execute_reply.started": "2023-11-08T18:45:00.863430Z" + "iopub.execute_input": "2023-11-08T20:29:52.554694Z", + "iopub.status.busy": "2023-11-08T20:29:52.554371Z", + "iopub.status.idle": "2023-11-08T20:30:00.241274Z", + "shell.execute_reply": "2023-11-08T20:30:00.240562Z", + "shell.execute_reply.started": "2023-11-08T20:29:52.554658Z" }, "id": "hWQbKgcncvLE", "outputId": "218bfc13-27fd-4331-b3c3-304c7986a0b1", @@ -152,15 +152,15 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "id": "99f89fd4", "metadata": { "execution": { - "iopub.execute_input": "2023-11-08T18:45:07.526596Z", - "iopub.status.busy": "2023-11-08T18:45:07.526358Z", - "iopub.status.idle": "2023-11-08T18:45:08.262790Z", - "shell.execute_reply": "2023-11-08T18:45:08.262065Z", - "shell.execute_reply.started": "2023-11-08T18:45:07.526571Z" + "iopub.execute_input": "2023-11-08T20:30:00.242902Z", + "iopub.status.busy": "2023-11-08T20:30:00.242658Z", + "iopub.status.idle": "2023-11-08T20:30:01.113570Z", + "shell.execute_reply": "2023-11-08T20:30:01.112970Z", + "shell.execute_reply.started": "2023-11-08T20:30:00.242869Z" }, "papermill": {}, "tags": [] @@ -483,285 +483,19 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 4, "id": "835a322b-3967-42f3-a77d-720d1308a998", "metadata": { "execution": { - "iopub.execute_input": "2023-11-08T18:54:45.305804Z", - "iopub.status.busy": "2023-11-08T18:54:45.305495Z", - "iopub.status.idle": "2023-11-08T18:55:10.645700Z", - "shell.execute_reply": "2023-11-08T18:55:10.645085Z", - "shell.execute_reply.started": "2023-11-08T18:54:45.305773Z" - } - }, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "06595a18e15a4366b66b302f78c67a90", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Downloading builder script: 0%| | 0.00/4.38k [00:00 Date: Mon, 13 Nov 2023 08:00:21 +0100 Subject: [PATCH 3/5] implementing hugging face few shot inference --- ...Few_Shot_Learning_with_Inference_API.ipynb | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb b/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb index aa725e0723..1348c34f53 100644 --- a/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb +++ b/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb @@ -70,7 +70,7 @@ ] }, "source": [ - "**Description:** This notebook demonstrates how to utilize the inference endpoints of hugging face models. Additionally, it demonstrates how to use few shot learning for a specific task in a model." + "**Description:** This notebook demonstrates how to utilize the inference endpoints (additional information can be found here: link) of hugging face models. Additionally, it demonstrates how to use few shot learning for a specific task in a model." ] }, { @@ -183,6 +183,14 @@ "source": [ "### Add the Model and API token\n", "\n", + "#### Steps to get API token\n", + "- Create an account on Hugging Face\n", + "- Log in, and click on profile icon (top right corner)\n", + "- Go to settings\n", + "- Click on Access tokens\n", + "- Now, create a new access token with name: \"gpt-inference\" and role: \"read\"\n", + "- Copy the generated token and paste it below\n", + "\n", "We will use gpt-neo-1.3B model for our demonstration. " ] }, @@ -291,7 +299,7 @@ "id": "f0724801-389c-4184-b3a1-a3491573e24e", "metadata": {}, "source": [ - " The model usually takes time to load in the hugging face server. For example, model gpt-neo-1.3B takes approximately 212 seconds" + ">The model usually takes time to load in the hugging face server. For example, model gpt-neo-1.3B takes approximately 212 seconds" ] }, { @@ -302,7 +310,9 @@ "tags": [] }, "source": [ - "### Zero-shot" + "### Zero-shot\n", + "\n", + "Zero-shot learning means to generate meaningful responses from model for tasks or topics it has never been explicitly trained on, showcasing a capacity to generalize and understand novel concepts without specific examples during training." ] }, { @@ -357,7 +367,9 @@ "tags": [] }, "source": [ - "### One-shot" + "### One-shot\n", + "\n", + "One-shot learning refers to the model's ability to understand and generate meaningful responses after being exposed to a single example or prompt during the inference phase, showcasing its capacity to generalize knowledge from limited input." ] }, { @@ -412,7 +424,9 @@ "id": "96831b89-e92f-4ddb-8703-0124c26c8613", "metadata": {}, "source": [ - "### Two-shot" + "### Two-shot\n", + "\n", + "Similar to one-shot, we will have the model exposed to two examples to generalize knowledge and make predictions." ] }, { From 4e57f0051143e54b55543c4302c5e6cb084d1557 Mon Sep 17 00:00:00 2001 From: Sriniketh J <81156510+srini047@users.noreply.github.com> Date: Tue, 14 Nov 2023 22:41:16 +0530 Subject: [PATCH 4/5] fix: minor bugs --- ...Few_Shot_Learning_with_Inference_API.ipynb | 221 +++--------------- 1 file changed, 27 insertions(+), 194 deletions(-) diff --git a/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb b/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb index 1348c34f53..8b2f32ac46 100644 --- a/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb +++ b/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb @@ -105,35 +105,18 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "72d1eac5", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, - "execution": { - "iopub.execute_input": "2023-11-08T20:29:52.554694Z", - "iopub.status.busy": "2023-11-08T20:29:52.554371Z", - "iopub.status.idle": "2023-11-08T20:30:00.241274Z", - "shell.execute_reply": "2023-11-08T20:30:00.240562Z", - "shell.execute_reply.started": "2023-11-08T20:29:52.554658Z" - }, "id": "hWQbKgcncvLE", "outputId": "218bfc13-27fd-4331-b3c3-304c7986a0b1", "papermill": {}, "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[33mWARNING: You are using pip version 22.0.3; however, version 23.3.1 is available.\n", - "You should consider upgrading via the '/opt/conda/bin/python3 -m pip install --upgrade pip' command.\u001b[0m\u001b[33m\n", - "\u001b[0m" - ] - } - ], + "outputs": [], "source": [ "!pip install -q datasets" ] @@ -152,16 +135,9 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "99f89fd4", "metadata": { - "execution": { - "iopub.execute_input": "2023-11-08T20:30:00.242902Z", - "iopub.status.busy": "2023-11-08T20:30:00.242658Z", - "iopub.status.idle": "2023-11-08T20:30:01.113570Z", - "shell.execute_reply": "2023-11-08T20:30:01.112970Z", - "shell.execute_reply.started": "2023-11-08T20:30:00.242869Z" - }, "papermill": {}, "tags": [] }, @@ -170,7 +146,8 @@ "from datasets import load_dataset\n", "import numpy as np\n", "import requests\n", - "import json" + "import json\n", + "import naas" ] }, { @@ -184,11 +161,11 @@ "### Add the Model and API token\n", "\n", "#### Steps to get API token\n", - "- Create an account on Hugging Face\n", + "- Create an account on [Hugging Face](https://huggingface.co)\n", "- Log in, and click on profile icon (top right corner)\n", "- Go to settings\n", - "- Click on Access tokens\n", - "- Now, create a new access token with name: \"gpt-inference\" and role: \"read\"\n", + "- Click on [Access tokens](https://huggingface.co/settings/tokens)\n", + "- Now, create a new access token with name: `GPT_INFERENCE` and role: `read`\n", "- Copy the generated token and paste it below\n", "\n", "We will use gpt-neo-1.3B model for our demonstration. " @@ -196,23 +173,20 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "id": "5d02938f", "metadata": { - "execution": { - "iopub.execute_input": "2023-11-08T19:11:34.006773Z", - "iopub.status.busy": "2023-11-08T19:11:34.006521Z", - "iopub.status.idle": "2023-11-08T19:11:34.010795Z", - "shell.execute_reply": "2023-11-08T19:11:34.010166Z", - "shell.execute_reply.started": "2023-11-08T19:11:34.006746Z" - }, "papermill": {}, "tags": [] }, "outputs": [], "source": [ + "# Uncomment to store the environment variable in Naas cloud\n", + "# naas.secret.add(\"GPT_INFERENCE\", \"Paste_token_here\")\n", + "\n", + "# Set the environment variables\n", "MODEL = \"EleutherAI/gpt-neo-1.3B\"\n", - "API_TOKEN = \"\"" + "API_TOKEN = naas.secret.get(\"GPT_INFERENCE\")" ] }, { @@ -239,16 +213,9 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "37c02550", "metadata": { - "execution": { - "iopub.execute_input": "2023-11-08T18:47:21.815058Z", - "iopub.status.busy": "2023-11-08T18:47:21.814813Z", - "iopub.status.idle": "2023-11-08T18:47:21.821996Z", - "shell.execute_reply": "2023-11-08T18:47:21.821243Z", - "shell.execute_reply.started": "2023-11-08T18:47:21.815034Z" - }, "papermill": {}, "tags": [] }, @@ -256,7 +223,7 @@ "source": [ "def query(\n", " payload='', \n", - " model = 'EleutherAI/gpt-neo-1.3B', \n", + " model = MODEL,\n", " parameters = {\n", " 'max_new_tokens':5,\n", " 'temperature': 0.5\n", @@ -317,38 +284,18 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "acc20e20", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, - "execution": { - "iopub.execute_input": "2023-11-08T18:51:14.968874Z", - "iopub.status.busy": "2023-11-08T18:51:14.968639Z", - "iopub.status.idle": "2023-11-08T18:51:16.537316Z", - "shell.execute_reply": "2023-11-08T18:51:16.536640Z", - "shell.execute_reply.started": "2023-11-08T18:51:14.968851Z" - }, "id": "9yETPBqgoWpO", "outputId": "062820d9-a750-45ea-b263-ec617df962d6", "papermill": {}, "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "Sentence: I loved todays movie.\n", - "Sentiment: \n", - "\n", - "A:\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "prompt = \"\"\"\n", "Sentence: I loved todays movie.\n", @@ -374,37 +321,13 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "45e3ccf1", "metadata": { - "execution": { - "iopub.execute_input": "2023-11-08T18:52:00.192577Z", - "iopub.status.busy": "2023-11-08T18:52:00.192341Z", - "iopub.status.idle": "2023-11-08T18:52:01.729565Z", - "shell.execute_reply": "2023-11-08T18:52:01.728932Z", - "shell.execute_reply.started": "2023-11-08T18:52:00.192552Z" - }, "papermill": {}, "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "Sentence: I loved todays movie.\n", - "Sentiment: positive\n", - "\n", - "#####\n", - "\n", - "Sentence: I didn't like the action.\n", - "Sentiment: negative\n", - "\n", - "#####\n" - ] - } - ], + "outputs": [], "source": [ "prompt = \"\"\"\n", "Sentence: I loved todays movie.\n", @@ -431,41 +354,12 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "id": "166b6be0-c30a-4f70-93c4-b993558e741f", "metadata": { - "execution": { - "iopub.execute_input": "2023-11-08T18:52:51.711071Z", - "iopub.status.busy": "2023-11-08T18:52:51.710766Z", - "iopub.status.idle": "2023-11-08T18:52:53.495876Z", - "shell.execute_reply": "2023-11-08T18:52:53.495096Z", - "shell.execute_reply.started": "2023-11-08T18:52:51.711037Z" - }, "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "Sentence: I loved todays movie.\n", - "Sentiment: positive\n", - "\n", - "#####\n", - "\n", - "Sentence: I didn't like the action.\n", - "Sentiment: negative\n", - "\n", - "#####\n", - "\n", - "Sentence: Liked the direction and scene settings.\n", - "Sentiment: positive\n", - "\n", - "#####\n" - ] - } - ], + "outputs": [], "source": [ "prompt = \"\"\"\n", "Sentence: I loved todays movie.\n", @@ -497,16 +391,9 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "835a322b-3967-42f3-a77d-720d1308a998", "metadata": { - "execution": { - "iopub.execute_input": "2023-11-08T20:30:17.284487Z", - "iopub.status.busy": "2023-11-08T20:30:17.284259Z", - "iopub.status.idle": "2023-11-08T20:30:18.444948Z", - "shell.execute_reply": "2023-11-08T20:30:18.444377Z", - "shell.execute_reply.started": "2023-11-08T20:30:17.284463Z" - }, "tags": [] }, "outputs": [], @@ -526,38 +413,12 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "id": "a4156dc7-b4ce-482b-91bc-759097782b6c", "metadata": { - "execution": { - "iopub.execute_input": "2023-11-08T18:58:08.522236Z", - "iopub.status.busy": "2023-11-08T18:58:08.521989Z", - "iopub.status.idle": "2023-11-08T18:58:11.522555Z", - "shell.execute_reply": "2023-11-08T18:58:11.521823Z", - "shell.execute_reply.started": "2023-11-08T18:58:08.522211Z" - }, "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Sentence: wow! I have so much homework for tomorrow!\n", - "Sentiment: negative\n", - "\n", - "#####\n", - "\n", - "Sentence: @thepete I know. I hate that/those shows. (Actually there's one I do get addicted to - X-Factor) But I hate it too!\n", - "Sentiment: positive\n", - "\n", - "#####\n", - "\n", - "Sentence: @cakesandbakes Ohh nooo! We're in America! Lol spoilt little brat aren't I?\n", - "Sentiment: \n" - ] - } - ], + "outputs": [], "source": [ "prompt = generate_prompt_with_examples(data=data['train'], target_col=\"feeling\", num_of_examples=2)\n", "print(prompt)" @@ -565,40 +426,12 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": null, "id": "b1e80171-fb45-495f-99fa-dbb801cec7af", "metadata": { - "execution": { - "iopub.execute_input": "2023-11-08T18:58:35.364455Z", - "iopub.status.busy": "2023-11-08T18:58:35.364202Z", - "iopub.status.idle": "2023-11-08T18:58:37.323862Z", - "shell.execute_reply": "2023-11-08T18:58:37.323135Z", - "shell.execute_reply.started": "2023-11-08T18:58:35.364427Z" - }, "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Sentence: wow! I have so much homework for tomorrow!\n", - "Sentiment: negative\n", - "\n", - "#####\n", - "\n", - "Sentence: @thepete I know. I hate that/those shows. (Actually there's one I do get addicted to - X-Factor) But I hate it too!\n", - "Sentiment: positive\n", - "\n", - "#####\n", - "\n", - "Sentence: @cakesandbakes Ohh nooo! We're in America! Lol spoilt little brat aren't I?\n", - "Sentiment: positive\n", - "\n", - "#####\n" - ] - } - ], + "outputs": [], "source": [ "response = query(payload=prompt, model=MODEL)\n", "print(response)" From 19a084703bf662810aee64acbb83ed9e17fc0f88 Mon Sep 17 00:00:00 2001 From: Saurabh Sawant Date: Thu, 16 Nov 2023 07:47:07 +0100 Subject: [PATCH 5/5] Implement Hugging Face few shot inference --- .../Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb b/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb index 8b2f32ac46..a109756793 100644 --- a/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb +++ b/Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb @@ -168,7 +168,8 @@ "- Now, create a new access token with name: `GPT_INFERENCE` and role: `read`\n", "- Copy the generated token and paste it below\n", "\n", - "We will use gpt-neo-1.3B model for our demonstration. " + "We use GPT based models since they excel in few-shot learning due to their ability to generate coherent and contextually relevant responses based on limited examples, capturing relationships in data more effectively than many other large language models.\n", + "In this demonstration, we will utilize the gpt-neo-1.3B model; additional GPT-based models can be explored here. Developed by EleutherAI, GPT⁠-⁠Neo is a series of transformer-based language models built on the GPT architecture. EleutherAI aims to create a model of GPT⁠-⁠3's scale and provide open access." ] }, { @@ -386,7 +387,7 @@ "source": [ "### Few-shot learning with custom dataset\n", "\n", - "You can also use any custom dataset and generate prompts like above. For example, below we will use twitter-sentiment-analysis. More datasets in huggingface can be found here" + "You can also use any custom dataset and generate prompts like above. For example, below we will use twitter-sentiment-analysis. More datasets in huggingface can be found here." ] }, {