From 18a873c1c9b84207c147f359e84d46d0e2aa902e Mon Sep 17 00:00:00 2001 From: Pradip Nichite Date: Mon, 31 Oct 2022 23:32:16 +0530 Subject: [PATCH] Created using Colaboratory --- Youtube_Huggingface_Inference_API.ipynb | 209 ++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 Youtube_Huggingface_Inference_API.ipynb diff --git a/Youtube_Huggingface_Inference_API.ipynb b/Youtube_Huggingface_Inference_API.ipynb new file mode 100644 index 0000000..b43a511 --- /dev/null +++ b/Youtube_Huggingface_Inference_API.ipynb @@ -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": [ + "\"Open" + ] + }, + { + "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": [] + } + ] +} \ No newline at end of file