Spaces:
Build error
Build error
File size: 5,248 Bytes
dac5ca8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
{
"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": [
"<div><iframe src=\"http://127.0.0.1:7865/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"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
}
|