forked from PradipNichite/Youtube-Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eef4e71
commit 18a873c
Showing
1 changed file
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"colab": { | ||
"provenance": [], | ||
"collapsed_sections": [], | ||
"authorship_tag": "ABX9TyM+cc5cEWHoYFLe1dezcv7b", | ||
"include_colab_link": true | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "view-in-github", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"<a href=\"https://colab.research.google.com/github/PradipNichite/Youtube-Tutorials/blob/main/Youtube_Huggingface_Inference_API.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "hEtwUU9bQBk8", | ||
"outputId": "a90f34bc-3be5-4384-e194-29269dcc38dc" | ||
}, | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"[[{'label': 'LABEL_0', 'score': 0.002866477705538273},\n", | ||
" {'label': 'LABEL_1', 'score': 0.018881773576140404},\n", | ||
" {'label': 'LABEL_2', 'score': 0.9782518148422241}]]" | ||
] | ||
}, | ||
"metadata": {}, | ||
"execution_count": 1 | ||
} | ||
], | ||
"source": [ | ||
"import requests\n", | ||
"\n", | ||
"API_URL = \"https://api-inference.huggingface.co/models/cardiffnlp/twitter-roberta-base-sentiment\"\n", | ||
"headers = {\"Authorization\": \"Bearer hf_EriWcQYZIQNwIbfeXGfJslkA\"}\n", | ||
"\n", | ||
"def query(payload):\n", | ||
"\tresponse = requests.post(API_URL, headers=headers, json=payload)\n", | ||
"\treturn response.json()\n", | ||
"\t\n", | ||
"output = query({\n", | ||
"\t\"inputs\": \"I like you. I love you\",\n", | ||
"})\n", | ||
"output" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"output[0]" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "ghCkb5hqQIE4", | ||
"outputId": "5759f587-c38c-440f-9104-e5b3fae8fdfd" | ||
}, | ||
"execution_count": 2, | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"[{'label': 'LABEL_0', 'score': 0.002866477705538273},\n", | ||
" {'label': 'LABEL_1', 'score': 0.018881773576140404},\n", | ||
" {'label': 'LABEL_2', 'score': 0.9782518148422241}]" | ||
] | ||
}, | ||
"metadata": {}, | ||
"execution_count": 2 | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"sorted_output = sorted(output[0], key=lambda d: d['score'],reverse=True) \n", | ||
"sorted_output" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "_HJq0eZ-QOFX", | ||
"outputId": "8d93b040-1c25-4046-b1cf-20e8a0e41fb2" | ||
}, | ||
"execution_count": 3, | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"[{'label': 'LABEL_2', 'score': 0.9782518148422241},\n", | ||
" {'label': 'LABEL_1', 'score': 0.018881773576140404},\n", | ||
" {'label': 'LABEL_0', 'score': 0.002866477705538273}]" | ||
] | ||
}, | ||
"metadata": {}, | ||
"execution_count": 3 | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"prediction = sorted_output[0]\n", | ||
"prediction" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "GxO7pU4qRVvo", | ||
"outputId": "75a0837e-f6d5-4303-c4f5-80fd91fce837" | ||
}, | ||
"execution_count": 4, | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"{'label': 'LABEL_2', 'score': 0.9782518148422241}" | ||
] | ||
}, | ||
"metadata": {}, | ||
"execution_count": 4 | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"label_mapping = {\n", | ||
" 'LABEL_0':'Negative',\n", | ||
" 'LABEL_1':'Neutral',\n", | ||
" 'LABEL_2':'Positive'\n", | ||
"}" | ||
], | ||
"metadata": { | ||
"id": "EOvTojxaQzsx" | ||
}, | ||
"execution_count": 5, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"label_mapping[prediction['label']]" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/", | ||
"height": 35 | ||
}, | ||
"id": "l4yngO4DRSeC", | ||
"outputId": "c7f51237-18fa-4bc6-9da5-b7d6014c21ce" | ||
}, | ||
"execution_count": 6, | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"'Positive'" | ||
], | ||
"application/vnd.google.colaboratory.intrinsic+json": { | ||
"type": "string" | ||
} | ||
}, | ||
"metadata": {}, | ||
"execution_count": 6 | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [], | ||
"metadata": { | ||
"id": "3G7Z-ePnRdBQ" | ||
}, | ||
"execution_count": null, | ||
"outputs": [] | ||
} | ||
] | ||
} |