Skip to main content

Generalist and Lightweight Model for Text Classification

Project description

⭐ GLiClass: Generalist and Lightweight Model for Sequence Classification

GLiClass is an efficient, zero-shot sequence classification model inspired by the GLiNER framework. It achieves comparable performance to traditional cross-encoder models while being significantly more computationally efficient, offering classification results approximately 10 times faster by performing classification in a single forward pass.

📄 Blog   •   📢 Discord   •   📺 Demo   •   🤗 Available models   •  

🚀 Quick Start

Install GLiClass easily using pip:

pip install gliclass

Install from Source

Clone and install directly from GitHub:

git clone https://github.com/Knowledgator/GLiClass
cd GLiClass

python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate

pip install -r requirements.txt
pip install .

Verify your installation:

import gliclass
print(gliclass.__version__)

🧑‍💻 Usage Example

from gliclass import GLiClassModel, ZeroShotClassificationPipeline
from transformers import AutoTokenizer

model = GLiClassModel.from_pretrained("knowledgator/gliclass-small-v1.0")
tokenizer = AutoTokenizer.from_pretrained("knowledgator/gliclass-small-v1.0")

pipeline = ZeroShotClassificationPipeline(
    model, tokenizer, classification_type='multi-label', device='cuda:0'
)

text = "One day I will see the world!"
labels = ["travel", "dreams", "sport", "science", "politics"]
results = pipeline(text, labels, threshold=0.5)[0]

for result in results:
    print(f"{result['label']} => {result['score']:.3f}")

🔥 New Features

Hierarchical Labels

GLiClass now supports hierarchical label structures using dot notation:

hierarchical_labels = {
    "sentiment": ["positive", "negative", "neutral"],
    "topic": ["product", "service", "shipping"]
}

text = "The product quality is amazing but delivery was slow"
results = pipeline(text, hierarchical_labels, threshold=0.5)[0]

for result in results:
    print(f"{result['label']} => {result['score']:.3f}")
# Output:
# sentiment.positive => 0.892
# topic.product => 0.921
# topic.shipping => 0.763

Get hierarchical output matching your input structure:

results = pipeline(text, hierarchical_labels, return_hierarchical=True)[0]
print(results)
# Output:
# {
#     "sentiment": {"positive": 0.892, "negative": 0.051, "neutral": 0.124},
#     "topic": {"product": 0.921, "service": 0.153, "shipping": 0.763}
# }

Few-Shot Examples

Improve classification accuracy with in-context examples using the <<EXAMPLE>> token:

examples = [
    {
        "text": "Love this item, great quality!",
        "labels": ["positive", "product"]
    },
    {
        "text": "Customer support was unhelpful",
        "labels": ["negative", "service"]
    }
]

text = "Fast delivery and the item works perfectly!"
labels = ["positive", "negative", "product", "service", "shipping"]

results = pipeline(text, labels, examples=examples, threshold=0.5)[0]

for result in results:
    print(f"{result['label']} => {result['score']:.3f}")

Task Description Prompts

Add custom prompts to guide the classification task:

text = "The battery life on this phone is incredible"
labels = ["positive", "negative", "neutral"]

results = pipeline(
    text,
    labels,
    prompt="Classify the sentiment of this product review:",
    threshold=0.5
)[0]

Use per-text prompts for batch processing:

texts = ["Review about electronics", "Review about clothing"]
prompts = [
    "Analyze this electronics review:",
    "Analyze this clothing review:"
]

results = pipeline(texts, labels, prompt=prompts)

Long Document Classification

Process long documents with automatic text chunking:

from gliclass import ZeroShotClassificationWithChunkingPipeline

chunking_pipeline = ZeroShotClassificationWithChunkingPipeline(
    model,
    tokenizer,
    text_chunk_size=8192,
    text_chunk_overlap=256,
    labels_chunk_size=8
)

long_document = "..." # Very long text
labels = ["category1", "category2", "category3"]

results = chunking_pipeline(long_document, labels, threshold=0.5)

🌟 Retrieval-Augmented Classification (RAC)

With new models trained with retrieval-agumented classification, such as this model you can specify examples to improve classification accuracy:

example = {
    "text": "A new machine learning platform automates complex data workflows but faces integration issues.",
    "all_labels": ["AI", "automation", "data_analysis", "usability", "integration"],
    "true_labels": ["AI", "integration", "automation"]
}

text = "The new AI-powered tool streamlines data analysis but has limited integration capabilities."
labels = ["AI", "automation", "data_analysis", "usability", "integration"]

results = pipeline(text, labels, threshold=0.1, rac_examples=[example])[0]

for predict in results:
    print(f"{predict['label']} => {predict['score']:.3f}")

🎯 Key Use Cases

  • Sentiment Analysis: Rapidly classify texts as positive, negative, or neutral.
  • Document Classification: Efficiently organize and categorize large document collections.
  • Search Results Re-ranking: Improve relevance and precision by reranking search outputs.
  • News Categorization: Automatically tag and organize news articles into predefined categories.
  • Fact Checking: Quickly validate and categorize statements based on factual accuracy.

🛠️ How to Train

Prepare your training data as follows:

[
  {"text": "Sample text.", "all_labels": ["sports", "science", "business"], "true_labels": ["sports"]},
  ...
]

Optionally, specify confidence scores explicitly:

[
  {"text": "Sample text.", "all_labels": ["sports", "science"], "true_labels": {"sports": 0.9}},
  ...
]

Please, refer to the train.py script to set up your training from scratch or fine-tune existing models.

⚙️ Advanced Configuration

Architecture Types

GLiClass supports multiple architecture types:

  • uni-encoder: Single encoder for both text and labels (default, most efficient)
  • bi-encoder: Separate encoders for text and labels
  • bi-encoder-fused: Bi-encoder with label embeddings fused into text encoding
  • encoder-decoder: Encoder-decoder architecture for sequence-to-sequence tasks
from gliclass import GLiClassBiEncoder

# Load a bi-encoder model
model = GLiClassBiEncoder.from_pretrained("knowledgator/gliclass-biencoder-v1.0")

Pooling Strategies

Configure how token embeddings are pooled:

  • first: First token (CLS token)
  • avg: Average pooling
  • max: Max pooling
  • last: Last token
  • sum: Sum pooling
  • rms: Root mean square pooling
  • abs_max: Max of absolute values
  • abs_avg: Average of absolute values
from gliclass import GLiClassModelConfig

config = GLiClassModelConfig(
    pooling_strategy='avg',
    class_token_pooling='average'  # or 'first'
)

Scoring Mechanisms

Choose different scoring mechanisms for classification:

  • simple: Dot product (fastest)
  • weighted-dot: Weighted dot product with learned projections
  • mlp: Multi-layer perceptron scorer
  • hopfield: Hopfield network-based scorer
config = GLiClassModelConfig(
    scorer_type='mlp'
)

Flash Attention Backends

Enable Flash Attention for faster inference (requires additional packages):

# Install flash backends
pip install flashdeberta  # For DeBERTa models
pip install turbot5       # For T5 models

# Enable in config
config = GLiClassModelConfig(
    use_flash=True
)

📚 Citations

If you find GLiClass useful in your research or project, please cite our papers:

@misc{stepanov2025gliclassgeneralistlightweightmodel,
      title={GLiClass: Generalist Lightweight Model for Sequence Classification Tasks}, 
      author={Ihor Stepanov and Mykhailo Shtopko and Dmytro Vodianytskyi and Oleksandr Lukashov and Alexander Yavorskyi and Mykyta Yaroshenko},
      year={2025},
      eprint={2508.07662},
      archivePrefix={arXiv},
      primaryClass={cs.LG},
      url={https://arxiv.org/abs/2508.07662}, 
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gliclass-0.1.13.tar.gz (44.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gliclass-0.1.13-py3-none-any.whl (45.4 kB view details)

Uploaded Python 3

File details

Details for the file gliclass-0.1.13.tar.gz.

File metadata

  • Download URL: gliclass-0.1.13.tar.gz
  • Upload date:
  • Size: 44.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for gliclass-0.1.13.tar.gz
Algorithm Hash digest
SHA256 d226a300c79766c8f4000bb1544ec8175012fb178e1ec078ce25980379b7b519
MD5 5b4e82683aab9b176d2ac165499cb7a9
BLAKE2b-256 40d87f0355207a320e8a309e93238c2d4fcbfd50c057fd2ef31411c9e37ebbe2

See more details on using hashes here.

File details

Details for the file gliclass-0.1.13-py3-none-any.whl.

File metadata

  • Download URL: gliclass-0.1.13-py3-none-any.whl
  • Upload date:
  • Size: 45.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for gliclass-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 e8ff392c9ecef1ffb53577ca865b8fe33e7d459a3050f48cd8b8a9a3477f68a9
MD5 e024f4728ef459daacc20e73fcbc95d2
BLAKE2b-256 c7d8c49b270cbfb1325978c91397c0969d92b89757ceb979a5b547451cb122f2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page