Skip to main content

Quickstart

Welcome to the GLiClass Framework Quickstart Guide! This document will help you get started with the basics of using GLiClass.

Installation​

To install GLiClass, run the following command:

pip install gliclass

Basic Usage​

Here is a simple example to get started:

import torch
from gliclass import GLiClassModel, ZeroShotClassificationPipeline
from transformers import AutoTokenizer
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

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=device
)

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}")
Expected Output
travel => 1.000  
dreams => 1.000
sport => 1.000
science => 1.000
politics => 0.817

Next Steps​

Explore advanced features in the usage guide:

  • Hierarchical Labels: Organize labels into categories for structured classification
  • Task Prompts: Guide classification with custom task descriptions
  • Label Descriptions: Define ambiguous or domain-specific labels in the task prompt
  • Long Document Processing: Process documents exceeding token limits with automatic chunking
  • Few-Shot Learning: Improve accuracy with in-context examples
  • Retrieval-Augmented Classification (RAC): Enhance predictions with reference examples
  • Production Serving: Deploy with Ray Serve, dynamic batching, and memory-aware processing
  • Multiple Architectures: Choose from uni-encoder, bi-encoder, bi-encoder-fused, or encoder-decoder

See the production serving guide to expose GLiClass over HTTP or use its in-process serving API.