wadood commited on
Commit
c50c9d2
β€’
1 Parent(s): 19d19d4

added tabs

Browse files
Files changed (3) hide show
  1. app.py +93 -91
  2. constants.py +6 -0
  3. evaluation_metrics.py +1 -1
app.py CHANGED
@@ -1,9 +1,11 @@
1
  import pandas as pd
2
  import streamlit as st
3
- from annotated_text import annotated_text
 
4
  from annotated_text.util import get_annotated_html
5
  from streamlit_annotation_tools import text_labeler
6
 
 
7
  from evaluation_metrics import EVALUATION_METRICS
8
  from predefined_example import EXAMPLES
9
  from span_dataclass_converters import (
@@ -26,110 +28,110 @@ def get_examples_attributes(selected_example):
26
 
27
  if __name__ == "__main__":
28
  st.set_page_config(layout="wide")
29
- st.title("NER Metrics Comparison")
30
 
31
  st.write(
32
  "Evaluation for the NER task requires a ground truth and a prediction that will be evaluated. The ground truth is shown below, add predictions in the next section to compare the evaluation metrics."
33
  )
 
34
 
35
- # with st.container():
36
- st.subheader("Ground Truth") # , divider='rainbow')
37
 
38
- selected_example = st.selectbox(
39
- "Select an example text from the drop down below",
40
- [example for example in EXAMPLES],
41
- format_func=lambda ex: ex.text,
42
- )
43
 
44
- text, gt_labels, gt_spans, predictions, tags = get_examples_attributes(
45
- selected_example
46
- )
 
 
47
 
48
- annotated_text(
49
- get_highlight_spans_from_ner_spans(
50
- get_ner_spans_from_annotations(gt_labels), text
51
  )
52
- )
53
 
54
- annotated_predictions = [
55
- get_annotated_html(get_highlight_spans_from_ner_spans(ner_span, text))
56
- for ner_span in predictions
57
- ]
58
- predictions_df = pd.DataFrame(
59
- {
60
- # "ID": [f"Prediction_{index}" for index in range(len(predictions))],
61
- "Prediction": annotated_predictions,
62
- "ner_spans": predictions,
63
- },
64
- index=[f"Prediction_{index}" for index in range(len(predictions))],
65
- )
 
 
 
 
 
 
 
66
 
67
- st.subheader("Predictions") # , divider='rainbow')
68
 
69
- with st.expander("Click to Add Predictions"):
70
- st.subheader("Adding predictions")
71
- st.markdown(
72
- """
73
- Add predictions to the list of predictions on which the evaluation metric will be caculated.
74
- - Select the entity type/label name and then highlight the span in the text below.
75
- - To remove a span, double click on the higlighted text.
76
- - Once you have your desired prediction, click on the 'Add' button.(The prediction created is shown in a json below)
77
- """
78
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  st.write(
80
- "Note: Only the spans of the selected label name is shown at a given instance.",
81
  )
82
- labels = text_labeler(text, gt_labels)
83
- st.json(labels, expanded=False)
84
-
85
- # if st.button("Add Prediction"):
86
- # labels = text_labeler(text)
87
- if st.button("Add!"):
88
- spans = get_ner_spans_from_annotations(labels)
89
- spans = sorted(spans, key=lambda span: span["start"])
90
- predictions.append(spans)
91
- annotated_predictions.append(
92
- get_annotated_html(get_highlight_spans_from_ner_spans(spans, text))
93
- )
94
- predictions_df = pd.DataFrame(
95
- {
96
- # "ID": [f"Prediction_{index}" for index in range(len(predictions))],
97
- "Prediction": annotated_predictions,
98
- "ner_spans": predictions,
99
- },
100
- index=[f"Prediction_{index}" for index in range(len(predictions))],
101
- )
102
- print("added")
103
-
104
- highlighted_predictions_df = predictions_df[["Prediction"]]
105
- st.write(highlighted_predictions_df.to_html(escape=False), unsafe_allow_html=True)
106
- st.divider()
107
-
108
- ### EVALUATION METRICS COMPARISION ###
109
-
110
- st.subheader("Evaluation Metrics Comparision") # , divider='rainbow')
111
- st.markdown("""
112
- The different evaluation metrics we have for the NER task are
113
- - Span Based Evaluation with Partial Overlap
114
- - Token Based Evaluation with Micro Avg
115
- - Token Based Evaluation with Macro Avg
116
- """)
117
-
118
- with st.expander("View Predictions Details"):
119
- st.write(predictions_df.to_html(escape=False), unsafe_allow_html=True)
120
-
121
- if st.button("Get Metrics!"):
122
- for evaluation_metric in EVALUATION_METRICS:
123
- predictions_df[evaluation_metric.name] = predictions_df.ner_spans.apply(
124
- lambda ner_spans: evaluation_metric.get_evaluation_metric(
125
- # metric_type=evaluation_metric_type,
126
- gt_ner_span=gt_spans,
127
- pred_ner_span=ner_spans,
128
- text=text,
129
- tags=tags,
130
  )
131
- )
132
 
133
- metrics_df = predictions_df.drop(["ner_spans"], axis=1)
134
 
135
- st.write(metrics_df.to_html(escape=False), unsafe_allow_html=True)
 
1
  import pandas as pd
2
  import streamlit as st
3
+
4
+ # from annotated_text import annotated_text
5
  from annotated_text.util import get_annotated_html
6
  from streamlit_annotation_tools import text_labeler
7
 
8
+ from constants import PREDICTION_ADDITION_INSTRUCTION
9
  from evaluation_metrics import EVALUATION_METRICS
10
  from predefined_example import EXAMPLES
11
  from span_dataclass_converters import (
 
28
 
29
  if __name__ == "__main__":
30
  st.set_page_config(layout="wide")
31
+ st.title("πŸ“ˆ NER Metrics Comparison βš–οΈ")
32
 
33
  st.write(
34
  "Evaluation for the NER task requires a ground truth and a prediction that will be evaluated. The ground truth is shown below, add predictions in the next section to compare the evaluation metrics."
35
  )
36
+ explanation_tab, comparision_tab = st.tabs(["πŸ“™ Explanation", "βš–οΈ Comparision"])
37
 
38
+ with explanation_tab:
39
+ st.write("This is the place holder for explanation of all the metrics")
40
 
41
+ with comparision_tab:
42
+ # with st.container():
43
+ st.subheader("Ground Truth & Predictions") # , divider='rainbow')
 
 
44
 
45
+ selected_example = st.selectbox(
46
+ "Select an example text from the drop down below",
47
+ [example for example in EXAMPLES],
48
+ format_func=lambda ex: ex.text,
49
+ )
50
 
51
+ text, gt_labels, gt_spans, predictions, tags = get_examples_attributes(
52
+ selected_example
 
53
  )
 
54
 
55
+ # annotated_text(
56
+ # get_highlight_spans_from_ner_spans(
57
+ # get_ner_spans_from_annotations(gt_labels), text
58
+ # )
59
+ # )
60
+
61
+ annotated_predictions = [
62
+ get_annotated_html(get_highlight_spans_from_ner_spans(ner_span, text))
63
+ for ner_span in predictions
64
+ ]
65
+ predictions_df = pd.DataFrame(
66
+ {
67
+ # "ID": [f"Prediction_{index}" for index in range(len(predictions))],
68
+ "Prediction": annotated_predictions,
69
+ "ner_spans": predictions,
70
+ },
71
+ index=["Ground Truth"]
72
+ + [f"Prediction_{index}" for index in range(len(predictions) - 1)],
73
+ )
74
 
75
+ # st.subheader("Predictions") # , divider='rainbow')
76
 
77
+ with st.expander("Click to Add Predictions"):
78
+ st.subheader("Adding predictions")
79
+ st.markdown(PREDICTION_ADDITION_INSTRUCTION)
80
+ st.write(
81
+ "Note: Only the spans of the selected label name is shown at a given instance.",
82
+ )
83
+ labels = text_labeler(text, gt_labels)
84
+ st.json(labels, expanded=False)
85
+
86
+ # if st.button("Add Prediction"):
87
+ # labels = text_labeler(text)
88
+ if st.button("Add!"):
89
+ spans = get_ner_spans_from_annotations(labels)
90
+ spans = sorted(spans, key=lambda span: span["start"])
91
+ predictions.append(spans)
92
+ annotated_predictions.append(
93
+ get_annotated_html(get_highlight_spans_from_ner_spans(spans, text))
94
+ )
95
+ predictions_df = pd.DataFrame(
96
+ {
97
+ # "ID": [f"Prediction_{index}" for index in range(len(predictions))],
98
+ "Prediction": annotated_predictions,
99
+ "ner_spans": predictions,
100
+ },
101
+ index=["Ground Truth"]
102
+ + [f"Prediction_{index}" for index in range(len(predictions) - 1)],
103
+ )
104
+ print("added")
105
+
106
+ highlighted_predictions_df = predictions_df[["Prediction"]]
107
  st.write(
108
+ highlighted_predictions_df.to_html(escape=False), unsafe_allow_html=True
109
  )
110
+ st.divider()
111
+
112
+ ### EVALUATION METRICS COMPARISION ###
113
+
114
+ st.subheader("Evaluation Metrics Comparision") # , divider='rainbow')
115
+ st.markdown(
116
+ "The different evaluation metrics we have for the NER task are\n"
117
+ f"{''.join(['- '+evaluation_metric.name+'\n' for evaluation_metric in EVALUATION_METRICS])}"
118
+ )
119
+
120
+ with st.expander("View Predictions Details"):
121
+ st.write(predictions_df.to_html(escape=False), unsafe_allow_html=True)
122
+
123
+ if st.button("Get Metrics!"):
124
+ for evaluation_metric in EVALUATION_METRICS:
125
+ predictions_df[evaluation_metric.name] = predictions_df.ner_spans.apply(
126
+ lambda ner_spans: evaluation_metric.get_evaluation_metric(
127
+ # metric_type=evaluation_metric_type,
128
+ gt_ner_span=gt_spans,
129
+ pred_ner_span=ner_spans,
130
+ text=text,
131
+ tags=tags,
132
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  )
 
134
 
135
+ metrics_df = predictions_df.drop(["ner_spans"], axis=1)
136
 
137
+ st.write(metrics_df.to_html(escape=False), unsafe_allow_html=True)
constants.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ PREDICTION_ADDITION_INSTRUCTION = """
2
+ Add predictions to the list of predictions on which the evaluation metric will be caculated.
3
+ - Select the entity type/label name and then highlight the span in the text below.
4
+ - To remove a span, double click on the higlighted text.
5
+ - Once you have your desired prediction, click on the 'Add' button.(The prediction created is shown in a json below)
6
+ """
evaluation_metrics.py CHANGED
@@ -47,7 +47,7 @@ class TokenMicroMetric(EvaluationMetric):
47
  def __init__(self) -> None:
48
  super().__init__()
49
 
50
- self.name = "Span Based Evaluation with Micro Average"
51
  self.description = ""
52
 
53
  @staticmethod
 
47
  def __init__(self) -> None:
48
  super().__init__()
49
 
50
+ self.name = "Token Based Evaluation with Micro Average"
51
  self.description = ""
52
 
53
  @staticmethod