标签
标签管道使用文本分类模型为输入文本应用标签。此管道可以使用零样本模型(动态标签)或标准文本分类模型(固定标签)对文本进行分类。
示例
以下展示了一个使用此管道的简单示例。
from txtai.pipeline import Labels
# 创建并运行管道
labels = Labels()
labels(
["好消息", "那很糟糕"],
["正面", "负面"]
)
请参阅下面的链接以获取更详细的示例。
笔记本 | 描述 | |
---|---|---|
使用零样本分类应用标签 | 使用零样本学习进行标签、分类和主题建模 |
配置驱动的示例
管道可以通过 Python 或配置运行。管道可以通过使用管道的小写名称在配置中实例化。配置驱动的管道可以通过工作流或API运行。
config.yml
# 使用小写类名创建管道
labels:
# 使用工作流运行管道
workflow:
labels:
tasks:
- action: labels
args: [["正面", "负面"]]
使用工作流运行
from txtai import Application
# 使用工作流创建并运行管道
app = Application("config.yml")
list(app.workflow("labels", ["好消息", "那很糟糕"]))
使用 API 运行
CONFIG=config.yml uvicorn "txtai.api:app" &
curl \
-X POST "http://localhost:8000/workflow" \
-H "Content-Type: application/json" \
-d '{"name":"labels", "elements": ["好消息", "那很糟糕"]}'
方法
管道的 Python 文档。
__init__(path=None, quantize=False, gpu=True, model=None, dynamic=True, **kwargs)
Source code in txtai/pipeline/text/labels.py
13 14 15 16 17 |
|
__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
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|