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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, Union
|
2 |
+
from gliner import GLiNER
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
from typing import Dict, Union
|
6 |
+
from gliner import GLiNER
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
model = GLiNER.from_pretrained("numind/NuZero_token")
|
10 |
+
|
11 |
+
examples = [
|
12 |
+
[
|
13 |
+
"Libretto by Marius Petipa, based on the 1822 novella ``Trilby, ou Le Lutin d'Argail`` by Charles Nodier, first presented by the Ballet of the Moscow Imperial Bolshoi Theatre on January 25/February 6 (Julian/Gregorian calendar dates), 1870, in Moscow with Polina Karpakova as Trilby and Ludiia Geiten as Miranda and restaged by Petipa for the Imperial Ballet at the Imperial Bolshoi Kamenny Theatre on January 17–29, 1871 in St. Petersburg with Adèle Grantzow as Trilby and Lev Ivanov as Count Leopold.",
|
14 |
+
"person, book, location, date, actor, character",
|
15 |
+
0.3,
|
16 |
+
True,
|
17 |
+
],
|
18 |
+
[
|
19 |
+
"""
|
20 |
+
* Data Scientist, Data Analyst, or Data Engineer with 1+ years of experience.
|
21 |
+
* Experience with technologies such as Docker, Kubernetes, or Kubeflow
|
22 |
+
* Machine Learning experience preferred
|
23 |
+
* Experience with programming languages such as Python, C++, or SQL preferred
|
24 |
+
* Experience with technologies such as Databricks, Qlik, TensorFlow, PyTorch, Python, Dash, Pandas, or NumPy preferred
|
25 |
+
* BA or BS degree
|
26 |
+
* Active Secret OR Active Top Secret or Active TS/SCI clearance
|
27 |
+
""",
|
28 |
+
"software package, programing language, software tool, degree, job title",
|
29 |
+
0.3,
|
30 |
+
False,
|
31 |
+
],
|
32 |
+
[
|
33 |
+
"However, both models lack other frequent DM symptoms including the fibre-type dependent atrophy, myotonia, cataract and male-infertility.",
|
34 |
+
"disease, symptom",
|
35 |
+
0.3,
|
36 |
+
False,
|
37 |
+
],
|
38 |
+
[
|
39 |
+
"Synergy between signal transduction pathways is obligatory for expression of c-fos in B and T cell lines: implication for c-fos control via surface immunoglobulin and T cell antigen receptors.",
|
40 |
+
"DNA, RNA, cell line, cell type, protein",
|
41 |
+
0.3,
|
42 |
+
False,
|
43 |
+
],
|
44 |
+
[
|
45 |
+
"The choice of the encoder and decoder modules of dnpg can be quite flexible, for instance long short term memory networks (lstm) or convolutional neural network (cnn).",
|
46 |
+
"short acronym, long acronym",
|
47 |
+
0.3,
|
48 |
+
False,
|
49 |
+
],
|
50 |
+
[
|
51 |
+
"Amelia Earhart flew her single engine Lockheed Vega 5B across the Atlantic to Paris.",
|
52 |
+
"person, company, location, airplane",
|
53 |
+
0.3,
|
54 |
+
True,
|
55 |
+
],
|
56 |
+
[
|
57 |
+
"Feldman is a contributor to NBC Sports Boston's ``State of the Revs`` and ``Revolution Postgame Live`` programs as well as to 98.5 the SportsHub, SiriusXM FC's MLS coverage and to other New England and national radio outlets and podcasts.",
|
58 |
+
"person, company, location",
|
59 |
+
0.3,
|
60 |
+
False,
|
61 |
+
],
|
62 |
+
[
|
63 |
+
"On 25 July 1948, on the 39th anniversary of Bleriot's crossing of the English Channel, the Type 618 Nene-Viking flew Heathrow to Paris (Villacoublay) in the morning carrying letters to Bleriot's widow and son (secretary of the FAI), who met it at the airport.",
|
64 |
+
"date, location, person, organization",
|
65 |
+
0.3,
|
66 |
+
False,
|
67 |
+
],
|
68 |
+
[
|
69 |
+
"Leo & Ian won the 1962 Bathurst Six Hour Classic at Mount Panorama driving a Daimler SP250 sports car, (that year the 500 mile race for touring cars were held at Phillip Island)",
|
70 |
+
"person, date, location, organization, competition",
|
71 |
+
0.3,
|
72 |
+
False,
|
73 |
+
],
|
74 |
+
[
|
75 |
+
"The Shore Line route of the CNS & M until 1955 served, from south to north, the Illinois communities of Chicago, Evanston, Wilmette, Kenilworth, Winnetka, Glencoe, Highland Park, Highwood, Fort Sheridan, Lake Forest, Lake Bluff, North Chicago, Waukegan, Zion, and Winthrop Harbor as well as Kenosha, Racine, and Milwaukee (the ``KRM'') in Wisconsin.",
|
76 |
+
"location, organization, date",
|
77 |
+
0.3,
|
78 |
+
False,
|
79 |
+
],
|
80 |
+
[
|
81 |
+
"Comet C/2006 M4 (SWAN) is a non-periodic comet discovered in late June 2006 by Robert D. Matson of Irvine, California and Michael Mattiazzo of Adelaide, South Australia in publicly available images of the Solar and Heliospheric Observatory (SOHO).",
|
82 |
+
"person, organization, date, location",
|
83 |
+
0.3,
|
84 |
+
False,
|
85 |
+
],
|
86 |
+
[
|
87 |
+
"From November 29, 2011 to March 31, 2012, Karimloo returned to ``Les Misérables`` to play the lead role of Jean Valjean at The Queen's Theatre, London, for which he won the 2013 Theatregoers' Choice Award for Best Takeover in a Role.",
|
88 |
+
"person, actor, award, date, location",
|
89 |
+
0.3,
|
90 |
+
False,
|
91 |
+
],
|
92 |
+
[
|
93 |
+
"A Mexicali health clinic supported by former Baja California gubernatorial candidate Enrique Acosta Fregoso (PRI) was closed on June 15 after selling a supposed COVID-19 ``cure'' for between MXN $10,000 and $50,000.",
|
94 |
+
"location, organization, person, date, currency",
|
95 |
+
0.3,
|
96 |
+
False,
|
97 |
+
],
|
98 |
+
[
|
99 |
+
"Built in 1793, it was the home of Mary Young Pickersgill when she moved to Baltimore in 1806 and the location where she later sewed the ``Star Spangled Banner'', in 1813, the huge out-sized garrison flag that flew over Fort McHenry at Whetstone Point in Baltimore Harbor in the summer of 1814 during the British Royal Navy attack in the Battle of Baltimore during the War of 1812.",
|
100 |
+
"date, person, location, organization, event, flag",
|
101 |
+
0.3,
|
102 |
+
False,
|
103 |
+
],
|
104 |
+
]
|
105 |
+
|
106 |
+
|
107 |
+
def merge_entities(entities):
|
108 |
+
if not entities:
|
109 |
+
return []
|
110 |
+
|
111 |
+
merged = []
|
112 |
+
current = entities[0]
|
113 |
+
|
114 |
+
for next_entity in entities[1:]:
|
115 |
+
if next_entity['entity'] == current['entity'] and (next_entity['start'] == current['end'] + 1 or next_entity['start'] == current['end']):
|
116 |
+
current['word'] += ' ' + next_entity['word']
|
117 |
+
current['end'] = next_entity['end']
|
118 |
+
else
|
119 |
+
merged.append(current)
|
120 |
+
current = next_entity
|
121 |
+
|
122 |
+
# Append the last entity
|
123 |
+
merged.append(current)
|
124 |
+
|
125 |
+
return merged
|
126 |
+
|
127 |
+
def ner(
|
128 |
+
text, labels: str, threshold: float, nested_ner: bool
|
129 |
+
) -> Dict[str, Union[str, int, float]]:
|
130 |
+
labels = labels.split(",")
|
131 |
+
r = {
|
132 |
+
"text": text,
|
133 |
+
"entities": [
|
134 |
+
{
|
135 |
+
"entity": entity["label"],
|
136 |
+
"word": entity["text"],
|
137 |
+
"start": entity["start"],
|
138 |
+
"end": entity["end"],
|
139 |
+
"score": 0,
|
140 |
+
}
|
141 |
+
for entity in model.predict_entities(
|
142 |
+
text, labels, flat_ner=not nested_ner, threshold=threshold
|
143 |
+
)
|
144 |
+
],
|
145 |
+
}
|
146 |
+
|
147 |
+
print(r["entities"])
|
148 |
+
r["entities"] = merge_entities(r["entities"])
|
149 |
+
return r
|
150 |
+
|
151 |
+
|
152 |
+
with gr.Blocks(title="GLiNER-medium-v2.1") as demo:
|
153 |
+
gr.Markdown(
|
154 |
+
"""
|
155 |
+
# GLiNER-medium-v2.1, zero-shot NER
|
156 |
+
GLiNER is a Named Entity Recognition (NER) model capable of identifying any entity type using a bidirectional transformer encoder (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. This model has the commercially permissive Apache 2.0 license.
|
157 |
+
## Links
|
158 |
+
* Model: https://huggingface.co/urchade/gliner_medium-v2.1
|
159 |
+
* All GLiNER models: https://huggingface.co/models?library=gliner
|
160 |
+
* Paper: https://arxiv.org/abs/2311.08526
|
161 |
+
* Repository: https://github.com/urchade/GLiNER
|
162 |
+
"""
|
163 |
+
)
|
164 |
+
with gr.Accordion("How to run this model locally", open=False):
|
165 |
+
gr.Markdown(
|
166 |
+
"""
|
167 |
+
## Installation
|
168 |
+
To use this model, you must install the GLiNER Python library:
|
169 |
+
```
|
170 |
+
!pip install gliner
|
171 |
+
```
|
172 |
+
|
173 |
+
## Usage
|
174 |
+
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`.
|
175 |
+
"""
|
176 |
+
)
|
177 |
+
gr.Code(
|
178 |
+
'''
|
179 |
+
from gliner import GLiNER
|
180 |
+
model = GLiNER.from_pretrained("urchade/gliner_base")
|
181 |
+
text = """
|
182 |
+
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.
|
183 |
+
"""
|
184 |
+
labels = ["person", "award", "date", "competitions", "teams"]
|
185 |
+
entities = model.predict_entities(text, labels)
|
186 |
+
for entity in entities:
|
187 |
+
print(entity["text"], "=>", entity["label"])
|
188 |
+
''',
|
189 |
+
language="python",
|
190 |
+
)
|
191 |
+
gr.Code(
|
192 |
+
"""
|
193 |
+
Cristiano Ronaldo dos Santos Aveiro => person
|
194 |
+
5 February 1985 => date
|
195 |
+
Al Nassr => teams
|
196 |
+
Portugal national team => teams
|
197 |
+
Ballon d'Or => award
|
198 |
+
UEFA Men's Player of the Year Awards => award
|
199 |
+
European Golden Shoes => award
|
200 |
+
UEFA Champions Leagues => competitions
|
201 |
+
UEFA European Championship => competitions
|
202 |
+
UEFA Nations League => competitions
|
203 |
+
Champions League => competitions
|
204 |
+
European Championship => competitions
|
205 |
+
"""
|
206 |
+
)
|
207 |
+
|
208 |
+
input_text = gr.Textbox(
|
209 |
+
value=examples[0][0], label="Text input", placeholder="Enter your text here"
|
210 |
+
)
|
211 |
+
with gr.Row() as row:
|
212 |
+
labels = gr.Textbox(
|
213 |
+
value=examples[0][1],
|
214 |
+
label="Labels",
|
215 |
+
placeholder="Enter your labels here (comma separated)",
|
216 |
+
scale=2,
|
217 |
+
)
|
218 |
+
threshold = gr.Slider(
|
219 |
+
0,
|
220 |
+
1,
|
221 |
+
value=0.3,
|
222 |
+
step=0.01,
|
223 |
+
label="Threshold",
|
224 |
+
info="Lower the threshold to increase how many entities get predicted.",
|
225 |
+
scale=1,
|
226 |
+
)
|
227 |
+
nested_ner = gr.Checkbox(
|
228 |
+
value=examples[0][2],
|
229 |
+
label="Nested NER",
|
230 |
+
info="Allow for nested NER?",
|
231 |
+
scale=0,
|
232 |
+
)
|
233 |
+
output = gr.HighlightedText(label="Predicted Entities")
|
234 |
+
submit_btn = gr.Button("Submit")
|
235 |
+
examples = gr.Examples(
|
236 |
+
examples,
|
237 |
+
fn=ner,
|
238 |
+
inputs=[input_text, labels, threshold, nested_ner],
|
239 |
+
outputs=output,
|
240 |
+
cache_examples=True,
|
241 |
+
)
|
242 |
+
|
243 |
+
# Submitting
|
244 |
+
input_text.submit(
|
245 |
+
fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
|
246 |
+
)
|
247 |
+
labels.submit(
|
248 |
+
fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
|
249 |
+
)
|
250 |
+
threshold.release(
|
251 |
+
fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
|
252 |
+
)
|
253 |
+
submit_btn.click(
|
254 |
+
fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
|
255 |
+
)
|
256 |
+
nested_ner.change(
|
257 |
+
fn=ner, inputs=[input_text, labels, threshold, nested_ner], outputs=output
|
258 |
+
)
|
259 |
+
|
260 |
+
demo.queue()
|
261 |
+
demo.launch(debug=True)
|