Token Classification
GLiNER
PyTorch
English
NER
GLiNER
information extraction
encoder
entity recognition
modernbert
Ihor commited on
Commit
7a43d86
·
verified ·
1 Parent(s): fde8cea

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +133 -3
README.md CHANGED
@@ -1,3 +1,133 @@
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
+ - numind/NuNER
9
+ - knowledgator/GLINER-multi-task-synthetic-data
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
+ This particular version utilize bi-encoder architecture, where textual encoder is [ModernBERT-base](https://huggingface.co/answerdotai/ModernBERT-base) and entity label encoder is sentence transformer - [BGE-small-en](https://huggingface.co/BAAI/bge-small-en-v1.5).
23
+
24
+ Such architecture brings several advantages over uni-encoder GLiNER:
25
+ * An unlimited amount of entities can be recognized at a single time;
26
+ * Faster inference if entity embeddings are preprocessed;
27
+ * Better generalization to unseen entities;
28
+
29
+ Utilization of ModernBERT uncovers up to 3 times better efficiency in comparison to DeBERTa-based models and context length up to 8,192 tokens while demonstrating comparable results.
30
+
31
+ However, bi-encoder architecture has some drawbacks such as a lack of inter-label interactions that make it hard for the model to disambiguate semantically similar but contextually different entities.
32
+
33
+ ### Installation & Usage
34
+ Install or update the gliner package:
35
+ ```bash
36
+ pip install gliner -U
37
+ ```
38
+ You need to install the latest version of transformers to use this model:
39
+ ```bash
40
+ pip install git+https://github.com/huggingface/transformers.git
41
+ ```
42
+ 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`.
43
+
44
+ ```python
45
+ from gliner import GLiNER
46
+
47
+ model = GLiNER.from_pretrained("knowledgator/modern-gliner-bi-base-v1.0")
48
+
49
+ text = """
50
+ 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.
51
+ """
52
+
53
+ labels = ["person", "award", "date", "competitions", "teams"]
54
+
55
+ entities = model.predict_entities(text, labels, threshold=0.3)
56
+
57
+ for entity in entities:
58
+ print(entity["text"], "=>", entity["label"])
59
+ ```
60
+
61
+ ```
62
+ Cristiano Ronaldo dos Santos Aveiro => person
63
+ 5 February 1985 => date
64
+ Al Nassr => teams
65
+ Portugal national team => teams
66
+ Ballon d'Or => award
67
+ UEFA Men's Player of the Year Awards => award
68
+ European Golden Shoes => award
69
+ UEFA Champions Leagues => competitions
70
+ UEFA European Championship => competitions
71
+ UEFA Nations League => competitions
72
+ Champions League => competitions
73
+ European Championship => competitions
74
+ ```
75
+
76
+ If you want to use **flash attention** or increase sequence length, please, check the following code:
77
+
78
+ ```python
79
+ model = GLiNER.from_pretrained("knowledgator/modern-gliner-bi-base-v1.0",
80
+ _attn_implementation = 'flash_attention_2',
81
+ max_len = 2048).to('cuda:0')
82
+ ```
83
+
84
+ If you have a large amount of entities and want to pre-embed them, please, refer to the following code snippet:
85
+
86
+ ```python
87
+ labels = ["your entities"]
88
+ texts = ["your texts"]
89
+
90
+ entity_embeddings = model.encode_labels(labels, batch_size = 8)
91
+
92
+ outputs = model.batch_predict_with_embeds(texts, entity_embeddings, labels)
93
+ ```
94
+
95
+ ### Benchmarks
96
+ Below you can see the table with benchmarking results on various named entity recognition datasets:
97
+
98
+ | Dataset | Score |
99
+ |------------------------|--------|
100
+ | ACE 2004 | 29.9% |
101
+ | ACE 2005 | 25.8% |
102
+ | AnatEM | 39.2% |
103
+ | Broad Tweet Corpus | 70.8% |
104
+ | CoNLL 2003 | 62.5% |
105
+ | FabNER | 22.7% |
106
+ | FindVehicle | 40.7% |
107
+ | GENIA_NER | 47.7% |
108
+ | HarveyNER | 14.8% |
109
+ | MultiNERD | 64.4% |
110
+ | Ontonotes | 32.8% |
111
+ | PolyglotNER | 45.2% |
112
+ | TweetNER7 | 37.8% |
113
+ | WikiANN en | 53.3% |
114
+ | WikiNeural | 77.2% |
115
+ | bc2gm | 51.8% |
116
+ | bc4chemd | 49.3% |
117
+ | bc5cdr | 64.6% |
118
+ | ncbi | 58.9% |
119
+ | **Average** | **46.8%** |
120
+ | | |
121
+ | CrossNER_AI | 54.6% |
122
+ | CrossNER_literature | 59.7% |
123
+ | CrossNER_music | 69.7% |
124
+ | CrossNER_politics | 73.2% |
125
+ | CrossNER_science | 62.7% |
126
+ | mit-movie | 47.9% |
127
+ | mit-restaurant | 40.0% |
128
+ | **Average (zero-shot benchmark)** | **58.3%** |
129
+
130
+
131
+ ### Join Our Discord
132
+
133
+ Connect with our community on Discord for news, support, and discussion about our models. Join [Discord](https://discord.gg/dkyeAgs9DG).