|
import json |
|
from collections import defaultdict |
|
from typing import Dict, List, Optional, Tuple, Union |
|
|
|
import gradio as gr |
|
from pie_modules.models import * |
|
from pie_modules.taskmodules import * |
|
from pytorch_ie.annotations import BinaryRelation, LabeledSpan |
|
from pytorch_ie.auto import AutoPipeline |
|
from pytorch_ie.documents import TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions |
|
from pytorch_ie.models import * |
|
from pytorch_ie.taskmodules import * |
|
|
|
RENDER_WITH_DISPLACY = "displaCy + highlighted arguments" |
|
RENDER_WITH_PRETTY_TABLE = "Pretty Table" |
|
|
|
|
|
def render_pretty_table( |
|
document: TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions, **render_kwargs |
|
): |
|
from prettytable import PrettyTable |
|
|
|
t = PrettyTable() |
|
t.field_names = ["head", "tail", "relation"] |
|
t.align = "l" |
|
for relation in list(document.binary_relations) + list(document.binary_relations.predictions): |
|
t.add_row([str(relation.head), str(relation.tail), relation.label]) |
|
|
|
html = t.get_html_string(format=True) |
|
html = "<div style='max-width:100%; max-height:360px; overflow:auto'>" + html + "</div>" |
|
|
|
return html |
|
|
|
|
|
def render_spacy( |
|
document: TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions, |
|
style="ent", |
|
inject_relations=True, |
|
colors_hover=None, |
|
**render_kwargs, |
|
): |
|
from spacy import displacy |
|
|
|
spans = list(document.labeled_spans) + list(document.labeled_spans.predictions) |
|
spacy_doc = { |
|
"text": document.text, |
|
"ents": [ |
|
{"start": entity.start, "end": entity.end, "label": entity.label} for entity in spans |
|
], |
|
"title": None, |
|
} |
|
|
|
html = displacy.render( |
|
spacy_doc, page=True, manual=True, minify=True, style=style, **render_kwargs |
|
) |
|
html = "<div style='max-width:100%; max-height:360px; overflow:auto'>" + html + "</div>" |
|
if inject_relations: |
|
binary_relations = list(document.binary_relations) + list( |
|
document.binary_relations.predictions |
|
) |
|
sorted_entities = sorted(spans, key=lambda x: (x.start, x.end)) |
|
html = inject_relation_data( |
|
html, |
|
sorted_entities=sorted_entities, |
|
binary_relations=binary_relations, |
|
additional_colors=colors_hover, |
|
) |
|
return html |
|
|
|
|
|
def inject_relation_data( |
|
html: str, |
|
sorted_entities, |
|
binary_relations: List[BinaryRelation], |
|
additional_colors: Optional[Dict[str, Union[str, dict]]] = None, |
|
) -> str: |
|
from bs4 import BeautifulSoup |
|
|
|
|
|
soup = BeautifulSoup(html, "html.parser") |
|
|
|
entity2tails = defaultdict(list) |
|
entity2heads = defaultdict(list) |
|
for relation in binary_relations: |
|
entity2heads[relation.tail].append((relation.head, relation.label)) |
|
entity2tails[relation.head].append((relation.tail, relation.label)) |
|
|
|
entity2id = {entity: f"entity-{idx}" for idx, entity in enumerate(sorted_entities)} |
|
|
|
|
|
entities = soup.find_all(class_="entity") |
|
for idx, entity in enumerate(entities): |
|
entity["id"] = f"entity-{idx}" |
|
original_color = entity["style"].split("background:")[1].split(";")[0].strip() |
|
entity["data-color-original"] = original_color |
|
if additional_colors is not None: |
|
for key, color in additional_colors.items(): |
|
entity[f"data-color-{key}"] = ( |
|
json.dumps(color) if isinstance(color, dict) else color |
|
) |
|
entity_annotation = sorted_entities[idx] |
|
|
|
if str(entity_annotation) != entity.next: |
|
raise ValueError(f"Entity text mismatch: {entity_annotation} != {entity.text}") |
|
entity["data-label"] = entity_annotation.label |
|
entity["data-relation-tails"] = json.dumps( |
|
[ |
|
{"entity-id": entity2id[tail], "label": label} |
|
for tail, label in entity2tails.get(entity_annotation, []) |
|
] |
|
) |
|
entity["data-relation-heads"] = json.dumps( |
|
[ |
|
{"entity-id": entity2id[head], "label": label} |
|
for head, label in entity2heads.get(entity_annotation, []) |
|
] |
|
) |
|
|
|
|
|
return str(soup) |
|
|
|
|
|
def predict(text: str) -> Tuple[dict, str]: |
|
document = TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions(text=text) |
|
|
|
|
|
document.labeled_partitions.append(LabeledSpan(start=0, end=len(text), label="text")) |
|
|
|
|
|
pipeline(document) |
|
|
|
document_dict = document.asdict() |
|
return document_dict, json.dumps(document_dict) |
|
|
|
|
|
def render(document_txt: str, render_with: str, render_kwargs_json: str) -> str: |
|
document_dict = json.loads(document_txt) |
|
document = TextDocumentWithLabeledSpansBinaryRelationsAndLabeledPartitions.fromdict( |
|
document_dict |
|
) |
|
render_kwargs = json.loads(render_kwargs_json) |
|
if render_with == RENDER_WITH_PRETTY_TABLE: |
|
html = render_pretty_table(document, **render_kwargs) |
|
elif render_with == RENDER_WITH_DISPLACY: |
|
html = render_spacy(document, **render_kwargs) |
|
else: |
|
raise ValueError(f"Unknown render_with value: {render_with}") |
|
|
|
return html |
|
|
|
|
|
def open_accordion(): |
|
return gr.Accordion(open=True) |
|
|
|
|
|
def close_accordion(): |
|
return gr.Accordion(open=False) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
model_name_or_path = "ArneBinder/sam-pointer-bart-base-v0.3" |
|
|
|
|
|
|
|
example_text = "Scholarly Argumentation Mining (SAM) has recently gained attention due to its potential to help scholars with the rapid growth of published scientific literature. It comprises two subtasks: argumentative discourse unit recognition (ADUR) and argumentative relation extraction (ARE), both of which are challenging since they require e.g. the integration of domain knowledge, the detection of implicit statements, and the disambiguation of argument structure. While previous work focused on dataset construction and baseline methods for specific document sections, such as abstract or results, full-text scholarly argumentation mining has seen little progress. In this work, we introduce a sequential pipeline model combining ADUR and ARE for full-text SAM, and provide a first analysis of the performance of pretrained language models (PLMs) on both subtasks. We establish a new SotA for ADUR on the Sci-Arg corpus, outperforming the previous best reported result by a large margin (+7% F1). We also present the first results for ARE, and thus for the full AM pipeline, on this benchmark dataset. Our detailed error analysis reveals that non-contiguous ADUs as well as the interpretation of discourse connectors pose major challenges and that data annotation needs to be more consistent." |
|
|
|
pipeline = AutoPipeline.from_pretrained(model_name_or_path, device=-1, num_workers=0) |
|
re_pipeline = AutoPipeline.from_pretrained( |
|
model_name_or_path, |
|
device=-1, |
|
num_workers=0, |
|
|
|
) |
|
|
|
default_render_kwargs = { |
|
"style": "ent", |
|
"options": { |
|
|
|
"colors": { |
|
"own_claim".upper(): "#009933", |
|
"background_claim".upper(): "#99ccff", |
|
"data".upper(): "#993399", |
|
} |
|
}, |
|
"colors_hover": { |
|
"selected": "#ffa", |
|
|
|
"tail": { |
|
|
|
"supports": "#9f9", |
|
|
|
"contradicts": "#f99", |
|
|
|
"parts_of_same": None, |
|
}, |
|
"head": None, |
|
"other": None, |
|
}, |
|
} |
|
|
|
with gr.Blocks() as demo: |
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
text = gr.Textbox( |
|
label="Input Text", |
|
lines=20, |
|
value=example_text, |
|
) |
|
|
|
predict_btn = gr.Button("Predict") |
|
|
|
output_txt = gr.Textbox(visible=False) |
|
|
|
with gr.Column(scale=1): |
|
|
|
with gr.Accordion("See plain result ...", open=False) as output_accordion: |
|
output_json = gr.JSON(label="Model Output") |
|
|
|
with gr.Accordion("Render Options", open=False): |
|
render_as = gr.Dropdown( |
|
label="Render with", |
|
choices=[RENDER_WITH_PRETTY_TABLE, RENDER_WITH_DISPLACY], |
|
value=RENDER_WITH_DISPLACY, |
|
) |
|
render_kwargs = gr.Textbox( |
|
label="Render Arguments", |
|
lines=5, |
|
value=json.dumps(default_render_kwargs, indent=2), |
|
) |
|
render_btn = gr.Button("Re-render") |
|
|
|
rendered_output = gr.HTML(label="Rendered Output") |
|
|
|
render_button_kwargs = dict( |
|
fn=render, inputs=[output_txt, render_as, render_kwargs], outputs=rendered_output |
|
) |
|
predict_btn.click(open_accordion, inputs=[], outputs=[output_accordion]).then( |
|
fn=predict, inputs=text, outputs=[output_json, output_txt], api_name="predict" |
|
).success(**render_button_kwargs).success( |
|
close_accordion, inputs=[], outputs=[output_accordion] |
|
) |
|
render_btn.click(**render_button_kwargs, api_name="render") |
|
|
|
js = """ |
|
() => { |
|
function maybeSetColor(entity, colorAttributeKey, colorDictKey) { |
|
var color = entity.getAttribute('data-color-' + colorAttributeKey); |
|
// if color is a json string, parse it and use the value at colorDictKey |
|
try { |
|
const colors = JSON.parse(color); |
|
color = colors[colorDictKey]; |
|
} catch (e) {} |
|
if (color) { |
|
console.log('setting color', color); |
|
console.log('entity', entity); |
|
entity.style.backgroundColor = color; |
|
entity.style.color = '#000'; |
|
} |
|
} |
|
|
|
function highlightRelationArguments(entityId) { |
|
const entities = document.querySelectorAll('.entity'); |
|
// reset all entities |
|
entities.forEach(entity => { |
|
const color = entity.getAttribute('data-color-original'); |
|
entity.style.backgroundColor = color; |
|
entity.style.color = ''; |
|
}); |
|
|
|
if (entityId !== null) { |
|
var visitedEntities = new Set(); |
|
// highlight selected entity |
|
const selectedEntity = document.getElementById(entityId); |
|
if (selectedEntity) { |
|
const label = selectedEntity.getAttribute('data-label'); |
|
maybeSetColor(selectedEntity, 'selected', label); |
|
visitedEntities.add(selectedEntity); |
|
} |
|
// highlight tails |
|
const relationTailsAndLabels = JSON.parse(selectedEntity.getAttribute('data-relation-tails')); |
|
relationTailsAndLabels.forEach(relationTail => { |
|
const tailEntity = document.getElementById(relationTail['entity-id']); |
|
if (tailEntity) { |
|
const label = relationTail['label']; |
|
maybeSetColor(tailEntity, 'tail', label); |
|
visitedEntities.add(tailEntity); |
|
} |
|
}); |
|
// highlight heads |
|
const relationHeadsAndLabels = JSON.parse(selectedEntity.getAttribute('data-relation-heads')); |
|
relationHeadsAndLabels.forEach(relationHead => { |
|
const headEntity = document.getElementById(relationHead['entity-id']); |
|
if (headEntity) { |
|
const label = relationHead['label']; |
|
maybeSetColor(headEntity, 'head', label); |
|
visitedEntities.add(headEntity); |
|
} |
|
}); |
|
// highlight other entities |
|
entities.forEach(entity => { |
|
if (!visitedEntities.has(entity)) { |
|
const label = entity.getAttribute('data-label'); |
|
maybeSetColor(entity, 'other', label); |
|
} |
|
}); |
|
} |
|
} |
|
|
|
const entities = document.querySelectorAll('.entity'); |
|
entities.forEach(entity => { |
|
const alreadyHasListener = entity.getAttribute('data-has-listener'); |
|
if (alreadyHasListener) { |
|
return; |
|
} |
|
entity.addEventListener('mouseover', () => { |
|
highlightRelationArguments(entity.id); |
|
}); |
|
entity.addEventListener('mouseout', () => { |
|
highlightRelationArguments(null); |
|
}); |
|
entity.setAttribute('data-has-listener', 'true'); |
|
}); |
|
} |
|
""" |
|
|
|
rendered_output.change(fn=None, js=js, inputs=[], outputs=[]) |
|
|
|
demo.launch() |
|
|