Nathan Butters commited on
Commit
401217e
1 Parent(s): 19df78d

attempt to remediate

Browse files
Files changed (3) hide show
  1. NLselector.py +221 -0
  2. WNgen.py +314 -0
  3. app.py +349 -0
NLselector.py ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import altair as alt
6
+ import streamlit as st
7
+ from annotated_text import annotated_text as ant
8
+
9
+ #Import the libraries to support the model and predictions.
10
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
11
+ import lime
12
+ import torch
13
+ import torch.nn.functional as F
14
+ from lime.lime_text import LimeTextExplainer
15
+
16
+ #Import WNgen.py
17
+ from WNgen import *
18
+
19
+ class_names = ['negative', 'positive']
20
+ explainer = LimeTextExplainer(class_names=class_names)
21
+ tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
22
+ model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
23
+ pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)
24
+
25
+ def predictor(texts):
26
+ outputs = model(**tokenizer(texts, return_tensors="pt", padding=True))
27
+ probas = F.softmax(outputs.logits, dim=1).detach().numpy()
28
+ return probas
29
+
30
+ @st.experimental_singleton
31
+ def critical_words(document, options=False):
32
+ '''This function is meant to select the critical part of a sentence. Critical, in this context means
33
+ the part of the sentence that is either: A) a NOUN or PROPN from the correct entity group, B) a NOUN,
34
+ C) a NOUN + ADJ combination, or D) ADJ and PROPN used to modify other NOUN tokens.
35
+ It also checks this against what the model thinks is important if the user defines "options" as "LIME" or True.'''
36
+ if type(document) is not spacy.tokens.doc.Doc:
37
+ document = nlp(document)
38
+ chunks = list(document.noun_chunks)
39
+ pos_options = []
40
+ lime_options = []
41
+
42
+ #Identify what the model cares about.
43
+ if options:
44
+ #Run Lime Setup code
45
+ exp = explainer.explain_instance(document.text, predictor, num_features=15, num_samples=2000)
46
+ lime_results = exp.as_list()
47
+ for feature in lime_results:
48
+ lime_options.append(feature[0])
49
+ lime_results = pd.DataFrame(lime_results, columns=["Word","Weight"])
50
+
51
+ #Identify what we care about "parts of speech"
52
+
53
+ # Here I am going to try to pick up pronouns, which are people, and Adjectival Compliments.
54
+ for token in document:
55
+ if (token.text not in pos_options) and ((token.text in lime_options) or (options == False)):
56
+ #print(f"executed {token.text} with {token.pos_} and {token.dep_}") #QA
57
+ if (token.pos_ in ["ADJ","PROPN"]) and (token.dep_ in ["compound", "amod"]) and (document[token.i - 1].dep_ in ["compound", "amod"]):
58
+ compound = document[token.i - 1: token.i +1].text
59
+ pos_options.append(compound)
60
+ print(f'Added {compound} based on "amod" and "compound" adjectives.')
61
+ elif (token.pos_ in ["NOUN"]) and (token.dep_ in ["compound", "amod", "conj"]) and (document[token.i - 1].dep_ in ["compound"]):
62
+ compound = document[token.i - 1: token.i +1].text
63
+ pos_options.append(compound)
64
+ print(f'Added {compound} based on "amod" and "compound" and "conj" nouns.')
65
+ elif (token.pos_ == "PROPN") and (token.dep_ in ["prep","amod"]):
66
+ pos_options.append(token.text)
67
+ print(f"Added '{token.text}' based on their adjectival state.")
68
+ elif (token.pos_ == "ADJ") and (token.dep_ in ["acomp","conj","amod"]):
69
+ pos_options.append(token.text)
70
+ print(f"Added '{token.text}' based on their adjectival state.")
71
+ elif (token.pos_ == "PRON") and (len(token.morph) !=0):
72
+ if (token.morph.get("PronType") == "Prs"):
73
+ pos_options.append(token.text)
74
+ print(f"Added '{token.text}' because it's a human pronoun.")
75
+
76
+ #Noun Chunks parsing
77
+ for chunk in chunks:
78
+ #The use of chunk[-1] is due to testing that it appears to always match the root
79
+ root = chunk[-1]
80
+ #This currently matches to a list I've created. I don't know the best way to deal with this so I'm leaving it as is for the moment.
81
+ if root.ent_type_:
82
+ cur_values = []
83
+ if (len(chunk) > 1) and (chunk[-2].dep_ == "compound"):
84
+ #creates the compound element of the noun
85
+ compound = [x.text for x in chunk if x.dep_ == "compound"]
86
+ print(f"This is the contents of {compound} and it is {all(elem in lime_options for elem in compound)} that all elements are present in {lime_options}.") #for QA
87
+ #checks to see all elements in the compound are important to the model or use the compound if not checking importance.
88
+ if (all(elem in lime_options for elem in cur_values) and (options is True)) or ((options is False)):
89
+ #creates a span for the entirety of the compound noun and adds it to the list.
90
+ span = -1 * (1 + len(compound))
91
+ pos_options.append(chunk[span:].text)
92
+ cur_values + [token.text for token in chunk if token.pos_ in ["ADJ","NOUN","PROPN"]]
93
+ else:
94
+ print(f"The elmenents in {compound} could not be added to the final list because they are not all relevant to the model.")
95
+ else:
96
+ cur_values = [token.text for token in chunk if (token.ent_type_) or (token.pos_ == "ADJ")]
97
+ if (all(elem in lime_options for elem in cur_values) and (options is True)) or ((options is False)):
98
+ pos_options.extend(cur_values)
99
+ print(f"From {chunk.text}, {cur_values} added to pos_options due to entity recognition.") #for QA
100
+ elif len(chunk) >= 1:
101
+ cur_values = [token.text for token in chunk if token.pos_ in ["NOUN","ADJ","PROPN"]]
102
+ if (all(elem in lime_options for elem in cur_values) and (options is True)) or ((options is False)):
103
+ pos_options.extend(cur_values)
104
+ print(f"From {chunk.text}, {cur_values} added to pos_options due to wildcard.") #for QA
105
+ else:
106
+ print(f"No options added for \'{chunk.text}\' ")
107
+
108
+ pos_options = list(set(pos_options))
109
+
110
+ if options:
111
+ return pos_options, lime_results
112
+ else:
113
+ return pos_options
114
+
115
+ # Return the Viz of elements critical to LIME.
116
+ def lime_viz(df):
117
+ if not isinstance(df, pd.DataFrame):
118
+ df = pd.DataFrame(df, columns=["Word","Weight"])
119
+ single_nearest = alt.selection_single(on='mouseover', nearest=True)
120
+ viz = alt.Chart(df).encode(
121
+ alt.X('Weight:Q', scale=alt.Scale(domain=(-1, 1))),
122
+ alt.Y('Word:N', sort='x', axis=None),
123
+ color=alt.Color("Weight", scale=alt.Scale(scheme='blueorange', domain=[0], type="threshold", range='diverging'), legend=None),
124
+ tooltip = ("Word","Weight")
125
+ ).mark_bar().properties(title ="Importance of individual words")
126
+
127
+ text = viz.mark_text(
128
+ fill="black",
129
+ align='right',
130
+ baseline='middle'
131
+ ).encode(
132
+ text='Word:N'
133
+ )
134
+ limeplot = alt.LayerChart(layer=[viz,text], width = 300).configure_axis(grid=False).configure_view(strokeWidth=0)
135
+ return limeplot
136
+
137
+ # Evaluate Predictions using the model and pipe.
138
+ def eval_pred(text, return_all = False):
139
+ '''A basic function for evaluating the prediction from the model and turning it into a visualization friendly number.'''
140
+ preds = pipe(text)
141
+ neg_score = -1 * preds[0][0]['score']
142
+ sent_neg = preds[0][0]['label']
143
+ pos_score = preds[0][1]['score']
144
+ sent_pos = preds[0][1]['label']
145
+ prediction = 0
146
+ sentiment = ''
147
+ if pos_score > abs(neg_score):
148
+ prediction = pos_score
149
+ sentiment = sent_pos
150
+ elif abs(neg_score) > pos_score:
151
+ prediction = neg_score
152
+ sentiment = sent_neg
153
+
154
+ if return_all:
155
+ return prediction, sentiment
156
+ else:
157
+ return prediction
158
+
159
+ def construct_nlexp(text,sentiment,probability):
160
+ prob = str(np.round(100 * abs(probability),2))
161
+ if sentiment == "NEGATIVE":
162
+ color_sent = ant('The model predicts the sentiment of the sentence you provided is ', (sentiment, "-", "#FFA44F"), ' with a probability of ', (prob, "neg", "#FFA44F"),"%.")
163
+ elif sentiment == "POSITIVE":
164
+ color_sent = ant('The model predicts the sentiment of the sentence you provided is ', (sentiment, "+", "#50A9FF"), ' with a probability of ', (prob, "pos", "#50A9FF"),"%.")
165
+ return color_sent
166
+
167
+ def get_min_max(df, seed):
168
+ '''This function provides the alternatives with the highest spaCy similarity scores and the lowest similarity scores. As similarity is based on vectorization of words and documents this may not be the best way to identify bias.
169
+
170
+ text2 = Most Similar
171
+ text3 = Least Similar'''
172
+ maximum = df[df['similarity'] < .9999].similarity.max()
173
+ text2 = df.loc[df['similarity'] == maximum, 'text'].iloc[0]
174
+ minimum = df[df['similarity'] > .0001].similarity.min()
175
+ text3 = df.loc[df['similarity'] == minimum, 'text'].iloc[0]
176
+ return text2, text3
177
+
178
+ # Inspired by https://stackoverflow.com/questions/17758023/return-rows-in-a-dataframe-closest-to-a-user-defined-number/17758115#17758115
179
+ def abs_dif(df,seed):
180
+ '''This function enables a user to identify the alternative that is closest to the seed and farthest from the seed should that be the what they wish to display.
181
+
182
+ text2 = Nearest Prediction
183
+ text3 = Farthest Prediction'''
184
+ seed = process_text(seed)
185
+ target = df[df['Words'] == seed].pred.iloc[0]
186
+ sub_df = df[df['Words'] != seed].reset_index()
187
+ nearest_prediction = sub_df.pred[(sub_df.pred-target).abs().argsort()[:1]]
188
+ farthest_prediction = sub_df.pred[(sub_df.pred-target).abs().argsort()[-1:]]
189
+ text2 = sub_df.text.iloc[nearest_prediction.index[0]]
190
+ text3 = sub_df.text.iloc[farthest_prediction.index[0]]
191
+ return text2, text3
192
+
193
+ #@st.experimental_singleton #I've enabled this to prevent it from triggering every time the code runs... which could get very messy
194
+ def sampled_alts(df, seed, fixed=False):
195
+ '''This function enables a user to select an alternate way of choosing which counterfactuals are shown for MultiNLC, MultiNLC + Lime, and VizNLC. If you use this then you are enabling random sampling over other options (ex. spaCy similarity scores, or absolute difference).
196
+
197
+ Both samples are random.'''
198
+ sub_df = df[df['Words'] != seed]
199
+ if fixed:
200
+ sample = sub_df.sample(n=2, random_state = 2052)
201
+ else:
202
+ sample = sub_df.sample(n=2)
203
+ text2 = sample.text.iloc[0]
204
+ text3 = sample.text.iloc[1]
205
+ return text2, text3
206
+
207
+ def gen_cf_country(df,_document,selection):
208
+ df['text'] = df.Words.apply(lambda x: re.sub(r'\b'+selection+r'\b',x,_document.text))
209
+ df['pred'] = df.text.apply(eval_pred)
210
+ df['seed'] = df.Words.apply(lambda x: 'seed' if x == selection else 'alternative')
211
+ df['similarity'] = df.Words.apply(lambda x: nlp(selection).similarity(nlp(x)))
212
+ return df
213
+
214
+ def gen_cf_profession(df,_document,selection):
215
+ category = df.loc[df['Words'] == selection, 'Major'].iloc[0]
216
+ df = df[df.Major == category]
217
+ df['text'] = df.Words.apply(lambda x: re.sub(r'\b'+selection+r'\b',x,_document.text))
218
+ df['pred'] = df.text.apply(eval_pred)
219
+ df['seed'] = df.Words.apply(lambda x: 'seed' if x == selection else 'alternative')
220
+ df['similarity'] = df.Words.apply(lambda x: nlp(selection).similarity(nlp(x)))
221
+ return df
WNgen.py ADDED
@@ -0,0 +1,314 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Import necessary libraries.
2
+ import re, nltk, pandas as pd, numpy as np, ssl, streamlit as st
3
+ from nltk.corpus import wordnet
4
+ import spacy
5
+ nlp = spacy.load("en_core_web_lg")
6
+
7
+ #Import necessary parts for predicting things.
8
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
9
+ import torch
10
+ import torch.nn.functional as F
11
+ tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
12
+ model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
13
+ pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)
14
+
15
+ #If an error is thrown that the corpus "omw-1.4" isn't discoverable you can use this code. (https://stackoverflow.com/questions/38916452/nltk-download-ssl-certificate-verify-failed)
16
+ '''try:
17
+ _create_unverified_https_context = ssl._create_unverified_context
18
+ except AttributeError:
19
+ pass
20
+ else:
21
+ ssl._create_default_https_context = _create_unverified_https_context
22
+
23
+ nltk.download('omw-1.4')'''
24
+
25
+ # A simple function to pull synonyms and antonyms using spacy's POS
26
+ def syn_ant(word,POS=False,human=True):
27
+ pos_options = ['NOUN','VERB','ADJ','ADV']
28
+ synonyms = []
29
+ antonyms = []
30
+ #WordNet hates spaces so you have to remove them
31
+ if " " in word:
32
+ word = word.replace(" ", "_")
33
+
34
+ if POS in pos_options:
35
+ for syn in wordnet.synsets(word, pos=getattr(wordnet, POS)):
36
+ for l in syn.lemmas():
37
+ current = l.name()
38
+ if human:
39
+ current = re.sub("_"," ",current)
40
+ synonyms.append(current)
41
+ if l.antonyms():
42
+ for ant in l.antonyms():
43
+ cur_ant = ant.name()
44
+ if human:
45
+ cur_ant = re.sub("_"," ",cur_ant)
46
+ antonyms.append(cur_ant)
47
+ else:
48
+ for syn in wordnet.synsets(word):
49
+ for l in syn.lemmas():
50
+ current = l.name()
51
+ if human:
52
+ current = re.sub("_"," ",current)
53
+ synonyms.append(current)
54
+ if l.antonyms():
55
+ for ant in l.antonyms():
56
+ cur_ant = ant.name()
57
+ if human:
58
+ cur_ant = re.sub("_"," ",cur_ant)
59
+ antonyms.append(cur_ant)
60
+ synonyms = list(set(synonyms))
61
+ antonyms = list(set(antonyms))
62
+ return synonyms, antonyms
63
+
64
+ def process_text(text):
65
+ doc = nlp(text.lower())
66
+ result = []
67
+ for token in doc:
68
+ if (token.is_stop) or (token.is_punct) or (token.lemma_ == '-PRON-'):
69
+ continue
70
+ result.append(token.lemma_)
71
+ return " ".join(result)
72
+
73
+ def clean_definition(syn):
74
+ #This function removes stop words from sentences to improve on document level similarity for differentiation.
75
+ if type(syn) is str:
76
+ synset = wordnet.synset(syn).definition()
77
+ elif type(syn) is nltk.corpus.reader.wordnet.Synset:
78
+ synset = syn.definition()
79
+ definition = nlp(process_text(synset))
80
+ return definition
81
+
82
+ def check_sim(a,b):
83
+ if type(a) is str and type(b) is str:
84
+ a = nlp(a)
85
+ b = nlp(b)
86
+ similarity = a.similarity(b)
87
+ return similarity
88
+
89
+ # Builds a dataframe dynamically from WordNet using NLTK.
90
+ def wordnet_df(word,POS=False,seed_definition=None):
91
+ pos_options = ['NOUN','VERB','ADJ','ADV']
92
+ synonyms, antonyms = syn_ant(word,POS,False)
93
+ #print(synonyms, antonyms) #for QA purposes
94
+ words = []
95
+ cats = []
96
+ #WordNet hates spaces so you have to remove them
97
+ m_word = word.replace(" ", "_")
98
+
99
+ #Allow the user to pick a seed definition if it is not provided directly to the function. Currently not working so it's commented out.
100
+ '''#commented out the way it was designed to allow for me to do it through Streamlit (keeping it for posterity, and for anyone who wants to use it without streamlit.)
101
+ for d in range(len(seed_definitions)):
102
+ print(f"{d}: {seed_definitions[d]}")
103
+ #choice = int(input("Which of the definitions above most aligns to your selection?"))
104
+ seed_definition = seed_definitions[choice]'''
105
+ try:
106
+ definition = seed_definition
107
+ except:
108
+ st.write("You did not supply a definition.")
109
+
110
+ if POS in pos_options:
111
+ for syn in wordnet.synsets(m_word, pos=getattr(wordnet, POS)):
112
+ if check_sim(process_text(seed_definition),process_text(syn.definition())) > .7:
113
+ cur_lemmas = syn.lemmas()
114
+ hypos = syn.hyponyms()
115
+ for hypo in hypos:
116
+ cur_lemmas.extend(hypo.lemmas())
117
+ for lemma in cur_lemmas:
118
+ ll = lemma.name()
119
+ cats.append(re.sub("_"," ", syn.name().split(".")[0]))
120
+ words.append(re.sub("_"," ",ll))
121
+
122
+ if len(synonyms) > 0:
123
+ for w in synonyms:
124
+ w = w.replace(" ","_")
125
+ for syn in wordnet.synsets(w, pos=getattr(wordnet, POS)):
126
+ if check_sim(process_text(seed_definition),process_text(syn.definition())) > .6:
127
+ cur_lemmas = syn.lemmas()
128
+ hypos = syn.hyponyms()
129
+ for hypo in hypos:
130
+ cur_lemmas.extend(hypo.lemmas())
131
+ for lemma in cur_lemmas:
132
+ ll = lemma.name()
133
+ cats.append(re.sub("_"," ", syn.name().split(".")[0]))
134
+ words.append(re.sub("_"," ",ll))
135
+ if len(antonyms) > 0:
136
+ for a in antonyms:
137
+ a = a.replace(" ","_")
138
+ for syn in wordnet.synsets(a, pos=getattr(wordnet, POS)):
139
+ if check_sim(process_text(seed_definition),process_text(syn.definition())) > .26:
140
+ cur_lemmas = syn.lemmas()
141
+ hypos = syn.hyponyms()
142
+ for hypo in hypos:
143
+ cur_lemmas.extend(hypo.lemmas())
144
+ for lemma in cur_lemmas:
145
+ ll = lemma.name()
146
+ cats.append(re.sub("_"," ", syn.name().split(".")[0]))
147
+ words.append(re.sub("_"," ",ll))
148
+ else:
149
+ for syn in wordnet.synsets(m_word):
150
+ if check_sim(process_text(seed_definition),process_text(syn.definition())) > .7:
151
+ cur_lemmas = syn.lemmas()
152
+ hypos = syn.hyponyms()
153
+ for hypo in hypos:
154
+ cur_lemmas.extend(hypo.lemmas())
155
+ for lemma in cur_lemmas:
156
+ ll = lemma.name()
157
+ cats.append(re.sub("_"," ", syn.name().split(".")[0]))
158
+ words.append(re.sub("_"," ",ll))
159
+ if len(synonyms) > 0:
160
+ for w in synonyms:
161
+ w = w.replace(" ","_")
162
+ for syn in wordnet.synsets(w):
163
+ if check_sim(process_text(seed_definition),process_text(syn.definition())) > .6:
164
+ cur_lemmas = syn.lemmas()
165
+ hypos = syn.hyponyms()
166
+ for hypo in hypos:
167
+ cur_lemmas.extend(hypo.lemmas())
168
+ for lemma in cur_lemmas:
169
+ ll = lemma.name()
170
+ cats.append(re.sub("_"," ", syn.name().split(".")[0]))
171
+ words.append(re.sub("_"," ",ll))
172
+ if len(antonyms) > 0:
173
+ for a in antonyms:
174
+ a = a.replace(" ","_")
175
+ for syn in wordnet.synsets(a):
176
+ if check_sim(process_text(seed_definition),process_text(syn.definition())) > .26:
177
+ cur_lemmas = syn.lemmas()
178
+ hypos = syn.hyponyms()
179
+ for hypo in hypos:
180
+ cur_lemmas.extend(hypo.lemmas())
181
+ for lemma in cur_lemmas:
182
+ ll = lemma.name()
183
+ cats.append(re.sub("_"," ", syn.name().split(".")[0]))
184
+ words.append(re.sub("_"," ",ll))
185
+
186
+ df = {"Categories":cats, "Words":words}
187
+ df = pd.DataFrame(df)
188
+ df = df.drop_duplicates().reset_index()
189
+ df = df.drop("index", axis=1)
190
+ return df
191
+
192
+ def eval_pred_test(text, return_all = False):
193
+ '''A basic function for evaluating the prediction from the model and turning it into a visualization friendly number.'''
194
+ preds = pipe(text)
195
+ neg_score = -1 * preds[0][0]['score']
196
+ sent_neg = preds[0][0]['label']
197
+ pos_score = preds[0][1]['score']
198
+ sent_pos = preds[0][1]['label']
199
+ prediction = 0
200
+ sentiment = ''
201
+ if pos_score > abs(neg_score):
202
+ prediction = pos_score
203
+ sentiment = sent_pos
204
+ elif abs(neg_score) > pos_score:
205
+ prediction = neg_score
206
+ sentiment = sent_neg
207
+
208
+ if return_all:
209
+ return prediction, sentiment
210
+ else:
211
+ return prediction
212
+
213
+ def get_parallel(word, seed_definition, QA=False):
214
+ cleaned = nlp(process_text(seed_definition))
215
+ root_syns = wordnet.synsets(word)
216
+ hypers = []
217
+ new_hypos = []
218
+
219
+ for syn in root_syns:
220
+ hypers.extend(syn.hypernyms())
221
+
222
+ for syn in hypers:
223
+ new_hypos.extend(syn.hyponyms())
224
+
225
+ hypos = list(set([syn for syn in new_hypos if cleaned.similarity(nlp(process_text(syn.definition()))) >=.75]))[:25]
226
+ # with st.sidebar:
227
+ # st.write(f"The number of hypos is {len(hypos)} during get Parallel at Similarity >= .75.") #QA
228
+
229
+ if len(hypos) <= 1:
230
+ hypos = root_syns
231
+ elif len(hypos) < 3:
232
+ hypos = list(set([syn for syn in new_hypos if cleaned.similarity(nlp(process_text(syn.definition()))) >=.5]))[:25] # added a cap to each
233
+ elif len(hypos) < 10:
234
+ hypos = list(set([syn for syn in new_hypos if cleaned.similarity(nlp(process_text(syn.definition()))) >=.66]))[:25]
235
+ elif len(hypos) >= 10:
236
+ hypos = list(set([syn for syn in new_hypos if cleaned.similarity(nlp(process_text(syn.definition()))) >=.8]))[:25]
237
+
238
+ if QA:
239
+ print(hypers)
240
+ print(hypos)
241
+ return hypers, hypos
242
+ else:
243
+ return hypos
244
+
245
+ # Builds a dataframe dynamically from WordNet using NLTK.
246
+ def wordnet_parallel_df(word,seed_definition=None):
247
+ words = []
248
+ cats = []
249
+ #WordNet hates spaces so you have to remove them
250
+ m_word = word.replace(" ", "_")
251
+
252
+ # add synonyms and antonyms for diversity
253
+ synonyms, antonyms = syn_ant(word)
254
+ words.extend(synonyms)
255
+ cats.extend(["synonyms" for n in range(len(synonyms))])
256
+ words.extend(antonyms)
257
+ cats.extend(["antonyms" for n in range(len(antonyms))])
258
+
259
+ try:
260
+ hypos = get_parallel(m_word,seed_definition)
261
+ except:
262
+ st.write("You did not supply a definition.")
263
+ #Allow the user to pick a seed definition if it is not provided directly to the function.
264
+ '''if seed_definition is None:
265
+ if POS in pos_options:
266
+ seed_definitions = [syn.definition() for syn in wordnet.synsets(m_word, pos=getattr(wordnet, POS))]
267
+ else:
268
+ seed_definitions = [syn.definition() for syn in wordnet.synsets(m_word)]
269
+ for d in range(len(seed_definitions)):
270
+ print(f"{d}: {seed_definitions[d]}")
271
+ choice = int(input("Which of the definitions above most aligns to your selection?"))
272
+ seed_definition = seed_definitions[choice]'''
273
+
274
+ #This is a QA section
275
+ # with st.sidebar:
276
+ # st.write(f"The number of hypos is {len(hypos)} during parallel df creation.") #QA
277
+
278
+ #Transforms hypos into lemmas
279
+ for syn in hypos:
280
+ cur_lemmas = syn.lemmas()
281
+ hypos = syn.hyponyms()
282
+ for hypo in hypos:
283
+ cur_lemmas.extend(hypo.lemmas())
284
+ for lemma in cur_lemmas:
285
+ ll = lemma.name()
286
+ cats.append(re.sub("_"," ", syn.name().split(".")[0]))
287
+ words.append(re.sub("_"," ",ll))
288
+ # with st.sidebar:
289
+ # st.write(f'There are {len(words)} words in the dataframe at the beginning of df creation.') #QA
290
+
291
+ df = {"Categories":cats, "Words":words}
292
+ df = pd.DataFrame(df)
293
+ df = df.drop_duplicates("Words").reset_index()
294
+ df = df.drop("index", axis=1)
295
+ return df
296
+
297
+ #@st.experimental_singleton(suppress_st_warning=True)
298
+ def cf_from_wordnet_df(seed,text,seed_definition=False):
299
+ seed_token = nlp(seed)
300
+ seed_POS = seed_token[0].pos_
301
+ #print(seed_POS) QA
302
+ try:
303
+ df = wordnet_parallel_df(seed,seed_definition)
304
+ except:
305
+ st.write("You did not supply a definition.")
306
+
307
+ df["text"] = df.Words.apply(lambda x: re.sub(r'\b'+seed+r'\b',x,text))
308
+ df["similarity"] = df.Words.apply(lambda x: seed_token[0].similarity(nlp(x)[0]))
309
+ df = df[df["similarity"] > 0].reset_index()
310
+ df.drop("index", axis=1, inplace=True)
311
+ df["pred"] = df.text.apply(eval_pred_test)
312
+ # added this because I think it will make the end results better if we ensure the seed is in the data we generate counterfactuals from.
313
+ df['seed'] = df.Words.apply(lambda x: 'seed' if x.lower() == seed.lower() else 'alternative')
314
+ return df
app.py ADDED
@@ -0,0 +1,349 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.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)")
67
+ layouts = ['Natural Language Explanation', 'Lime Explanation', 'MultiNLC', 'MultiNLC + Lime', 'VizNLC']
68
+ alternatives = ['Similarity', 'Sampling (Random)', 'Sampling (Fixed)', 'Probability']
69
+ alt_choice = "Similarity"
70
+
71
+ #Content in the Sidebar.
72
+ 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.')
73
+ layout = st.sidebar.selectbox("Select a layout to explore.", layouts)
74
+ alt_choice = st.sidebar.selectbox("Choose the way you want to display alternatives.", alternatives) #Commented out until we decide this is useful functionality.
75
+
76
+ #Set up the Main Area Layout
77
+ st.title('Natural Language Counterfactuals (NLC) Prototype')
78
+ st.subheader(f'Current Layout: {layout}')
79
+ text = st.text_input('Provide a sentence you want to evaluate.', placeholder = "I like you. I love you.", key="input")
80
+
81
+ #Prepare the model, data, and Lime. Set starting variables.
82
+ tokenizer, model, pipe = prepare_model()
83
+ countries, professions, word_lists = prepare_lists()
84
+ explainer = set_up_explainer()
85
+ text2 = ""
86
+ text3 = ""
87
+ cf_df = pd.DataFrame()
88
+ if 'definition' not in st.session_state:
89
+ st.session_state.definition = "<(^_')>"
90
+
91
+ #Outline the various user interfaces we have built.
92
+
93
+ col1, col2, col3 = st.columns(3)
94
+ if layout == 'Natural Language Explanation':
95
+ with col1:
96
+ if st.session_state.input != "":
97
+ st.caption("This is the sentence you provided.")
98
+ st.write(text)
99
+ probability, sentiment = eval_pred(text, return_all=True)
100
+ nat_lang_explanation = construct_nlexp(text,sentiment,probability)
101
+
102
+ if layout == 'Lime Explanation':
103
+ with col1:
104
+ #Use spaCy to make the sentence into a doc so we can do NLP.
105
+ doc = nlp(st.session_state.input)
106
+ #Evaluate the provided sentence for sentiment and probability.
107
+ if st.session_state.input != "":
108
+ st.caption("This is the sentence you provided.")
109
+ st.write(text)
110
+ probability, sentiment = eval_pred(text, return_all=True)
111
+ options, lime = critical_words(st.session_state.input,options=True)
112
+ nat_lang_explanation = construct_nlexp(text,sentiment,probability)
113
+ st.write(" ")
114
+ st.altair_chart(lime_viz(lime))
115
+
116
+ if layout == 'MultiNLC':
117
+ with col1:
118
+ #Use spaCy to make the sentence into a doc so we can do NLP.
119
+ doc = nlp(st.session_state.input)
120
+ #Evaluate the provided sentence for sentiment and probability.
121
+ if st.session_state.input != "":
122
+ st.caption("This is the sentence you provided.")
123
+ st.write(text)
124
+ probability, sentiment = eval_pred(text, return_all=True)
125
+ options, lime = critical_words(st.session_state.input,options=True)
126
+ nat_lang_explanation = construct_nlexp(text,sentiment,probability)
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.')
135
+ definition = get_def(option)
136
+ else:
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:
147
+ with st.sidebar:
148
+ ant("Generating alternatives for",(option,"opt","#E0FBFB"), "with a definition of: ",(st.session_state.definition,"def","#E0FBFB"),".")
149
+ cf_df = cf_from_wordnet_df(option,text,seed_definition=st.session_state.definition)
150
+ st.success('Alternatives created.')
151
+
152
+ if len(cf_df) != 0:
153
+ if alt_choice == "Similarity":
154
+ text2, text3 = get_min_max(cf_df, option)
155
+ col2.caption(f"This sentence is 'similar' to {option}.")
156
+ col3.caption(f"This sentence is 'not similar' to {option}.")
157
+ elif alt_choice == "Sampling (Random)":
158
+ text2, text3 = sampled_alts(cf_df, option)
159
+ col2.caption(f"This sentence is a random sample from the alternatives.")
160
+ col3.caption(f"This sentence is a random sample from the alternatives.")
161
+ elif alt_choice == "Sampling (Fixed)":
162
+ text2, text3 = sampled_alts(cf_df, option, fixed=True)
163
+ col2.caption(f"This sentence is a fixed sample of the alternatives.")
164
+ col3.caption(f"This sentence is a fixed sample of the alternatives.")
165
+ elif alt_choice == "Probability":
166
+ text2, text3 = abs_dif(cf_df, option)
167
+ col2.caption(f"This sentence is the closest prediction in the model.")
168
+ col3.caption(f"This sentence is the farthest prediction in the model.")
169
+ with st.sidebar:
170
+ st.info(f"Alternatives generated: {len(cf_df)}")
171
+
172
+ with col2:
173
+ if text2 != "":
174
+ sim2 = cf_df.loc[cf_df['text'] == text2, 'similarity'].iloc[0]
175
+ st.write(text2)
176
+ probability2, sentiment2 = eval_pred(text2, return_all=True)
177
+ nat_lang_explanation = construct_nlexp(text2,sentiment2,probability2)
178
+ #st.info(f" Similarity Score: {np.round(sim2, 2)}, Num Checked: {len(cf_df)}") #for QA purposes
179
+
180
+ with col3:
181
+ if text3 != "":
182
+ sim3 = cf_df.loc[cf_df['text'] == text3, 'similarity'].iloc[0]
183
+ st.write(text3)
184
+ probability3, sentiment3 = eval_pred(text3, return_all=True)
185
+ nat_lang_explanation = construct_nlexp(text3,sentiment3,probability3)
186
+ #st.info(f"Similarity Score: {np.round(sim3, 2)}, Num Checked: {len(cf_df)}") #for QA purposes
187
+
188
+ if layout == 'MultiNLC + Lime':
189
+ with col1:
190
+
191
+ #Use spaCy to make the sentence into a doc so we can do NLP.
192
+ doc = nlp(st.session_state.input)
193
+ #Evaluate the provided sentence for sentiment and probability.
194
+ if st.session_state.input != "":
195
+ st.caption("This is the sentence you provided.")
196
+ st.write(text)
197
+ probability, sentiment = eval_pred(text, return_all=True)
198
+ options, lime = critical_words(st.session_state.input,options=True)
199
+ nat_lang_explanation = construct_nlexp(text,sentiment,probability)
200
+ st.write(" ")
201
+ st.altair_chart(lime_viz(lime))
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.')
210
+ definition = get_def(option)
211
+ else:
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:
222
+ with st.sidebar:
223
+ ant("Generating alternatives for",(option,"opt","#E0FBFB"), "with a definition of: ",(st.session_state.definition,"def","#E0FBFB"),".")
224
+ cf_df = cf_from_wordnet_df(option,text,seed_definition=st.session_state.definition)
225
+ st.success('Alternatives created.')
226
+
227
+ if len(cf_df) != 0:
228
+ if alt_choice == "Similarity":
229
+ text2, text3 = get_min_max(cf_df, option)
230
+ col2.caption(f"This sentence is 'similar' to {option}.")
231
+ col3.caption(f"This sentence is 'not similar' to {option}.")
232
+ elif alt_choice == "Sampling (Random)":
233
+ text2, text3 = sampled_alts(cf_df, option)
234
+ col2.caption(f"This sentence is a random sample from the alternatives.")
235
+ col3.caption(f"This sentence is a random sample from the alternatives.")
236
+ elif alt_choice == "Sampling (Fixed)":
237
+ text2, text3 = sampled_alts(cf_df, option, fixed=True)
238
+ col2.caption(f"This sentence is a fixed sample of the alternatives.")
239
+ col3.caption(f"This sentence is a fixed sample of the alternatives.")
240
+ elif alt_choice == "Probability":
241
+ text2, text3 = abs_dif(cf_df, option)
242
+ col2.caption(f"This sentence is the closest prediction in the model.")
243
+ col3.caption(f"This sentence is the farthest prediction in the model.")
244
+ with st.sidebar:
245
+ st.info(f"Alternatives generated: {len(cf_df)}")
246
+
247
+ with col2:
248
+ if text2 != "":
249
+ sim2 = cf_df.loc[cf_df['text'] == text2, 'similarity'].iloc[0]
250
+ st.write(text2)
251
+ probability2, sentiment2 = eval_pred(text2, return_all=True)
252
+ nat_lang_explanation = construct_nlexp(text2,sentiment2,probability2)
253
+ exp2 = explainer.explain_instance(text2, predictor, num_features=15, num_samples=2000)
254
+ lime_results2 = exp2.as_list()
255
+ st.write(" ")
256
+ st.altair_chart(lime_viz(lime_results2))
257
+
258
+ with col3:
259
+ if text3 != "":
260
+ sim3 = cf_df.loc[cf_df['text'] == text3, 'similarity'].iloc[0]
261
+ st.write(text3)
262
+ probability3, sentiment3 = eval_pred(text3, return_all=True)
263
+ nat_lang_explanation = construct_nlexp(text3,sentiment3,probability3)
264
+ exp3 = explainer.explain_instance(text3, predictor, num_features=15, num_samples=2000)
265
+ lime_results3 = exp3.as_list()
266
+ st.write(" ")
267
+ st.altair_chart(lime_viz(lime_results3))
268
+
269
+ if layout == 'VizNLC':
270
+ with col1:
271
+
272
+ #Use spaCy to make the sentence into a doc so we can do NLP.
273
+ doc = nlp(st.session_state.input)
274
+ #Evaluate the provided sentence for sentiment and probability.
275
+ if st.session_state.input != "":
276
+ st.caption("This is the sentence you provided.")
277
+ st.write(text)
278
+ probability, sentiment = eval_pred(text, return_all=True)
279
+ options, lime = critical_words(st.session_state.input,options=True)
280
+ nat_lang_explanation = construct_nlexp(text,sentiment,probability)
281
+ st.write(" ")
282
+ st.altair_chart(lime_viz(lime))
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.')
291
+ definition = get_def(option)
292
+ else:
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:
303
+ with st.sidebar:
304
+ ant("Generating alternatives for",(option,"opt","#E0FBFB"), "with a definition of: ",(st.session_state.definition,"def","#E0FBFB"),".")
305
+ cf_df = cf_from_wordnet_df(option,text,seed_definition=st.session_state.definition)
306
+ st.success('Alternatives created.')
307
+
308
+ if len(cf_df) != 0:
309
+ if alt_choice == "Similarity":
310
+ text2, text3 = get_min_max(cf_df, option)
311
+ col2.caption(f"This sentence is 'similar' to {option}.")
312
+ col3.caption(f"This sentence is 'not similar' to {option}.")
313
+ elif alt_choice == "Sampling (Random)":
314
+ text2, text3 = sampled_alts(cf_df, option)
315
+ col2.caption(f"This sentence is a random sample from the alternatives.")
316
+ col3.caption(f"This sentence is a random sample from the alternatives.")
317
+ elif alt_choice == "Sampling (Fixed)":
318
+ text2, text3 = sampled_alts(cf_df, option, fixed=True)
319
+ col2.caption(f"This sentence is a fixed sample of the alternatives.")
320
+ col3.caption(f"This sentence is a fixed sample of the alternatives.")
321
+ elif alt_choice == "Probability":
322
+ text2, text3 = abs_dif(cf_df, option)
323
+ col2.caption(f"This sentence is the closest prediction in the model.")
324
+ col3.caption(f"This graph represents the {len(cf_df)} alternatives to {option}.")
325
+ with st.sidebar:
326
+ st.info(f"Alternatives generated: {len(cf_df)}")
327
+
328
+ with col2:
329
+ if text2 != "":
330
+ sim2 = cf_df.loc[cf_df['text'] == text2, 'similarity'].iloc[0]
331
+ st.write(text2)
332
+ probability2, sentiment2 = eval_pred(text2, return_all=True)
333
+ nat_lang_explanation = construct_nlexp(text2,sentiment2,probability2)
334
+ exp2 = explainer.explain_instance(text2, predictor, num_features=15, num_samples=2000)
335
+ lime_results2 = exp2.as_list()
336
+ st.write(" ")
337
+ st.altair_chart(lime_viz(lime_results2))
338
+
339
+ with col3:
340
+ if not cf_df.empty:
341
+ single_nearest = alt.selection_single(on='mouseover', nearest=True)
342
+ full = alt.Chart(cf_df).encode(
343
+ alt.X('similarity:Q', scale=alt.Scale(zero=False)),
344
+ alt.Y('pred:Q'),
345
+ color=alt.Color('Categories:N', legend=alt.Legend(title="Color of Categories")),
346
+ size=alt.Size('seed:O'),
347
+ tooltip=('Categories','text','pred')
348
+ ).mark_circle(opacity=.5).properties(width=450, height=450).add_selection(single_nearest)
349
+ st.altair_chart(full)