Raihan Hidayatullah Djunaedi commited on
Commit
94028e0
·
1 Parent(s): 099e31f

Update README.md to enhance model documentation and examples for zero-shot classification

Browse files
Files changed (5) hide show
  1. README.md +114 -39
  2. UPLOAD_INSTRUCTIONS.md +112 -0
  3. example_usage.py +95 -0
  4. requirements.txt +4 -0
  5. tokenizer_config.json +17 -1
README.md CHANGED
@@ -1,47 +1,90 @@
1
  ---
2
- language:
3
- - multilingual
4
- - en
5
- - fr
6
- - es
7
- - de
8
- - el
9
- - bg
10
- - ru
11
- - tr
12
- - ar
13
- - vi
14
- - th
15
- - zh
16
- - hi
17
- - sw
18
- - ur
19
  tags:
20
- - text-classification
21
- - pytorch
22
- - tensorflow
 
 
 
 
 
23
  datasets:
24
- - multi_nli
25
- - xnli
26
  license: mit
27
  pipeline_tag: zero-shot-classification
 
 
 
 
 
 
 
 
 
 
28
  widget:
29
- - text: "За кого вы голосуете в 2020 году?"
30
- candidate_labels: "politique étrangère, Europe, élections, affaires, politique"
31
- multi_class: true
32
- - text: "لمن تصوت في 2020؟"
33
- candidate_labels: "السياسة الخارجية, أوروبا, الانتخابات, الأعمال, السياسة"
34
- multi_class: true
35
- - text: "2020'de kime oy vereceksiniz?"
36
- candidate_labels: "dış politika, Avrupa, seçimler, ticaret, siyaset"
37
- multi_class: true
 
 
 
 
 
 
 
38
  ---
39
 
40
- # xlm-roberta-large-xnli
41
 
42
  ## Model Description
43
 
44
- This model takes [xlm-roberta-large](https://huggingface.co/xlm-roberta-large) and fine-tunes it on a combination of NLI data in 15 languages. It is intended to be used for zero-shot text classification, such as with the Hugging Face [ZeroShotClassificationPipeline](https://huggingface.co/transformers/master/main_classes/pipelines.html#transformers.ZeroShotClassificationPipeline).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  ## Intended Usage
47
 
@@ -72,14 +115,14 @@ For English-only classification, it is recommended to use
72
  [bart-large-mnli](https://huggingface.co/facebook/bart-large-mnli) or
73
  [a distilled bart MNLI model](https://huggingface.co/models?filter=pipeline_tag%3Azero-shot-classification&search=valhalla).
74
 
75
- #### With the zero-shot classification pipeline
76
 
77
  The model can be loaded with the `zero-shot-classification` pipeline like so:
78
 
79
  ```python
80
  from transformers import pipeline
81
  classifier = pipeline("zero-shot-classification",
82
- model="joeddav/xlm-roberta-large-xnli")
83
  ```
84
 
85
  You can then classify in any of the above languages. You can even pass the labels in one language and the sequence to
@@ -109,13 +152,13 @@ classifier(sequence_to_classify, candidate_labels, hypothesis_template=hypothesi
109
  # 'sequence': '¿A quién vas a votar en 2020?'}
110
  ```
111
 
112
- #### With manual PyTorch
113
 
114
  ```python
115
  # pose sequence as a NLI premise and label as a hypothesis
116
  from transformers import AutoModelForSequenceClassification, AutoTokenizer
117
- nli_model = AutoModelForSequenceClassification.from_pretrained('joeddav/xlm-roberta-large-xnli')
118
- tokenizer = AutoTokenizer.from_pretrained('joeddav/xlm-roberta-large-xnli')
119
 
120
  premise = sequence
121
  hypothesis = f'This example is {label}.'
@@ -126,7 +169,7 @@ x = tokenizer.encode(premise, hypothesis, return_tensors='pt',
126
  logits = nli_model(x.to(device))[0]
127
 
128
  # we throw away "neutral" (dim 1) and take the probability of
129
- # "entailment" (2) as the probability of the label being true
130
  entail_contradiction_logits = logits[:,[0,2]]
131
  probs = entail_contradiction_logits.softmax(dim=1)
132
  prob_label_is_true = probs[:,1]
@@ -139,3 +182,35 @@ This model was pre-trained on set of 100 languages, as described in
139
  MNLI train set and the XNLI validation and test sets. Finally, it was trained for one additional epoch on only XNLI
140
  data where the translations for the premise and hypothesis are shuffled such that the premise and hypothesis for
141
  each example come from the same original English example but the premise and hypothesis are of different languages.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ language:
3
+ - multilingual
4
+ - en
5
+ - fr
6
+ - es
7
+ - de
8
+ - el
9
+ - bg
10
+ - ru
11
+ - tr
12
+ - ar
13
+ - vi
14
+ - th
15
+ - zh
16
+ - hi
17
+ - sw
18
+ - ur
19
  tags:
20
+ - text-classification
21
+ - pytorch
22
+ - tensorflow
23
+ - zero-shot-classification
24
+ - xlm-roberta
25
+ - multilingual
26
+ - nli
27
+ - natural-language-inference
28
  datasets:
29
+ - multi_nli
30
+ - xnli
31
  license: mit
32
  pipeline_tag: zero-shot-classification
33
+ library_name: transformers
34
+ model-index:
35
+ - name: xlm-roberta-large-xnli
36
+ results:
37
+ - task:
38
+ type: zero-shot-classification
39
+ name: Zero-Shot Classification
40
+ dataset:
41
+ name: XNLI
42
+ type: xnli
43
  widget:
44
+ - text: "За кого вы голосуете в 2020 году?"
45
+ candidate_labels: "politique étrangère, Europe, élections, affaires, politique"
46
+ multi_class: true
47
+ example_title: "Russian Political Classification"
48
+ - text: "لمن تصوت في 2020؟"
49
+ candidate_labels: "السياسة الخارجية, أوروبا, الانتخابات, الأعمال, السياسة"
50
+ multi_class: true
51
+ example_title: "Arabic Political Classification"
52
+ - text: "2020'de kime oy vereceksiniz?"
53
+ candidate_labels: "dış politika, Avrupa, seçimler, ticaret, siyaset"
54
+ multi_class: true
55
+ example_title: "Turkish Political Classification"
56
+ - text: "I love this movie"
57
+ candidate_labels: "positive, negative, neutral"
58
+ multi_class: false
59
+ example_title: "English Sentiment Analysis"
60
  ---
61
 
62
+ # XLM-RoBERTa Large for Zero-Shot Classification (XNLI)
63
 
64
  ## Model Description
65
 
66
+ This model is based on the excellent work by [joeddav/xlm-roberta-large-xnli](https://huggingface.co/joeddav/xlm-roberta-large-xnli). It takes [xlm-roberta-large](https://huggingface.co/xlm-roberta-large) and fine-tunes it on a combination of NLI data in 15 languages.
67
+
68
+ **Original Model Credit**: This model is a copy of [joeddav/xlm-roberta-large-xnli](https://huggingface.co/joeddav/xlm-roberta-large-xnli) by Joe Davison. All credit for the training and development goes to the original author.
69
+
70
+ This model is intended to be used for zero-shot text classification, such as with the Hugging Face [ZeroShotClassificationPipeline](https://huggingface.co/docs/transformers/main_classes/pipelines#transformers.ZeroShotClassificationPipeline).
71
+
72
+ ## Quick Start
73
+
74
+ ```python
75
+ from transformers import pipeline
76
+
77
+ # Load the zero-shot classification pipeline
78
+ classifier = pipeline("zero-shot-classification",
79
+ model="YOUR_USERNAME/zero-shot-classification")
80
+
81
+ # Example usage
82
+ text = "I love this new smartphone, it's amazing!"
83
+ candidate_labels = ["technology", "sports", "politics", "entertainment"]
84
+
85
+ result = classifier(text, candidate_labels)
86
+ print(result)
87
+ ```
88
 
89
  ## Intended Usage
90
 
 
115
  [bart-large-mnli](https://huggingface.co/facebook/bart-large-mnli) or
116
  [a distilled bart MNLI model](https://huggingface.co/models?filter=pipeline_tag%3Azero-shot-classification&search=valhalla).
117
 
118
+ ### Using the zero-shot classification pipeline
119
 
120
  The model can be loaded with the `zero-shot-classification` pipeline like so:
121
 
122
  ```python
123
  from transformers import pipeline
124
  classifier = pipeline("zero-shot-classification",
125
+ model="YOUR_USERNAME/zero-shot-classification")
126
  ```
127
 
128
  You can then classify in any of the above languages. You can even pass the labels in one language and the sequence to
 
152
  # 'sequence': '¿A quién vas a votar en 2020?'}
153
  ```
154
 
155
+ ### Using with manual PyTorch
156
 
157
  ```python
158
  # pose sequence as a NLI premise and label as a hypothesis
159
  from transformers import AutoModelForSequenceClassification, AutoTokenizer
160
+ nli_model = AutoModelForSequenceClassification.from_pretrained('YOUR_USERNAME/zero-shot-classification')
161
+ tokenizer = AutoTokenizer.from_pretrained('YOUR_USERNAME/zero-shot-classification')
162
 
163
  premise = sequence
164
  hypothesis = f'This example is {label}.'
 
169
  logits = nli_model(x.to(device))[0]
170
 
171
  # we throw away "neutral" (dim 1) and take the probability of
172
+ # "entailment" (2) as the probability of the label being true
173
  entail_contradiction_logits = logits[:,[0,2]]
174
  probs = entail_contradiction_logits.softmax(dim=1)
175
  prob_label_is_true = probs[:,1]
 
182
  MNLI train set and the XNLI validation and test sets. Finally, it was trained for one additional epoch on only XNLI
183
  data where the translations for the premise and hypothesis are shuffled such that the premise and hypothesis for
184
  each example come from the same original English example but the premise and hypothesis are of different languages.
185
+
186
+ ## Model Performance
187
+
188
+ This model achieves excellent performance on multilingual zero-shot classification tasks. For detailed performance metrics, please refer to the [original model](https://huggingface.co/joeddav/xlm-roberta-large-xnli).
189
+
190
+ ## Limitations and Bias
191
+
192
+ - The model may have biases inherited from the training data (MNLI and XNLI datasets)
193
+ - Performance may vary across different languages and domains
194
+ - The model works best with the 15 languages explicitly included in the XNLI training data
195
+ - For English-only tasks, consider using specialized English models like `facebook/bart-large-mnli`
196
+
197
+ ## Citation
198
+
199
+ If you use this model, please cite the original work:
200
+
201
+ ```bibtex
202
+ @misc{davison2020zero,
203
+ title={Zero-Shot Learning in Modern NLP},
204
+ author={Joe Davison},
205
+ year={2020},
206
+ howpublished={\url{https://joeddav.github.io/blog/2020/05/29/ZSL.html}},
207
+ }
208
+ ```
209
+
210
+ ## License
211
+
212
+ This model is released under the MIT License, following the original model's licensing.
213
+
214
+ ## Contact
215
+
216
+ This is a copy of the original model by Joe Davison. For questions about the model architecture and training, please refer to the [original repository](https://huggingface.co/joeddav/xlm-roberta-large-xnli).
UPLOAD_INSTRUCTIONS.md ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # How to Upload Your Model to Hugging Face
2
+
3
+ Follow these steps to upload your zero-shot classification model to Hugging Face and make it available for use through the transformers library.
4
+
5
+ ## Prerequisites
6
+
7
+ 1. Install required packages:
8
+
9
+ ```bash
10
+ pip install huggingface_hub transformers
11
+ ```
12
+
13
+ 2. Create a Hugging Face account at https://huggingface.co/
14
+
15
+ 3. Get your access token:
16
+ - Go to https://huggingface.co/settings/tokens
17
+ - Create a new token with "Write" permissions
18
+ - Copy the token (keep it secure!)
19
+
20
+ ## Upload Steps
21
+
22
+ ### Method 1: Using the Web Interface (Recommended for beginners)
23
+
24
+ 1. Go to https://huggingface.co/new
25
+ 2. Choose "Model" and give it a name (e.g., `zero-shot-classification`)
26
+ 3. Set visibility (Public/Private)
27
+ 4. Click "Create model repository"
28
+ 5. Upload files using the web interface:
29
+ - Drag and drop all files from your model directory
30
+ - Or use git (see Method 2)
31
+
32
+ ### Method 2: Using Git/Command Line
33
+
34
+ 1. Login to Hugging Face CLI:
35
+
36
+ ```bash
37
+ huggingface-cli login
38
+ # Enter your token when prompted
39
+ ```
40
+
41
+ 2. Clone your repository:
42
+
43
+ ```bash
44
+ git clone https://huggingface.co/YOUR_USERNAME/zero-shot-classification
45
+ cd zero-shot-classification
46
+ ```
47
+
48
+ 3. Copy your model files:
49
+
50
+ ```bash
51
+ # Copy all files from your model directory to the cloned repository
52
+ cp /path/to/your/model/* .
53
+ ```
54
+
55
+ 4. Upload to Hugging Face:
56
+
57
+ ```bash
58
+ git add .
59
+ git commit -m "Upload XLM-RoBERTa zero-shot classification model"
60
+ git push
61
+ ```
62
+
63
+ ### Method 3: Using Python API
64
+
65
+ ```python
66
+ from huggingface_hub import HfApi, create_repo
67
+
68
+ # Initialize API
69
+ api = HfApi()
70
+
71
+ # Create repository (optional if not exists)
72
+ repo_id = "YOUR_USERNAME/zero-shot-classification"
73
+ create_repo(repo_id, repo_type="model", private=False)
74
+
75
+ # Upload files
76
+ api.upload_folder(
77
+ folder_path="/path/to/your/model/directory",
78
+ repo_id=repo_id,
79
+ repo_type="model"
80
+ )
81
+ ```
82
+
83
+ ## Important Notes
84
+
85
+ 1. **Replace placeholders**: Before uploading, make sure to replace `YOUR_USERNAME` in the README.md and example files with your actual Hugging Face username.
86
+
87
+ 2. **Model card**: The README.md serves as your model card. Make sure it's complete and accurate.
88
+
89
+ 3. **File size**: Large files (>10MB) are automatically handled by Git LFS, which is already configured in .gitattributes.
90
+
91
+ 4. **Testing**: After upload, test your model:
92
+
93
+ ```python
94
+ from transformers import pipeline
95
+ classifier = pipeline("zero-shot-classification", model="YOUR_USERNAME/zero-shot-classification")
96
+ ```
97
+
98
+ ## Making Your Model Discoverable
99
+
100
+ 1. Add relevant tags in your README.md frontmatter
101
+ 2. Add a good description
102
+ 3. Include example usage
103
+ 4. Consider adding a model card with performance metrics
104
+
105
+ ## Troubleshooting
106
+
107
+ - **Authentication errors**: Make sure your token has write permissions
108
+ - **Large file errors**: Ensure Git LFS is properly configured
109
+ - **Model loading errors**: Check that all required files are present (config.json, model files, tokenizer files)
110
+
111
+ After successful upload, your model will be available at:
112
+ `https://huggingface.co/YOUR_USERNAME/zero-shot-classification`
example_usage.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Example script demonstrating how to use the XLM-RoBERTa Zero-Shot Classification model.
4
+ This script shows various use cases including multilingual classification.
5
+ """
6
+
7
+ import torch
8
+ from transformers import pipeline
9
+
10
+
11
+ def main():
12
+ print("Loading XLM-RoBERTa Zero-Shot Classification model...")
13
+
14
+ # Initialize the zero-shot classification pipeline
15
+ # Replace 'YOUR_USERNAME/zero-shot-classification' with your actual model path
16
+ classifier = pipeline(
17
+ "zero-shot-classification",
18
+ model="YOUR_USERNAME/zero-shot-classification",
19
+ device=0 if torch.cuda.is_available() else -1, # Use GPU if available
20
+ )
21
+
22
+ print("Model loaded successfully!\n")
23
+
24
+ # Example 1: English sentiment analysis
25
+ print("Example 1: English Sentiment Analysis")
26
+ text_en = "I love this new smartphone, it's absolutely amazing!"
27
+ labels_en = ["positive", "negative", "neutral"]
28
+
29
+ result = classifier(text_en, labels_en)
30
+ print(f"Text: {text_en}")
31
+ print(f"Predicted label: {result['labels'][0]} (score: {result['scores'][0]:.4f})")
32
+ print()
33
+
34
+ # Example 2: Multilingual topic classification
35
+ print("Example 2: Multilingual Topic Classification")
36
+ texts = [
37
+ (
38
+ "English",
39
+ "The government announced new economic policies today.",
40
+ ["politics", "sports", "technology", "entertainment"],
41
+ ),
42
+ (
43
+ "Spanish",
44
+ "El nuevo iPhone tiene características increíbles.",
45
+ ["tecnología", "deportes", "política", "entretenimiento"],
46
+ ),
47
+ (
48
+ "French",
49
+ "Le match de football était très excitant hier soir.",
50
+ ["sport", "politique", "technologie", "divertissement"],
51
+ ),
52
+ (
53
+ "German",
54
+ "Die neue KI-Technologie wird die Zukunft verändern.",
55
+ ["Technologie", "Sport", "Politik", "Unterhaltung"],
56
+ ),
57
+ ]
58
+
59
+ for language, text, labels in texts:
60
+ result = classifier(text, labels)
61
+ print(f"{language}: {text}")
62
+ print(f"Predicted: {result['labels'][0]} (score: {result['scores'][0]:.4f})")
63
+ print()
64
+
65
+ # Example 3: Multi-label classification
66
+ print("Example 3: Multi-label Classification")
67
+ text_multi = "This movie has great action scenes and amazing special effects, but the story is quite boring."
68
+ labels_multi = ["action", "drama", "comedy", "boring", "exciting", "visual effects"]
69
+
70
+ result = classifier(text_multi, labels_multi, multi_label=True)
71
+ print(f"Text: {text_multi}")
72
+ print("All predictions:")
73
+ for label, score in zip(result["labels"], result["scores"]):
74
+ print(f" {label}: {score:.4f}")
75
+ print()
76
+
77
+ # Example 4: Custom hypothesis template
78
+ print("Example 4: Custom Hypothesis Template (Spanish)")
79
+ text_es = "Esta película es realmente fantástica y emocionante."
80
+ labels_es = ["positivo", "negativo", "neutro"]
81
+ hypothesis_template = "Este texto es {}."
82
+
83
+ result = classifier(text_es, labels_es, hypothesis_template=hypothesis_template)
84
+ print(f"Text: {text_es}")
85
+ print(f"Predicted: {result['labels'][0]} (score: {result['scores'][0]:.4f})")
86
+ print(f"Using custom template: '{hypothesis_template}'")
87
+ print()
88
+
89
+ print(
90
+ "Demo completed! You can now use this model for your own zero-shot classification tasks."
91
+ )
92
+
93
+
94
+ if __name__ == "__main__":
95
+ main()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch>=1.9.0
2
+ transformers>=4.21.0
3
+ tokenizers>=0.13.0
4
+ numpy>=1.21.0
tokenizer_config.json CHANGED
@@ -1 +1,17 @@
1
- {"model_max_length": 512}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_max_length": 512,
3
+ "tokenizer_class": "XLMRobertaTokenizer",
4
+ "do_lower_case": false,
5
+ "bos_token": "<s>",
6
+ "eos_token": "</s>",
7
+ "sep_token": "</s>",
8
+ "cls_token": "<s>",
9
+ "unk_token": "<unk>",
10
+ "pad_token": "<pad>",
11
+ "mask_token": "<mask>",
12
+ "special_tokens_map_file": null,
13
+ "name_or_path": "xlm-roberta-large",
14
+ "tokenize_chinese_chars": true,
15
+ "strip_accents": null,
16
+ "do_basic_tokenize": true
17
+ }