Token Classification
GLiNER
PyTorch
multilingual
NER
GLiNER
information extraction
encoder
entity recognition
Ihor commited on
Commit
296c54a
1 Parent(s): cb91124

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +129 -3
README.md CHANGED
@@ -1,3 +1,129 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ language:
4
+ - multilingual
5
+ library_name: gliner
6
+ datasets:
7
+ - urchade/pile-mistral-v0.1
8
+ - knowledgator/GLINER-multi-task-synthetic-data
9
+ - EmergentMethods/AskNews-NER-v0
10
+ pipeline_tag: token-classification
11
+ tags:
12
+ - NER
13
+ - GLiNER
14
+ - information extraction
15
+ - encoder
16
+ - entity recognition
17
+ ---
18
+ # About
19
+
20
+ 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.
21
+
22
+ 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.
23
+
24
+ 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.
25
+
26
+ Key Advantages Over Previous GLiNER Models:
27
+
28
+ * Enhanced performance and generalization capabilities
29
+ * Support for Flash Attention
30
+ * Extended context window (up to 32k tokens)
31
+
32
+ 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.
33
+
34
+ ### Installation & Usage
35
+ Install or update the gliner package:
36
+ ```bash
37
+ pip install gliner -U
38
+ ```
39
+
40
+ 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`.
41
+
42
+ ```python
43
+ from gliner import GLiNER
44
+
45
+ model = GLiNER.from_pretrained("knowledgator/gliner-qwen-1.5B-v1.0")
46
+
47
+ text = """
48
+ 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.
49
+ """
50
+
51
+ labels = ["person", "award", "date", "competitions", "teams"]
52
+
53
+ entities = model.predict_entities(text, labels, threshold=0.5)
54
+
55
+ for entity in entities:
56
+ print(entity["text"], "=>", entity["label"])
57
+ ```
58
+
59
+ ```
60
+ Cristiano Ronaldo dos Santos Aveiro => person
61
+ 5 February 1985 => date
62
+ Al Nassr => teams
63
+ Portugal national team => teams
64
+ Ballon d'Or => award
65
+ UEFA Men's Player of the Year Awards => award
66
+ European Golden Shoes => award
67
+ UEFA Champions Leagues => competitions
68
+ UEFA European Championship => competitions
69
+ UEFA Nations League => competitions
70
+ Champions League => competitions
71
+ European Championship => competitions
72
+ ```
73
+
74
+ If you want to use flash attention or increase sequence length, please, check the following code:
75
+ ```python
76
+ model = GLiNER.from_pretrained("knowledgator/gliner-qwen-1.5B-v1.0",
77
+ _attn_implementation = 'flash_attention_2',
78
+ max_len = 2048).to('cuda:0')
79
+ ```
80
+
81
+ If you have a large amount of entities and want to pre-embed them, please, refer to the following code snippet:
82
+
83
+ ```python
84
+ labels = ["your entities"]
85
+ texts = ["your texts"]
86
+
87
+ entity_embeddings = model.encode_labels(labels, batch_size = 8)
88
+
89
+ outputs = model.batch_predict_with_embeds(texts, entity_embeddings, labels)
90
+ ```
91
+
92
+ ### Benchmarks
93
+ Below you can see the table with benchmarking results on various named entity recognition datasets:
94
+
95
+ | Dataset | Score |
96
+ |-------------------------|--------|
97
+ | ACE 2004 | 26.8% |
98
+ | ACE 2005 | 29.2% |
99
+ | AnatEM | 25.3% |
100
+ | Broad Tweet Corpus | 66.8% |
101
+ | CoNLL 2003 | 60.3% |
102
+ | FabNER | 21.2% |
103
+ | FindVehicle | 28.3% |
104
+ | GENIA_NER | 58.3% |
105
+ | HarveyNER | 18.3% |
106
+ | MultiNERD | 64.7% |
107
+ | Ontonotes | 28.4% |
108
+ | PolyglotNER | 45.3% |
109
+ | TweetNER7 | 35.9% |
110
+ | WikiANN en | 53.6% |
111
+ | WikiNeural | 73.4% |
112
+ | bc2gm | 63.2% |
113
+ | bc4chemd | 56.8% |
114
+ | bc5cdr | 71.3% |
115
+ | ncbi | 64.9% |
116
+ | **Average** | **47.0%** |
117
+ | | |
118
+ | CrossNER_AI | 56.7% |
119
+ | CrossNER_literature | 61.5% |
120
+ | CrossNER_music | 70.2% |
121
+ | CrossNER_politics | 75.6% |
122
+ | CrossNER_science | 66.8% |
123
+ | mit-movie | 39.9% |
124
+ | mit-restaurant | 41.7% |
125
+ | **Average (zero-shot benchmark)** | **58.9%** |
126
+
127
+ ### Join Our Discord
128
+
129
+ Connect with our community on Discord for news, support, and discussion about our models. Join [Discord](https://discord.gg/dkyeAgs9DG).