Update README.md
Browse files
README.md
CHANGED
@@ -1,75 +1,75 @@
|
|
1 |
-
#
|
2 |
|
3 |
-
This
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
6 |
|
7 |
---
|
8 |
|
9 |
-
## π Model
|
10 |
|
11 |
- **Model:** `facebook/bart-large-mnli`
|
12 |
-
- **
|
13 |
-
- **
|
14 |
-
- **
|
15 |
|
16 |
---
|
17 |
|
18 |
-
## π Dataset
|
19 |
|
20 |
-
|
21 |
|
22 |
```python
|
23 |
from datasets import load_dataset
|
24 |
|
25 |
-
dataset = load_dataset("
|
26 |
```
|
27 |
|
28 |
-
# π§
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
from transformers import pipeline
|
31 |
|
32 |
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
text = "I can't believe this is happening again. So frustrating."
|
37 |
|
38 |
-
result = classifier(
|
39 |
print(result)
|
40 |
```
|
41 |
|
42 |
-
#
|
|
|
43 |
|
44 |
```python
|
45 |
from sklearn.metrics import accuracy_score
|
46 |
|
47 |
-
|
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
|
55 |
-
|
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 |
-
|
60 |
-
|
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 |
-
|
|
|
|
|
74 |
|
75 |
-
|
|
|
|
|
|
1 |
+
# Zero-Shot Text Classification using `facebook/bart-large-mnli`
|
2 |
|
3 |
+
This repository demonstrates how to use the [`facebook/bart-large-mnli`](https://huggingface.co/facebook/bart-large-mnli) model for **zero-shot text classification** based on **natural language inference (NLI)**.
|
4 |
|
5 |
+
We extend the base usage by:
|
6 |
+
- Using a labeled dataset for benchmarking
|
7 |
+
- Performing optional fine-tuning
|
8 |
+
- Quantizing the model to FP16
|
9 |
+
- Scoring model performance
|
10 |
|
11 |
---
|
12 |
|
13 |
+
## π Model Description
|
14 |
|
15 |
- **Model:** `facebook/bart-large-mnli`
|
16 |
+
- **Type:** NLI-based zero-shot classifier
|
17 |
+
- **Architecture:** BART (Bidirectional and Auto-Regressive Transformers)
|
18 |
+
- **Usage:** Classifies text by scoring label hypotheses as NLI entailment
|
19 |
|
20 |
---
|
21 |
|
22 |
+
## π Dataset
|
23 |
|
24 |
+
We use the [`yahoo_answers_topics`](https://huggingface.co/datasets/yahoo_answers_topics) dataset from Hugging Face for evaluation. It contains questions categorized into 10 topics.
|
25 |
|
26 |
```python
|
27 |
from datasets import load_dataset
|
28 |
|
29 |
+
dataset = load_dataset("yahoo_answers_topics")
|
30 |
```
|
31 |
|
32 |
+
# π§ Zero-Shot Classification Logic
|
33 |
+
The model checks whether a text entails a hypothesis like:
|
34 |
+
|
35 |
+
"This text is about sports."
|
36 |
+
|
37 |
+
For each candidate label (e.g., "sports", "education", "health"), we convert them into such hypotheses and use the model to score them.
|
38 |
+
|
39 |
+
# β
Example: Inference with Zero-Shot Pipeline
|
40 |
+
```python
|
41 |
from transformers import pipeline
|
42 |
|
43 |
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
44 |
|
45 |
+
sequence = "The team played well and won the championship."
|
46 |
+
labels = ["sports", "politics", "education", "technology"]
|
|
|
47 |
|
48 |
+
result = classifier(sequence, candidate_labels=labels)
|
49 |
print(result)
|
50 |
```
|
51 |
|
52 |
+
# π Scoring / Evaluation
|
53 |
+
Evaluate zero-shot classification using accuracy or top-k accuracy:
|
54 |
|
55 |
```python
|
56 |
from sklearn.metrics import accuracy_score
|
57 |
|
58 |
+
def evaluate_zero_shot(dataset, labels):
|
|
|
|
|
|
|
|
|
59 |
correct = 0
|
60 |
total = 0
|
61 |
+
for example in dataset:
|
62 |
+
result = classifier(example["question_content"], candidate_labels=labels)
|
|
|
|
|
63 |
predicted = result["labels"][0]
|
64 |
+
true = labels[example["topic"]]
|
65 |
+
correct += int(predicted == true)
|
66 |
total += 1
|
67 |
+
return correct / total
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
labels = ["Society & Culture", "Science & Mathematics", "Health", "Education",
|
70 |
+
"Computers & Internet", "Sports", "Business & Finance", "Entertainment & Music",
|
71 |
+
"Family & Relationships", "Politics & Government"]
|
72 |
|
73 |
+
acc = evaluate_zero_shot(dataset["test"].select(range(100)), labels)
|
74 |
+
print(f"Accuracy: {acc:.2%}")
|
75 |
+
```
|