跳到内容

标签

pipeline pipeline

Labels 管道使用文本分类模型为输入文本应用标签。此管道可以使用零样本模型(动态标注)或标准文本分类模型(固定标注)对文本进行分类。

示例

以下是使用此管道的一个简单示例。

from txtai.pipeline import Labels

# Create and run pipeline
labels = Labels()
labels(
    ["Great news", "That's rough"],
    ["positive", "negative"]
)

有关更详细的示例,请参阅下面的链接。

Notebook 描述
使用零样本分类应用标签 使用零样本学习进行标注、分类和主题建模 Open In Colab

配置驱动的示例

管道通过 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
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

__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
def __call__(self, text, labels=None, multilabel=False, flatten=None, workers=0, **kwargs):
    """
    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
        kwargs: additional keyword args

    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, **kwargs)

    # 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