Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Update labels pipeline to work with upstream text classification pipeline changes, closes #295

davidmezzettid73a556b8c67
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index f6e2f951..50665da8 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -38,7 +38,7 @@ jobs:
run: |
pip install -U pip
pip install -U wheel coverage coveralls
- pip install .[all] transformers==4.19.4 torch==1.11.0
+ pip install .[all] torch==1.11.0
python -c "import nltk; nltk.download('punkt')"
python --version
make data coverage
diff --git a/setup.py b/setup.py
index 26da799c..6c9324ed 100644
--- a/setup.py
+++ b/setup.py
@@ -6,7 +6,7 @@ with open("README.md", "r", encoding="utf-8") as f:
DESCRIPTION = "".join([line for line in f if "gh-dark-mode-only" not in line])
# Required dependencies
-install = ["faiss-cpu>=1.7.1.post2", "numpy>=1.18.4", "pyyaml>=5.3", "torch>=1.6.0", "transformers>=4.12.3", "tokenizers>=0.10.3"]
+install = ["faiss-cpu>=1.7.1.post2", "numpy>=1.18.4", "pyyaml>=5.3", "torch>=1.6.0", "transformers>=4.20.1"]
# Optional dependencies
extras = {}
@@ -49,7 +49,7 @@ extras["similarity"] = [
"hnswlib>=0.5.0",
"pymagnitude-lite>=0.1.43",
"scikit-learn>=0.23.1",
- "sentence-transformers>=2.1.0",
+ "sentence-transformers>=2.2.0",
]
extras["workflow"] = [
diff --git a/src/python/txtai/pipeline/text/labels.py b/src/python/txtai/pipeline/text/labels.py
index bbd188af..4b080477 100644
--- a/src/python/txtai/pipeline/text/labels.py
+++ b/src/python/txtai/pipeline/text/labels.py
@@ -45,10 +45,10 @@ class Labels(HFPipeline):
function = "none" if multilabel is None else "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, num_workers=workers)
+ results = self.pipeline(text, top_k=None, function_to_apply=function, num_workers=workers)
# Convert results to a list if necessary
- if not isinstance(results, list):
+ if isinstance(text, str):
results = [results]
# Build list of outputs and return
@@ -90,13 +90,11 @@ class Labels(HFPipeline):
outputs.append([(labels.index(label), result["scores"][x]) for x, label in enumerate(result["labels"])])
else:
if flatten:
- result = sorted(result, key=lambda x: x["score"], reverse=True)
result = [x["label"] for x in result if x["score"] >= threshold and (not labels or x["label"] in labels)]
outputs.append(result[:1] if isinstance(flatten, bool) else result)
else:
# Filter results using labels, if provided
- result = self.limit(result, labels)
- outputs.append(sorted(enumerate(result), key=lambda x: x[1], reverse=True))
+ outputs.append(self.limit(result, labels))
return outputs
@@ -105,19 +103,21 @@ class Labels(HFPipeline):
Filter result using labels. If labels is None, original result is returned.
Args:
- result: results array
+ result: results array sorted by score descending
labels: list of labels or None
Returns:
filtered results
"""
- # Extract scores from result
- result = [x["score"] for x in result]
+ # Get config
+ config = self.pipeline.model.config
+
+ # Resolve label ids for labels
+ result = [(config.label2id.get(x["label"], 0), x["score"]) for x in result]
if labels:
- config = self.pipeline.model.config
- indices = []
+ matches = []
for label in labels:
# Lookup label keys from model config
if label.isdigit():
@@ -127,9 +127,10 @@ class Labels(HFPipeline):
label = label.lower()
keys = [x.lower() for x in config.label2id.keys()]
- # Get index, default to 0 if not found
- indices.append(keys.index(label) if label in keys else 0)
+ # Find and add label match
+ if label in keys:
+ matches.append(keys.index(label))
- return [result[i] for i in indices]
+ return [(label, score) for label, score in result if label in matches]
return result