<jupyter_start><jupyter_text>**Text Classification** Install the useful libraries here<jupyter_code>!pip install transformers
!pip install datasets<jupyter_output>Requirement already satisfied: transformers in /usr/local/lib/python3.7/dist-packages (4.17.0)
Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from transformers) (2.23.0)
Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.7/dist-packages (from transformers) (4.63.0)
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.7/dist-packages (from transformers) (21.3)
Requirement already satisfied: huggingface-hub<1.0,>=0.1.0 in /usr/local/lib/python3.7/dist-packages (from transformers) (0.4.0)
Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.7/dist-packages (from transformers) (2019.12.20)
Requirement already satisfied: sacremoses in /usr/local/lib/python3.7/dist-packages (from transformers) (0.0.49)
Requirement already satisfied: filelock in /usr/local/lib/python3.7/dist-packages (from transformers) (3.6.0)
Requirement already satisfied: tokenizers!=0.11.3,>=0.11.1 in /usr/local/lib/python3.7/di[...]<jupyter_text>Connect to Google Drive<jupyter_code># Load the Drive helper and mount
from google.colab import drive
drive.mount('/content/drive')<jupyter_output>Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).<jupyter_text>Import libraries<jupyter_code>import pandas as pd
from datasets import load_dataset
from transformers import AutoTokenizer, DataCollatorWithPadding, AutoModelForSequenceClassification
import numpy as np
from torch.utils.data import DataLoader
from transformers import AdamW
from transformers import get_scheduler
import torch
from tqdm.auto import tqdm
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score, ConfusionMatrixDisplay
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Read CSV Emotion dataset<jupyter_code>df = pd.read_csv("/content/drive/MyDrive/Data/Emotion_final.csv")
df.head()
df['Emotion'].value_counts()<jupyter_output><empty_output><jupyter_text>Balance the labels<jupyter_code>emotions = df.Emotion.unique()
data = pd.DataFrame()
for i in emotions:
  temp = df[df['Emotion']==i].iloc[:879]
  data = [data, temp]
  data = pd.concat(data)
data
data['Emotion'].value_counts()<jupyter_output><empty_output><jupyter_text>Convert Text Labels into Numeric Labels<jupyter_code>data_dict = {0:'happy', 1:'anger', 2:'love', 3:'surprise', 4:'fear', 5:'sadness'}
my_dict = { data_dict[k]:k for k in data_dict}
data['Emotion'] = [my_dict.get(i,i) for i in list(data['Emotion'])]
data.head()<jupyter_output><empty_output><jupyter_text>Split data into train, validation and test<jupyter_code>train, test = np.split(data.sample(frac=1, random_state=42), [int(.8*len(data))])
train['Emotion'].value_counts()
test['Emotion'].value_counts()<jupyter_output><empty_output><jupyter_text>Save in your Google Drive<jupyter_code>train.to_csv("/content/drive/MyDrive/Data/text_classification/train.csv", index=False)
test.to_csv("/content/drive/MyDrive/Data/text_classification/test.csv", index=False)<jupyter_output><empty_output><jupyter_text>Load Data using datasets library<jupyter_code>data_files = {'train': "/content/drive/MyDrive/Data/text_classification/train.csv",
              'test': "/content/drive/MyDrive/Data/text_classification/test.csv"}
dataset = load_dataset('csv', data_files=data_files)
dataset<jupyter_output><empty_output><jupyter_text>Use Tokenizer to prepare dataset____<jupyter_code>checkpoint = "bert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
def tokenize_function(example):
    return tokenizer(example["Text"], truncation=True)
tokenized_datasets = dataset.map(tokenize_function, batched=True)
tokenized_datasets
tokenized_datasets = tokenized_datasets.remove_columns(["Text"])
tokenized_datasets = tokenized_datasets.rename_column("Emotion", "labels")
tokenized_datasets.set_format("torch")
tokenized_datasets["train"].column_names
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
train_dataloader = DataLoader(
    tokenized_datasets["train"], shuffle=True, batch_size=8, collate_fn=data_collator
)
test_dataloader = DataLoader(
    tokenized_datasets["test"], batch_size=8, collate_fn=data_collator
)<jupyter_output><empty_output><jupyter_text>Use Model to train dataset<jupyter_code>model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=6)
optimizer = AdamW(model.parameters(), lr=5e-5)
num_epochs = 3
num_training_steps = num_epochs * len(train_dataloader)
lr_scheduler = get_scheduler(
    "linear",
    optimizer=optimizer,
    num_warmup_steps=0,
    num_training_steps=num_training_steps,
)
print(num_training_steps)
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model.to(device)
device<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>progress_bar = tqdm(range(num_training_steps))

model.train()
for epoch in range(num_epochs):
    for batch in train_dataloader:
        batch = {k: v.to(device) for k, v in batch.items()}
        outputs = model(**batch)
        loss = outputs.loss
        loss.backward()

        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()
        progress_bar.update(1)<jupyter_output><empty_output><jupyter_text>Testing<jupyter_code>test = []
test_pred = []
model.eval()
for batch in test_dataloader:
    batch = {k: v.to(device) for k, v in batch.items()}
    with torch.no_grad():
        outputs = model(**batch)
    test_pred = test_pred + (outputs.logits.argmax(axis=-1).flatten().tolist())
    test = test + batch['labels'].tolist()
test = [data_dict.get(i,i) for i in test]
test_pred = [data_dict.get(i,i) for i in test_pred]
cr_test = classification_report(test,test_pred)
test_accuracy = accuracy_score(test,test_pred)
print("Testing accuracy:", test_accuracy)
print(cr_test)
def confusion_ma(y_true, y_pred, class_names):
    cm = confusion_matrix(y_true, y_pred, normalize='true')
    disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=class_names)
    disp.plot(cmap=plt.cm.Blues)
    return plt.show()
confusion_ma(test,test_pred, emotions)<jupyter_output><empty_output><jupyter_text>User Input<jupyter_code>inp = input('Enter Sentence:')
single_tokenized_test = tokenizer(inp,truncation=True, return_tensors='pt')
with torch.no_grad():
  single_tokenized_test = {k: v.to(device) for k, v in single_tokenized_test.items()}
  output = model(**single_tokenized_test)
  single_test_pred = output.logits.argmax(axis=-1).flatten().tolist()
print("The emotion of this sentence is: "+str(data_dict[single_test_pred[0]]))<jupyter_output>Enter Sentence:I am so handsome, Masha-ALLAH
The emotion of this sentence is: happy
{'fraction_non_alphanumeric': 0.12561791218377436, 'fraction_numerical': 0.014829892410584472, 'mean_word_length': 7.008149010477299, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '601292', 'license': 'MIT', 'n_tokens_mistral': 2396, 'n_tokens_neox': 2340, 'n_words': 431, 'path': 'EmotionClassification.ipynb', 'repo_name': 'hamziqureshi/Emotion_Classification_with_BERT', 'size': '6878'}