{ "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 }