Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -221,7 +221,7 @@ relik_models = {
|
|
221 |
}
|
222 |
|
223 |
|
224 |
-
def get_span_annotations(response, doc):
|
225 |
dict_ents = {}
|
226 |
el_link_wrapper = (
|
227 |
"<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>"
|
@@ -236,7 +236,7 @@ def get_span_annotations(response, doc):
|
|
236 |
span.end,
|
237 |
el_link_wrapper.format(
|
238 |
span.label.replace(" ", "_"), span.label
|
239 |
-
) if span.label != "--NME--" else
|
240 |
# kb_id=span.label.replace(" ", "_")
|
241 |
)
|
242 |
)
|
@@ -249,7 +249,7 @@ def get_span_annotations(response, doc):
|
|
249 |
colors = get_random_color(set([span.label_ for span in spans]))
|
250 |
return spans, colors, dict_ents
|
251 |
|
252 |
-
def generate_graph(spans, response, colors, dict_ents, bgcolor="#111827", font_color="white"):
|
253 |
g = Network(
|
254 |
width="720px",
|
255 |
height="600px",
|
@@ -269,7 +269,7 @@ def generate_graph(spans, response, colors, dict_ents, bgcolor="#111827", font_c
|
|
269 |
|
270 |
for ent in spans:
|
271 |
# if not NME use title:
|
272 |
-
if dict_ents[(ent.start, ent.end)][2] != "--NME--":
|
273 |
g.add_node(
|
274 |
dict_ents[(ent.start, ent.end)][2],
|
275 |
label=dict_ents[(ent.start, ent.end)][2],
|
@@ -289,22 +289,23 @@ def generate_graph(spans, response, colors, dict_ents, bgcolor="#111827", font_c
|
|
289 |
)
|
290 |
seen_rels = set()
|
291 |
for rel in response.triplets:
|
292 |
-
if
|
293 |
-
if (rel.subject.
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
|
|
304 |
|
305 |
g.add_edge(
|
306 |
-
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],
|
307 |
-
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],
|
308 |
label=rel.label,
|
309 |
title=rel.label,
|
310 |
)
|
@@ -344,7 +345,7 @@ def text_analysis(Text, Model, Relation_Threshold, Window_Size, Window_Stride):
|
|
344 |
nlp = spacy.blank("xx")
|
345 |
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)
|
346 |
doc = Doc(nlp.vocab, words=[token.text for token in annotated_text.tokens])
|
347 |
-
spans, colors, dict_ents = get_span_annotations(response=annotated_text, doc=doc)
|
348 |
doc.spans["sc"] = spans
|
349 |
|
350 |
# build the EL display
|
@@ -362,7 +363,7 @@ def text_analysis(Text, Model, Relation_Threshold, Window_Size, Window_Stride):
|
|
362 |
display_re = ""
|
363 |
if annotated_text.triplets:
|
364 |
# background_color should be the same as the background of the page
|
365 |
-
display_re = generate_graph(spans, annotated_text, colors, dict_ents)
|
366 |
return display_el, display_re
|
367 |
|
368 |
|
|
|
221 |
}
|
222 |
|
223 |
|
224 |
+
def get_span_annotations(response, doc, ner=False):
|
225 |
dict_ents = {}
|
226 |
el_link_wrapper = (
|
227 |
"<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>"
|
|
|
236 |
span.end,
|
237 |
el_link_wrapper.format(
|
238 |
span.label.replace(" ", "_"), span.label
|
239 |
+
) if span.label != "--NME--" and not ner else span.label,
|
240 |
# kb_id=span.label.replace(" ", "_")
|
241 |
)
|
242 |
)
|
|
|
249 |
colors = get_random_color(set([span.label_ for span in spans]))
|
250 |
return spans, colors, dict_ents
|
251 |
|
252 |
+
def generate_graph(spans, response, colors, dict_ents, bgcolor="#111827", font_color="white", ner=False):
|
253 |
g = Network(
|
254 |
width="720px",
|
255 |
height="600px",
|
|
|
269 |
|
270 |
for ent in spans:
|
271 |
# if not NME use title:
|
272 |
+
if dict_ents[(ent.start, ent.end)][2] != "--NME--" or not ner:
|
273 |
g.add_node(
|
274 |
dict_ents[(ent.start, ent.end)][2],
|
275 |
label=dict_ents[(ent.start, ent.end)][2],
|
|
|
289 |
)
|
290 |
seen_rels = set()
|
291 |
for rel in response.triplets:
|
292 |
+
if not ner:
|
293 |
+
if dict_ents[(rel.subject.start, rel.subject.end)][2] == "--NME--" and dict_ents[(rel.object.start, rel.object.end)][2] == "--NME--":
|
294 |
+
if (rel.subject.text, rel.object.text, rel.label) in seen_rels:
|
295 |
+
continue
|
296 |
+
elif dict_ents[(rel.subject.start, rel.subject.end)][2] == "--NME--" and dict_ents[(rel.object.start, rel.object.end)][2] != "--NME--":
|
297 |
+
if (rel.subject.text, dict_ents[(rel.object.start, rel.object.end)][2], rel.label) in seen_rels:
|
298 |
+
continue
|
299 |
+
elif dict_ents[(rel.subject.start, rel.subject.end)][2] != "--NME--" and dict_ents[(rel.object.start, rel.object.end)][2] == "--NME--":
|
300 |
+
if (dict_ents[(rel.subject.start, rel.subject.end)][2], rel.object.text, rel.label) in seen_rels:
|
301 |
+
continue
|
302 |
+
else:
|
303 |
+
if (dict_ents[(rel.subject.start, rel.subject.end)][2], dict_ents[(rel.object.start, rel.object.end)][2], rel.label) in seen_rels:
|
304 |
+
continue
|
305 |
|
306 |
g.add_edge(
|
307 |
+
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],
|
308 |
+
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],
|
309 |
label=rel.label,
|
310 |
title=rel.label,
|
311 |
)
|
|
|
345 |
nlp = spacy.blank("xx")
|
346 |
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)
|
347 |
doc = Doc(nlp.vocab, words=[token.text for token in annotated_text.tokens])
|
348 |
+
spans, colors, dict_ents = get_span_annotations(response=annotated_text, doc=doc, ner="ner" in Model)
|
349 |
doc.spans["sc"] = spans
|
350 |
|
351 |
# build the EL display
|
|
|
363 |
display_re = ""
|
364 |
if annotated_text.triplets:
|
365 |
# background_color should be the same as the background of the page
|
366 |
+
display_re = generate_graph(spans, annotated_text, colors, dict_ents, ner="ner" in Model)
|
367 |
return display_el, display_re
|
368 |
|
369 |
|