Skip to content

标签

pipeline pipeline

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

示例

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

from txtai.pipeline import Labels

# 创建并运行管道
labels = Labels()
labels(
    ["好消息", "那很糟糕"],
    ["正面", "负面"]
)

请参阅下面的链接以获取更详细的示例。

笔记本 描述
使用零样本分类应用标签 使用零样本学习进行标签、分类和主题建模 在 Colab 中打开

配置驱动的示例

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

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
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