Spaces:
Sleeping
Sleeping
Delete mood_classifier.py
Browse files- mood_classifier.py +0 -92
mood_classifier.py
DELETED
@@ -1,92 +0,0 @@
|
|
1 |
-
import torch
|
2 |
-
import torch.nn as nn
|
3 |
-
import torch.optim as optim
|
4 |
-
from transformers import BertTokenizer, BertForSequenceClassification
|
5 |
-
from datasets import load_dataset
|
6 |
-
from torch.utils.data import DataLoader, Dataset, random_split
|
7 |
-
from tqdm import tqdm
|
8 |
-
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
|
9 |
-
|
10 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
11 |
-
|
12 |
-
# Load GoEmotions dataset
|
13 |
-
dataset = load_dataset("go_emotions", split="train")
|
14 |
-
dataset = dataset.map(lambda x: {"label": x["labels"][0]}) # Convert multi-label to single-label
|
15 |
-
|
16 |
-
labels = list(set(dataset["label"])) # Unique labels
|
17 |
-
num_labels = len(labels)
|
18 |
-
|
19 |
-
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
20 |
-
|
21 |
-
class MoodDataset(Dataset):
|
22 |
-
def __init__(self, texts, labels):
|
23 |
-
self.texts = texts
|
24 |
-
self.labels = labels
|
25 |
-
|
26 |
-
def __len__(self):
|
27 |
-
return len(self.texts)
|
28 |
-
|
29 |
-
def __getitem__(self, idx):
|
30 |
-
inputs = tokenizer(self.texts[idx], return_tensors="pt", padding="max_length", truncation=True, max_length=128)
|
31 |
-
return {key: val.squeeze(0) for key, val in inputs.items()}, torch.tensor(labels.index(self.labels[idx]))
|
32 |
-
|
33 |
-
dataset = MoodDataset(dataset["text"], dataset["label"])
|
34 |
-
train_size = int(0.8 * len(dataset))
|
35 |
-
train_set, test_set = random_split(dataset, [train_size, len(dataset) - train_size])
|
36 |
-
|
37 |
-
train_loader = DataLoader(train_set, batch_size=32, shuffle=True)
|
38 |
-
test_loader = DataLoader(test_set, batch_size=32)
|
39 |
-
|
40 |
-
model = BertForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=num_labels).to(device)
|
41 |
-
optimizer = optim.AdamW(model.parameters(), lr=2e-5)
|
42 |
-
criterion = nn.CrossEntropyLoss()
|
43 |
-
|
44 |
-
num_epochs = 3
|
45 |
-
for epoch in range(num_epochs):
|
46 |
-
model.train()
|
47 |
-
epoch_loss, correct, total = 0, 0, 0
|
48 |
-
preds, labels_list = [], []
|
49 |
-
|
50 |
-
for batch in tqdm(train_loader, desc=f"Epoch {epoch+1}/{num_epochs} Training"):
|
51 |
-
optimizer.zero_grad()
|
52 |
-
inputs = {key: val.to(device) for key, val in batch[0].items()}
|
53 |
-
labels = batch[1].to(device)
|
54 |
-
|
55 |
-
outputs = model(**inputs).logits
|
56 |
-
loss = criterion(outputs, labels)
|
57 |
-
|
58 |
-
loss.backward()
|
59 |
-
optimizer.step()
|
60 |
-
|
61 |
-
epoch_loss += loss.item()
|
62 |
-
correct += (outputs.argmax(dim=1) == labels).sum().item()
|
63 |
-
total += labels.size(0)
|
64 |
-
|
65 |
-
preds.extend(outputs.argmax(dim=1).cpu().numpy())
|
66 |
-
labels_list.extend(labels.cpu().numpy())
|
67 |
-
|
68 |
-
train_acc = accuracy_score(labels_list, preds)
|
69 |
-
precision, recall, f1, _ = precision_recall_fscore_support(labels_list, preds, average="weighted")
|
70 |
-
|
71 |
-
print(f"Epoch {epoch+1}: Loss: {epoch_loss:.4f}, Train Acc: {train_acc:.4f}, Precision: {precision:.4f}, Recall: {recall:.4f}, F1: {f1:.4f}")
|
72 |
-
|
73 |
-
# **Evaluate on Test Set**
|
74 |
-
model.eval()
|
75 |
-
test_preds, test_labels = [], []
|
76 |
-
|
77 |
-
with torch.no_grad():
|
78 |
-
for batch in tqdm(test_loader, desc="Evaluating on Test Set"):
|
79 |
-
inputs = {key: val.to(device) for key, val in batch[0].items()}
|
80 |
-
labels = batch[1].to(device)
|
81 |
-
|
82 |
-
outputs = model(**inputs).logits
|
83 |
-
test_preds.extend(outputs.argmax(dim=1).cpu().numpy())
|
84 |
-
test_labels.extend(labels.cpu().numpy())
|
85 |
-
|
86 |
-
test_acc = accuracy_score(test_labels, test_preds)
|
87 |
-
precision, recall, f1, _ = precision_recall_fscore_support(test_labels, test_preds, average="weighted")
|
88 |
-
|
89 |
-
print(f"Test Accuracy: {test_acc:.4f}, Precision: {precision:.4f}, Recall: {recall:.4f}, F1-score: {f1:.4f}")
|
90 |
-
|
91 |
-
# Save model
|
92 |
-
model.save_pretrained("mood_classifier")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|