File size: 2,358 Bytes
2275eaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# ๐ŸŽฏ Tone Detection using `facebook/bart-large-mnli` (Zero-Shot Classification)

This project demonstrates how to perform **Tone Detection** using the [`facebook/bart-large-mnli`](https://huggingface.co/facebook/bart-large-mnli) model through **zero-shot classification** based on Natural Language Inference (NLI).

This approach enables you to classify emotional tone (e.g., joy, anger, sadness) **without training**, by framing it as a textual entailment task.

---

## ๐Ÿ“Œ Model Details

- **Model:** `facebook/bart-large-mnli`
- **Task:** Zero-shot classification via NLI
- **Approach:** Checks if the input sentence entails a hypothesis (e.g., "This text expresses anger.")
- **Strength:** No labeled training data required

---

## ๐Ÿ“‚ Dataset Used

For benchmarking and scoring, we use the [`go_emotions`](https://huggingface.co/datasets/go_emotions) dataset:

```python
from datasets import load_dataset

dataset = load_dataset("go_emotions")
```

# ๐Ÿง  Tone Detection (Inference)
```Python
from transformers import pipeline

classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")

labels = ["joy", "anger", "sadness", "fear", "surprise", "neutral"]

text = "I can't believe this is happening again. So frustrating."

result = classifier(text, candidate_labels=labels, hypothesis_template="This text expresses {}.")
print(result)
```

# ๐Ÿงช Evaluation with Scoring

```python
from sklearn.metrics import accuracy_score

# Mapping GoEmotions label indices to names
id2label = dataset["train"].features["labels"].feature.names

# Evaluate on a small sample
def evaluate(dataset, candidate_labels):
    correct = 0
    total = 0
    for row in dataset.select(range(100)):  # Use more samples as needed
        text = row["text"]
        true_labels = [id2label[i] for i in row["labels"]]
        result = classifier(text, candidate_labels=candidate_labels, hypothesis_template="This text expresses {}.")
        predicted = result["labels"][0]
        if predicted in true_labels:
            correct += 1
        total += 1
    return correct/total

accuracy = evaluate(dataset["test"], candidate_labels=labels)
print(f"Zero-shot Accuracy: {accuracy:.2%}")
```

# โš™๏ธ Use Cases
Customer support tone analysis

Chat moderation for emotional tone

Feedback sentiment detection

Real-time conversation emotion tagging