CoolFace
Modelpublic

QuophyDzifa/Sentiment-Analysis-Model

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes26downloads
Inference-Huggingface.ipynb151 linesDownload Raw Back to notebooks
1{2 "cells": [3  {4   "cell_type": "code",5   "execution_count": 1,6   "metadata": {},7   "outputs": [8    {9     "name": "stderr",10     "output_type": "stream",11     "text": [12      "/Users/emmanuelkoupoh/Documents/Github/LP_NLP/venv/lib/python3.9/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",13      "  from .autonotebook import tqdm as notebook_tqdm\n"14     ]15    }16   ],17   "source": [18    "from transformers import AutoModelForSequenceClassification\n",19    "from transformers import TFAutoModelForSequenceClassification\n",20    "from transformers import AutoTokenizer, AutoConfig\n",21    "import numpy as np\n",22    "from scipy.special import softmax"23   ]24  },25  {26   "cell_type": "code",27   "execution_count": 23,28   "metadata": {},29   "outputs": [],30   "source": [31    "\n",32    "tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')\n",33    "\n",34    "model_path = f\"test_trainer/checkpoint-1000/\"\n",35    "config = AutoConfig.from_pretrained(model_path)\n",36    "model = AutoModelForSequenceClassification.from_pretrained(model_path)"37   ]38  },39  {40   "cell_type": "code",41   "execution_count": 16,42   "metadata": {},43   "outputs": [],44   "source": [45    "# Preprocess text (username and link placeholders)\n",46    "def preprocess(text):\n",47    "    new_text = []\n",48    "    for t in text.split(\" \"):\n",49    "        t = '@user' if t.startswith('@') and len(t) > 1 else t\n",50    "        t = 'http' if t.startswith('http') else t\n",51    "        new_text.append(t)\n",52    "    return \" \".join(new_text)"53   ]54  },55  {56   "cell_type": "code",57   "execution_count": 17,58   "metadata": {},59   "outputs": [],60   "source": [61    "# Input preprocessing\n",62    "text = \"Covid cases are increasing fast!\"\n",63    "text = preprocess(text)\n",64    "\n",65    "# PyTorch-based models\n",66    "encoded_input = tokenizer(text, return_tensors='pt')\n",67    "output = model(**encoded_input)\n",68    "scores = output[0][0].detach().numpy()\n",69    "scores = softmax(scores)\n",70    "\n",71    "# TensorFlow-based models\n",72    "# model = TFAutoModelForSequenceClassification.from_pretrained(model_path)\n",73    "# model.save_pretrained(model_path)\n",74    "# text = \"Covid cases are increasing fast!\"\n",75    "# encoded_input = tokenizer(text, return_tensors='tf')\n",76    "# output = model(encoded_input)\n",77    "# scores = output[0][0].numpy()\n",78    "# scores = softmax(scores)"79   ]80  },81  {82   "cell_type": "code",83   "execution_count": 26,84   "metadata": {},85   "outputs": [],86   "source": [87    "config.id2label = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}"88   ]89  },90  {91   "cell_type": "code",92   "execution_count": 27,93   "metadata": {},94   "outputs": [95    {96     "name": "stdout",97     "output_type": "stream",98     "text": [99      "1) NEUTRAL 0.9564\n",100      "2) POSITIVE 0.0389\n",101      "3) NEGATIVE 0.0047\n"102     ]103    }104   ],105   "source": [106    "# Print labels and scores\n",107    "ranking = np.argsort(scores)\n",108    "ranking = ranking[::-1]\n",109    "for i in range(scores.shape[0]):\n",110    "    l = config.id2label[ranking[i]]\n",111    "    s = scores[ranking[i]]\n",112    "    print(f\"{i+1}) {l} {np.round(float(s), 4)}\")"113   ]114  },115  {116   "cell_type": "code",117   "execution_count": null,118   "metadata": {},119   "outputs": [],120   "source": []121  }122 ],123 "metadata": {124  "kernelspec": {125   "display_name": "Python 3.9.6 ('venv': venv)",126   "language": "python",127   "name": "python3"128  },129  "language_info": {130   "codemirror_mode": {131    "name": "ipython",132    "version": 3133   },134   "file_extension": ".py",135   "mimetype": "text/x-python",136   "name": "python",137   "nbconvert_exporter": "python",138   "pygments_lexer": "ipython3",139   "version": "3.9.6"140  },141  "orig_nbformat": 4,142  "vscode": {143   "interpreter": {144    "hash": "1ab24538aa0da4b2d8c48eaca591ff7ffc54671225fb0511b432fd9e26a098ba"145   }146  }147 },148 "nbformat": 4,149 "nbformat_minor": 2150}151