Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🎯 Tone Detection using `facebook/bart-large-mnli` (Zero-Shot Classification)
|
| 2 |
+
|
| 3 |
+
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).
|
| 4 |
+
|
| 5 |
+
This approach enables you to classify emotional tone (e.g., joy, anger, sadness) **without training**, by framing it as a textual entailment task.
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## 📌 Model Details
|
| 10 |
+
|
| 11 |
+
- **Model:** `facebook/bart-large-mnli`
|
| 12 |
+
- **Task:** Zero-shot classification via NLI
|
| 13 |
+
- **Approach:** Checks if the input sentence entails a hypothesis (e.g., "This text expresses anger.")
|
| 14 |
+
- **Strength:** No labeled training data required
|
| 15 |
+
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
## 📂 Dataset Used
|
| 19 |
+
|
| 20 |
+
For benchmarking and scoring, we use the [`go_emotions`](https://huggingface.co/datasets/go_emotions) dataset:
|
| 21 |
+
|
| 22 |
+
```python
|
| 23 |
+
from datasets import load_dataset
|
| 24 |
+
|
| 25 |
+
dataset = load_dataset("go_emotions")
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
# 🧠 Tone Detection (Inference)
|
| 29 |
+
```Python
|
| 30 |
+
from transformers import pipeline
|
| 31 |
+
|
| 32 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 33 |
+
|
| 34 |
+
labels = ["joy", "anger", "sadness", "fear", "surprise", "neutral"]
|
| 35 |
+
|
| 36 |
+
text = "I can't believe this is happening again. So frustrating."
|
| 37 |
+
|
| 38 |
+
result = classifier(text, candidate_labels=labels, hypothesis_template="This text expresses {}.")
|
| 39 |
+
print(result)
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
# 🧪 Evaluation with Scoring
|
| 43 |
+
|
| 44 |
+
```python
|
| 45 |
+
from sklearn.metrics import accuracy_score
|
| 46 |
+
|
| 47 |
+
# Mapping GoEmotions label indices to names
|
| 48 |
+
id2label = dataset["train"].features["labels"].feature.names
|
| 49 |
+
|
| 50 |
+
# Evaluate on a small sample
|
| 51 |
+
def evaluate(dataset, candidate_labels):
|
| 52 |
+
correct = 0
|
| 53 |
+
total = 0
|
| 54 |
+
for row in dataset.select(range(100)): # Use more samples as needed
|
| 55 |
+
text = row["text"]
|
| 56 |
+
true_labels = [id2label[i] for i in row["labels"]]
|
| 57 |
+
result = classifier(text, candidate_labels=candidate_labels, hypothesis_template="This text expresses {}.")
|
| 58 |
+
predicted = result["labels"][0]
|
| 59 |
+
if predicted in true_labels:
|
| 60 |
+
correct += 1
|
| 61 |
+
total += 1
|
| 62 |
+
return correct/total
|
| 63 |
+
|
| 64 |
+
accuracy = evaluate(dataset["test"], candidate_labels=labels)
|
| 65 |
+
print(f"Zero-shot Accuracy: {accuracy:.2%}")
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
# ⚙️ Use Cases
|
| 69 |
+
Customer support tone analysis
|
| 70 |
+
|
| 71 |
+
Chat moderation for emotional tone
|
| 72 |
+
|
| 73 |
+
Feedback sentiment detection
|
| 74 |
+
|
| 75 |
+
Real-time conversation emotion tagging
|