{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "2534f67e", "metadata": {}, "outputs": [], "source": [ "# Installing Gradio\n", "!pip install gradio transformers -q\n", "\n", "# Import the required Libraries\n", "import gradio as gr\n", "import numpy as np\n", "import pandas as pd\n", "import pickle\n", "import transformers\n", "from transformers import AutoTokenizer \n", "from transformers import AutoConfig\n", "from transformers import AutoModelForSequenceClassification\n", "from transformers import TFAutoModelForSequenceClassification\n", "from transformers import pipeline\n", "from scipy.special import softmax" ] }, { "cell_type": "code", "execution_count": 2, "id": "1dc8e034", "metadata": {}, "outputs": [], "source": [ "# Requirements\n", "model_path =\"HOLYBOY/Sentiment_Analysis\"\n", "tokenizer = AutoTokenizer.from_pretrained(model_path)\n", "config = AutoConfig.from_pretrained(model_path)\n", "model = AutoModelForSequenceClassification.from_pretrained(model_path)" ] }, { "cell_type": "code", "execution_count": 3, "id": "02f78081", "metadata": {}, "outputs": [], "source": [ "# Preprocess text (username and link placeholders)\n", "def preprocess(text):\n", " new_text = []\n", " for t in text.split(\" \"):\n", " t = \"@user\" if t.startswith(\"@\") and len(t) > 1 else t\n", " t = \"http\" if t.startswith(\"http\") else t\n", " new_text.append(t)\n", " return \" \".join(new_text)" ] }, { "cell_type": "code", "execution_count": 4, "id": "70126857", "metadata": {}, "outputs": [], "source": [ "# ---- Function to process the input and return prediction\n", "def sentiment_analysis(text):\n", " text = preprocess(text)\n", "\n", " encoded_input = tokenizer(text, return_tensors = \"pt\") # for PyTorch-based models\n", " output = model(**encoded_input)\n", " scores_ = output[0][0].detach().numpy()\n", " scores_ = softmax(scores_)\n", " \n", " # Format output dict of scores\n", " labels = [\"Negative\", \"Neutral\", \"Positive\"]\n", " scores = {l:float(s) for (l,s) in zip(labels, scores_) }\n", " \n", " return scores" ] }, { "cell_type": "code", "execution_count": 10, "id": "4901894b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7865\n", "\n", "Could not create share link. Please check your internet connection or our status page: https://status.gradio.app. \n", "\n", "Also please ensure that your antivirus or firewall is not blocking the binary file located at: C:\\Users\\user\\anaconda3\\lib\\site-packages\\gradio\\frpc_windows_amd64_v0.2\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ---- Gradio app interface\n", "app = gr.Interface(fn = sentiment_analysis,\n", " inputs = gr.Textbox(\"Input your tweet to classify or use the example provided below...\"),\n", " outputs = \"label\",\n", " title = \"Public Perception of COVID-19 Vaccines\",\n", " description = \"This app analyzes Perception of text based on tweets about COVID-19 Vaccines using a fine-tuned distilBERT model\",\n", " interpretation = \"default\",\n", " examples = [[\"The idea of introducing the vaccine is good\"],\n", " [\"I am definately not taking the jab\"], \n", " [\"The vaccine is bad and can cause serious health implications\"], \n", " [\"I dont have any opinion \"]]\n", " )\n", "\n", "app.launch(share =True)" ] }, { "cell_type": "code", "execution_count": null, "id": "21753359", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "6c96cb45", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }