Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Add sequences pipeline, closes #261

davidmezzetti2d4f905a7cd3
diff --git a/src/python/txtai/pipeline/text/__init__.py b/src/python/txtai/pipeline/text/__init__.py
index 19603640..e1ac9b54 100644
--- a/src/python/txtai/pipeline/text/__init__.py
+++ b/src/python/txtai/pipeline/text/__init__.py
@@ -6,6 +6,7 @@ from .entity import Entity
from .extractor import Extractor
from .labels import Labels
from .questions import Questions
+from .sequences import Sequences
from .similarity import Similarity
from .summary import Summary
from .translation import Translation
diff --git a/src/python/txtai/pipeline/text/sequences.py b/src/python/txtai/pipeline/text/sequences.py
new file mode 100644
index 00000000..e03160b6
--- /dev/null
+++ b/src/python/txtai/pipeline/text/sequences.py
@@ -0,0 +1,54 @@
+"""
+Sequences module
+"""
+
+from ..hfpipeline import HFPipeline
+
+
+class Sequences(HFPipeline):
+ """
+ Runs text through a sequence-sequence model.
+ """
+
+ def __init__(self, path=None, quantize=False, gpu=True, model=None):
+ super().__init__("text2text-generation", path, quantize, gpu, model)
+
+ def __call__(self, text, prefix=None, max_length=512, workers=0):
+ """
+ Runs a sequence-sequence model for input texts.
+
+ Args:
+ text: text|list
+ workers: number of concurrent workers to use for processing data, defaults to None
+
+ Returns:
+ generated text
+ """
+
+ # List of texts
+ texts = text if isinstance(text, list) else [text]
+
+ # Add prefix, if necessary
+ if prefix:
+ texts = [f"{prefix}{x}" for x in texts]
+
+ # Run text2text pipeline
+ results = self.pipeline(texts, max_length=max_length, num_workers=workers)
+
+ # Get generated text
+ results = [self.clean(x["generated_text"]) for x in results]
+
+ return results[0] if isinstance(text, str) else results
+
+ def clean(self, text):
+ """
+ Applies a series of rules to clean generated text.
+
+ Args:
+ text: input text
+
+ Returns:
+ clean text
+ """
+
+ return text.replace("$=", "<=")
diff --git a/test/python/testpipeline/testsequences.py b/test/python/testpipeline/testsequences.py
new file mode 100644
index 00000000..88884847
--- /dev/null
+++ b/test/python/testpipeline/testsequences.py
@@ -0,0 +1,21 @@
+"""
+Sequences module tests
+"""
+
+import unittest
+
+from txtai.pipeline import Sequences
+
+
+class TestSequences(unittest.TestCase):
+ """
+ Sequences tests.
+ """
+
+ def testGeneration(self):
+ """
+ Test text2text pipeline generation
+ """
+
+ model = Sequences("t5-small")
+ self.assertEqual(model("Testing the model", prefix="translate English to German: "), "Das Modell zu testen")