Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Fix translation chunk concatenation when showmodels is enabled (#1052)

JiangNan79908eee5a4c
diff --git a/src/python/txtai/pipeline/text/translation.py b/src/python/txtai/pipeline/text/translation.py
index 17dd1b51..cf00d7f3 100644
--- a/src/python/txtai/pipeline/text/translation.py
+++ b/src/python/txtai/pipeline/text/translation.py
@@ -202,11 +202,15 @@ class Translation(HFModel):
# Combine translations - handle splits on large text from tokenizer
results, last = [], -1
for x, i in enumerate(indices):
- v = (path, translated[x]) if showmodels else translated[x]
if i == last:
- results[-1] += v
+ if showmodels:
+ # Concatenate text portion of (path, text) tuple
+ prev = results[-1]
+ results[-1] = (prev[0], prev[1] + translated[x])
+ else:
+ results[-1] += translated[x]
else:
- results.append(v)
+ results.append((path, translated[x]) if showmodels else translated[x])
last = i
diff --git a/test/python/testpipeline/testtext/testtranslation.py b/test/python/testpipeline/testtext/testtranslation.py
index a383d968..e42b69f4 100644
--- a/test/python/testpipeline/testtext/testtranslation.py
+++ b/test/python/testpipeline/testtext/testtranslation.py
@@ -122,6 +122,31 @@ class TestTranslation(unittest.TestCase):
# Validate no translation
self.assertEqual(text, translation)
+ def testShowmodelsChunked(self):
+ """
+ Test a long translation with showmodels flag. When text is chunked
+ by the tokenizer, results should still be properly concatenated as
+ a 3-tuple (translation, language, model) rather than a malformed tuple.
+ """
+
+ text = "This is a test translation to Spanish. " * 100
+ result = self.translate(text, "es", showmodels=True)
+
+ # Result should be a tuple of exactly 3 elements
+ self.assertIsInstance(result, tuple)
+ self.assertEqual(len(result), 3)
+
+ translation, language, modelpath = result
+
+ # Translation should be a single string, not a nested tuple
+ self.assertIsInstance(translation, str)
+ self.assertIsNotNone(translation)
+ self.assertGreater(len(translation), 0)
+
+ # Language and model should be valid strings
+ self.assertEqual(language, "en")
+ self.assertIsInstance(modelpath, str)
+
def testTranslationWithShowmodels(self):
"""
Test a translation using Marian models and showmodels flag to return