GLiNER: Generalist & Lightweight Named Entity Recognition

0.2.26 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates how to load a pre-trained GLiNER model and use it to extract named entities from a given text based on a custom list of labels. The `predict_entities` method returns a list of dictionaries, each containing the extracted text, its predicted label, and a confidence score.

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}")

view raw JSON →