gliner pytorch NER GLiNER information extraction encoder entity recognition token-classification multilingual dataset:urchade/pile-mistral-v0.1 dataset:knowledgator/GLINER-multi-task-synthetic-data dataset:EmergentMethods/AskNews-NER-v0 license:apache-2.0 region:us
---
datasets:
- urchade/pile-mistral-v0.1
- knowledgator/GLINER-multi-task-synthetic-data
- EmergentMethods/AskNews-NER-v0
language:
- multilingual
library_name: gliner
license: apache-2.0
pipeline_tag: token-classification
tags:
- NER
- GLiNER
- information extraction
- encoder
- entity recognition
---
# About

GLiNER is a Named Entity Recognition (NER) model capable of identifying any entity type using a bidirectional transformer encoders (BERT-like). It provides a practical alternative to traditional NER models, which are limited to predefined entities, and Large Language Models (LLMs) that, despite their flexibility, are costly and large for resource-constrained scenarios.

The initial versions of GLiNER relied on older encoder architectures like BERT and DeBERTA. These models, however, were trained on smaller datasets and lacked support for modern optimization techniques such as flash attention. Additionally, their context window was typically limited to 512 tokens, which is insufficient for many practical applications. Recognizing these limitations, we began exploring alternative backbones for GLiNER.

This latest model leverages the LLM2Vec approach, transforming the initial decoder model into a bidirectional encoder. We further enhanced the model by pre-training it on the masked token prediction task using the Wikipedia corpus. This approach introduces several advancements for GLiNER, including support for flash attention, an extended context window, and faster inference times. Additionally, by utilizing modern decoders trained on large, up-to-date datasets, the model exhibits improved generalization and performance.

Key Advantages Over Previous GLiNER Models:

* Enhanced performance and generalization capabilities
* Support for Flash Attention
* Extended context window (up to 32k tokens)

While these models are larger and require more computational resources compared to older encoders, they are still considered relatively small given current standards and provide significant benefits for a wide range of use cases.

### Installation & Usage
Install or update the gliner package:
```bash
pip install gliner -U
```
And LLM2Vec packages:
```bash
pip install llm2vec
```

Once you've downloaded the GLiNER library, you can import the GLiNER class. You can then load this model using `GLiNER.from_pretrained` and predict entities with `predict_entities`.

```python
from gliner import GLiNER

model = GLiNER.from_pretrained("knowledgator/gliner-llama-1B-v1.0")

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,[note 3] a record three UEFA Men's Player of the Year Awards, and four European Golden Shoes, the most by a European player. He has won 33 trophies in his career, including seven league titles, five UEFA Champions Leagues, the UEFA European Championship and the UEFA Nations League. Ronaldo holds the records for most appearances (183), goals (140) and assists (42) in the Champions League, goals in the European Championship (14), international goals (128) and international appearances (205). He is one of the few players to have made over 1,200 professional career appearances, the most by an outfield player, and has scored over 850 official senior career goals for club and country, making him the top goalscorer of all time.
"""

labels = ["person", "award", "date", "competitions", "teams"]

entities = model.predict_entities(text, labels, threshold=0.5)

for entity in entities:
    print(entity["text"], "=>", entity["label"])
```

```
Cristiano Ronaldo dos Santos Aveiro => person
5 February 1985 => date
Al Nassr => teams
Portugal national team => teams
Ballon d'Or => award
UEFA Men's Player of the Year Awards => award
European Golden Shoes => award
UEFA Champions Leagues => competitions
UEFA European Championship => competitions
UEFA Nations League => competitions
Champions League => competitions
European Championship => competitions
```

If you want to use flash attention or increase sequence length, please, check the following code:
```python
from gliner import GLiNER
import torch

model = GLiNER.from_pretrained("knowledgator/gliner-llama-1B-v1.0",
                                _attn_implementation = 'flash_attention_2',
                                                max_length = 2048).to('cuda:0', dtype=torch.float16)
```


### Benchmarks
Below you can see the table with benchmarking results on various named entity recognition datasets:

| Dataset                     | Score  |
|-----------------------------|--------|
| ACE 2004                    | 29.0%  |
| ACE 2005                    | 30.4%  |
| AnatEM                      | 40.2%  |
| Broad Tweet Corpus          | 65.3%  |
| CoNLL 2003                  | 62.4%  |
| FabNER                      | 25.3%  |
| FindVehicle                 | 39.7%  |
| GENIA_NER                   | 55.6%  |
| HarveyNER                   | 25.1%  |
| MultiNERD                   | 61.3%  |
| Ontonotes                   | 25.6%  |
| PolyglotNER                 | 42.7%  |
| TweetNER7                   | 36.2%  |
| WikiANN en                  | 55.7%  |
| WikiNeural                  | 73.6%  |
| bc2gm                       | 55.5%  |
| bc4chemd                    | 65.1%  |
| bc5cdr                      | 74.0%  |
| ncbi                        | 64.8%  |
| **Average**                 | **48.8%** |
|                             |        |
| CrossNER_AI                 | 57.5%  |
| CrossNER_literature         | 68.1%  |
| CrossNER_music              | 65.3%  |
| CrossNER_politics           | 73.3%  |
| CrossNER_science            | 67.8%  |
| mit-movie                   | 47.7%  |
| mit-restaurant              | 40.0%  |
| **Average (zero-shot benchmark)** | **60.1%** |


### Join Our Discord

Connect with our community on Discord for news, support, and discussion about our models. Join [Discord](https://discord.gg/dkyeAgs9DG).