Labels
The Labels pipeline uses a text classification model to apply labels to input text. This pipeline can classify text using either a zero shot model (dynamic labeling) or a standard text classification model (fixed labeling).
Example
The following shows a simple example using this pipeline.
from txtai.pipeline import Labels
# Create and run pipeline
labels = Labels()
labels(
["Great news", "That's rough"],
["positive", "negative"]
)
See the link below for a more detailed example.
Notebook | Description | |
---|---|---|
Apply labels with zero shot classification | Use zero shot learning for labeling, classification and topic modeling |
Configuration-driven example
Pipelines are run with Python or configuration. Pipelines can be instantiated in configuration using the lower case name of the pipeline. Configuration-driven pipelines are run with workflows or the API.
config.yml
# Create pipeline using lower case class name
labels:
# Run pipeline with workflow
workflow:
labels:
tasks:
- action: labels
args: [["positive", "negative"]]
Run with Workflows
from txtai.app import Application
# Create and run pipeline with workflow
app = Application("config.yml")
list(app.workflow("labels", ["Great news", "That's rough"]))
Run with API
CONFIG=config.yml uvicorn "txtai.api:app" &
curl \
-X POST "http://localhost:8000/workflow" \
-H "Content-Type: application/json" \
-d '{"name":"labels", "elements": ["Great news", "Thats rough"]}'
Methods
Python documentation for the pipeline.
Source code in txtai/pipeline/text/labels.py
def __init__(self, path=None, quantize=False, gpu=True, model=None, dynamic=True, **kwargs):
super().__init__("zero-shot-classification" if dynamic else "text-classification", path, quantize, gpu, model, **kwargs)
# Set if labels are dynamic (zero shot) or fixed (standard text classification)
self.dynamic = dynamic
Applies a text classifier to text. Returns a list of (id, score) sorted by highest score, where id is the index in labels. For zero shot classification, a list of labels is required. For text classification models, a list of labels is optional, otherwise all trained labels are returned.
This method supports text as a string or a list. If the input is a string, the return type is a 1D list of (id, score). If text is a list, a 2D list of (id, score) is returned with a row per string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text | text|list | required | |
labels | list of labels | None | |
multilabel | labels are independent if True, scores are normalized to sum to 1 per text item if False, raw scores returned if None | False | |
flatten | flatten output to a list of labels if present. Accepts a boolean or float value to only keep scores greater than that number. | None | |
workers | number of concurrent workers to use for processing data, defaults to None | 0 |
Returns:
Type | Description |
---|---|
list of (id, score) or list of labels depending on flatten parameter |
Source code in txtai/pipeline/text/labels.py
def __call__(self, text, labels=None, multilabel=False, flatten=None, workers=0):
"""
Applies a text classifier to text. Returns a list of (id, score) sorted by highest score,
where id is the index in labels. For zero shot classification, a list of labels is required.
For text classification models, a list of labels is optional, otherwise all trained labels are returned.
This method supports text as a string or a list. If the input is a string, the return
type is a 1D list of (id, score). If text is a list, a 2D list of (id, score) is
returned with a row per string.
Args:
text: text|list
labels: list of labels
multilabel: labels are independent if True, scores are normalized to sum to 1 per text item if False, raw scores returned if None
flatten: flatten output to a list of labels if present. Accepts a boolean or float value to only keep scores greater than that number.
workers: number of concurrent workers to use for processing data, defaults to None
Returns:
list of (id, score) or list of labels depending on flatten parameter
"""
if self.dynamic:
# Run zero shot classification pipeline
results = self.pipeline(text, labels, multi_label=multilabel, truncation=True, num_workers=workers)
else:
# Set classification function based on inputs
function = "none" if multilabel is None else "sigmoid" if multilabel or len(self.labels()) == 1 else "softmax"
# Run text classification pipeline
results = self.pipeline(text, top_k=None, function_to_apply=function, num_workers=workers)
# Convert results to a list if necessary
if isinstance(text, str):
results = [results]
# Build list of outputs and return
outputs = self.outputs(results, labels, flatten)
return outputs[0] if isinstance(text, str) else outputs