Spaces:
Runtime error
Runtime error
File size: 9,419 Bytes
a50f42c 175c436 a50f42c |
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 |
import os
import streamlit as st
from spacy import displacy
from PIL import Image
import json
import requests
from pyvis.network import Network
import streamlit.components.v1 as components
from util.process_data import Entity, EntityType, Relation, Sample, SampleList
from util.tokenizer import Tokenizer
from model_inference import TransformersInference
from util.configuration import InferenceConfiguration
inference_config = InferenceConfiguration()
tokenizer = Tokenizer(inference_config.spacy_model)
SAMPLE_66 = "EStG § 66 Höhe des Kindergeldes, Zahlungszeitraum (1) Das Kindergeld beträgt monatlich für das erste und zweite Kind jeweils 219 Euro, für das dritte Kind 225 Euro und für das vierte und jedes weitere Kind jeweils 250 Euro."
SAMPLE_9 = "EStG § 9 Werbungskosten ... Zur Abgeltung dieser Aufwendungen ist für jeden Arbeitstag, an dem der Arbeitnehmer die erste Tätigkeitsstätte aufsucht eine Entfernungspauschale für jeden vollen Kilometer der Entfernung zwischen Wohnung und erster Tätigkeitsstätte von 0,30 Euro anzusetzen, höchstens jedoch 4 500 Euro im Kalenderjahr; ein höherer Betrag als 4 500 Euro ist anzusetzen, soweit der Arbeitnehmer einen eigenen oder ihm zur Nutzung überlassenen Kraftwagen benutzt."
############################################################
## Constants
############################################################
max_width_str = f"max-width: 60%;"
paragraph = None
style = "<style>mark.entity { display: inline-block }</style>"
graph_options = '''
var options = {
"edges": {
"arrows": {
"to": {
"enabled": true,
"scaleFactor": 1.2
}
}
}
}
'''
legend_content = {
"text": "StatedKeyFigure StatedExpression Unit Range Factor Condition DeclarativeKeyFigure DeclarativeExpression",
"ents": [
{"start": 0, "end": 15, "label": "K"},
{"start": 16, "end": 32, "label": "E"},
{"start": 33, "end": 37, "label": "U"},
{"start": 38, "end": 43, "label": "R"},
{"start": 44, "end": 50, "label": "F"},
{"start": 51, "end": 60, "label": "C"},
{"start": 61, "end": 81, "label": "DK"},
{"start": 82, "end": 103, "label": "DE"},
]}
legend_options = {
"ents": ["K","U","E","R","F","C","DK","DE"],
"colors": {'K': '#46d000',"U": "#e861ef", "E": "#538cff", "R": "#ffbe00", "F": "#0fd5dc", "C":"#ff484b", "DK":"#46d000", "DE":"#538cff"}
}
legend_mapping = {"StatedKeyFigure": "K","Unit": "U","StatedExpression": "E","Range": "R","Factor": "F","Condition": "C","DeclarativeKeyFigure": "DK","DeclarativeExpression": "DE"}
edge_colors = {'hasKeyFigure': '#46d000',"hasUnit": "#e861ef", "hasExpression": "#538cff", "hasRange": "#ffbe00", "hasFactor": "#0fd5dc", "hasCondition":"#ff484b", "join":"#aaa", "Typ":"#aaa", "hasParagraph": "#FF8B15"}
############################################################
## Function definitions
############################################################
def get_html(html: str, legend=False):
"""Convert HTML so it can be rendered."""
WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1.5rem">{}</div>"""
if legend: WRAPPER = """<div style="overflow-x: auto; padding: 1rem">{}</div>"""
# Newlines seem to mess with the rendering
html = html.replace("\n", " ")
return WRAPPER.format(html)
def get_displacy_ent_obj(paragraph, bedingungen=False, send_request=False):
entities = []
for entity in paragraph['entities']:
label = entity["entity"] if not send_request else entity["ent_type"]["label"]
if (bedingungen and label == "Condition") or (not bedingungen and label != "Condition") :
entities.append({
'start': entity['start'],
'end': entity["end"],
'label': legend_mapping[label]
})
return [{'text': paragraph['text'], 'ents': entities}]
def request_extractor(text_data):
try:
data = SampleList(
samples=[
Sample(
idx=0,
text=str(text_data),
entities=[],
relations=[]
)
]
)
tokenizer.run(data)
model_inference = TransformersInference(inference_config)
model_inference.run_inference(data)
return data.dict()["samples"][0]
except Exception as e:
result = e
return {"text":"error","entities":[], "relations":[]}
def generate_graph(nodes, edges, send_request=False):
net = Network(height="450px", width="100%")#, bgcolor="#222222", font_color="white", select_menu=True, filter_menu=True)
for node in nodes:
if "id" in node:
label = node["entity"] if not send_request else node["ent_type"]["label"]
node_color = legend_options["colors"][legend_mapping[label]]
node_label = node["text"] if len(node["text"]) < 30 else (node["text"][:27]+" ...")
if label in ["Kennzahl", "Kennzahlumschreibung"]:
net.add_node(node["id"], label=node_label, title=node["text"], mass=2, shape="ellipse", color=node_color, physics=False)
else:
net.add_node(node["id"], label=node_label, title=node["text"], mass=1, shape="ellipse", color=node_color)
for edge in edges:
label = edge["relation"] if not send_request else edge["rel_type"]["label"]
net.add_edge(edge["head"], edge["tail"], width=1, title=label, arrowStrikethrough=False, color=edge_colors[label])
# net.force_atlas_2based() # barnes_hut() force_atlas_2based() hrepulsion() repulsion()
net.toggle_physics(True)
net.set_edge_smooth("dynamic") # dynamic, continuous, discrete, diagonalCross, straightCross, horizontal, vertical, curvedCW, curvedCCW, cubicBezier
net.set_options(graph_options)
html_graph = net.generate_html()
return html_graph
############################################################
## Page configuration
############################################################
st.set_page_config(
page_title="NLP Gesetzestexte",
menu_items={
'Get Help': None,
'Report a bug': None,
'About': "## Demonstrator NLP"
}
# layout="wide")
)
st.markdown(
f"""
<style>
.appview-container .main .block-container{{
{max_width_str}
}}
</style>
""",
unsafe_allow_html=True,
)
# radio button formatting in line
st.write('<style>div.row-widget.stRadio > div{flex-direction:row;justify-content: left;} </style>', unsafe_allow_html=True)
############################################################
## Page formating
############################################################
col3, col4 = st.columns([2.4,1.6])
st.write('\n')
st.write('\n')
with col3:
st.subheader("Extraction of Key Figures from Tax Legal Texts")
st.write("Demonstrator Application for Paper 'Semantic Extraction of Key Figures and Their Properties From Tax Legal Texts using Neural Models' presented at the sixth Workshop on Automated Semantic Analysis of Information in Legal Text (ASAIL 2023)")
st.write("The paper can be found here: [Paper](https://ceur-ws.org/Vol-3441/paper7.pdf)")
st.write('\n')
st.write('This demmosntrator processes German tax laws as input and outputs the extracted key figures with their properties and relations, based on the presented semantic model.')
with col4:
st.caption("Semantic Model")
image = Image.open('util/ontology.png')
st.image(image, width=350)
text_option = st.radio("Select Example", ["Insert your paragraph", "EStG § 66 Kindergeld", "EStG § 9 Werbungskosten"])
st.write('\n')
if text_option == "EStG § 66 Kindergeld":
text_area_input = st.text_area("Given paragraph", SAMPLE_66, height=200)
elif text_option == "EStG § 9 Werbungskosten":
text_area_input = st.text_area("Given paragraph", SAMPLE_9, height=200)
else:
text_area_input = st.text_area("Given paragraph", "", height=200)
if st.button("Start Extraction") and text_area_input != "":
with st.spinner('Executing Extraction ...'):
paragraph = request_extractor(text_area_input)
if paragraph["text"] == "error":
st.error("Error while executing extraction.")
else:
legend = displacy.render([legend_content], style="ent", options=legend_options, manual=True)
st.write(f"{style}{get_html(legend, True)}", unsafe_allow_html=True)
st.caption("Entities:")
extracted_data = get_displacy_ent_obj(paragraph, False, True)
html = displacy.render(extracted_data, style="ent", options=legend_options, manual=True)
st.write(f"{style}{get_html(html)}", unsafe_allow_html=True)
st.write('\n')
st.caption("Conditions:")
extracted_data = get_displacy_ent_obj(paragraph, True, True)
html = displacy.render(extracted_data, style="ent", options=legend_options, manual=True)
st.write(f"{style}{get_html(html)}", unsafe_allow_html=True)
st.write('\n')
st.caption("\n\nRelations:")
html_graph_req = generate_graph(paragraph["entities"], paragraph["relations"], send_request=True)
components.html(html_graph_req, height=500)
st.write('\n')
with st.expander("Show JSON"):
st.json(paragraph)
|