Views
No views yet
yiyanghkust/finbert-toneyiyanghkust/finbert-tone].yiyanghkust/finbert-tonego_emotions dataset:1from datasets import load_dataset
2
3dataset = load_dataset("go_emotions")1from transformers import pipeline
2
3classifier = pipeline("zero-shot-classification", model="yiyanghkust/finbert-tone")
4
5labels = ["positive", "neutral", "negative"]
6
7text = "I can't believe this is happening again. So frustrating."
8
9result = classifier(text, candidate_labels=labels, hypothesis_template="This text expresses {}.")
10print(result)1from sklearn.metrics import accuracy_score
2
3# Mapping GoEmotions label indices to names
4id2label = dataset["train"].features["labels"].feature.names
5
6# Evaluate on a small sample
7def evaluate(dataset, candidate_labels):
8 correct = 0
9 total = 0
10 for row in dataset.select(range(100)): # Use more samples as needed
11 text = row["text"]
12 true_labels = [id2label[i] for i in row["labels"]]
13 result = classifier(text, candidate_labels=candidate_labels, hypothesis_template="This text expresses {}.")
14 predicted = result["labels"][0]
15 if predicted in true_labels:
16 correct += 1
17 total += 1
18 return correct/total
19
20accuracy = evaluate(dataset["test"], candidate_labels=labels)
21print(f"Zero-shot Accuracy: {accuracy:.2%}")