{ "cells": [ { "cell_type": "code", "execution_count": 4, "metadata": { "id": "I-5YovjdgLu0" }, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'torch'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[1;32mc:\\Users\\LENOVO\\OneDrive\\Desktop\\Twitter_sentiment\\Twitter-Sentiment-Analysis-Web\\sentiment_analysis_Twitter.ipynb Cell 1\u001b[0m line \u001b[0;36m2\n\u001b[0;32m 1\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mos\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mtorch\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mtorch\u001b[39;00m \u001b[39mimport\u001b[39;00m nn\n\u001b[0;32m 4\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mtorch\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mutils\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mdata\u001b[39;00m \u001b[39mimport\u001b[39;00m DataLoader, Dataset\n", "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'torch'" ] } ], "source": [ "import os\n", "import torch\n", "from torch import nn\n", "from torch.utils.data import DataLoader, Dataset\n", "from transformers import BertTokenizer, BertModel, AdamW, get_linear_schedule_with_warmup\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import accuracy_score, classification_report\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "NHhBadHEDv0e", "outputId": "94c8fdab-c4cd-4b6f-d2f4-9ad5675054d7" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/content\n" ] } ], "source": [ "import os\n", "current_directory = os.getcwd()\n", "print(current_directory)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ll5qSoY5gT5Y", "outputId": "1a477d0d-f4a1-4b95-eac9-9d7129a1353f" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Defaulting to user installation because normal site-packages is not writeable\n", "Collecting transformers\n", " Downloading transformers-4.35.2-py3-none-any.whl.metadata (123 kB)\n", " ---------------------------------------- 0.0/123.5 kB ? eta -:--:--\n", " --- ------------------------------------ 10.2/123.5 kB ? eta -:--:--\n", " --- ------------------------------------ 10.2/123.5 kB ? eta -:--:--\n", " --- ------------------------------------ 10.2/123.5 kB ? eta -:--:--\n", " --- ------------------------------------ 10.2/123.5 kB ? eta -:--:--\n", " ------------ ------------------------ 41.0/123.5 kB 164.3 kB/s eta 0:00:01\n", " ------------------ ------------------ 61.4/123.5 kB 204.8 kB/s eta 0:00:01\n", " ------------------ ------------------ 61.4/123.5 kB 204.8 kB/s eta 0:00:01\n", " ------------------ ------------------ 61.4/123.5 kB 204.8 kB/s eta 0:00:01\n", " -------------------------------- --- 112.6/123.5 kB 252.2 kB/s eta 0:00:01\n", " ------------------------------------ 123.5/123.5 kB 278.9 kB/s eta 0:00:00\n", "Requirement already satisfied: filelock in c:\\users\\lenovo\\appdata\\roaming\\python\\python312\\site-packages (from transformers) (3.13.1)\n", "Collecting huggingface-hub<1.0,>=0.16.4 (from transformers)\n", " Downloading huggingface_hub-0.19.4-py3-none-any.whl.metadata (14 kB)\n", "Requirement already satisfied: numpy>=1.17 in c:\\users\\lenovo\\appdata\\roaming\\python\\python312\\site-packages (from transformers) (1.26.2)\n", "Requirement already satisfied: packaging>=20.0 in c:\\users\\lenovo\\appdata\\roaming\\python\\python312\\site-packages (from transformers) (23.2)\n", "Requirement already satisfied: pyyaml>=5.1 in c:\\users\\lenovo\\appdata\\roaming\\python\\python312\\site-packages (from transformers) (6.0.1)\n", "Requirement already satisfied: regex!=2019.12.17 in c:\\users\\lenovo\\appdata\\roaming\\python\\python312\\site-packages (from transformers) (2023.10.3)\n", "Requirement already satisfied: requests in c:\\users\\lenovo\\appdata\\roaming\\python\\python312\\site-packages (from transformers) (2.31.0)\n", "Collecting tokenizers<0.19,>=0.14 (from transformers)\n", " Downloading tokenizers-0.15.0.tar.gz (318 kB)\n", " ---------------------------------------- 0.0/318.5 kB ? eta -:--:--\n", " ---- -------------------------------- 41.0/318.5 kB 991.0 kB/s eta 0:00:01\n", " ---------- ---------------------------- 81.9/318.5 kB 1.2 MB/s eta 0:00:01\n", " -------------- ----------------------- 122.9/318.5 kB 1.0 MB/s eta 0:00:01\n", " ---------------- ------------------- 143.4/318.5 kB 853.3 kB/s eta 0:00:01\n", " ------------------ ----------------- 163.8/318.5 kB 893.0 kB/s eta 0:00:01\n", " ------------------- ---------------- 174.1/318.5 kB 655.4 kB/s eta 0:00:01\n", " ----------------------- ------------ 204.8/318.5 kB 692.4 kB/s eta 0:00:01\n", " --------------------------- -------- 245.8/318.5 kB 754.9 kB/s eta 0:00:01\n", " ---------------------------- ------- 256.0/318.5 kB 655.4 kB/s eta 0:00:01\n", " ------------------------------------ 318.5/318.5 kB 730.5 kB/s eta 0:00:00\n", " Installing build dependencies: started\n", " Installing build dependencies: finished with status 'done'\n", " Getting requirements to build wheel: started\n", " Getting requirements to build wheel: finished with status 'done'\n", " Preparing metadata (pyproject.toml): started\n", " Preparing metadata (pyproject.toml): finished with status 'error'\n", "Note: you may need to restart the kernel to use updated packages.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " error: subprocess-exited-with-error\n", " \n", " × Preparing metadata (pyproject.toml) did not run successfully.\n", " │ exit code: 1\n", " ╰─> [6 lines of output]\n", " Checking for Rust toolchain....\n", " \n", " Cargo, the Rust package manager, is not installed or is not on PATH.\n", " This package requires Rust and Cargo to compile extensions. Install it through\n", " the system's package manager or via https://rustup.rs/\n", " \n", " [end of output]\n", " \n", " note: This error originates from a subprocess, and is likely not a problem with pip.\n", "error: metadata-generation-failed\n", "\n", "× Encountered error while generating package metadata.\n", "╰─> See above for output.\n", "\n", "note: This is an issue with the package mentioned above, not pip.\n", "hint: See above for details.\n" ] } ], "source": [ "pip install transformers\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "dOvDqAjGp3Gl", "outputId": "97e2bcf5-725f-4f2c-c626-1b97980c0f36" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mounted at /content/drive\n" ] } ], "source": [ "from google.colab import drive\n", "drive.mount('/content/drive')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "vrm1tJu8H9Hb" }, "outputs": [], "source": [ "df=pd.read_csv(\"/Twitter_Data.csv\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 419 }, "id": "lUZsmGDtmoLl", "outputId": "a673f581-3f2e-40f5-966c-cfbff840c647" }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " | clean_text | \n", "category | \n", "
---|---|---|
0 | \n", "when modi promised “minimum government maximum... | \n", "-1.0 | \n", "
1 | \n", "talk all the nonsense and continue all the dra... | \n", "0.0 | \n", "
2 | \n", "what did just say vote for modi welcome bjp t... | \n", "1.0 | \n", "
3 | \n", "asking his supporters prefix chowkidar their n... | \n", "1.0 | \n", "
4 | \n", "answer who among these the most powerful world... | \n", "1.0 | \n", "
... | \n", "... | \n", "... | \n", "
139418 | \n", "per capita income average indians have gone wh... | \n", "1.0 | \n", "
139419 | \n", "wow what response behold all south bangalorean... | \n", "1.0 | \n", "
139420 | \n", "modi also not visited there | \n", "0.0 | \n", "
139421 | \n", "does his categorisation cover bjp many states ... | \n", "1.0 | \n", "
139422 | \n", "problembut take responsibility for unprecedent... | \n", "NaN | \n", "
139423 rows × 2 columns
\n", "\n", " | clean_text | \n", "category | \n", "
---|---|---|
0 | \n", "when modi promised “minimum government maximum... | \n", "-1.0 | \n", "
2 | \n", "what did just say vote for modi welcome bjp t... | \n", "1.0 | \n", "
3 | \n", "asking his supporters prefix chowkidar their n... | \n", "1.0 | \n", "
4 | \n", "answer who among these the most powerful world... | \n", "1.0 | \n", "
8 | \n", "with upcoming election india saga going import... | \n", "1.0 | \n", "
... | \n", "... | \n", "... | \n", "
16084 | \n", "this scheme very good but the congress governm... | \n", "1.0 | \n", "
16085 | \n", "sorry modi came 2001 kindly see the condition ... | \n", "1.0 | \n", "
16086 | \n", "feels like total conspiracy holding the seat t... | \n", "1.0 | \n", "
16088 | \n", "told them love and respect their president the... | \n", "1.0 | \n", "
16089 | \n", "imagine the hatred modi and shah h | \n", "NaN | \n", "
10518 rows × 2 columns
\n", "\n", " | clean_text | \n", "category | \n", "
---|---|---|
0 | \n", "when modi promised “minimum government maximum... | \n", "-1.0 | \n", "
1 | \n", "talk all the nonsense and continue all the dra... | \n", "0.0 | \n", "
2 | \n", "what did just say vote for modi welcome bjp t... | \n", "1.0 | \n", "
3 | \n", "asking his supporters prefix chowkidar their n... | \n", "1.0 | \n", "
4 | \n", "answer who among these the most powerful world... | \n", "1.0 | \n", "
... | \n", "... | \n", "... | \n", "
162975 | \n", "why these 456 crores paid neerav modi not reco... | \n", "-1.0 | \n", "
162976 | \n", "dear rss terrorist payal gawar what about modi... | \n", "-1.0 | \n", "
162977 | \n", "did you cover her interaction forum where she ... | \n", "0.0 | \n", "
162978 | \n", "there big project came into india modi dream p... | \n", "0.0 | \n", "
162979 | \n", "have you ever listen about like gurukul where ... | \n", "1.0 | \n", "
162980 rows × 2 columns
\n", "