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 support for GLiNER models, closes #862

davidmezzetti13bafb01c6fb
diff --git a/src/python/txtai/pipeline/text/entity.py b/src/python/txtai/pipeline/text/entity.py
index ebd88c0c..61dde68b 100644
--- a/src/python/txtai/pipeline/text/entity.py
+++ b/src/python/txtai/pipeline/text/entity.py
@@ -2,6 +2,17 @@
Entity module
"""
+# Conditional import
+try:
+ from gliner import GLiNER
+
+ GLINER = True
+except ImportError:
+ GLINER = False
+
+from transformers.utils import cached_file
+
+from ...models import Models
from ..hfpipeline import HFPipeline
@@ -11,7 +22,18 @@ class Entity(HFPipeline):
"""
def __init__(self, path=None, quantize=False, gpu=True, model=None, **kwargs):
- super().__init__("token-classification", path, quantize, gpu, model, **kwargs)
+ # Create a new entity pipeline
+ self.gliner = self.isgliner(path)
+ if self.gliner:
+ if not GLINER:
+ raise ImportError('GLiNER is not available - install "pipeline" extra to enable')
+
+ # GLiNER entity pipeline
+ self.pipeline = GLiNER.from_pretrained(path)
+ self.pipeline = self.pipeline.to(Models.device(Models.deviceid(gpu)))
+ else:
+ # Standard entity pipeline
+ super().__init__("token-classification", path, quantize, gpu, model, **kwargs)
def __call__(self, text, labels=None, aggregate="simple", flatten=None, join=False, workers=0):
"""
@@ -30,7 +52,7 @@ class Entity(HFPipeline):
"""
# Run token classification pipeline
- results = self.pipeline(text, aggregation_strategy=aggregate, num_workers=workers)
+ results = self.execute(text, labels, aggregate, workers)
# Convert results to a list if necessary
if isinstance(text, str):
@@ -50,6 +72,58 @@ class Entity(HFPipeline):
return outputs[0] if isinstance(text, str) else outputs
+ def isgliner(self, path):
+ """
+ Tests if path is a GLiNER model.
+
+ Args:
+ path: model path
+
+ Returns:
+ True if this is a GLiNER model, False otherwise
+ """
+
+ try:
+ # Test if this model has a gliner_config.json file
+ return cached_file(path_or_repo_id=path, filename="gliner_config.json") is not None
+
+ # Ignore this error - invalid repo or directory
+ except OSError:
+ pass
+
+ return False
+
+ def execute(self, text, labels, aggregate, workers):
+ """
+ Runs the entity extraction pipeline.
+
+ Args:
+ text: text|list
+ labels: list of entity type labels to accept, defaults to None which accepts all
+ aggregate: method to combine multi token entities - options are "simple" (default), "first", "average" or "max"
+ workers: number of concurrent workers to use for processing data, defaults to None
+
+ Returns:
+ list of entities and labels
+ """
+
+ if self.gliner:
+ # Extract entities with GLiNER. Use default CoNLL-2003 labels when not otherwise provided.
+ results = self.pipeline.batch_predict_entities(
+ text if isinstance(text, list) else [text], labels if labels else ["person", "organization", "location"]
+ )
+
+ # Map results to same format as Transformers token classifier
+ entities = []
+ for result in results:
+ entities.append([{"word": x["text"], "entity_group": x["label"], "score": x["score"]} for x in result])
+
+ # Return extracted entities
+ return entities if isinstance(text, list) else entities[0]
+
+ # Standard Transformers token classification pipeline
+ return self.pipeline(text, aggregation_strategy=aggregate, num_workers=workers)
+
def accept(self, etype, labels):
"""
Determines if entity type is in valid entity type.
diff --git a/test/python/testoptional.py b/test/python/testoptional.py
index 74ebfc2d..d54980e3 100644
--- a/test/python/testoptional.py
+++ b/test/python/testoptional.py
@@ -27,6 +27,7 @@ class TestOptional(unittest.TestCase):
"docling.document_converter",
"duckdb",
"fastapi",
+ "gliner",
"grand-cypher",
"grand-graph",
"hnswlib",
@@ -186,6 +187,7 @@ class TestOptional(unittest.TestCase):
AudioMixer,
AudioStream,
Caption,
+ Entity,
FileToHTML,
HFOnnx,
HFTrainer,
@@ -213,6 +215,9 @@ class TestOptional(unittest.TestCase):
with self.assertRaises(ImportError):
Caption()
+ with self.assertRaises(ImportError):
+ Entity("neuml/gliner-bert-tiny")
+
with self.assertRaises(ImportError):
FileToHTML(backend="docling")
diff --git a/test/python/testpipeline/testtext/testentity.py b/test/python/testpipeline/testtext/testentity.py
index f722f040..9868f7ed 100644
--- a/test/python/testpipeline/testtext/testentity.py
+++ b/test/python/testpipeline/testtext/testentity.py
@@ -52,3 +52,12 @@ class TestEntity(unittest.TestCase):
# Run entity extraction
entities = self.entity("Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg", labels=["PER"])
self.assertFalse(entities)
+
+ def testGliner(self):
+ """
+ Test entity pipeline with a GLiNER model
+ """
+
+ entity = Entity("neuml/gliner-bert-tiny")
+ entities = entity("My name is John Smith.", flatten=True)
+ self.assertEqual(entities, ["John Smith"])