Implementation:Neuml Txtai Text Classification
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, NLP, Text Classification, Transformers |
| Last Updated | 2026-02-10 01:00 GMT |
Overview
Concrete tool for applying text classification models (both zero-shot and standard) to text provided by txtai.
Description
Labels extends HFPipeline and supports two modes of text classification: zero-shot classification (dynamic labels at inference time) and standard text classification (fixed labels from model training). In dynamic mode, it uses the Hugging Face zero-shot-classification pipeline with user-supplied candidate labels. In standard mode, it uses the text-classification pipeline with model-trained labels. The class supports multilabel classification, score normalization (sigmoid, softmax, or raw), label filtering, score thresholding via the flatten parameter, and batch processing.
Usage
Use Labels when you need to classify text against a set of labels. Choose dynamic mode (default) for zero-shot classification where labels can change at inference time, or standard mode for traditional text classification with fixed model-trained labels.
Code Reference
Source Location
- Repository: Neuml_Txtai
- File:
src/python/txtai/pipeline/text/labels.py
Signature
class Labels(HFPipeline):
def __init__(self, path=None, quantize=False, gpu=True, model=None, dynamic=True, **kwargs)
def __call__(self, text, labels=None, multilabel=False, flatten=None, workers=0, **kwargs)
def labels(self)
def outputs(self, results, labels, flatten)
def limit(self, result, labels)
Import
from txtai.pipeline.text.labels import Labels
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| text | str or list | Yes | Input text or list of texts to classify. |
| labels | list | Conditional | List of candidate labels. Required for zero-shot (dynamic) mode; optional for standard mode to filter results. |
| multilabel | bool or None | No | If True, labels are independent (sigmoid). If False, normalized to sum to 1 (softmax). If None, raw scores. Defaults to False. |
| flatten | bool or float | No | If set, returns label names instead of (id, score) tuples. If a float, only labels with scores >= that value are returned. If True, returns top-1 label. |
| workers | int | No | Number of concurrent workers for data processing. Defaults to 0. |
| dynamic | bool | No (init) | If True (default), uses zero-shot classification. If False, uses standard text classification. |
| kwargs | dict | No | Additional keyword arguments passed to the pipeline. |
Outputs
| Name | Type | Description |
|---|---|---|
| result | list | When flatten is not set: list of (label_id, score) tuples sorted by descending score. When flatten is set: list of label name strings. For string input, returns a 1D list; for list input, returns a 2D list. |
Usage Examples
from txtai.pipeline.text.labels import Labels
# Zero-shot classification (dynamic labels)
labels = Labels()
result = labels("This is a great movie", labels=["positive", "negative"])
# Returns: [(0, 0.95), (1, 0.05)]
# Flatten to get just the top label
result = labels("This is a great movie", labels=["positive", "negative"], flatten=True)
# Returns: ["positive"]
# Standard text classification (fixed labels)
labels = Labels("distilbert-base-uncased-finetuned-sst-2-english", dynamic=False)
result = labels("This is a great movie")
# Returns: [(1, 0.99), (0, 0.01)] where 1=POSITIVE, 0=NEGATIVE
# Batch classification
results = labels(["Great movie!", "Terrible film."])
# Returns: [[(1, 0.99), (0, 0.01)], [(0, 0.98), (1, 0.02)]]
# Multilabel classification
result = labels("This is a fun action movie", labels=["action", "comedy", "drama"], multilabel=True)