Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Store classification labels under the label key for custom label columns (#1477)

Rohit Behera9d2381f4466c
diff --git a/src/python/txtai/data/labels.py b/src/python/txtai/data/labels.py
index 54816949..531e1956 100644
--- a/src/python/txtai/data/labels.py
+++ b/src/python/txtai/data/labels.py
@@ -35,8 +35,8 @@ class Labels(Data):
# Tokenizer inputs can be single string or string pair, depending on task
text = (data[text1], data[text2]) if text2 else (data[text1],)
- # Tokenize text and add label
+ # Tokenize text and add label, models expect it under the label key
inputs = self.tokenizer(*text, max_length=self.maxlength, padding=True, truncation=True)
- inputs[label] = data[label]
+ inputs["label"] = data[label]
return inputs
diff --git a/test/python/testpipeline/testtrain/testtrainer.py b/test/python/testpipeline/testtrain/testtrainer.py
index 1dfe1279..653b4e71 100644
--- a/test/python/testpipeline/testtrain/testtrainer.py
+++ b/test/python/testpipeline/testtrain/testtrainer.py
@@ -86,6 +86,19 @@ class TestTrainer(unittest.TestCase):
labels = Labels((model, tokenizer), dynamic=False)
self.assertEqual(labels("cat")[0][0], 1)
+ def testCustomLabel(self):
+ """
+ Test training a model with a label column not named label
+ """
+
+ data = [{"text": x["text"], "target": x["label"]} for x in self.data]
+
+ trainer = HFTrainer()
+ model, tokenizer = trainer("google/bert_uncased_L-2_H-128_A-2", data, columns=("text", "target"))
+
+ labels = Labels((model, tokenizer), dynamic=False)
+ self.assertEqual(labels("cat")[0][0], 1)
+
def testDataFrame(self):
"""
Test training a model with a mock pandas DataFrame