Skip to content

概述

pipeline pipeline

概述管道用于总结文本。该管道运行一个文本到文本模型,该模型抽象地生成输入文本的摘要。

示例

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

from txtai.pipeline import Summary

# 创建并运行管道
summary = Summary()
summary("在此输入需要总结的长篇详细文本")

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

笔记本 描述
构建抽象文本摘要 运行抽象文本摘要 在 Colab 中打开

配置驱动的示例

管道可以通过 Python 或配置来运行。管道可以通过配置中使用管道的小写名称来实例化。配置驱动的管道可以通过工作流API运行。

config.yml

# 使用小写类名创建管道
summary:

# 使用工作流运行管道
workflow:
  summary:
    tasks:
      - action: summary

使用工作流运行

from txtai import Application

# 使用工作流创建并运行管道
app = Application("config.yml")
list(app.workflow("summary", ["在此输入需要总结的长篇详细文本"]))

使用 API 运行

CONFIG=config.yml uvicorn "txtai.api:app" &

curl \
  -X POST "http://localhost:8000/workflow" \
  -H "Content-Type: application/json" \
  -d '{"name":"summary", "elements":["在此输入需要总结的长篇详细文本"]}'

方法

管道的 Python 文档。

__init__(path=None, quantize=False, gpu=True, model=None, **kwargs)

Source code in txtai/pipeline/text/summary.py
15
16
def __init__(self, path=None, quantize=False, gpu=True, model=None, **kwargs):
    super().__init__("summarization", path, quantize, gpu, model, **kwargs)

__call__(text, minlength=None, maxlength=None, workers=0)

Runs a summarization model against a block of text.

This method supports text as a string or a list. If the input is a string, the return type is text. If text is a list, a list of text is returned with a row per block of text.

Parameters:

Name Type Description Default
text

text|list

required
minlength

minimum length for summary

None
maxlength

maximum length for summary

None
workers

number of concurrent workers to use for processing data, defaults to None

0

Returns:

Type Description

summary text

Source code in txtai/pipeline/text/summary.py
18
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, minlength=None, maxlength=None, workers=0):
    """
    Runs a summarization model against a block of text.

    This method supports text as a string or a list. If the input is a string, the return
    type is text. If text is a list, a list of text is returned with a row per block of text.

    Args:
        text: text|list
        minlength: minimum length for summary
        maxlength: maximum length for summary
        workers: number of concurrent workers to use for processing data, defaults to None

    Returns:
        summary text
    """

    # Validate text length greater than max length
    check = maxlength if maxlength else self.maxlength()

    # Skip text shorter than max length
    texts = text if isinstance(text, list) else [text]
    params = [(x, text if len(text) >= check else None) for x, text in enumerate(texts)]

    # Build keyword arguments
    kwargs = self.args(minlength, maxlength)

    inputs = [text for _, text in params if text]
    if inputs:
        # Run summarization pipeline
        results = self.pipeline(inputs, num_workers=workers, **kwargs)

        # Pull out summary text
        results = iter([self.clean(x["summary_text"]) for x in results])
        results = [next(results) if text else texts[x] for x, text in params]
    else:
        # Return original
        results = texts

    return results[0] if isinstance(text, str) else results