Spaces:
Running
on
Zero
Running
on
Zero
File size: 34,739 Bytes
8c42108 b6816e1 3b0d426 b6816e1 53256c4 b6816e1 8b7e9f0 b6816e1 8b7e9f0 b6816e1 05fea77 b6816e1 8b7e9f0 b6816e1 53256c4 8c42108 b6816e1 8c42108 b6816e1 53256c4 b6816e1 92254d3 650ccf5 4712226 b6816e1 11d8b13 650ccf5 53256c4 b6816e1 4712226 b6816e1 8c42108 11ecbac b6816e1 92254d3 b6816e1 650ccf5 53256c4 11d8b13 11ecbac 8c42108 11ecbac b6816e1 650ccf5 b6816e1 11ecbac 8c42108 11ecbac b6816e1 8432c73 b6816e1 7d340b1 b6816e1 8432c73 b6816e1 7d340b1 b6816e1 8432c73 b6816e1 8432c73 b6816e1 8c42108 b6816e1 8432c73 b6816e1 8432c73 b6816e1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
import spaces
from functools import partial
import os
from typing import Any, Dict, List, Tuple, Optional, Union
import gradio as gr
import spacy
from pyvis.network import Network
from spacy import displacy
from spacy.tokens import Doc, Span
from relik.common.utils import CONFIG_NAME, from_cache
from relik.inference.annotator import Relik
from relik.inference.serve.frontend.utils import get_random_color
from relik.retriever.pytorch_modules.model import GoldenRetriever
from relik.retriever.indexers.inmemory import InMemoryDocumentIndex
from relik.inference.data.objects import TaskType
from relik.retriever.pytorch_modules import RetrievedSample
from relik.retriever.indexers.document import Document, DocumentStore
from relik.retriever.indexers.base import BaseDocumentIndex
LOGO = """
<div style="text-align: center; display: flex; flex-direction: column; align-items: center;">
<img src="https://github.com/SapienzaNLP/relik/blob/main/relik.png?raw=true" style="max-width: 850px; height: auto;">
</div>
"""
DESCRIPTION = """
<div style="display:flex; justify-content: center; align-items: center; flex-direction: row;">
<a href="https://2024.aclweb.org/"><img src="http://img.shields.io/badge/ACL-2024-4b44ce.svg"></a>
<a href="https://aclanthology.org/"><img src="http://img.shields.io/badge/paper-ACL--anthology-B31B1B.svg"></a>
<a href="https://arxiv.org/abs/2408.00103"><img src="https://img.shields.io/badge/arXiv-2408.00103-b31b1b.svg"></a>
</div>
<br>
<div style="display:flex; justify-content: center; align-items: center; flex-direction: row;">
<a href="https://huggingface.co/collections/sapienzanlp/relik-retrieve-read-and-link-665d9e4a5c3ecba98c1bef19"><img src="https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Collection-FCD21D"></a>
<a href="https://github.com/SapienzaNLP/relik"><img src="https://img.shields.io/badge/GitHub-Repo-121013?logo=github&logoColor=white"></a>
<a href="https://github.com/SapienzaNLP/relik/releases"><img src="https://img.shields.io/github/v/release/SapienzaNLP/relik"></a>
</div>
<br>
<div style="display:flex; justify-content: center; align-items: center; flex-direction: row;">
<a href="https://nlp.uniroma1.it/"><img src="https://img.shields.io/badge/Sapienza NLP-802433.svg?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAMAAABrrFhUAAADAFBMVEUAAAD///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////8HPQsIAAAA/3RSTlMAAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXp7fH1+f4CBgoOEhYaHiImKi4yNjo+QkZKTlJWWl5iZmpucnZ6foKGio6SlpqeoqaqrrK2ur7CxsrO0tba3uLm6u7y9vr/AwcLDxMXGx8jJysvMzc7P0NHS09TV1tfY2drb3N3e3+Dh4uPk5ebn6Onq6+zt7u/w8fLz9PX29/j5+vv8/f7rCNk1AAAfWElEQVQYGe3BCYBM9QMH8O/M3us+cx/ryFEpuSWUckVLiYRS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TTwBvzSS65SUSQNN8bn59MDehytEqvxgOigqQQGGmq/Jon5Q2AADvLjKU0eBdEwAAAABJRU5ErkJggg=="></a>
</div>
<br>
<div style="text-align: center; display: flex; flex-direction: column; align-items: center;">
<h2>
Retrieve, Read and LinK: Fast and Accurate Entity Linking and Relation Extraction on an Academic Budget
<br>
<span style="font-size: 0.9em;">
A blazing fast and lightweight Information Extraction model for Entity Linking and Relation Extraction.
</span>
<br>
<span style="color: #919191; font-weight: 400; font-size: 0.8em;">
<a href="https://riccardorlando.xyz/" style="color: #919191;" target="_blank">Riccardo Orlando</a>,
<a href="https://littlepea13.github.io/" style="color: #919191;" target="_blank">Pere-Lluís Huguet Cabot</a>,
<a href="https://edobobo.github.io/" style="color: #919191;" target="_blank">Edoardo Barba</a>,
and <a href="https://www.diag.uniroma1.it/navigli/" style="color: #919191;" target="_blank">Roberto Navigli</a>
</span>
<h2>
</div>
"""
INSTRUCTION = """
## Use it locally
Installation from PyPI
```bash
pip install relik
```
ReLiK is a lightweight and fast model for **Entity Linking** and **Relation Extraction**.
It is composed of two main components: a **retriever** and a **reader**.
The retriever is responsible for retrieving relevant documents from a large collection of documents,
while the reader is responsible for extracting entities and relations from the retrieved documents.
ReLiK can be used with the `from_pretrained` method to load a pre-trained pipeline.
Here is an example of how to use ReLiK for Entity Linking:
```python
from relik import Relik
from relik.inference.data.objects import RelikOutput
relik = Relik.from_pretrained("sapienzanlp/relik-entity-linking-large")
relik_out: RelikOutput = relik("Michael Jordan was one of the best players in the NBA.")
# RelikOutput(
# text="Michael Jordan was one of the best players in the NBA.",
# tokens=['Michael', 'Jordan', 'was', 'one', 'of', 'the', 'best', 'players', 'in', 'the', 'NBA', '.'],
# id=0,
# spans=[
# Span(start=0, end=14, label="Michael Jordan", text="Michael Jordan"),
# Span(start=50, end=53, label="National Basketball Association", text="NBA"),
# ],
# triplets=[],
# candidates=Candidates(
# span=[
# [
# [
# {"text": "Michael Jordan", "id": 4484083},
# {"text": "National Basketball Association", "id": 5209815},
# {"text": "Walter Jordan", "id": 2340190},
# {"text": "Jordan", "id": 3486773},
# {"text": "50 Greatest Players in NBA History", "id": 1742909},
# ...
# ]
# ]
# ]
# ),
# )
```
and for Relation Extraction:
```python
from relik import Relik
from relik.inference.data.objects import RelikOutput
relik = Relik.from_pretrained("sapienzanlp/relik-relation-extraction-large")
relik_out: RelikOutput = relik("Michael Jordan was one of the best players in the NBA.")
```
For more information, please refer to the [source code](https://github.com/SapienzaNLP/relik/).
"""
class GoldenSillyRetriever(GoldenRetriever):
def __init__(self, documents: List[str], *args, **kwargs):
self.documents = DocumentStore([Document(doc) for doc in documents])
self.document_index = BaseDocumentIndex(self.documents)
def retrieve(self,
text: Optional[Union[str, List[str]]] = None,
k: int = 100,
*args,
**kwargs,
) -> List[List[RetrievedSample]]:
if isinstance(text, str):
text = [text]
elif text is None:
text = []
return [
[RetrievedSample(score=1.0, document=doc) for doc in self.documents[:k]]
for _ in text
]
def index(self):
pass
def eval(self):
pass
def save_pretrained(self):
pass
def to(self, device):
pass
wikipedia_retriever = GoldenRetriever("relik-ie/encoder-e5-base-v2-wikipedia", device="cuda")
wikipedia_index = InMemoryDocumentIndex.from_pretrained("relik-ie/encoder-e5-base-v2-wikipedia-index", index_precision="bf16", device="cuda")
wikidata_retriever = GoldenRetriever("relik-ie/encoder-e5-small-v2-wikipedia-relations", device="cuda")
wikidata_index = InMemoryDocumentIndex.from_pretrained("relik-ie/encoder-e5-small-v2-wikipedia-relations-index", index_precision="bf16", device="cuda")
ner_type_retriever = GoldenSillyRetriever(
documents=['media', 'disease', 'miscellaneous', 'event', 'person', 'location', 'time', 'celestial', 'organization', 'concept']
)
relik_available_models = [
"relik-ie/relik-cie-small",
"relik-ie/relik-cie-xl",
"sapienzanlp/relik-entity-linking-large",
"relik-ie/relik-entity-linking-large-robust",
"relik-ie/relik-relation-extraction-small",
"relik-ie/relik-relation-extraction-large",
"relik-ie/relik-relation-extraction-small-wikipedia-ner",
]
relik_models = {
"sapienzanlp/relik-entity-linking-large": Relik.from_pretrained(
"sapienzanlp/relik-entity-linking-large",
device="cuda",
index={
TaskType.SPAN: wikipedia_index,
},
retriever={
TaskType.SPAN:wikipedia_retriever,
},
reader_kwargs={"dataset_kwargs": {"use_nme": True}},
),
"relik-ie/relik-cie-small": Relik.from_pretrained(
"relik-ie/relik-cie-small",
device="cuda",
index={
TaskType.SPAN: wikipedia_index,
TaskType.TRIPLET: wikidata_index,
},
retriever={
TaskType.SPAN: wikipedia_retriever,
TaskType.TRIPLET: wikidata_retriever,
},
reader_kwargs={"dataset_kwargs": {"use_nme": True}},
),
"relik-ie/relik-cie-xl": Relik.from_pretrained(
"relik-ie/relik-cie-xl",
device="cuda",
index={
TaskType.SPAN: wikipedia_index,
TaskType.TRIPLET: wikidata_index,
},
retriever={
TaskType.SPAN: wikipedia_retriever,
TaskType.TRIPLET: wikidata_retriever,
},
reader_kwargs={"dataset_kwargs": {"use_nme": True}},
),
"relik-ie/relik-relation-extraction-small-wikipedia-ner": Relik.from_pretrained(
"relik-ie/relik-relation-extraction-small-wikipedia-ner",
device="cuda",
use_nme=True,
retriever={
TaskType.SPAN: ner_type_retriever,
TaskType.TRIPLET: wikidata_retriever,
},
index={
TaskType.SPAN: ner_type_retriever.document_index,
TaskType.TRIPLET: wikidata_index,
}
),
"relik-ie/relik-relation-extraction-small": Relik.from_pretrained(
"relik-ie/relik-relation-extraction-small",
index={
TaskType.TRIPLET:wikidata_index,
},
device="cuda",
retriever={
TaskType.TRIPLET: wikidata_retriever,
},
),
"relik-ie/relik-relation-extraction-large": Relik.from_pretrained(
"relik-ie/relik-relation-extraction-large",
index={
TaskType.TRIPLET:wikidata_index,
},
device="cuda",
retriever={
TaskType.TRIPLET: wikidata_retriever,
},
),
"relik-ie/relik-entity-linking-large-robust": Relik.from_pretrained(
"relik-ie/relik-entity-linking-large-robust",
index={
TaskType.SPAN: wikipedia_index,
},
device="cuda",
retriever={
TaskType.SPAN: wikipedia_retriever,
},
reader_kwargs={"dataset_kwargs": {"use_nme": True}},
),
}
def get_span_annotations(response, doc, ner=False):
dict_ents = {}
el_link_wrapper = (
"<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css'><a href='https://en.wikipedia.org/wiki/{}' style='color: #414141'><i class='fa-brands fa-wikipedia-w fa-xs' style='color: #414141'></i> {}</a>"
)
spans = []
for idx, span in enumerate(response.spans):
spans.append(
Span(
doc,
span.start,
span.end,
el_link_wrapper.format(
span.label.replace(" ", "_"), span.label
) if (span.label != "--NME--" and not ner) else span.label,
# kb_id=span.label.replace(" ", "_")
)
)
dict_ents[(span.start, span.end)] = (
span.label + str(idx),
doc[span.start : span.end].text,
span.label,
span.label.replace(" ", "_"),
)
colors = get_random_color(set([span.label_ for span in spans]))
return spans, colors, dict_ents
def generate_graph(spans, response, colors, dict_ents, bgcolor="#111827", font_color="white", ner=False):
g = Network(
width="720px",
height="600px",
directed=True,
notebook=False,
bgcolor=bgcolor,
font_color=font_color,
)
g.barnes_hut(
gravity=-3000,
central_gravity=0.3,
spring_length=50,
spring_strength=0.001,
damping=0.09,
overlap=0,
)
for ent in spans:
# if not NME use title:
if dict_ents[(ent.start, ent.end)][2] != "--NME--" and not ner:
g.add_node(
dict_ents[(ent.start, ent.end)][2],
label=dict_ents[(ent.start, ent.end)][2],
color=colors[ent.label_],
title=dict_ents[(ent.start, ent.end)][2],
size=15,
labelHighlightBold=True,
)
else:
g.add_node(
ent.text,
label=ent.text,
color=colors[ent.label_],
title=ent.text,
size=15,
labelHighlightBold=True,
)
seen_rels = set()
for rel in response.triplets:
if not ner:
if dict_ents[(rel.subject.start, rel.subject.end)][2] == "--NME--" and dict_ents[(rel.object.start, rel.object.end)][2] == "--NME--":
if (rel.subject.text, rel.object.text, rel.label) in seen_rels:
continue
elif dict_ents[(rel.subject.start, rel.subject.end)][2] == "--NME--" and dict_ents[(rel.object.start, rel.object.end)][2] != "--NME--":
if (rel.subject.text, dict_ents[(rel.object.start, rel.object.end)][2], rel.label) in seen_rels:
continue
elif dict_ents[(rel.subject.start, rel.subject.end)][2] != "--NME--" and dict_ents[(rel.object.start, rel.object.end)][2] == "--NME--":
if (dict_ents[(rel.subject.start, rel.subject.end)][2], rel.object.text, rel.label) in seen_rels:
continue
else:
if (dict_ents[(rel.subject.start, rel.subject.end)][2], dict_ents[(rel.object.start, rel.object.end)][2], rel.label) in seen_rels:
continue
g.add_edge(
dict_ents[(rel.subject.start, rel.subject.end)][2] if dict_ents[(rel.subject.start, rel.subject.end)][2] != "--NME--" and not ner else dict_ents[(rel.subject.start, rel.subject.end)][1],
dict_ents[(rel.object.start, rel.object.end)][2] if dict_ents[(rel.object.start, rel.object.end)][2] != "--NME--" and not ner else dict_ents[(rel.object.start, rel.object.end)][1],
label=rel.label,
title=rel.label,
)
if dict_ents[(rel.subject.start, rel.subject.end)][2] != "--NME--" and dict_ents[(rel.object.start, rel.object.end)][2] != "--NME--":
seen_rels.add((dict_ents[(rel.subject.start, rel.subject.end)][2], dict_ents[(rel.object.start, rel.object.end)][2], rel.label))
elif dict_ents[(rel.subject.start, rel.subject.end)][2] != "--NME--" and dict_ents[(rel.object.start, rel.object.end)][2] == "--NME--":
seen_rels.add((dict_ents[(rel.subject.start, rel.subject.end)][2], rel.object.text, rel.label))
elif dict_ents[(rel.subject.start, rel.subject.end)][2] == "--NME--" and dict_ents[(rel.object.start, rel.object.end)][2] != "--NME--":
seen_rels.add((rel.subject.text, dict_ents[(rel.object.start, rel.object.end)][2], rel.label))
else:
seen_rels.add((rel.subject.text, rel.object.text, rel.label))
# g.show(filename, notebook=False)
html = g.generate_html()
# need to remove ' from HTML
html = html.replace("'", '"')
return f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
display-capture; encrypted-media;" sandbox="allow-modals allow-forms
allow-scripts allow-same-origin allow-popups
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
@spaces.GPU
def text_analysis(Text, Model, Relation_Threshold, Window_Size, Window_Stride):
global loaded_model
if Model is None:
return "", ""
# if loaded_model is None or loaded_model["key"] != Model:
# relik = Relik.from_pretrained(Model, index_precision="bf16")
# loaded_model = {"key": Model, "model": relik}
# else:
# relik = loaded_model["model"]
if Model not in relik_models:
raise ValueError(f"Model {Model} not found.")
relik = relik_models[Model]
# spacy for span visualization
nlp = spacy.blank("xx")
annotated_text = relik(Text, annotation_type="word", num_workers=0, remove_nmes= False, relation_threshold = Relation_Threshold, window_size=Window_Size, window_stride=Window_Stride)
doc = Doc(nlp.vocab, words=[token.text for token in annotated_text.tokens])
spans, colors, dict_ents = get_span_annotations(response=annotated_text, doc=doc, ner="ner" in Model)
doc.spans["sc"] = spans
# build the EL display
display_el = displacy.render(doc, style="span", options={"colors": colors})#, "kb_url_template": "https://en.wikipedia.org/wiki/{}"})
display_el = display_el.replace("\n", " ")
# heuristic, prevents split of annotation decorations
display_el = display_el.replace(
"border-radius: 0.35em;",
"border-radius: 0.35em; white-space: nowrap;",
)
display_el = display_el.replace(
"span style",
"span id='el' style",
)
display_re = ""
if annotated_text.triplets:
# background_color should be the same as the background of the page
display_re = generate_graph(spans, annotated_text, colors, dict_ents, ner="ner" in Model)
return display_el, display_re
theme = theme = gr.themes.Base(
primary_hue="rose",
secondary_hue="rose",
text_size="lg",
# font=[gr.themes.GoogleFont("Montserrat"), "Arial", "sans-serif"],
)
css = """
h1 {
text-align: center;
display: block;
}
mark {
color: black;
}
#el {
white-space: nowrap;
}
"""
with gr.Blocks(fill_height=True, css=css, theme=theme) as demo:
# check if demo is running in dark mode
gr.Markdown(LOGO)
gr.Markdown(DESCRIPTION)
gr.Interface(
text_analysis,
[
gr.Textbox(label="Input Text", placeholder="Enter sentence here..."),
gr.Dropdown(
relik_available_models,
value=relik_available_models[0],
label="Relik Model",
),
gr.Slider(
minimum=0,
maximum=1,
step=0.05,
value=0.5,
label="Relation Threshold",
info="Minimum confidence for relation extraction (Only for RE and cIE)",
),
gr.Slider(
minimum=16,
maximum=128,
step=16,
value=32,
label="Window Size",
info="Window size for the sliding window",
),
gr.Slider(
minimum=8,
maximum=64,
step=8,
value=16,
label="Window Stride",
info="Window stride for the sliding window",
),
],
[gr.HTML(label="Entities"), gr.HTML(label="Relations")],
examples=[
["Avram Noam Chomsky born December 7, 1928) is an American professor and public intellectual known for his work in linguistics, political activism, and social criticism. Sometimes called 'the father of modern linguistics', Chomsky is also a major figure in analytic philosophy and one of the founders of the field of cognitive science. He is a laureate professor of linguistics at the University of Arizona and an institute professor emeritus at the Massachusetts Institute of Technology (MIT). Among the most cited living authors, Chomsky has written more than 150 books on topics such as linguistics, war, and politics. In addition to his work in linguistics, since the 1960s Chomsky has been an influential voice on the American left as a consistent critic of U.S. foreign policy, contemporary capitalism, and corporate influence on political institutions and the media."],
["'Bella ciao' (Italian pronunciation: [ˈbɛlla ˈtʃaːo]; 'Goodbye beautiful') is an Italian song dedicated to the partisans of the Italian resistance, which fought against the occupying troops of Nazi Germany and the collaborationist Fascist forces during the liberation of Italy. It was based on a folk song of the late 19th century, sung by female workers of the paddy fields in Northern Italy (mondine) in protest against harsh working conditions. Versions of 'Bella ciao' continue to be sung worldwide as a hymn of resistance."],
],
allow_flagging="never",
)
gr.Markdown("")
gr.Markdown(INSTRUCTION)
if __name__ == "__main__":
demo.launch() |