GLiNER: Generalist & Lightweight Named Entity Recognition
GLiNER is a Python library providing a Generalist and Lightweight Model for Named Entity Recognition (NER). It's built on a bidirectional transformer encoder (BERT-like) and can identify any entity type using zero-shot capabilities. It serves as an efficient alternative to traditional NER models and large language models (LLMs) for resource-constrained environments, offering competitive performance on CPUs and consumer hardware. The library currently stands at version 0.2.26 with frequent updates and an active development cadence.
Warnings
- gotcha GLiNER requires `transformers>=4.51.3,<5`. This specific version constraint can lead to conflicts when integrating GLiNER into projects that use other machine learning libraries (e.g., `sentence-transformers`) which might require `transformers>=4.57` (or `transformers 5.x`).
- breaking Starting from `v0.2.23`, GLiNER's updated `transformers` dependency (`>=4.57.3`, pulling `transformers 5.x`) breaks deployment on HuggingFace Inference Endpoints due to incompatibility with the `huggingface-inference-toolkit`.
- gotcha Older versions of GLiNER (prior to `0.2.25`) contained bugs that could cause crashes related to `apex.amp` imports if `torch` was not installed with CUDA, or issues with ONNX model loading when a `torch` file was unexpectedly absent.
Install
-
pip install gliner -
pip install 'gliner[gpu]' # for GPU-backed ONNX runtime
Imports
- GLiNER
from gliner import GLiNER
Quickstart
from gliner import GLiNER
# Initialize GLiNER with a pre-trained model
# Available models: "urchade/gliner_base", "urchade/gliner_small-v2.1", "urchade/gliner_medium-v2.1", "urchade/gliner_large-v2.1"
model = GLiNER.from_pretrained("urchade/gliner_medium-v2.1")
text = """Cristiano Ronaldo dos Santos Aveiro (Portuguese pronunciation: [kɾiʃˈtjɐnu ʁɔˈnaldu]; born 5 February 1985) is a Portuguese professional footballer who plays as a forward for and captains both Saudi Pro League club Al Nassr and the Portugal national team. Widely regarded as one of the greatest players of all time, Ronaldo has won five Ballon d'Or awards, a record three UEFA Men's Player of the Year Awards, and four European Golden Shoes, the most by a European player."""
# Define the entity labels you want to extract
labels = ["person", "team", "organization", "location", "award", "nationality", "sport", "date"]
# Predict entities
entities = model.predict_entities(text, labels)
# Print the extracted entities
for entity in entities:
print(f"Text: {entity['text']}, Label: {entity['label']}, Score: {entity['score']:.2f}")