14----运行管道工作流程

txtai提供了一种使用管道和工作流来组合不同机器学习模型的方法,如HuggingFace的模型和PyTorch模型。工作流使得能够串联文本提取、摘要、转录和翻译等任务,有效地处理大量数据。通过YAML配置,工作流可以作为独立应用或FastAPI服务运行,支持自然语言处理任务的自动化流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

txtai 通过其管道框架提供了越来越多的模型列表。管道包装机器学习模型并转换数据。目前,管道可以包装 Hugging Face 模型、Hugging Face 管道或 PyTorch 模型(对 TensorFlow 的支持在待办事项中)。

以下是当前实施的管道列表。

问题- 使用文本上下文回答问题
标签- 使用零样本分类模型将标签应用于文本。还支持相似度比较。
摘要- 抽象文本摘要
Textractor - 从文档中提取文本
转录- 将音频转录为文本
翻译- 机器翻译
管道很棒,可以更轻松地使用各种机器学习模型。但是,如果我们想将不同管道的结果粘合在一起怎么办?例如,提取文本,对其进行总结,将其翻译成英文并将其加载到 Embedding 索引中。这将需要代码以有效的方式将这些操作连接在一起。

输入工作流程。工作流是一种简单但功能强大的构造,它接受可调用元素并返回元素。工作流不知道它们正在使用管道,但可以有效地处理管道数据。工作流本质上是流式的,可以批量处理数据,从而可以高效地处理大量数据。

安装依赖
安装txtai和所有依赖项。由于本文使用可选的管道/工作流,我们需要安装管道和工作流附加包。
pip install txtai[pipeline,workflow] sacremoses

Get test data

wget -N https://github.com/neuml/txtai/releases/download/v2.0.0/tests.tar.gz
tar -xvzf tests.tar.gz
创建一系列管道以在此笔记本中使用
from txtai.pipeline import Summary, Textractor, Transcription, Translation

Summary instance

summary = Summary()

Text extraction

textractor = Textractor()

Transcription instance

transcribe = Transcription(“facebook/wav2vec2-large-960h”)

Create a translation instance

translate = Translation()
基本工作流程
下面显示了一个基本的工作流程!
from txtai.workflow import Workflow, Task

Workflow that translate text to French

workflow = Workflow([Task(lambda x: translate(x, “fr”))])

Data to run through the pipeline

data = [“The sky is blue”, “Forest through the trees”]

Workflows are generators for efficiency, read results to list for display

list(workflow(data))
[‘Le ciel est bleu’, ‘Forêt à travers les arbres’]
这与之前的管道示例没有太大区别。唯一的区别是数据是通过工作流提供的。在此示例中,工作流调用翻译管道并将文本翻译成法语。让我们看一个更复杂的例子。

多步骤工作流程
以下工作流读取一系列音频文件,将它们转录为文本并将文本翻译成法语。这是基于来自Introducing txtai的经典 txtai 示例。

工作流有两个主要参数。要执行的操作是可调用的,也是用于过滤数据的模式。过滤器接受的数据将被处理,否则将被传递到下一个任务。
from txtai.workflow import FileTask

tasks = [
FileTask(transcribe, r".wav$"),
Task(lambda x: translate(x, “fr”))
]

List of files to process

data = [
“txtai/US_tops_5_million.wav”,
“txtai/Canadas_last_fully.wav”,
“txtai/Beijing_mobilises.wav”,
“txtai/The_National_Park.wav”,
“txtai/Maine_man_wins_1_mil.wav”,
“txtai/Make_huge_profits.wav”
]

Workflow that translate text to French

workflow = Workflow(tasks)

Run workflow

list(workflow(data))
[“Les cas de virus U sont en tête d’un million”,
“La dernière plate-forme de glace entièrement intacte du Canada s’est soudainement effondrée en formant un berge de glace de taille manhatten”,
“Bagage mobilise les embarcations d’invasion le long des côtes à mesure que les tensions tiwaniennes s’intensifient”,
“Le service des parcs nationaux met en garde contre le sacrifice d’amis plus lents dans une attaque nue”,
“L’homme principal gagne du billet de loterie”,
“Faire d’énormes profits sans travailler faire jusqu’à cent mille dollars par jour”]
复杂的工作流程
让我们将所有这些整合到一个成熟的工作流程中,以构建嵌入索引。此工作流程适用于文档和音频文件。文档将提取和总结文本。音频文件将被转录。两个结果将被合并,翻译成法语并加载到嵌入索引中。
from txtai.embeddings import Embeddings, Documents
from txtai.workflow import FileTask, WorkflowTask

Embeddings index

embeddings = Embeddings({“path”: “sentence-transformers/paraphrase-multilingual-mpnet-base-v2”, “content”: True})
documents = Documents()

List of files to process

files = [
“txtai/article.pdf”,
“txtai/US_tops_5_million.wav”,
“txtai/Canadas_last_fully.wav”,
“txtai/Beijing_mobilises.wav”,
“txtai/The_National_Park.wav”,
“txtai/Maine_man_wins_1_mil.wav”,
“txtai/Make_huge_profits.wav”
]

data = [(x, element, None) for x, element in enumerate(files)]

Workflow that extracts text and builds a summary

articles = Workflow([
FileTask(textractor),
Task(summary)
])

Define workflow tasks. Workflows can also be tasks!

tasks = [
WorkflowTask(articles, r"..pdf"),FileTask(transcribe,r"w˙av"), FileTask(transcribe, r"\.wav"),FileTask(transcribe,r"w˙av"),
Task(lambda x: translate(x, “fr”)),
Task(documents.add, unpack=False)
]

Workflow that translate text to French

workflow = Workflow(tasks)

Run workflow and show results to be indexed

for x in workflow(data):
print(x)

Build the embeddings index

embeddings.index(documents)

Cleanup temporary storage

documents.close()
(0, “Txtai, un moteur de recherche alimenté par l’IA construit sur Transformers, permet la recherche basée sur la compréhension du langage naturel (NLU) dans n’importe quelle application. Le champ de traitement du langage naturel (NLP) évolue rapidement avec un certain nombre de nouveaux développements. Le moteur de recherche open-source est open source et disponible sur GitHub.”, None)
(1, “Les cas de virus U sont en tête d’un million”, None)
(2, “La dernière plate-forme de glace entièrement intacte du Canada s’est soudainement effondrée en formant un berge de glace de taille manhatten”, None)
(3, “Bagage mobilise les embarcations d’invasion le long des côtes à mesure que les tensions tiwaniennes s’intensifient”, None)
(4, “Le service des parcs nationaux met en garde contre le sacrifice d’amis plus lents dans une attaque nue”, None)
(5, “L’homme principal gagne du billet de loterie”, None)
(6, “Faire d’énormes profits sans travailler faire jusqu’à cent mille dollars par jour”, None)
用法语查询结果

Run a search query and show the result.

embeddings.search(“changement climatique”, 1)[0]
{‘id’: ‘2’,
‘score’: 0.2982647716999054,
‘text’: “La dernière plate-forme de glace entièrement intacte du Canada s’est soudainement effondrée en formant un berge de glace de taille manhatten”}

Run a search query and show the result.

embeddings.search(“traitement du langage naturel”, 1)[0]
{‘id’: ‘0’,
‘score’: 0.47031939029693604,
‘text’: “Txtai, un moteur de recherche alimenté par l’IA construit sur Transformers, permet la recherche basée sur la compréhension du langage naturel (NLU) dans n’importe quelle application. Le champ de traitement du langage naturel (NLP) évolue rapidement avec un certain nombre de nouveaux développements. Le moteur de recherche open-source est open source et disponible sur GitHub.”}
配置驱动的工作流
工作流也可以使用 YAML 定义并作为应用程序运行。应用程序可以独立运行或作为 FastAPI 实例运行。可以在此处找到更多信息。
workflow = “”"
writable: true
embeddings:
path: sentence-transformers/paraphrase-multilingual-mpnet-base-v2
content: True

Summarize text

summary:

Extract text from documents

textractor:

Transcribe audio to text

transcription:
path: facebook/wav2vec2-large-960h

Translate text between languages

translation:

workflow:
summarize:
tasks:
- action: textractor
task: file
- summary
index:
tasks:
- action: summarize
select: ‘\.pdf′−action:transcriptionselect:′.wav' - action: transcription select: '\\.wavaction:transcriptionselect:.wav
task: file
- action: translation
args: [‘fr’]
- action: index
“”"

Create and run the workflow

from txtai.app import Application

Create and run the workflow

app = Application(workflow)
list(app.workflow(“index”, files))
[“Txtai, un moteur de recherche alimenté par l’IA construit sur Transformers, permet la recherche basée sur la compréhension du langage naturel (NLU) dans n’importe quelle application. Le champ de traitement du langage naturel (NLP) évolue rapidement avec un certain nombre de nouveaux développements. Le moteur de recherche open-source est open source et disponible sur GitHub.”,
“Les cas de virus U sont en tête d’un million”,
“La dernière plate-forme de glace entièrement intacte du Canada s’est soudainement effondrée en formant un berge de glace de taille manhatten”,
“Bagage mobilise les embarcations d’invasion le long des côtes à mesure que les tensions tiwaniennes s’intensifient”,
“Le service des parcs nationaux met en garde contre le sacrifice d’amis plus lents dans une attaque nue”,
“L’homme principal gagne du billet de loterie”,
“Faire d’énormes profits sans travailler faire jusqu’à cent mille dollars par jour”]

Run a search query and show the result.

app.search(“changement climatique”, 1)[0]
{‘id’: ‘2’,
‘score’: 0.2982647716999054,
‘text’: “La dernière plate-forme de glace entièrement intacte du Canada s’est soudainement effondrée en formant un berge de glace de taille manhatten”}

Run a search query and show the result.

app.search(“traitement du langage naturel”, 1)[0
{‘id’: ‘0’,
‘score’: 0.47031939029693604,
‘text’: “Txtai, un moteur de recherche alimenté par l’IA construit sur Transformers, permet la recherche basée sur la compréhension du langage naturel (NLU) dans n’importe quelle application. Le champ de traitement du langage naturel (NLP) évolue rapidement avec un certain nombre de nouveaux développements. Le moteur de recherche open-source est open source et disponible sur GitHub.”}
包起来
结果很好!我们可以看到工作流的力量以及它们如何以有效的方式将一系列管道连接在一起。工作流可以与任何可调用对象一起使用,而不仅仅是管道,工作流将数据从一种格式转换为另一种格式。工作流是 txtai 的一个令人兴奋和有前途的发展。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Q shen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值