Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Disable default parallel processing for pipelines #118

davidmezzetti8aa0daaf9440
diff --git a/src/python/txtai/pipeline/labels.py b/src/python/txtai/pipeline/labels.py
index 253acd34..04e84f8d 100644
--- a/src/python/txtai/pipeline/labels.py
+++ b/src/python/txtai/pipeline/labels.py
@@ -16,7 +16,7 @@ class Labels(HFPipeline):
# Set if labels are dynamic (zero shot) or fixed (standard text classification)
self.dynamic = dynamic
- def __call__(self, text, labels=None, multilabel=False):
+ def __call__(self, text, labels=None, multilabel=False, 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.
@@ -30,6 +30,7 @@ class Labels(HFPipeline):
text: text|list
labels: list of labels
multilabel: labels are independent if True, otherwise scores are normalized to sum to 1 per text item
+ workers: number of parallel workers to use for processing data, defaults to none
Returns:
list of (id, score)
@@ -37,7 +38,7 @@ class Labels(HFPipeline):
if self.dynamic:
# Run zero shot classification pipeline
- results = self.pipeline(text, labels, multi_label=multilabel, truncation=True)
+ results = self.pipeline(text, labels, multi_label=multilabel, truncation=True, num_workers=workers)
# Convert results to a list if necessary
if not isinstance(results, list):
@@ -50,7 +51,7 @@ class Labels(HFPipeline):
function = "sigmoid" if multilabel or len(self.labels()) == 1 else "softmax"
# Run text classification pipeline
- results = self.pipeline(text, return_all_scores=True, function_to_apply=function)
+ results = self.pipeline(text, return_all_scores=True, function_to_apply=function, num_workers=workers)
# Build list of (id, score)
scores = []
diff --git a/src/python/txtai/pipeline/questions.py b/src/python/txtai/pipeline/questions.py
index db23fb3a..7d718339 100644
--- a/src/python/txtai/pipeline/questions.py
+++ b/src/python/txtai/pipeline/questions.py
@@ -13,13 +13,14 @@ class Questions(HFPipeline):
def __init__(self, path=None, quantize=False, gpu=False, model=None):
super().__init__("question-answering", path, quantize, gpu, model)
- def __call__(self, questions, contexts):
+ def __call__(self, questions, contexts, workers=0):
"""
Runs a extractive question-answering model against each question-context pair, finding the best answers.
Args:
questions: list of questions
contexts: list of contexts to pull answers from
+ workers: number of parallel workers to use for processing data, defaults to none
Returns:
list of answers
@@ -30,7 +31,7 @@ class Questions(HFPipeline):
for x, question in enumerate(questions):
if question and contexts[x]:
# Run the QA pipeline
- result = self.pipeline(question=question, context=contexts[x])
+ result = self.pipeline(question=question, context=contexts[x], num_workers=workers)
# Get answer and score
answer, score = result["answer"], result["score"]
diff --git a/src/python/txtai/pipeline/summary.py b/src/python/txtai/pipeline/summary.py
index cb371068..4d9de8b1 100644
--- a/src/python/txtai/pipeline/summary.py
+++ b/src/python/txtai/pipeline/summary.py
@@ -15,7 +15,7 @@ class Summary(HFPipeline):
def __init__(self, path=None, quantize=False, gpu=True, model=None):
super().__init__("summarization", path, quantize, gpu, model)
- def __call__(self, text, minlength=None, maxlength=None):
+ def __call__(self, text, minlength=None, maxlength=None, workers=0):
"""
Runs a summarization model against a block of text.
@@ -26,6 +26,7 @@ class Summary(HFPipeline):
text: text|list
minlength: minimum length for summary
maxlength: maximum length for summary
+ workers: number of parallel workers to use for processing data, defaults to none
Returns:
summary text
@@ -47,7 +48,7 @@ class Summary(HFPipeline):
inputs = [text for _, text in params if text]
if inputs:
# Run summarization pipeline
- results = self.pipeline(inputs, **kwargs)
+ results = self.pipeline(inputs, num_workers=workers, **kwargs)
# Pull out summary text
results = iter([self.clean(x["summary_text"]) for x in results])
diff --git a/test/python/testsummary.py b/test/python/testsummary.py
index 8b85d952..144521c4 100644
--- a/test/python/testsummary.py
+++ b/test/python/testsummary.py
@@ -2,7 +2,6 @@
Summary module tests
"""
-import os
import unittest
from txtai.pipeline import Summary
@@ -30,7 +29,6 @@ class TestSummary(unittest.TestCase):
cls.summary = Summary("t5-small")
- @unittest.skipIf(os.name == "nt", "testSummary skipped on Windows")
def testSummary(self):
"""
Test summarization of text
@@ -38,7 +36,6 @@ class TestSummary(unittest.TestCase):
self.assertEqual(self.summary(self.text, minlength=15, maxlength=15), "txtai is an AI-powered search engine that")
- @unittest.skipIf(os.name == "nt", "testSummaryBatch skipped on Windows")
def testSummaryBatch(self):
"""
Test batch summarization of text
diff --git a/test/python/testworkflow.py b/test/python/testworkflow.py
index bddca266..4c9126a7 100644
--- a/test/python/testworkflow.py
+++ b/test/python/testworkflow.py
@@ -2,7 +2,6 @@
Workflow module tests
"""
-import os
import unittest
from txtai.embeddings import Documents, Embeddings
@@ -32,7 +31,6 @@ class TestWorkflow(unittest.TestCase):
self.assertEqual(len(results), 2)
- @unittest.skipIf(os.name == "nt", "testComplexWorkflow skipped on Windows")
def testComplexWorkflow(self):
"""
Tests a complex workflow