Text Classification
Safetensors
GLiClass
text classification
nli
sentiment analysis
Ihor commited on
Commit
adf7db5
·
verified ·
1 Parent(s): c8abcde

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +277 -3
README.md CHANGED
@@ -1,3 +1,277 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ datasets:
4
+ - BioMike/formal-logic-reasoning-gliclass-2k
5
+ - knowledgator/gliclass-v3-logic-dataset
6
+ - tau/commonsense_qa
7
+ metrics:
8
+ - f1
9
+ tags:
10
+ - text classification
11
+ - nli
12
+ - sentiment analysis
13
+ pipeline_tag: text-classification
14
+ ---
15
+
16
+ ![image/png](instruct.png)
17
+
18
+ # GLiClass-multitask: Efficient zero-shot and few-shot multi-task model via sequence classification
19
+
20
+ GLiClass is an efficient zero-shot sequence classification model designed to achieve SoTA performance while being much faster than cross-encoders and LLMs, while preserving strong generalization capabilities.
21
+
22
+ The model supports text classification with any labels and can be used for the following tasks:
23
+ * Topic Classification
24
+ * Sentiment Analysis
25
+ * Intent Classification
26
+ * Reranking
27
+ * Hallucination Detection
28
+ * Rule-following Verification
29
+ * LLM-safety Classification
30
+ * Natural Language Inference
31
+
32
+ ## ✨ What's New in V3
33
+
34
+ - **Hierarchical Labels** — Organize labels into groups using dot notation or dictionaries (e.g., `sentiment.positive`, `topic.product`).
35
+ - **Few-Shot Examples** — Provide in-context examples to boost accuracy on your specific task.
36
+ - **Label Descriptions** — Add natural-language descriptions to labels for more precise classification.
37
+ - **Task Prompts** — Prepend a custom prompt to guide the model's classification behavior.
38
+
39
+ See the [GLiClass library README](https://github.com/Knowledgator/GLiClass) for full details on these features.
40
+
41
+ ## Installation
42
+
43
+ ```bash
44
+ pip install gliclass
45
+ ```
46
+
47
+ ## Quick Start
48
+
49
+ ```python
50
+ from gliclass import GLiClassModel, ZeroShotClassificationPipeline
51
+ from transformers import AutoTokenizer
52
+
53
+ model = GLiClassModel.from_pretrained("knowledgator/gliclass-instruct-base-v3.0")
54
+ tokenizer = AutoTokenizer.from_pretrained("knowledgator/gliclass-instruct-base-v3.0")
55
+ pipeline = ZeroShotClassificationPipeline(model, tokenizer, classification_type='multi-label', device='cuda:0')
56
+ ```
57
+
58
+ ---
59
+
60
+ ## Task Examples
61
+
62
+ ### 1. Topic Classification
63
+
64
+ ```python
65
+ text = "NASA launched a new Mars rover to search for signs of ancient life."
66
+ labels = ["space", "politics", "sports", "technology", "health"]
67
+
68
+ results = pipeline(text, labels, threshold=0.5)[0]
69
+ for r in results:
70
+ print(r["label"], "=>", r["score"])
71
+ ```
72
+
73
+ #### With hierarchical labels
74
+
75
+ ```python
76
+ hierarchical_labels = {
77
+ "science": ["space", "biology", "physics"],
78
+ "society": ["politics", "economics", "culture"]
79
+ }
80
+
81
+ results = pipeline(text, hierarchical_labels, threshold=0.5)[0]
82
+ for r in results:
83
+ print(r["label"], "=>", r["score"])
84
+ # e.g. science.space => 0.95
85
+ ```
86
+
87
+ ### 2. Sentiment Analysis
88
+
89
+ ```python
90
+ text = "The food was excellent but the service was painfully slow."
91
+ labels = ["positive", "negative", "neutral"]
92
+
93
+ results = pipeline(text, labels, threshold=0.5)[0]
94
+ for r in results:
95
+ print(r["label"], "=>", r["score"])
96
+ ```
97
+
98
+ #### With a task prompt
99
+
100
+ ```python
101
+ results = pipeline(
102
+ text, labels,
103
+ prompt="Classify the sentiment of this restaurant review:",
104
+ threshold=0.5
105
+ )[0]
106
+ ```
107
+
108
+ ### 3. Intent Classification
109
+
110
+ ```python
111
+ text = "Can you set an alarm for 7am tomorrow?"
112
+ labels = ["set_alarm", "play_music", "get_weather", "send_message", "set_reminder"]
113
+
114
+ results = pipeline(text, labels, threshold=0.5)[0]
115
+ for r in results:
116
+ print(r["label"], "=>", r["score"])
117
+ ```
118
+
119
+ #### With few-shot examples
120
+
121
+ ```python
122
+ examples = [
123
+ {"text": "Wake me up at 6:30.", "labels": ["set_alarm"]},
124
+ {"text": "Play some jazz.", "labels": ["play_music"]},
125
+ ]
126
+
127
+ results = pipeline(text, labels, examples=examples, threshold=0.5)[0]
128
+ for r in results:
129
+ print(r["label"], "=>", r["score"])
130
+ ```
131
+
132
+ ### 4. Natural Language Inference
133
+
134
+ Represent your premise as the text and the hypothesis as a label. The model works best with a single hypothesis at a time.
135
+
136
+ ```python
137
+ text = "The cat slept on the windowsill all afternoon."
138
+ labels = ["The cat was awake and playing outside."]
139
+
140
+ results = pipeline(text, labels, threshold=0.0)[0]
141
+ print(results)
142
+ # Low score → contradiction
143
+ ```
144
+
145
+ ### 5. Reranking
146
+
147
+ Score query–passage relevance by treating passages as texts and the query as the label:
148
+
149
+ ```python
150
+ query = "How to train a neural network?"
151
+ passages = [
152
+ "Backpropagation is the key algorithm for training deep neural networks.",
153
+ "The stock market rallied on strong earnings reports.",
154
+ "Gradient descent optimizes model weights during training.",
155
+ ]
156
+
157
+ for passage in passages:
158
+ score = pipeline(passage, [query], threshold=0.0)[0][0]["score"]
159
+ print(f"{score:.3f} {passage[:60]}")
160
+ ```
161
+
162
+ ### 6. Hallucination Detection
163
+
164
+ Concatenate context, question, and answer into the text field:
165
+
166
+ ```python
167
+ text = (
168
+ "Context: The Eiffel Tower was built from 1887 to 1889 and is 330 m tall. "
169
+ "It was the tallest structure until the Chrysler Building in 1930.\n"
170
+ "Question: When was the Eiffel Tower built and how tall is it?\n"
171
+ "Answer: It was built 1887–1889, stands 330 m tall, and was the tallest "
172
+ "structure until the Empire State Building in 1931."
173
+ )
174
+ labels = ["hallucinated", "correct"]
175
+
176
+ results = pipeline(text, labels, threshold=0.0)[0]
177
+ for r in results:
178
+ print(r["label"], "=>", r["score"])
179
+ # "hallucinated" should score higher (Empire State Building & 1931 are wrong)
180
+ ```
181
+
182
+ ### 7. Rule-following Verification
183
+
184
+ Include the domain and rules as part of the text:
185
+
186
+ ```python
187
+ text = (
188
+ "Domain: e-commerce product reviews\n"
189
+ "Rule: No promotion of illegal activity.\n"
190
+ "Text: The software is okay, but search for 'productname_patch_v2.zip' "
191
+ "to unlock all features for free."
192
+ )
193
+ labels = ["follows_guidelines", "violates_guidelines"]
194
+
195
+ results = pipeline(text, labels, threshold=0.0)[0]
196
+ for r in results:
197
+ print(r["label"], "=>", r["score"])
198
+ ```
199
+
200
+ ### 8. LLM-safety Classification
201
+
202
+ ```python
203
+ text = "I'm looking for a good Italian restaurant near downtown Chicago, budget ~$50/person."
204
+ labels = [
205
+ "benign request",
206
+ "prompt injection",
207
+ "system prompt extraction",
208
+ "jailbreak attempt",
209
+ "harmful content request",
210
+ "social engineering",
211
+ "data exfiltration",
212
+ ]
213
+
214
+ results = pipeline(text, labels, threshold=0.5)[0]
215
+ for r in results:
216
+ print(r["label"], "=>", r["score"])
217
+ ```
218
+
219
+ ---
220
+
221
+ ## Benchmarks
222
+
223
+ F1 scores on zero-shot text classification (no fine-tuning on these datasets):
224
+
225
+ GLiClass-V1 Multitask:
226
+
227
+ | Dataset | [large‑v1.0](https://huggingface.co/knowledgator/gliclass-instruct-large-v1.0) | [base‑v1.0](https://huggingface.co/knowledgator/gliclass-instruct-base-v1.0) | [edge‑v1.0](https://huggingface.co/knowledgator/gliclass-instruct-edge-v1.0) |
228
+ |---|---|---|---|
229
+ | CR | 0.9066 | 0.8922 | 0.7933 |
230
+ | sst2 | 0.9154 | 0.9198 | 0.7577 |
231
+ | sst5 | 0.3387 | 0.2266 | 0.2163 |
232
+ | 20_newsgroups | 0.5577 | 0.5189 | 0.2555 |
233
+ | spam | 0.9790 | 0.9380 | 0.7609 |
234
+ | financial_phrasebank | 0.8289 | 0.5217 | 0.3905 |
235
+ | imdb | 0.9397 | 0.9364 | 0.8159 |
236
+ | ag_news | 0.7521 | 0.6978 | 0.6043 |
237
+ | emotion | 0.4473 | 0.4454 | 0.2941 |
238
+ | cap_sotu | 0.4327 | 0.4579 | 0.2380 |
239
+ | rotten_tomatoes | 0.8491 | 0.8458 | 0.5455 |
240
+ | massive | 0.5824 | 0.4757 | 0.2090 |
241
+ | banking | 0.6987 | 0.6072 | 0.4635 |
242
+ | snips | 0.8509 | 0.6515 | 0.5461 |
243
+ | **AVERAGE** | **0.7199** | **0.6525** | **0.4922** |
244
+
245
+ GLiClass-V3:
246
+
247
+ | Dataset | [large‑v3.0](https://huggingface.co/knowledgator/gliclass-large-v3.0) | [base‑v3.0](https://huggingface.co/knowledgator/gliclass-base-v3.0) | [modern‑large‑v3.0](https://huggingface.co/knowledgator/gliclass-modern-large-v3.0) | [modern‑base‑v3.0](https://huggingface.co/knowledgator/gliclass-modern-base-v3.0) | [edge‑v3.0](https://huggingface.co/knowledgator/gliclass-edge-v3.0) |
248
+ |---|---|---|---|---|---|
249
+ | CR | 0.9398 | 0.9127 | 0.8952 | 0.8902 | 0.8215 |
250
+ | sst2 | 0.9192 | 0.8959 | 0.9330 | 0.8959 | 0.8199 |
251
+ | sst5 | 0.4606 | 0.3376 | 0.4619 | 0.2756 | 0.2823 |
252
+ | 20_newsgroups | 0.5958 | 0.4759 | 0.3905 | 0.3433 | 0.2217 |
253
+ | spam | 0.7584 | 0.6760 | 0.5813 | 0.6398 | 0.5623 |
254
+ | financial_phrasebank | 0.9000 | 0.8971 | 0.5929 | 0.4200 | 0.5004 |
255
+ | imdb | 0.9366 | 0.9251 | 0.9402 | 0.9158 | 0.8485 |
256
+ | ag_news | 0.7181 | 0.7279 | 0.7269 | 0.6663 | 0.6645 |
257
+ | emotion | 0.4506 | 0.4447 | 0.4517 | 0.4254 | 0.3851 |
258
+ | cap_sotu | 0.4589 | 0.4614 | 0.4072 | 0.3625 | 0.2583 |
259
+ | rotten_tomatoes | 0.8411 | 0.7943 | 0.7664 | 0.7070 | 0.7024 |
260
+ | massive | 0.5649 | 0.5040 | 0.3905 | 0.3442 | 0.2414 |
261
+ | banking | 0.5574 | 0.4698 | 0.3683 | 0.3561 | 0.0272 |
262
+ | snips | 0.9692 | 0.9474 | 0.7707 | 0.5663 | 0.5257 |
263
+ | **AVERAGE** | **0.7193** | **0.6764** | **0.6197** | **0.5577** | **0.4900** |
264
+
265
+ ## Citation
266
+
267
+ ```bibtex
268
+ @misc{stepanov2025gliclassgeneralistlightweightmodel,
269
+ title={GLiClass: Generalist Lightweight Model for Sequence Classification Tasks},
270
+ author={Ihor Stepanov and Mykhailo Shtopko and Dmytro Vodianytskyi and Oleksandr Lukashov and Alexander Yavorskyi and Mykyta Yaroshenko},
271
+ year={2025},
272
+ eprint={2508.07662},
273
+ archivePrefix={arXiv},
274
+ primaryClass={cs.LG},
275
+ url={https://arxiv.org/abs/2508.07662},
276
+ }
277
+ ```