Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Hugging face - Few Shot Learning with Inference API #2364

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 27 additions & 194 deletions Hugging Face/Hugging_Face_Few_Shot_Learning_with_Inference_API.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand All @@ -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": []
},
Expand All @@ -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"
]
},
{
Expand All @@ -184,35 +161,32 @@
"### 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 <a href=\"https://huggingface.co/EleutherAI/gpt-neo-1.3B\">gpt-neo-1.3B</a> model for our demonstration. "
]
},
{
"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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srsawant34 Can you justify the reason for choosing the model over others? Anything specific that makes it unique as such. Add thus in the markdown above the cell. And will be good to go.

"API_TOKEN = \"<INSERT_API_TOKEN>\""
"API_TOKEN = naas.secret.get(\"GPT_INFERENCE\")"
]
},
{
Expand All @@ -239,24 +213,17 @@
},
{
"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": []
},
"outputs": [],
"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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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": [],
Expand All @@ -526,79 +413,25 @@
},
{
"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)"
]
},
{
"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)"
Expand Down
Loading