File size: 4,009 Bytes
a7758d0 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/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",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"from transformers import AutoModelForSequenceClassification\n",
"from transformers import TFAutoModelForSequenceClassification\n",
"from transformers import AutoTokenizer, AutoConfig\n",
"import numpy as np\n",
"from scipy.special import softmax"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"\n",
"tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')\n",
"\n",
"model_path = f\"test_trainer/checkpoint-1000/\"\n",
"config = AutoConfig.from_pretrained(model_path)\n",
"model = AutoModelForSequenceClassification.from_pretrained(model_path)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"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": 17,
"metadata": {},
"outputs": [],
"source": [
"# Input preprocessing\n",
"text = \"Covid cases are increasing fast!\"\n",
"text = preprocess(text)\n",
"\n",
"# PyTorch-based models\n",
"encoded_input = tokenizer(text, return_tensors='pt')\n",
"output = model(**encoded_input)\n",
"scores = output[0][0].detach().numpy()\n",
"scores = softmax(scores)\n",
"\n",
"# TensorFlow-based models\n",
"# model = TFAutoModelForSequenceClassification.from_pretrained(model_path)\n",
"# model.save_pretrained(model_path)\n",
"# text = \"Covid cases are increasing fast!\"\n",
"# encoded_input = tokenizer(text, return_tensors='tf')\n",
"# output = model(encoded_input)\n",
"# scores = output[0][0].numpy()\n",
"# scores = softmax(scores)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"config.id2label = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1) NEUTRAL 0.9564\n",
"2) POSITIVE 0.0389\n",
"3) NEGATIVE 0.0047\n"
]
}
],
"source": [
"# Print labels and scores\n",
"ranking = np.argsort(scores)\n",
"ranking = ranking[::-1]\n",
"for i in range(scores.shape[0]):\n",
" l = config.id2label[ranking[i]]\n",
" s = scores[ranking[i]]\n",
" print(f\"{i+1}) {l} {np.round(float(s), 4)}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.6 ('venv': venv)",
"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.9.6"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "1ab24538aa0da4b2d8c48eaca591ff7ffc54671225fb0511b432fd9e26a098ba"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|