mcmillanmajora commited on
Commit
648180a
1 Parent(s): c98bb79

added basic model comparison and ranking functionality

Browse files
Files changed (1) hide show
  1. posts/model_exploration.py +100 -9
posts/model_exploration.py CHANGED
@@ -1,22 +1,113 @@
1
  import streamlit as st
2
 
 
 
 
3
  title = "Model Exploration"
4
  description = "Comparison of hate speech detection models"
5
  date = "2022-01-26"
6
  thumbnail = "images/huggingface_logo.png"
7
 
 
 
 
8
  def run_article():
9
- st.markdown("""
10
- # Making a Hate Speech Detection Model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- This is where design choices will go.
 
 
 
 
 
 
 
13
 
14
- # Model Output Ranking Tool
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- For now, here's a link to the [space](https://huggingface.co/spaces/aymm/ModelOutputRankingTool).
17
 
18
- # Model Comparison Tool
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- For now, here's a link to the [space](https://huggingface.co/spaces/aymm/ModelComparisonTool).
21
- """)
22
-
 
1
  import streamlit as st
2
 
3
+ # from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
+ from transformers import pipeline
5
+
6
  title = "Model Exploration"
7
  description = "Comparison of hate speech detection models"
8
  date = "2022-01-26"
9
  thumbnail = "images/huggingface_logo.png"
10
 
11
+
12
+ # Creates the forms for receiving multiple inputs to compare for a single
13
+ # model or one input to compare for two models
14
  def run_article():
15
+ st.markdown("""
16
+ # Making a Hate Speech Detection Model
17
+
18
+ This is where design choices will go.
19
+
20
+ # Model Output Ranking
21
+
22
+ For now, here's a link to the [space](https://huggingface.co/spaces/aymm/ModelOutputRankingTool).""")
23
+ with st.expander("Model Output Ranking Tool", expanded=False):
24
+ with st.form(key='ranking'):
25
+ model_name = st.selectbox("Select a model to test",
26
+ ["classla/roberta-base-frenk-hate",
27
+ "cardiffnlp/twitter-roberta-base-hate",
28
+ "Hate-speech-CNERG/dehatebert-mono-english"],
29
+ )
30
+
31
+ input_1 = st.text_input("Input 1",
32
+ placeholder="We shouldn't let [IDENTITY] suffer.")
33
+ input_2 = st.text_input("Input 2",
34
+ placeholder="I'd rather die than date [IDENTITY].")
35
+ input_3 = st.text_input("Input 3",
36
+ placeholder="Good morning.")
37
+ inputs = [input_1, input_2, input_3]
38
 
39
+ if st.form_submit_button(label="Rank inputs"):
40
+ results = run_ranked(model_name, inputs)
41
+ st.dataframe(results)
42
+
43
+
44
+
45
+ st.markdown("""
46
+ # Model Comparison
47
 
48
+ For now, here's a link to the [space](https://huggingface.co/spaces/aymm/ModelComparisonTool).
49
+ """)
50
+ with st.expander("Model Comparison Tool", expanded=False):
51
+ with st.form(key='comparison'):
52
+ model_name_1 = st.selectbox("Select a model to compare",
53
+ ["cardiffnlp/twitter-roberta-base-hate",
54
+ "Hate-speech-CNERG/dehatebert-mono-english",
55
+ ],
56
+ key='compare_model_1'
57
+ )
58
+ model_name_2 = st.selectbox("Select another model to compare",
59
+ ["cardiffnlp/twitter-roberta-base-hate",
60
+ "Hate-speech-CNERG/dehatebert-mono-english",
61
+ ],
62
+ key='compare_model_2'
63
+ )
64
+ input_text = st.text_input("Comparison input")
65
+ if st.form_submit_button(label="Compare models"):
66
+ results = run_compare(model_name_1, model_name_2, input_text)
67
+ st.dataframe(results)
68
+
69
+
70
+ # Runs the received input strings through the given model and returns the
71
+ # output ranked by label and score (does not assume binary labels so the
72
+ # highest score for each label is at the top)
73
+ def run_ranked(model, input_list):
74
+ classifier = pipeline("text-classification", model=model)
75
+ output = []
76
+ labels = {}
77
+ for inputx in input_list:
78
+ result = classifier(inputx)
79
+ curr = {}
80
+ curr['Input'] = inputx
81
+ label = result[0]['label']
82
+ curr['Label'] = label
83
+ score = result[0]['score']
84
+ curr['Score'] = score
85
+ if label in labels:
86
+ labels[label].append(curr)
87
+ else:
88
+ labels[label] = [curr]
89
+ for label in labels:
90
+ sort_list = sorted(labels[label], key=lambda item:item['Score'], reverse=True)
91
+ output += sort_list
92
+ return output
93
 
 
94
 
95
+ # Takes in two model names and returns the output of both models for that
96
+ # given input string
97
+ def run_compare(name_1, name_2, text):
98
+ classifier_1 = pipeline("text-classification", model=name_1)
99
+ result_1 = classifier_1(text)
100
+ out_1 = {}
101
+ out_1['Model'] = name_1
102
+ out_1['Label'] = result_1[0]['label']
103
+ out_1['Score'] = result_1[0]['score']
104
+ classifier_2 = pipeline("text-classification", model=name_2)
105
+ result_2 = classifier_2(text)
106
+ out_2 = {}
107
+ out_2['Model'] = name_2
108
+ out_2['Label'] = result_2[0]['label']
109
+ out_2['Score'] = result_2[0]['score']
110
+ return [out_1, out_2]
111
+
112
 
113
+