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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from functools import partial
|
2 |
+
import os
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
import spacy
|
6 |
+
from pyvis.network import Network
|
7 |
+
from spacy import displacy
|
8 |
+
from spacy.tokens import Doc, Span
|
9 |
+
|
10 |
+
from relik.common.utils import CONFIG_NAME, from_cache
|
11 |
+
from relik.inference.annotator import Relik
|
12 |
+
from relik.inference.serve.frontend.utils import get_random_color
|
13 |
+
from relik.retriever.pytorch_modules.model import GoldenRetriever
|
14 |
+
from relik.retriever.indexers.inmemory import InMemoryDocumentIndex
|
15 |
+
from relik.inference.data.objects import TaskType
|
16 |
+
|
17 |
+
LOGO = """
|
18 |
+
<div style="text-align: center; display: flex; flex-direction: column; align-items: center;">
|
19 |
+
<img src="https://cdn-uploads.huggingface.co/production/uploads/5f0b462819cb630495b814d7/NJPRSmlwi6sY1qQEIcgRV.png" style="max-width: 850px; height: auto;">
|
20 |
+
</div>
|
21 |
+
"""
|
22 |
+
|
23 |
+
DESCRIPTION = """
|
24 |
+
<div style="display:flex; justify-content: center; align-items: center; flex-direction: row;">
|
25 |
+
<a href="https://2024.aclweb.org/"><img src="http://img.shields.io/badge/ACL-2024-4b44ce.svg"></a>
|
26 |
+
<a href="https://aclanthology.org/"><img src="http://img.shields.io/badge/paper-ACL--anthology-B31B1B.svg"></a>
|
27 |
+
<a href="https://arxiv.org/abs/placeholder"><img src="https://img.shields.io/badge/arXiv-placeholder-b31b1b.svg"></a>
|
28 |
+
</div>
|
29 |
+
<br>
|
30 |
+
<div style="display:flex; justify-content: center; align-items: center; flex-direction: row;">
|
31 |
+
<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>
|
32 |
+
<a href="https://github.com/SapienzaNLP/relik"><img src="https://img.shields.io/badge/GitHub-Repo-121013?logo=github&logoColor=white"></a>
|
33 |
+
<a href="https://github.com/SapienzaNLP/relik/releases"><img src="https://img.shields.io/github/v/release/SapienzaNLP/relik"></a>
|
34 |
+
</div>
|
35 |
+
<br>
|
36 |
+
<div style="display:flex; justify-content: center; align-items: center; flex-direction: row;">
|
37 |
+
<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/ryFEpuSWUckVLiYRSq9KlJyIrkQpRro1YJVKkJKEi/iVnIlqSyE1yLdbu2uvN9/9+7/fezJvZ2bWOdna0nw/y5cuXL1++fPny5cuXL1++fPnyXZHC45G7Wn+1bf6tyDvqpxZCburpIJlyJ/KMrnwIuSjoFIVfkWcM5KfIRTdTKoC8YizPBSH3VKYuJRB5xQSyDXLRegofI8+YSMYgF1VNJrmhGPKMSeRRO3JPZQdnt7Uj7xhO8i7knmFMCENe8jjJ2cg9uzgbeUp7kglhyC0NyLuRRxS3Q1ONmh64fAXtuAKT+LcdecTnTSCcIvkNLlvxQw9A1+D78/uGByJnAv/hBOQRtvMDICwnmX4DLtccRkK47SI1s5Ez7clbYVGmNHymAj+BMIyaobhMtzgYCWEJdbWRI5/yd7jU/5X8+Sb4SAP+BeFWag7acXnmkJEQDlP3MHKiUBKHwqlcPDUnSsI3WpMlIByhpjMuS+FkMhLCL9TdhZx4lI7KcBpJ3cvwjXvI9hDep2YFLstDJCMh9KPwRyByYhXXwGUedTPgG23IURDaUeOogcsxm2QkdG+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cT35k+QA6D6jZiM8KFRh0Yy8HdeTYSQXQjeKmv3woFCFRU2yHa4nFVTyGHQ9qEmCB4UqLIqRvXBd+ZxkFQg3USgEdwpVWNjSqeC6Up/kIxDsidRUhzuFKixKJPHIiwG4niwjp0O3hpqWcKdQhUvRPdQsxPWkTgb/gm48NU/AnUIVLqOoa4XryXtkdQhdqBkLdwpVuKyiLhrXk6LH+CyEoirJL+FOoQqXRdQ9h+tKJy6FbjPJHXCnUIVLVwrJ5XF9iU0MhjCaZLINbhSqsHjLQV6IxHUmdPtdEKo7SFaEG4UqrGo91acUrjtVbofufyTbwo1CFf8dPUgOg5txdBTEf0bALnIRLGzTSP7TFP8ZkeRBWPSn8E8B/GdsIEvAZQN1XfCfUeci74XLH9Q9jv+OAXwFLh9SVwf/HbbP+8Klyklq3sN/V5XZuzf0tyFfvnz58uXLly9fvnz58uXLly/fv+v/yxvXnTeskKkAAAAASUVORK5CYII="></a>
|
38 |
+
<a href="https://babelscape.it/"><img src="https://img.shields.io/badge/Babelscape-215489.svg?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAC3FBMVEUAAAAAAAAAAP8A//////8AAIAAgIAAgP+AgICAgP+qqqoAgL+AgL+Av7+/v7+/v/8AZpkzZpkAK4ArVYCAqtUAYJ9gn78ccao5cY4XRotddKIAK2oVQGoVaqqVqr9iibFJkraktsgRd6pQj69LeKUAY6oOOXEOY6oANnkbQ3lVeZ4XRnQAZKYWb6YrdaoJaKovcZdMcY4AYacSaqcRZqo8ZpEhc60APnQIPHgkUHwHarEjToAAN24cTHwNO3YTaqgfS4MfV4MkTYIXbq4RQ3oAXKMQaqogUIAUR4AURngAZ6cdcbEOQ3gOZagJZqwzWIszXYsfTH0JZacJZqYdb60YdKsPRHwaba0PZ6oPQngEQX4KQXcXRXkddK4TcKwacKwWbq0WcbAMaKkGZawGQHgRbasNQHYIP3kLP3Yaca4QRHgDYqkKP3gKQXgNRHsPbK0KaqgOa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zKCoPbk/zoaBEundkgiMD+UXa3dlhasQVqlgM+PBpZbUdI7HFOqYBF6ZnSJBQr6AbcH1hIFSGQ1YnJLurKlSVnQQD3i52F4ZKREzZc/bxixcvHx+erEKsAZ/XpfBARL1btwE1Q8HLg2W8RBlwv0MTLMQe1H31BQp41chFhAH70pjBXref9PAlqv4XL19WEjZgozmYz9d59gUWcNSAkAHLrMHWJ29+gR3ME8RvwGpTEMe47TUO/S+uOeM1YLkikMnktf3lC5ygAY8Bn1ZpgZxfc/kFHrCJE7cBx1yADKWel/j0v3hggtOAJzFA2mzOCwIgHJcBz9qB+VZ3DSH9LxJwGbCWiYFBby9B/S8ScRjwMYuBQXzWS8IGxGI3oHQuPwND0wsigBt2A6KiGRiCzxOh/4QCdgOEGBjYNhDjgKm4c3TdIyL0P47DqV//MjEOOKSO04AKYvQ/rsWpX2wHMQaswB0CSc+J0H/SF7cBzUSkofOpeIrlBQS1v9wVj0e//BlC+m/OEMFXLxgR0j/TTwBvzSS65SUSQNN8bn59MDehytEqvxgOigqQQGGmq/Jon5Q2AADvLjKU0eBdEwAAAABJRU5ErkJggg=="></a>
|
39 |
+
</div>
|
40 |
+
<br>
|
41 |
+
|
42 |
+
<div style="text-align: center; display: flex; flex-direction: column; align-items: center;">
|
43 |
+
|
44 |
+
<h2>
|
45 |
+
Retrieve, Read and LinK: Fast and Accurate Entity Linking and Relation Extraction on an Academic Budget
|
46 |
+
<br>
|
47 |
+
<span style="font-size: 0.9em;">
|
48 |
+
A blazing fast and lightweight Information Extraction model for Entity Linking and Relation Extraction.
|
49 |
+
</span>
|
50 |
+
<br>
|
51 |
+
<span style="color: #919191; font-weight: 400; font-size: 0.8em;">
|
52 |
+
<a href="https://riccardorlando.xyz/" style="color: #919191;" target="_blank">Riccardo Orlando</a>,
|
53 |
+
<a href="https://littlepea13.github.io/" style="color: #919191;" target="_blank">Pere-Lluís Huguet Cabot</a>,
|
54 |
+
<a href="https://edobobo.github.io/" style="color: #919191;" target="_blank">Edoardo Barba</a>,
|
55 |
+
and <a href="https://www.diag.uniroma1.it/navigli/" style="color: #919191;" target="_blank">Roberto Navigli</a>,
|
56 |
+
</span>
|
57 |
+
<h2>
|
58 |
+
</div>
|
59 |
+
"""
|
60 |
+
|
61 |
+
INSTRUCTION = """
|
62 |
+
## Use it locally
|
63 |
+
|
64 |
+
Installation from PyPI
|
65 |
+
|
66 |
+
```bash
|
67 |
+
pip install relik
|
68 |
+
```
|
69 |
+
|
70 |
+
ReLiK is a lightweight and fast model for **Entity Linking** and **Relation Extraction**.
|
71 |
+
It is composed of two main components: a **retriever** and a **reader**.
|
72 |
+
The retriever is responsible for retrieving relevant documents from a large collection of documents,
|
73 |
+
while the reader is responsible for extracting entities and relations from the retrieved documents.
|
74 |
+
ReLiK can be used with the `from_pretrained` method to load a pre-trained pipeline.
|
75 |
+
|
76 |
+
Here is an example of how to use ReLiK for Entity Linking:
|
77 |
+
|
78 |
+
```python
|
79 |
+
from relik import Relik
|
80 |
+
from relik.inference.data.objects import RelikOutput
|
81 |
+
|
82 |
+
relik = Relik.from_pretrained("sapienzanlp/relik-entity-linking-large")
|
83 |
+
relik_out: RelikOutput = relik("Michael Jordan was one of the best players in the NBA.")
|
84 |
+
|
85 |
+
# RelikOutput(
|
86 |
+
# text="Michael Jordan was one of the best players in the NBA.",
|
87 |
+
# tokens=['Michael', 'Jordan', 'was', 'one', 'of', 'the', 'best', 'players', 'in', 'the', 'NBA', '.'],
|
88 |
+
# id=0,
|
89 |
+
# spans=[
|
90 |
+
# Span(start=0, end=14, label="Michael Jordan", text="Michael Jordan"),
|
91 |
+
# Span(start=50, end=53, label="National Basketball Association", text="NBA"),
|
92 |
+
# ],
|
93 |
+
# triplets=[],
|
94 |
+
# candidates=Candidates(
|
95 |
+
# span=[
|
96 |
+
# [
|
97 |
+
# [
|
98 |
+
# {"text": "Michael Jordan", "id": 4484083},
|
99 |
+
# {"text": "National Basketball Association", "id": 5209815},
|
100 |
+
# {"text": "Walter Jordan", "id": 2340190},
|
101 |
+
# {"text": "Jordan", "id": 3486773},
|
102 |
+
# {"text": "50 Greatest Players in NBA History", "id": 1742909},
|
103 |
+
# ...
|
104 |
+
# ]
|
105 |
+
# ]
|
106 |
+
# ]
|
107 |
+
# ),
|
108 |
+
# )
|
109 |
+
```
|
110 |
+
|
111 |
+
and for Relation Extraction:
|
112 |
+
|
113 |
+
```python
|
114 |
+
from relik import Relik
|
115 |
+
from relik.inference.data.objects import RelikOutput
|
116 |
+
|
117 |
+
relik = Relik.from_pretrained("sapienzanlp/relik-relation-extraction-large")
|
118 |
+
relik_out: RelikOutput = relik("Michael Jordan was one of the best players in the NBA.")
|
119 |
+
```
|
120 |
+
|
121 |
+
For more information, please refer to the [source code](https://github.com/SapienzaNLP/relik/).
|
122 |
+
"""
|
123 |
+
|
124 |
+
wikipedia_retriever = GoldenRetriever("relik-ie/encoder-e5-base-v2-wikipedia")
|
125 |
+
wikipedia_index = InMemoryDocumentIndex.from_pretrained("relik-ie/encoder-e5-base-v2-wikipedia-index")
|
126 |
+
|
127 |
+
wikidata_retriever = GoldenRetriever("relik-ie/encoder-e5-small-v2-wikipedia-relations")
|
128 |
+
wikidata_index = InMemoryDocumentIndex.from_pretrained("relik-ie/encoder-e5-small-v2-wikipedia-relations-index")
|
129 |
+
|
130 |
+
relik_available_models = [
|
131 |
+
"sapienzanlp/relik-entity-linking-large",
|
132 |
+
"relik-ie/relik-reader-small-cie-wikipedia",
|
133 |
+
"relik-ie/relik-relation-extraction-large-wikipedia",
|
134 |
+
"relik-ie/relik-entity-linking-large-robust",
|
135 |
+
]
|
136 |
+
# demo_index_el = {
|
137 |
+
# "span": {
|
138 |
+
# "_target_": "relik.retriever.indexers.inmemory.InMemoryDocumentIndex.from_pretrained",
|
139 |
+
# "name_or_path": "sapienzanlp/relik-retriever-e5-base-v2-aida-blink-wikipedia-2Mpopular-index",
|
140 |
+
# }
|
141 |
+
# }
|
142 |
+
relik_models = {
|
143 |
+
"sapienzanlp/relik-entity-linking-large": Relik.from_pretrained(
|
144 |
+
"sapienzanlp/relik-entity-linking-large",
|
145 |
+
index_precision="bf16",
|
146 |
+
index=wikipedia_index,
|
147 |
+
retriever=wikipedia_retriever,
|
148 |
+
reader_kwargs={"dataset_kwargs": {"use_nme": True}},
|
149 |
+
),
|
150 |
+
"relik-ie/relik-reader-small-cie-wikipedia": Relik.from_pretrained(
|
151 |
+
"relik-ie/relik-reader-small-cie-wikipedia",
|
152 |
+
index_precision="bf16",
|
153 |
+
# reader="/media/data/relik/models/PereLluis13/relik-reader-deberta-large-wikipedia-aida-full-interleave",
|
154 |
+
device="cuda",
|
155 |
+
index={
|
156 |
+
TaskType.SPAN: wikipedia_index,
|
157 |
+
TaskType.TRIPLET: wikidata_index,
|
158 |
+
},
|
159 |
+
retriever={
|
160 |
+
TaskType.SPAN: wikipedia_retriever,
|
161 |
+
TaskType.TRIPLET: wikidata_retriever,
|
162 |
+
},
|
163 |
+
reader_kwargs={"dataset_kwargs": {"use_nme": True}},
|
164 |
+
),
|
165 |
+
"relik-ie/relik-relation-extraction-large-wikipedia": Relik.from_pretrained(
|
166 |
+
"relik-ie/relik-relation-extraction-large-wikipedia",
|
167 |
+
index_precision="bf16",
|
168 |
+
index=wikidata_index,
|
169 |
+
retriever=wikidata_retriever,
|
170 |
+
device="cuda",
|
171 |
+
),
|
172 |
+
"relik-ie/relik-entity-linking-large-robust": Relik.from_pretrained(
|
173 |
+
"relik-ie/relik-entity-linking-large-robust",
|
174 |
+
index_precision="bf16",
|
175 |
+
index=wikipedia_index,
|
176 |
+
retriever=wikipedia_retriever,
|
177 |
+
reader_kwargs={"dataset_kwargs": {"use_nme": True}},
|
178 |
+
),
|
179 |
+
}
|
180 |
+
|
181 |
+
|
182 |
+
def get_span_annotations(response, doc):
|
183 |
+
dict_ents = {}
|
184 |
+
el_link_wrapper = (
|
185 |
+
"<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>"
|
186 |
+
)
|
187 |
+
|
188 |
+
spans = []
|
189 |
+
for idx, span in enumerate(response.spans):
|
190 |
+
spans.append(
|
191 |
+
Span(
|
192 |
+
doc,
|
193 |
+
span.start,
|
194 |
+
span.end,
|
195 |
+
el_link_wrapper.format(
|
196 |
+
span.label.replace(" ", "_"), span.label
|
197 |
+
) if span.label != "--NME--" else "--NME--",
|
198 |
+
# kb_id=span.label.replace(" ", "_")
|
199 |
+
)
|
200 |
+
)
|
201 |
+
dict_ents[(span.start, span.end)] = (
|
202 |
+
span.label + str(idx),
|
203 |
+
doc[span.start : span.end].text,
|
204 |
+
span.label,
|
205 |
+
span.label.replace(" ", "_"),
|
206 |
+
)
|
207 |
+
colors = get_random_color(set([span.label_ for span in spans]))
|
208 |
+
return spans, colors, dict_ents
|
209 |
+
|
210 |
+
def generate_graph(spans, response, colors, dict_ents, bgcolor="#111827", font_color="white"):
|
211 |
+
g = Network(
|
212 |
+
width="720px",
|
213 |
+
height="600px",
|
214 |
+
directed=True,
|
215 |
+
notebook=False,
|
216 |
+
bgcolor=bgcolor,
|
217 |
+
font_color=font_color,
|
218 |
+
)
|
219 |
+
g.barnes_hut(
|
220 |
+
gravity=-3000,
|
221 |
+
central_gravity=0.3,
|
222 |
+
spring_length=50,
|
223 |
+
spring_strength=0.001,
|
224 |
+
damping=0.09,
|
225 |
+
overlap=0,
|
226 |
+
)
|
227 |
+
|
228 |
+
for ent in spans:
|
229 |
+
# if not NME use title:
|
230 |
+
if dict_ents[(ent.start, ent.end)][2] != "--NME--":
|
231 |
+
g.add_node(
|
232 |
+
dict_ents[(ent.start, ent.end)][2],
|
233 |
+
label=dict_ents[(ent.start, ent.end)][2],
|
234 |
+
color=colors[ent.label_],
|
235 |
+
title=dict_ents[(ent.start, ent.end)][2],
|
236 |
+
size=15,
|
237 |
+
labelHighlightBold=True,
|
238 |
+
)
|
239 |
+
else:
|
240 |
+
g.add_node(
|
241 |
+
ent.text,
|
242 |
+
label=ent.text,
|
243 |
+
color=colors[ent.label_],
|
244 |
+
title=ent.text,
|
245 |
+
size=15,
|
246 |
+
labelHighlightBold=True,
|
247 |
+
)
|
248 |
+
seen_rels = set()
|
249 |
+
for rel in response.triplets:
|
250 |
+
if dict_ents[(rel.subject.start, rel.subject.end)][2] == "--NME--" and dict_ents[(rel.object.start, rel.object.end)][2] == "--NME--":
|
251 |
+
if (rel.subject.text, rel.object.text, rel.label) in seen_rels:
|
252 |
+
continue
|
253 |
+
elif dict_ents[(rel.subject.start, rel.subject.end)][2] == "--NME--" and dict_ents[(rel.object.start, rel.object.end)][2] != "--NME--":
|
254 |
+
if (rel.subject.text, dict_ents[(rel.object.start, rel.object.end)][2], rel.label) in seen_rels:
|
255 |
+
continue
|
256 |
+
elif dict_ents[(rel.subject.start, rel.subject.end)][2] != "--NME--" and dict_ents[(rel.object.start, rel.object.end)][2] == "--NME--":
|
257 |
+
if (dict_ents[(rel.subject.start, rel.subject.end)][2], rel.object.text, rel.label) in seen_rels:
|
258 |
+
continue
|
259 |
+
else:
|
260 |
+
if (dict_ents[(rel.subject.start, rel.subject.end)][2], dict_ents[(rel.object.start, rel.object.end)][2], rel.label) in seen_rels:
|
261 |
+
continue
|
262 |
+
|
263 |
+
g.add_edge(
|
264 |
+
dict_ents[(rel.subject.start, rel.subject.end)][2] if dict_ents[(rel.subject.start, rel.subject.end)][2] != "--NME--" else dict_ents[(rel.subject.start, rel.subject.end)][1],
|
265 |
+
dict_ents[(rel.object.start, rel.object.end)][2] if dict_ents[(rel.object.start, rel.object.end)][2] != "--NME--" else dict_ents[(rel.object.start, rel.object.end)][1],
|
266 |
+
label=rel.label,
|
267 |
+
title=rel.label,
|
268 |
+
)
|
269 |
+
if dict_ents[(rel.subject.start, rel.subject.end)][2] != "--NME--" and dict_ents[(rel.object.start, rel.object.end)][2] != "--NME--":
|
270 |
+
seen_rels.add((dict_ents[(rel.subject.start, rel.subject.end)][2], dict_ents[(rel.object.start, rel.object.end)][2], rel.label))
|
271 |
+
elif dict_ents[(rel.subject.start, rel.subject.end)][2] != "--NME--" and dict_ents[(rel.object.start, rel.object.end)][2] == "--NME--":
|
272 |
+
seen_rels.add((dict_ents[(rel.subject.start, rel.subject.end)][2], rel.object.text, rel.label))
|
273 |
+
elif dict_ents[(rel.subject.start, rel.subject.end)][2] == "--NME--" and dict_ents[(rel.object.start, rel.object.end)][2] != "--NME--":
|
274 |
+
seen_rels.add((rel.subject.text, dict_ents[(rel.object.start, rel.object.end)][2], rel.label))
|
275 |
+
else:
|
276 |
+
seen_rels.add((rel.subject.text, rel.object.text, rel.label))
|
277 |
+
# g.show(filename, notebook=False)
|
278 |
+
html = g.generate_html()
|
279 |
+
# need to remove ' from HTML
|
280 |
+
html = html.replace("'", '"')
|
281 |
+
|
282 |
+
return f"""<iframe style="width: 100%; height: 600px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera;
|
283 |
+
display-capture; encrypted-media;" sandbox="allow-modals allow-forms
|
284 |
+
allow-scripts allow-same-origin allow-popups
|
285 |
+
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen=""
|
286 |
+
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>"""
|
287 |
+
|
288 |
+
|
289 |
+
def text_analysis(Text, Model, Relation_Threshold, Window_Size, Window_Stride):
|
290 |
+
global loaded_model
|
291 |
+
if Model is None:
|
292 |
+
return "", ""
|
293 |
+
# if loaded_model is None or loaded_model["key"] != Model:
|
294 |
+
# relik = Relik.from_pretrained(Model, index_precision="bf16")
|
295 |
+
# loaded_model = {"key": Model, "model": relik}
|
296 |
+
# else:
|
297 |
+
# relik = loaded_model["model"]
|
298 |
+
if Model not in relik_models:
|
299 |
+
raise ValueError(f"Model {Model} not found.")
|
300 |
+
relik = relik_models[Model]
|
301 |
+
# spacy for span visualization
|
302 |
+
nlp = spacy.blank("xx")
|
303 |
+
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)
|
304 |
+
doc = Doc(nlp.vocab, words=[token.text for token in annotated_text.tokens])
|
305 |
+
spans, colors, dict_ents = get_span_annotations(response=annotated_text, doc=doc)
|
306 |
+
doc.spans["sc"] = spans
|
307 |
+
|
308 |
+
# build the EL display
|
309 |
+
display_el = displacy.render(doc, style="span", options={"colors": colors})#, "kb_url_template": "https://en.wikipedia.org/wiki/{}"})
|
310 |
+
display_el = display_el.replace("\n", " ")
|
311 |
+
# heuristic, prevents split of annotation decorations
|
312 |
+
display_el = display_el.replace(
|
313 |
+
"border-radius: 0.35em;",
|
314 |
+
"border-radius: 0.35em; white-space: nowrap;",
|
315 |
+
)
|
316 |
+
display_el = display_el.replace(
|
317 |
+
"span style",
|
318 |
+
"span id='el' style",
|
319 |
+
)
|
320 |
+
display_re = ""
|
321 |
+
if annotated_text.triplets:
|
322 |
+
# background_color should be the same as the background of the page
|
323 |
+
display_re = generate_graph(spans, annotated_text, colors, dict_ents)
|
324 |
+
return display_el, display_re
|
325 |
+
|
326 |
+
|
327 |
+
theme = theme = gr.themes.Base(
|
328 |
+
primary_hue="rose",
|
329 |
+
secondary_hue="rose",
|
330 |
+
text_size="lg",
|
331 |
+
# font=[gr.themes.GoogleFont("Montserrat"), "Arial", "sans-serif"],
|
332 |
+
)
|
333 |
+
|
334 |
+
css = """
|
335 |
+
h1 {
|
336 |
+
text-align: center;
|
337 |
+
display: block;
|
338 |
+
}
|
339 |
+
mark {
|
340 |
+
color: black;
|
341 |
+
}
|
342 |
+
#el {
|
343 |
+
white-space: nowrap;
|
344 |
+
}
|
345 |
+
"""
|
346 |
+
|
347 |
+
with gr.Blocks(fill_height=True, css=css, theme=theme) as demo:
|
348 |
+
# check if demo is running in dark mode
|
349 |
+
gr.Markdown(LOGO)
|
350 |
+
gr.Markdown(DESCRIPTION)
|
351 |
+
gr.Interface(
|
352 |
+
text_analysis,
|
353 |
+
[
|
354 |
+
gr.Textbox(label="Input Text", placeholder="Enter sentence here..."),
|
355 |
+
gr.Dropdown(
|
356 |
+
relik_available_models,
|
357 |
+
value=relik_available_models[0],
|
358 |
+
label="Relik Model",
|
359 |
+
),
|
360 |
+
gr.Slider(
|
361 |
+
minimum=0,
|
362 |
+
maximum=1,
|
363 |
+
step=0.05,
|
364 |
+
value=0.5,
|
365 |
+
label="Relation Threshold",
|
366 |
+
info="Minimum confidence for relation extraction (Only for RE and cIE)",
|
367 |
+
),
|
368 |
+
gr.Slider(
|
369 |
+
minimum=16,
|
370 |
+
maximum=128,
|
371 |
+
step=16,
|
372 |
+
value=32,
|
373 |
+
label="Window Size",
|
374 |
+
info="Window size for the sliding window",
|
375 |
+
),
|
376 |
+
gr.Slider(
|
377 |
+
minimum=8,
|
378 |
+
maximum=64,
|
379 |
+
step=8,
|
380 |
+
value=16,
|
381 |
+
label="Window Stride",
|
382 |
+
info="Window stride for the sliding window",
|
383 |
+
),
|
384 |
+
],
|
385 |
+
[gr.HTML(label="Entities"), gr.HTML(label="Relations")],
|
386 |
+
examples=[
|
387 |
+
["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."],
|
388 |
+
["'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."],
|
389 |
+
],
|
390 |
+
allow_flagging="never",
|
391 |
+
)
|
392 |
+
gr.Markdown("")
|
393 |
+
gr.Markdown(INSTRUCTION)
|
394 |
+
|
395 |
+
if __name__ == "__main__":
|
396 |
+
demo.launch()
|