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 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.
__init__(path=None, quantize=False, gpu=True, model=None, dynamic=True, **kwargs)
Source code in txtai/pipeline/text/labels.py
|
|
__call__(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.
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
|
|