标签
Labels 管道使用文本分类模型为输入文本应用标签。此管道可以使用零样本模型(动态标注)或标准文本分类模型(固定标注)对文本进行分类。
示例
以下是使用此管道的一个简单示例。
from txtai.pipeline import Labels
# Create and run pipeline
labels = Labels()
labels(
["Great news", "That's rough"],
["positive", "negative"]
)
有关更详细的示例,请参阅下面的链接。
Notebook | 描述 | |
---|---|---|
使用零样本分类应用标签 | 使用零样本学习进行标注、分类和主题建模 |
配置驱动的示例
管道通过 Python 或配置运行。可以在配置中使用管道的小写名称实例化管道。配置驱动的管道使用工作流或API运行。
config.yml
# Create pipeline using lower case class name
labels:
# Run pipeline with workflow
workflow:
labels:
tasks:
- action: labels
args: [["positive", "negative"]]
使用工作流运行
from txtai import Application
# Create and run pipeline with workflow
app = Application("config.yml")
list(app.workflow("labels", ["Great news", "That's rough"]))
使用 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"]}'
方法
此管道的 Python 文档。
__init__(path=None, quantize=False, gpu=True, model=None, dynamic=True, **kwargs)
源代码位于 txtai/pipeline/text/labels.py
13 14 15 16 17 |
|
__call__(text, labels=None, multilabel=False, flatten=None, workers=0, **kwargs)
将文本分类器应用于文本。返回按最高分数排序的 (id, score) 列表,其中 id 是标签中的索引。对于零样本分类,需要一个标签列表。对于文本分类模型,标签列表是可选的,否则返回所有已训练的标签。
此方法支持字符串或列表作为文本输入。如果输入是字符串,则返回类型为一维的 (id, score) 列表。如果文本是列表,则返回二维的 (id, score) 列表,每行对应一个字符串。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
text
|
字符串|列表 |
必需 | |
labels
|
标签列表 |
None
|
|
multilabel
|
如果为 True,则标签是独立的;如果为 False,则分数按文本项归一化,总和为 1;如果为 None,则返回原始分数。 |
False
|
|
flatten
|
如果存在,则将输出展平为标签列表。接受布尔值或浮点值,仅保留大于该值的分数。 |
None
|
|
workers
|
用于处理数据的并发工作者数量,默认为 None |
0
|
|
kwargs
|
附加关键字参数 |
{}
|
返回值
类型 | 描述 |
---|---|
根据 flatten 参数,返回 (id, score) 列表或标签列表 |
源代码位于 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 57 |
|