Nathan Butters commited on
Commit
e075ce5
1 Parent(s): 520e612

fixed NLTK that I broke

Browse files
.ipynb_checkpoints/app-checkpoint.py CHANGED
@@ -1,8 +1,8 @@
1
  #Import the libraries we know we'll need for the Generator.
2
- import pandas as pd, spacy, nltk, numpy as np
3
  from spacy.matcher import Matcher
4
  nlp = spacy.load("en_core_web_lg")
5
- nltk.download('omw-1.4')
6
 
7
  #Import the libraries to support the model and predictions.
8
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
@@ -35,27 +35,32 @@ def prepare_model():
35
 
36
  @st.experimental_singleton
37
  def prepare_lists():
 
 
38
  countries = pd.read_csv("Assets/Countries/combined-countries.csv")
39
  professions = pd.read_csv("Assets/Professions/soc-professions-2018.csv")
40
- word_lists = [list(countries.Words),list(professions.Words)]
41
  return countries, professions, word_lists
42
 
43
  #Provide all the functions necessary to run the app
44
  #get definitions for control flow in Streamlit
45
  def get_def(word, POS=False):
46
  pos_options = ['NOUN','VERB','ADJ','ADV']
47
- m_word = word.replace(" ", "_")
48
  if POS in pos_options:
49
  seed_definitions = [syn.definition() for syn in wordnet.synsets(m_word, pos=getattr(wordnet, POS))]
50
  else:
51
  seed_definitions = [syn.definition() for syn in wordnet.synsets(m_word)]
52
- seed_definition = col1.selectbox("Which definition is most relevant?", seed_definitions, key= "WN_definition")
53
- if col1.button("Choose Definition"):
54
- col1.write("You've chosen a definition.")
55
- st.session_state.definition = seed_definition
56
- return seed_definition
 
 
 
57
  else:
58
- col1.write("Please choose a definition.")
59
 
60
  ###Start coding the actual app###
61
  st.set_page_config(layout="wide", page_title="Natural Language Counterfactuals (NLC)")
@@ -122,7 +127,8 @@ if layout == 'MultiNLC':
122
 
123
  #Allow the user to pick an option to generate counterfactuals from.
124
  option = st.radio('Which word would you like to use to generate alternatives?', options, key = "option")
125
- if (any(option in sublist for sublist in word_lists)):
 
126
  st.write(f'You selected {option}. It matches a list.')
127
  elif option:
128
  st.write(f'You selected {option}. It does not match a list.')
@@ -131,10 +137,10 @@ if layout == 'MultiNLC':
131
  st.write('Awaiting your selection.')
132
 
133
  if st.button('Generate Alternatives'):
134
- if option in list(countries.Words):
135
  cf_df = gen_cf_country(countries, doc, option)
136
  st.success('Alternatives created.')
137
- elif option in list(professions.Words):
138
  cf_df = gen_cf_profession(professions, doc, option)
139
  st.success('Alternatives created.')
140
  else:
@@ -196,7 +202,8 @@ if layout == 'MultiNLC + Lime':
196
 
197
  #Allow the user to pick an option to generate counterfactuals from.
198
  option = st.radio('Which word would you like to use to generate alternatives?', options, key = "option")
199
- if (any(option in sublist for sublist in word_lists)):
 
200
  st.write(f'You selected {option}. It matches a list.')
201
  elif option:
202
  st.write(f'You selected {option}. It does not match a list.')
@@ -205,10 +212,10 @@ if layout == 'MultiNLC + Lime':
205
  st.write('Awaiting your selection.')
206
 
207
  if st.button('Generate Alternatives'):
208
- if option in list(countries.Words):
209
  cf_df = gen_cf_country(countries, doc, option)
210
  st.success('Alternatives created.')
211
- elif option in list(professions.Words):
212
  cf_df = gen_cf_profession(professions, doc, option)
213
  st.success('Alternatives created.')
214
  else:
@@ -276,7 +283,8 @@ if layout == 'VizNLC':
276
 
277
  #Allow the user to pick an option to generate counterfactuals from.
278
  option = st.radio('Which word would you like to use to generate alternatives?', options, key = "option")
279
- if (any(option in sublist for sublist in word_lists)):
 
280
  st.write(f'You selected {option}. It matches a list.')
281
  elif option:
282
  st.write(f'You selected {option}. It does not match a list.')
@@ -285,10 +293,10 @@ if layout == 'VizNLC':
285
  st.write('Awaiting your selection.')
286
 
287
  if st.button('Generate Alternatives'):
288
- if option in list(countries.Words):
289
  cf_df = gen_cf_country(countries, doc, option)
290
  st.success('Alternatives created.')
291
- elif option in list(professions.Words):
292
  cf_df = gen_cf_profession(professions, doc, option)
293
  st.success('Alternatives created.')
294
  else:
 
1
  #Import the libraries we know we'll need for the Generator.
2
+ import pandas as pd, spacy, nltk, numpy as np, re
3
  from spacy.matcher import Matcher
4
  nlp = spacy.load("en_core_web_lg")
5
+ from nltk.corpus import wordnet
6
 
7
  #Import the libraries to support the model and predictions.
8
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
 
35
 
36
  @st.experimental_singleton
37
  def prepare_lists():
38
+ nltk.download('omw-1.4')
39
+ nltk.download('wordnet')
40
  countries = pd.read_csv("Assets/Countries/combined-countries.csv")
41
  professions = pd.read_csv("Assets/Professions/soc-professions-2018.csv")
42
+ word_lists = [list(countries.Words.apply(lambda x: x.lower())),list(professions.Words)]
43
  return countries, professions, word_lists
44
 
45
  #Provide all the functions necessary to run the app
46
  #get definitions for control flow in Streamlit
47
  def get_def(word, POS=False):
48
  pos_options = ['NOUN','VERB','ADJ','ADV']
49
+ m_word = re.sub("(\W\s|\s)","_",word)
50
  if POS in pos_options:
51
  seed_definitions = [syn.definition() for syn in wordnet.synsets(m_word, pos=getattr(wordnet, POS))]
52
  else:
53
  seed_definitions = [syn.definition() for syn in wordnet.synsets(m_word)]
54
+ if len(seed_definitions) > 0:
55
+ seed_definition = col1.selectbox("Which definition is most relevant?", seed_definitions, key= "WN_definition")
56
+ if col1.button("Choose Definition"):
57
+ col1.write("You've chosen a definition.")
58
+ st.session_state.definition = seed_definition
59
+ return seed_definition
60
+ else:
61
+ col1.write("Please choose a definition.")
62
  else:
63
+ col1.error("The word you've chosen does not have a definition within WordNet.")
64
 
65
  ###Start coding the actual app###
66
  st.set_page_config(layout="wide", page_title="Natural Language Counterfactuals (NLC)")
 
127
 
128
  #Allow the user to pick an option to generate counterfactuals from.
129
  option = st.radio('Which word would you like to use to generate alternatives?', options, key = "option")
130
+ lc_option = option.lower()
131
+ if (any(lc_option in sublist for sublist in word_lists)):
132
  st.write(f'You selected {option}. It matches a list.')
133
  elif option:
134
  st.write(f'You selected {option}. It does not match a list.')
 
137
  st.write('Awaiting your selection.')
138
 
139
  if st.button('Generate Alternatives'):
140
+ if lc_option in word_lists[0]:
141
  cf_df = gen_cf_country(countries, doc, option)
142
  st.success('Alternatives created.')
143
+ elif lc_option in word_lists[1]:
144
  cf_df = gen_cf_profession(professions, doc, option)
145
  st.success('Alternatives created.')
146
  else:
 
202
 
203
  #Allow the user to pick an option to generate counterfactuals from.
204
  option = st.radio('Which word would you like to use to generate alternatives?', options, key = "option")
205
+ lc_option = option.lower()
206
+ if (any(lc_option in sublist for sublist in word_lists)):
207
  st.write(f'You selected {option}. It matches a list.')
208
  elif option:
209
  st.write(f'You selected {option}. It does not match a list.')
 
212
  st.write('Awaiting your selection.')
213
 
214
  if st.button('Generate Alternatives'):
215
+ if lc_option in word_lists[0]:
216
  cf_df = gen_cf_country(countries, doc, option)
217
  st.success('Alternatives created.')
218
+ elif lc_option in word_lists[1]:
219
  cf_df = gen_cf_profession(professions, doc, option)
220
  st.success('Alternatives created.')
221
  else:
 
283
 
284
  #Allow the user to pick an option to generate counterfactuals from.
285
  option = st.radio('Which word would you like to use to generate alternatives?', options, key = "option")
286
+ lc_option = option.lower()
287
+ if (any(lc_option in sublist for sublist in word_lists)):
288
  st.write(f'You selected {option}. It matches a list.')
289
  elif option:
290
  st.write(f'You selected {option}. It does not match a list.')
 
293
  st.write('Awaiting your selection.')
294
 
295
  if st.button('Generate Alternatives'):
296
+ if lc_option in word_lists[0]:
297
  cf_df = gen_cf_country(countries, doc, option)
298
  st.success('Alternatives created.')
299
+ elif lc_option in word_lists[1]:
300
  cf_df = gen_cf_profession(professions, doc, option)
301
  st.success('Alternatives created.')
302
  else:
.ipynb_checkpoints/backup-app-checkpoint.py ADDED
@@ -0,0 +1,343 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Import the libraries we know we'll need for the Generator.
2
+ import pandas as pd, spacy, nltk, numpy as np
3
+ from spacy.matcher import Matcher
4
+ nlp = spacy.load("en_core_web_lg")
5
+ from nltk.corpus import wordnet
6
+
7
+ #Import the libraries to support the model and predictions.
8
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
9
+ import lime
10
+ import torch
11
+ import torch.nn.functional as F
12
+ from lime.lime_text import LimeTextExplainer
13
+
14
+ #Import the libraries for human interaction and visualization.
15
+ import altair as alt
16
+ import streamlit as st
17
+ from annotated_text import annotated_text as ant
18
+
19
+ #Import functions needed to build dataframes of keywords from WordNet
20
+ from WNgen import *
21
+ from NLselector import *
22
+
23
+ @st.experimental_singleton
24
+ def set_up_explainer():
25
+ class_names = ['negative', 'positive']
26
+ explainer = LimeTextExplainer(class_names=class_names)
27
+ return explainer
28
+
29
+ @st.experimental_singleton
30
+ def prepare_model():
31
+ tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
32
+ model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
33
+ pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)
34
+ return tokenizer, model, pipe
35
+
36
+ @st.experimental_singleton
37
+ def prepare_lists():
38
+ nltk.download('omw-1.4')
39
+ nltk.download('wordnet')
40
+ countries = pd.read_csv("Assets/Countries/combined-countries.csv")
41
+ professions = pd.read_csv("Assets/Professions/soc-professions-2018.csv")
42
+ word_lists = [list(countries.Words),list(professions.Words)]
43
+ return countries, professions, word_lists
44
+
45
+ #Provide all the functions necessary to run the app
46
+ #get definitions for control flow in Streamlit
47
+ def get_def(word, POS=False):
48
+ pos_options = ['NOUN','VERB','ADJ','ADV']
49
+ m_word = word.replace(" ", "_")
50
+ if POS in pos_options:
51
+ seed_definitions = [syn.definition() for syn in wordnet.synsets(m_word, pos=getattr(wordnet, POS))]
52
+ else:
53
+ seed_definitions = [syn.definition() for syn in wordnet.synsets(m_word)]
54
+ seed_definition = col1.selectbox("Which definition is most relevant?", seed_definitions, key= "WN_definition")
55
+ if col1.button("Choose Definition"):
56
+ col1.write("You've chosen a definition.")
57
+ st.session_state.definition = seed_definition
58
+ return seed_definition
59
+ else:
60
+ col1.write("Please choose a definition.")
61
+
62
+ ###Start coding the actual app###
63
+ st.set_page_config(layout="wide", page_title="Natural Language Counterfactuals (NLC)")
64
+ layouts = ['Natural Language Explanation', 'Lime Explanation', 'MultiNLC', 'MultiNLC + Lime', 'VizNLC']
65
+ alternatives = ['Similarity', 'Sampling (Random)', 'Sampling (Fixed)', 'Probability']
66
+ alt_choice = "Similarity"
67
+
68
+ #Content in the Sidebar.
69
+ st.sidebar.info('This is an interface for exploring how different interfaces for exploring natural language explanations (NLE) may appear to people. It is intended to allow individuals to provide feedback on specific versions, as well as to compare what one offers over others for the same inputs.')
70
+ layout = st.sidebar.selectbox("Select a layout to explore.", layouts)
71
+ alt_choice = st.sidebar.selectbox("Choose the way you want to display alternatives.", alternatives) #Commented out until we decide this is useful functionality.
72
+
73
+ #Set up the Main Area Layout
74
+ st.title('Natural Language Counterfactuals (NLC) Prototype')
75
+ st.subheader(f'Current Layout: {layout}')
76
+ text = st.text_input('Provide a sentence you want to evaluate.', placeholder = "I like you. I love you.", key="input")
77
+
78
+ #Prepare the model, data, and Lime. Set starting variables.
79
+ tokenizer, model, pipe = prepare_model()
80
+ countries, professions, word_lists = prepare_lists()
81
+ explainer = set_up_explainer()
82
+ text2 = ""
83
+ text3 = ""
84
+ cf_df = pd.DataFrame()
85
+ if 'definition' not in st.session_state:
86
+ st.session_state.definition = "<(^_')>"
87
+
88
+ #Outline the various user interfaces we have built.
89
+
90
+ col1, col2, col3 = st.columns(3)
91
+ if layout == 'Natural Language Explanation':
92
+ with col1:
93
+ if st.session_state.input != "":
94
+ st.caption("This is the sentence you provided.")
95
+ st.write(text)
96
+ probability, sentiment = eval_pred(text, return_all=True)
97
+ nat_lang_explanation = construct_nlexp(text,sentiment,probability)
98
+
99
+ if layout == 'Lime Explanation':
100
+ with col1:
101
+ #Use spaCy to make the sentence into a doc so we can do NLP.
102
+ doc = nlp(st.session_state.input)
103
+ #Evaluate the provided sentence for sentiment and probability.
104
+ if st.session_state.input != "":
105
+ st.caption("This is the sentence you provided.")
106
+ st.write(text)
107
+ probability, sentiment = eval_pred(text, return_all=True)
108
+ options, lime = critical_words(st.session_state.input,options=True)
109
+ nat_lang_explanation = construct_nlexp(text,sentiment,probability)
110
+ st.write(" ")
111
+ st.altair_chart(lime_viz(lime))
112
+
113
+ if layout == 'MultiNLC':
114
+ with col1:
115
+ #Use spaCy to make the sentence into a doc so we can do NLP.
116
+ doc = nlp(st.session_state.input)
117
+ #Evaluate the provided sentence for sentiment and probability.
118
+ if st.session_state.input != "":
119
+ st.caption("This is the sentence you provided.")
120
+ st.write(text)
121
+ probability, sentiment = eval_pred(text, return_all=True)
122
+ options, lime = critical_words(st.session_state.input,options=True)
123
+ nat_lang_explanation = construct_nlexp(text,sentiment,probability)
124
+
125
+ #Allow the user to pick an option to generate counterfactuals from.
126
+ option = st.radio('Which word would you like to use to generate alternatives?', options, key = "option")
127
+ if (any(option in sublist for sublist in word_lists)):
128
+ st.write(f'You selected {option}. It matches a list.')
129
+ elif option:
130
+ st.write(f'You selected {option}. It does not match a list.')
131
+ definition = get_def(option)
132
+ else:
133
+ st.write('Awaiting your selection.')
134
+
135
+ if st.button('Generate Alternatives'):
136
+ if option in list(countries.Words):
137
+ cf_df = gen_cf_country(countries, doc, option)
138
+ st.success('Alternatives created.')
139
+ elif option in list(professions.Words):
140
+ cf_df = gen_cf_profession(professions, doc, option)
141
+ st.success('Alternatives created.')
142
+ else:
143
+ with st.sidebar:
144
+ ant("Generating alternatives for",(option,"opt","#E0FBFB"), "with a definition of: ",(st.session_state.definition,"def","#E0FBFB"),".")
145
+ cf_df = cf_from_wordnet_df(option,text,seed_definition=st.session_state.definition)
146
+ st.success('Alternatives created.')
147
+
148
+ if len(cf_df) != 0:
149
+ if alt_choice == "Similarity":
150
+ text2, text3 = get_min_max(cf_df, option)
151
+ col2.caption(f"This sentence is 'similar' to {option}.")
152
+ col3.caption(f"This sentence is 'not similar' to {option}.")
153
+ elif alt_choice == "Sampling (Random)":
154
+ text2, text3 = sampled_alts(cf_df, option)
155
+ col2.caption(f"This sentence is a random sample from the alternatives.")
156
+ col3.caption(f"This sentence is a random sample from the alternatives.")
157
+ elif alt_choice == "Sampling (Fixed)":
158
+ text2, text3 = sampled_alts(cf_df, option, fixed=True)
159
+ col2.caption(f"This sentence is a fixed sample of the alternatives.")
160
+ col3.caption(f"This sentence is a fixed sample of the alternatives.")
161
+ elif alt_choice == "Probability":
162
+ text2, text3 = abs_dif(cf_df, option)
163
+ col2.caption(f"This sentence is the closest prediction in the model.")
164
+ col3.caption(f"This sentence is the farthest prediction in the model.")
165
+ with st.sidebar:
166
+ st.info(f"Alternatives generated: {len(cf_df)}")
167
+
168
+ with col2:
169
+ if text2 != "":
170
+ sim2 = cf_df.loc[cf_df['text'] == text2, 'similarity'].iloc[0]
171
+ st.write(text2)
172
+ probability2, sentiment2 = eval_pred(text2, return_all=True)
173
+ nat_lang_explanation = construct_nlexp(text2,sentiment2,probability2)
174
+ #st.info(f" Similarity Score: {np.round(sim2, 2)}, Num Checked: {len(cf_df)}") #for QA purposes
175
+
176
+ with col3:
177
+ if text3 != "":
178
+ sim3 = cf_df.loc[cf_df['text'] == text3, 'similarity'].iloc[0]
179
+ st.write(text3)
180
+ probability3, sentiment3 = eval_pred(text3, return_all=True)
181
+ nat_lang_explanation = construct_nlexp(text3,sentiment3,probability3)
182
+ #st.info(f"Similarity Score: {np.round(sim3, 2)}, Num Checked: {len(cf_df)}") #for QA purposes
183
+
184
+ if layout == 'MultiNLC + Lime':
185
+ with col1:
186
+
187
+ #Use spaCy to make the sentence into a doc so we can do NLP.
188
+ doc = nlp(st.session_state.input)
189
+ #Evaluate the provided sentence for sentiment and probability.
190
+ if st.session_state.input != "":
191
+ st.caption("This is the sentence you provided.")
192
+ st.write(text)
193
+ probability, sentiment = eval_pred(text, return_all=True)
194
+ options, lime = critical_words(st.session_state.input,options=True)
195
+ nat_lang_explanation = construct_nlexp(text,sentiment,probability)
196
+ st.write(" ")
197
+ st.altair_chart(lime_viz(lime))
198
+
199
+ #Allow the user to pick an option to generate counterfactuals from.
200
+ option = st.radio('Which word would you like to use to generate alternatives?', options, key = "option")
201
+ if (any(option in sublist for sublist in word_lists)):
202
+ st.write(f'You selected {option}. It matches a list.')
203
+ elif option:
204
+ st.write(f'You selected {option}. It does not match a list.')
205
+ definition = get_def(option)
206
+ else:
207
+ st.write('Awaiting your selection.')
208
+
209
+ if st.button('Generate Alternatives'):
210
+ if option in list(countries.Words):
211
+ cf_df = gen_cf_country(countries, doc, option)
212
+ st.success('Alternatives created.')
213
+ elif option in list(professions.Words):
214
+ cf_df = gen_cf_profession(professions, doc, option)
215
+ st.success('Alternatives created.')
216
+ else:
217
+ with st.sidebar:
218
+ ant("Generating alternatives for",(option,"opt","#E0FBFB"), "with a definition of: ",(st.session_state.definition,"def","#E0FBFB"),".")
219
+ cf_df = cf_from_wordnet_df(option,text,seed_definition=st.session_state.definition)
220
+ st.success('Alternatives created.')
221
+
222
+ if len(cf_df) != 0:
223
+ if alt_choice == "Similarity":
224
+ text2, text3 = get_min_max(cf_df, option)
225
+ col2.caption(f"This sentence is 'similar' to {option}.")
226
+ col3.caption(f"This sentence is 'not similar' to {option}.")
227
+ elif alt_choice == "Sampling (Random)":
228
+ text2, text3 = sampled_alts(cf_df, option)
229
+ col2.caption(f"This sentence is a random sample from the alternatives.")
230
+ col3.caption(f"This sentence is a random sample from the alternatives.")
231
+ elif alt_choice == "Sampling (Fixed)":
232
+ text2, text3 = sampled_alts(cf_df, option, fixed=True)
233
+ col2.caption(f"This sentence is a fixed sample of the alternatives.")
234
+ col3.caption(f"This sentence is a fixed sample of the alternatives.")
235
+ elif alt_choice == "Probability":
236
+ text2, text3 = abs_dif(cf_df, option)
237
+ col2.caption(f"This sentence is the closest prediction in the model.")
238
+ col3.caption(f"This sentence is the farthest prediction in the model.")
239
+ with st.sidebar:
240
+ st.info(f"Alternatives generated: {len(cf_df)}")
241
+
242
+ with col2:
243
+ if text2 != "":
244
+ sim2 = cf_df.loc[cf_df['text'] == text2, 'similarity'].iloc[0]
245
+ st.write(text2)
246
+ probability2, sentiment2 = eval_pred(text2, return_all=True)
247
+ nat_lang_explanation = construct_nlexp(text2,sentiment2,probability2)
248
+ exp2 = explainer.explain_instance(text2, predictor, num_features=15, num_samples=2000)
249
+ lime_results2 = exp2.as_list()
250
+ st.write(" ")
251
+ st.altair_chart(lime_viz(lime_results2))
252
+
253
+ with col3:
254
+ if text3 != "":
255
+ sim3 = cf_df.loc[cf_df['text'] == text3, 'similarity'].iloc[0]
256
+ st.write(text3)
257
+ probability3, sentiment3 = eval_pred(text3, return_all=True)
258
+ nat_lang_explanation = construct_nlexp(text3,sentiment3,probability3)
259
+ exp3 = explainer.explain_instance(text3, predictor, num_features=15, num_samples=2000)
260
+ lime_results3 = exp3.as_list()
261
+ st.write(" ")
262
+ st.altair_chart(lime_viz(lime_results3))
263
+
264
+ if layout == 'VizNLC':
265
+ with col1:
266
+
267
+ #Use spaCy to make the sentence into a doc so we can do NLP.
268
+ doc = nlp(st.session_state.input)
269
+ #Evaluate the provided sentence for sentiment and probability.
270
+ if st.session_state.input != "":
271
+ st.caption("This is the sentence you provided.")
272
+ st.write(text)
273
+ probability, sentiment = eval_pred(text, return_all=True)
274
+ options, lime = critical_words(st.session_state.input,options=True)
275
+ nat_lang_explanation = construct_nlexp(text,sentiment,probability)
276
+ st.write(" ")
277
+ st.altair_chart(lime_viz(lime))
278
+
279
+ #Allow the user to pick an option to generate counterfactuals from.
280
+ option = st.radio('Which word would you like to use to generate alternatives?', options, key = "option")
281
+ if (any(option in sublist for sublist in word_lists)):
282
+ st.write(f'You selected {option}. It matches a list.')
283
+ elif option:
284
+ st.write(f'You selected {option}. It does not match a list.')
285
+ definition = get_def(option)
286
+ else:
287
+ st.write('Awaiting your selection.')
288
+
289
+ if st.button('Generate Alternatives'):
290
+ if option in list(countries.Words):
291
+ cf_df = gen_cf_country(countries, doc, option)
292
+ st.success('Alternatives created.')
293
+ elif option in list(professions.Words):
294
+ cf_df = gen_cf_profession(professions, doc, option)
295
+ st.success('Alternatives created.')
296
+ else:
297
+ with st.sidebar:
298
+ ant("Generating alternatives for",(option,"opt","#E0FBFB"), "with a definition of: ",(st.session_state.definition,"def","#E0FBFB"),".")
299
+ cf_df = cf_from_wordnet_df(option,text,seed_definition=st.session_state.definition)
300
+ st.success('Alternatives created.')
301
+
302
+ if len(cf_df) != 0:
303
+ if alt_choice == "Similarity":
304
+ text2, text3 = get_min_max(cf_df, option)
305
+ col2.caption(f"This sentence is 'similar' to {option}.")
306
+ col3.caption(f"This graph represents the {len(cf_df)} alternatives to {option}.")
307
+ elif alt_choice == "Sampling (Random)":
308
+ text2, text3 = sampled_alts(cf_df, option)
309
+ col2.caption(f"This sentence is a random sample from the alternatives.")
310
+ col3.caption(f"This graph represents the {len(cf_df)} alternatives to {option}.")
311
+ elif alt_choice == "Sampling (Fixed)":
312
+ text2, text3 = sampled_alts(cf_df, option, fixed=True)
313
+ col2.caption(f"This sentence is a fixed sample of the alternatives.")
314
+ col3.caption(f"This graph represents the {len(cf_df)} alternatives to {option}.")
315
+ elif alt_choice == "Probability":
316
+ text2, text3 = abs_dif(cf_df, option)
317
+ col2.caption(f"This sentence is the closest prediction in the model.")
318
+ col3.caption(f"This graph represents the {len(cf_df)} alternatives to {option}.")
319
+ with st.sidebar:
320
+ st.info(f"Alternatives generated: {len(cf_df)}")
321
+
322
+ with col2:
323
+ if text2 != "":
324
+ sim2 = cf_df.loc[cf_df['text'] == text2, 'similarity'].iloc[0]
325
+ st.write(text2)
326
+ probability2, sentiment2 = eval_pred(text2, return_all=True)
327
+ nat_lang_explanation = construct_nlexp(text2,sentiment2,probability2)
328
+ exp2 = explainer.explain_instance(text2, predictor, num_features=15, num_samples=2000)
329
+ lime_results2 = exp2.as_list()
330
+ st.write(" ")
331
+ st.altair_chart(lime_viz(lime_results2))
332
+
333
+ with col3:
334
+ if not cf_df.empty:
335
+ single_nearest = alt.selection_single(on='mouseover', nearest=True)
336
+ full = alt.Chart(cf_df).encode(
337
+ alt.X('similarity:Q', scale=alt.Scale(zero=False)),
338
+ alt.Y('pred:Q'),
339
+ color=alt.Color('Categories:N', legend=alt.Legend(title="Color of Categories")),
340
+ size=alt.Size('seed:O'),
341
+ tooltip=('Categories','text','pred')
342
+ ).mark_circle(opacity=.5).properties(width=450, height=450).add_selection(single_nearest)
343
+ st.altair_chart(full)
app.py CHANGED
@@ -2,6 +2,7 @@
2
  import pandas as pd, spacy, nltk, numpy as np, re
3
  from spacy.matcher import Matcher
4
  nlp = spacy.load("en_core_web_lg")
 
5
 
6
  #Import the libraries to support the model and predictions.
7
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
@@ -34,6 +35,8 @@ def prepare_model():
34
 
35
  @st.experimental_singleton
36
  def prepare_lists():
 
 
37
  countries = pd.read_csv("Assets/Countries/combined-countries.csv")
38
  professions = pd.read_csv("Assets/Professions/soc-professions-2018.csv")
39
  word_lists = [list(countries.Words.apply(lambda x: x.lower())),list(professions.Words)]
 
2
  import pandas as pd, spacy, nltk, numpy as np, re
3
  from spacy.matcher import Matcher
4
  nlp = spacy.load("en_core_web_lg")
5
+ from nltk.corpus import wordnet
6
 
7
  #Import the libraries to support the model and predictions.
8
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
 
35
 
36
  @st.experimental_singleton
37
  def prepare_lists():
38
+ nltk.download('omw-1.4')
39
+ nltk.download('wordnet')
40
  countries = pd.read_csv("Assets/Countries/combined-countries.csv")
41
  professions = pd.read_csv("Assets/Professions/soc-professions-2018.csv")
42
  word_lists = [list(countries.Words.apply(lambda x: x.lower())),list(professions.Words)]