alanakbik commited on
Commit
c80b30b
1 Parent(s): dacd25e

Update example for Flar 0.12.2

Browse files
Files changed (1) hide show
  1. app.py +30 -7
app.py CHANGED
@@ -11,6 +11,7 @@ st.set_page_config(layout="centered")
11
  model_map = {
12
  "find Entities (default)": "ner-large",
13
  "find Entities (18-class)": "ner-ontonotes-large",
 
14
  "find Parts-of-Speech": "pos-multi",
15
  }
16
 
@@ -25,7 +26,7 @@ selected_model_id = st.selectbox("This is a check box",
25
  st.subheader("Input your text here")
26
  input_text = st.text_area('Write or Paste Text Below',
27
  value='May visited the Eiffel Tower in Paris last May.\n\n'
28
- 'There she saw a sign in German that read: "Dirk mag den Eiffelturm"',
29
  height=128,
30
  max_chars=None,
31
  label_visibility="collapsed")
@@ -36,6 +37,16 @@ def get_model(model_name):
36
  return SequenceTagger.load(model_map[model_name])
37
 
38
 
 
 
 
 
 
 
 
 
 
 
39
  def get_html(html: str):
40
  WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""
41
  html = html.replace("\n", " ")
@@ -60,6 +71,9 @@ button_clicked = st.button("**Click here** to tag the input text", key=None)
60
 
61
  if button_clicked:
62
 
 
 
 
63
  # get a sentence splitter and split text into sentences
64
  splitter = SegtokSentenceSplitter()
65
  sentences = splitter.split(input_text)
@@ -73,11 +87,20 @@ if button_clicked:
73
  predicted_labels = set()
74
  for sentence in sentences:
75
  for prediction in sentence.get_labels():
76
- spacy_display["ents"].append(
77
- {"start": prediction.data_point.start_position + sentence.start_position,
78
- "end": prediction.data_point.end_position + sentence.start_position,
79
- "label": prediction.value})
80
- predicted_labels.add(prediction.value)
 
 
 
 
 
 
 
 
 
81
 
82
  # create colors for each label
83
  colors = {}
@@ -94,5 +117,5 @@ if button_clicked:
94
  },
95
  )
96
  style = "<style>mark.entity { display: inline-block }</style>"
97
- st.subheader("Found entities")
98
  st.write(f"{style}{get_html(html)}", unsafe_allow_html=True)
 
11
  model_map = {
12
  "find Entities (default)": "ner-large",
13
  "find Entities (18-class)": "ner-ontonotes-large",
14
+ "find Frames": "frame-large",
15
  "find Parts-of-Speech": "pos-multi",
16
  }
17
 
 
26
  st.subheader("Input your text here")
27
  input_text = st.text_area('Write or Paste Text Below',
28
  value='May visited the Eiffel Tower in Paris last May.\n\n'
29
+ 'There she ran across a sign in German that read: "Dirk liebt den Eiffelturm"',
30
  height=128,
31
  max_chars=None,
32
  label_visibility="collapsed")
 
37
  return SequenceTagger.load(model_map[model_name])
38
 
39
 
40
+ @st.cache(allow_output_mutation=True)
41
+ def get_frame_definitions():
42
+ frame_definition_map = {}
43
+ with open('propbank_frames_3.1.txt') as infile:
44
+ for line in infile:
45
+ frame_definition_map[line.split('\t')[0]] = line.split('\t')[1]
46
+
47
+ return frame_definition_map
48
+
49
+
50
  def get_html(html: str):
51
  WRAPPER = """<div style="overflow-x: auto; border: 1px solid #e6e9ef; border-radius: 0.25rem; padding: 1rem; margin-bottom: 2.5rem">{}</div>"""
52
  html = html.replace("\n", " ")
 
71
 
72
  if button_clicked:
73
 
74
+ if 'frame' in selected_model_id.lower():
75
+ frame_definition_map = get_frame_definitions()
76
+
77
  # get a sentence splitter and split text into sentences
78
  splitter = SegtokSentenceSplitter()
79
  sentences = splitter.split(input_text)
 
87
  predicted_labels = set()
88
  for sentence in sentences:
89
  for prediction in sentence.get_labels():
90
+ entity_fields = {
91
+ "start": prediction.data_point.start_position + sentence.start_position,
92
+ "end": prediction.data_point.end_position + sentence.start_position,
93
+ "label": prediction.value,
94
+ }
95
+
96
+ if 'frame' in selected_model_id.lower():
97
+ id = prediction.value.split('.')[-1]
98
+ verb = ''.join(prediction.value.split('.')[:-1])
99
+ kb_url = f"https://propbank.github.io/v3.4.0/frames/{verb}.html#{verb}.{id}"
100
+ entity_fields["label"] = f'<a style="text-decoration: underline; text-decoration-style: dotted; color: inherit; font-weight: bold" href="{kb_url}">{prediction.value}</a>'
101
+
102
+ spacy_display["ents"].append(entity_fields)
103
+ predicted_labels.add(entity_fields["label"])
104
 
105
  # create colors for each label
106
  colors = {}
 
117
  },
118
  )
119
  style = "<style>mark.entity { display: inline-block }</style>"
120
+ st.subheader("Tagged text")
121
  st.write(f"{style}{get_html(html)}", unsafe_allow_html=True)