Spaces:
Sleeping
Sleeping
deleted commented out code
Browse files- pages/2_Context-based_chatbot.py +17 -129
pages/2_Context-based_chatbot.py
CHANGED
|
@@ -31,49 +31,16 @@ def remove_punctuation(word):
|
|
| 31 |
|
| 32 |
return word_without_punctuation
|
| 33 |
|
| 34 |
-
def return_top_k_context(sentence
|
| 35 |
|
| 36 |
if sentence[-1] != ".":
|
| 37 |
sentence = sentence + "."
|
| 38 |
-
|
| 39 |
-
# if rels:
|
| 40 |
-
# inputs = [f"Description : It is related to '{word}' but not '{word}'. Word : "]
|
| 41 |
-
# else:
|
| 42 |
-
# inputs = [f"Description : {sentence} Word : "]
|
| 43 |
|
| 44 |
output = model_context(sentence)
|
| 45 |
output = [output[i]['token_str'].strip() for i in range(len(output))]
|
| 46 |
return output
|
| 47 |
|
| 48 |
|
| 49 |
-
# JS
|
| 50 |
-
# def get_related_words(word, num=5):
|
| 51 |
-
# model.eval()
|
| 52 |
-
# with torch.no_grad():
|
| 53 |
-
# sentence = [f"Descripton : It is related to {word} but not {word}. Word : "]
|
| 54 |
-
# #inputs = ["Description: It is something to cut stuff with. Word: "]
|
| 55 |
-
# print(sentence)
|
| 56 |
-
# inputs = tokenizer(sentence, padding=True, truncation=True, return_tensors="pt",)
|
| 57 |
-
|
| 58 |
-
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 59 |
-
# model.to(device)
|
| 60 |
-
|
| 61 |
-
# batch = {k: v.to(device) for k, v in inputs.items()}
|
| 62 |
-
# beam_outputs = model.generate(
|
| 63 |
-
# input_ids=batch['input_ids'], max_new_tokens=10, num_beams=num+2, num_return_sequences=num+2, early_stopping=True
|
| 64 |
-
# )
|
| 65 |
-
|
| 66 |
-
# #beam_preds = [tokenizer.decode(beam_output.detach().cpu().numpy(), skip_special_tokens=True) for beam_output in beam_outputs if ]
|
| 67 |
-
# beam_preds = []
|
| 68 |
-
# for beam_output in beam_outputs:
|
| 69 |
-
# prediction = tokenizer.decode(beam_output.detach().cpu().numpy(), skip_special_tokens=True).strip()
|
| 70 |
-
# if prediction not in " ".join(sentence):
|
| 71 |
-
# beam_preds.append(prediction)
|
| 72 |
-
|
| 73 |
-
# return ", ".join(beam_preds[:num])
|
| 74 |
-
|
| 75 |
-
#if 'messages_context' not in st.session_state:
|
| 76 |
-
|
| 77 |
def get_text():
|
| 78 |
input_text = st.chat_input()
|
| 79 |
return input_text
|
|
@@ -112,22 +79,6 @@ def ask_if_helped_context():
|
|
| 112 |
write_bot("Please give a sentence using a <mask> instead of the word you have in mind!")
|
| 113 |
st.session_state.is_helpful_context['ask'] = False
|
| 114 |
|
| 115 |
-
## removed: if st.session_state.actions_context[-1] == "result":
|
| 116 |
-
|
| 117 |
-
# JS
|
| 118 |
-
# def get_related_words_llama(relation, target, device, num=5):
|
| 119 |
-
# prompt_context = f"Provide {num} {relation}s for the word '{target}'. Your answer consists of these {num} words only. Do not include the word '{target}' itself in your answer"
|
| 120 |
-
|
| 121 |
-
# inputs = tokenizer([prompt_context], return_tensors='pt').to(device)
|
| 122 |
-
# output = model.generate(
|
| 123 |
-
# **inputs, max_new_tokens=40, temperature=.75, early_stopping=True,
|
| 124 |
-
# )
|
| 125 |
-
# chatbot_response = tokenizer.decode(output[:, inputs['input_ids'].shape[-1]:][0], skip_special_tokens=True).strip()
|
| 126 |
-
|
| 127 |
-
# postproc = [word for word in word_tokenize(chatbot_response) if len(word)>=3]
|
| 128 |
-
|
| 129 |
-
# return postproc[-num:] if len(postproc)>=num else postproc
|
| 130 |
-
|
| 131 |
|
| 132 |
def postproc_wn(related_words, syns=False):
|
| 133 |
if syns:
|
|
@@ -137,43 +88,7 @@ def postproc_wn(related_words, syns=False):
|
|
| 137 |
related_words = [word.replace("_", " ") for word in related_words]
|
| 138 |
|
| 139 |
return related_words
|
| 140 |
-
|
| 141 |
-
# JS
|
| 142 |
-
def get_available_cues(target):
|
| 143 |
-
wn_nouns = [word.name() for word in wn.all_synsets(pos='n')]
|
| 144 |
-
wn_nouns = [word.split('.')[0] if word[0] != "." else word.split('.')[1] for word in wn_nouns]
|
| 145 |
-
|
| 146 |
-
if target in wn_nouns:
|
| 147 |
-
available_cues = {}
|
| 148 |
-
synset_target = wn.synsets(target, pos=wn.NOUN)[0]
|
| 149 |
-
|
| 150 |
-
#if wn.synonyms(target)[0]:
|
| 151 |
-
# available_cues['Synonyms'] = postproc_wn(wn.synonyms(target)[0], syns=True)
|
| 152 |
-
|
| 153 |
-
#if synset_target.hypernyms():
|
| 154 |
-
# available_cues['Hypernyms'] = postproc_wn(synset_target.hypernyms())
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
#if synset_target.hyponyms():
|
| 158 |
-
# available_cues['Hyponyms'] = postproc_wn(synset_target.hyponyms())
|
| 159 |
-
|
| 160 |
-
if synset_target.examples():
|
| 161 |
-
examples = []
|
| 162 |
-
|
| 163 |
-
for example in synset_target.examples():
|
| 164 |
-
examples.append(example.replace(target, "..."))
|
| 165 |
-
|
| 166 |
-
available_cues['Examples'] = examples
|
| 167 |
-
|
| 168 |
-
return available_cues
|
| 169 |
-
|
| 170 |
-
else:
|
| 171 |
-
return None
|
| 172 |
-
|
| 173 |
-
# JS: moved the cue generation further down
|
| 174 |
-
#def cue_generation():
|
| 175 |
-
# if st.session_state.actions_context[-1] == 'cue':
|
| 176 |
-
|
| 177 |
if 'messages_context' not in st.session_state:
|
| 178 |
st.session_state.messages_context = []
|
| 179 |
|
|
@@ -194,7 +109,6 @@ if 'descriptions_context' not in st.session_state:
|
|
| 194 |
|
| 195 |
st.title("You name it! 🗣")
|
| 196 |
|
| 197 |
-
# JS: would remove Simon by some neutral avatar
|
| 198 |
with st.chat_message('user', avatar='julian.jpg'):
|
| 199 |
st.write("Hey assistant!")
|
| 200 |
|
|
@@ -213,28 +127,26 @@ for message in st.session_state.messages_context:
|
|
| 213 |
#display user message in chat message container
|
| 214 |
prompt_context = get_text()
|
| 215 |
if prompt_context:
|
| 216 |
-
#JS: would replace Simon by some neutral character
|
| 217 |
with st.chat_message('user', avatar="julian.jpg"):
|
| 218 |
st.markdown(prompt_context)
|
| 219 |
#add to history
|
| 220 |
st.session_state.messages_context.append({'role': 'user', 'content': prompt_context})
|
| 221 |
#TODO: replace it with zero-shot classifier
|
| 222 |
-
yes = ['yes', 'again', '
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
if st.session_state.actions_context[-1] == "result":
|
| 239 |
col1, col2, col3, col4, col5 = st.columns(5)
|
| 240 |
with col1:
|
|
@@ -273,11 +185,6 @@ if st.session_state.actions_context[-1] == 'cue':
|
|
| 273 |
with col4:
|
| 274 |
b4 = st.button("All words", key="3")
|
| 275 |
|
| 276 |
-
# JS
|
| 277 |
-
#if get_available_cues(target):
|
| 278 |
-
# avail_cues = get_available_cues(target)
|
| 279 |
-
#cues_buttons = {cue_type: st.button(cue_type) for cue_type in avail_cues}
|
| 280 |
-
|
| 281 |
b5 = st.button("I remembered the word!", key="4", type='primary')
|
| 282 |
b6 = st.button("Exit", key="5", type='primary')
|
| 283 |
new = st.button('Play again', key=64, type='primary')
|
|
@@ -288,21 +195,17 @@ if st.session_state.actions_context[-1] == 'cue':
|
|
| 288 |
|
| 289 |
if b1:
|
| 290 |
st.session_state.counter_context["letter_count"] += 1
|
| 291 |
-
#word_count = st.session_state.counter_context["word_count"]
|
| 292 |
letter_count = st.session_state.counter_context["letter_count"]
|
| 293 |
if letter_count < len(target):
|
| 294 |
write_bot(f'The word starts with {st.session_state.results_context["results_context"][word_count][:letter_count]}.', remember=False)
|
| 295 |
-
#ask_if_helped_context()
|
| 296 |
st.session_state.is_helpful_context['ask'] = True
|
| 297 |
else:
|
| 298 |
write_bot(f'This is my predicted word: "{target}". Does this match your query?')
|
| 299 |
-
#ask_if_helped_context()
|
| 300 |
st.session_state.is_helpful_context['ask'] = True
|
| 301 |
|
| 302 |
elif b2:
|
| 303 |
rels = return_top_k_context(st.session_state.descriptions_context[-1], word=target, rels=True)
|
| 304 |
write_bot(f'Here are words that are related to your word: {", ".join(rels)}.', remember=False)
|
| 305 |
-
#ask_if_helped_context()
|
| 306 |
st.session_state.is_helpful_context['ask'] = True
|
| 307 |
|
| 308 |
elif b3:
|
|
@@ -310,28 +213,13 @@ if st.session_state.actions_context[-1] == 'cue':
|
|
| 310 |
letter_count = st.session_state.counter_context["letter_count"]
|
| 311 |
st.session_state.counter_context["word_count"] += 1
|
| 312 |
word_count = st.session_state.counter_context["word_count"]
|
| 313 |
-
#write_bot(f'The next word starts with {st.session_state.results_context["results_context"][word_count][:letter_count]}', remember=False)
|
| 314 |
if letter_count < len(target):
|
| 315 |
write_bot(f'The next word starts with {st.session_state.results_context["results_context"][word_count][:letter_count]}.', remember=False)
|
| 316 |
-
#ask_if_helped_context()
|
| 317 |
st.session_state.is_helpful_context['ask'] = True
|
| 318 |
else:
|
| 319 |
write_bot(f'This is my predicted word: "{target}". Does this match your query?')
|
| 320 |
-
#ask_if_helped_context()
|
| 321 |
st.session_state.is_helpful_context['ask'] = True
|
| 322 |
|
| 323 |
-
#elif get_available_cues(target) and "Synonyms" in cues_buttons and cues_buttons['Synonyms']:
|
| 324 |
-
#write_bot(f'Here are synonyms for the current word: {", ".join(avail_cues["Synonyms"])}', remember=False)
|
| 325 |
-
|
| 326 |
-
#elif get_available_cues(target) and "Hypernyms" in cues_buttons and cues_buttons['Hypernyms']:
|
| 327 |
-
#write_bot(f'Here are hypernyms for the current word: {", ".join(avail_cues["Hypernyms"])}', remember=False)
|
| 328 |
-
|
| 329 |
-
#elif get_available_cues(target) and "Hyponyms" in cues_buttons and cues_buttons['Hyponyms']:
|
| 330 |
-
#write_bot(f'Here are hyponyms for the current word: {", ".join(avail_cues["Hyponyms"])}', remember=False)
|
| 331 |
-
|
| 332 |
-
#elif get_available_cues(target) and "Examples" in cues_buttons and cues_buttons['Examples']:
|
| 333 |
-
#write_bot(f'Here are example contexts for the current word: {", ".join(avail_cues["Examples"])}', remember=False)
|
| 334 |
-
|
| 335 |
elif b4:
|
| 336 |
write_bot(f"Here are all my guesses about your word: {st.session_state.results_context['results_context_print']}")
|
| 337 |
st.session_state.is_helpful_context['ask'] = True
|
|
|
|
| 31 |
|
| 32 |
return word_without_punctuation
|
| 33 |
|
| 34 |
+
def return_top_k_context(sentence):
|
| 35 |
|
| 36 |
if sentence[-1] != ".":
|
| 37 |
sentence = sentence + "."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
output = model_context(sentence)
|
| 40 |
output = [output[i]['token_str'].strip() for i in range(len(output))]
|
| 41 |
return output
|
| 42 |
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def get_text():
|
| 45 |
input_text = st.chat_input()
|
| 46 |
return input_text
|
|
|
|
| 79 |
write_bot("Please give a sentence using a <mask> instead of the word you have in mind!")
|
| 80 |
st.session_state.is_helpful_context['ask'] = False
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
def postproc_wn(related_words, syns=False):
|
| 84 |
if syns:
|
|
|
|
| 88 |
related_words = [word.replace("_", " ") for word in related_words]
|
| 89 |
|
| 90 |
return related_words
|
| 91 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
if 'messages_context' not in st.session_state:
|
| 93 |
st.session_state.messages_context = []
|
| 94 |
|
|
|
|
| 109 |
|
| 110 |
st.title("You name it! 🗣")
|
| 111 |
|
|
|
|
| 112 |
with st.chat_message('user', avatar='julian.jpg'):
|
| 113 |
st.write("Hey assistant!")
|
| 114 |
|
|
|
|
| 127 |
#display user message in chat message container
|
| 128 |
prompt_context = get_text()
|
| 129 |
if prompt_context:
|
|
|
|
| 130 |
with st.chat_message('user', avatar="julian.jpg"):
|
| 131 |
st.markdown(prompt_context)
|
| 132 |
#add to history
|
| 133 |
st.session_state.messages_context.append({'role': 'user', 'content': prompt_context})
|
| 134 |
#TODO: replace it with zero-shot classifier
|
| 135 |
+
yes = ['yes', 'again', 'sure', 'new word', 'yes!', 'yep', 'yeah']
|
| 136 |
+
try:
|
| 137 |
+
if prompt_context.lower() in yes:
|
| 138 |
+
write_bot("Please give a sentence using a <mask> instead of the word you have in mind!")
|
| 139 |
+
#if previously we asked to give a prompt_context
|
| 140 |
+
elif (st.session_state.messages_context[-2]['content'] == "Please give a sentence using a <mask> instead of the word you have in mind!") & (st.session_state.messages_context[-1]['content'] != "no"):
|
| 141 |
+
write_bot("Great! Let me think what it could be...")
|
| 142 |
+
st.session_state.descriptions_context.append(prompt_context)
|
| 143 |
+
st.session_state.results_context['results_context'] = return_top_k_context(st.session_state.descriptions_context[-1])
|
| 144 |
+
st.session_state.results_context['results_context_print'] = dict(zip(range(1, len(st.session_state.results_context['results_context'])+1), st.session_state.results_context['results_context']))
|
| 145 |
+
write_bot("I think I have some ideas. Do you want to see my guesses or do you want a cue?")
|
| 146 |
+
st.session_state.actions_context.append("result")
|
| 147 |
+
except:
|
| 148 |
+
write_bot("Sorry, I didn't understand you... I am still learning :sob: For now, could you respond with 'yes' or 'no'? ")
|
| 149 |
+
|
|
|
|
| 150 |
if st.session_state.actions_context[-1] == "result":
|
| 151 |
col1, col2, col3, col4, col5 = st.columns(5)
|
| 152 |
with col1:
|
|
|
|
| 185 |
with col4:
|
| 186 |
b4 = st.button("All words", key="3")
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
b5 = st.button("I remembered the word!", key="4", type='primary')
|
| 189 |
b6 = st.button("Exit", key="5", type='primary')
|
| 190 |
new = st.button('Play again', key=64, type='primary')
|
|
|
|
| 195 |
|
| 196 |
if b1:
|
| 197 |
st.session_state.counter_context["letter_count"] += 1
|
|
|
|
| 198 |
letter_count = st.session_state.counter_context["letter_count"]
|
| 199 |
if letter_count < len(target):
|
| 200 |
write_bot(f'The word starts with {st.session_state.results_context["results_context"][word_count][:letter_count]}.', remember=False)
|
|
|
|
| 201 |
st.session_state.is_helpful_context['ask'] = True
|
| 202 |
else:
|
| 203 |
write_bot(f'This is my predicted word: "{target}". Does this match your query?')
|
|
|
|
| 204 |
st.session_state.is_helpful_context['ask'] = True
|
| 205 |
|
| 206 |
elif b2:
|
| 207 |
rels = return_top_k_context(st.session_state.descriptions_context[-1], word=target, rels=True)
|
| 208 |
write_bot(f'Here are words that are related to your word: {", ".join(rels)}.', remember=False)
|
|
|
|
| 209 |
st.session_state.is_helpful_context['ask'] = True
|
| 210 |
|
| 211 |
elif b3:
|
|
|
|
| 213 |
letter_count = st.session_state.counter_context["letter_count"]
|
| 214 |
st.session_state.counter_context["word_count"] += 1
|
| 215 |
word_count = st.session_state.counter_context["word_count"]
|
|
|
|
| 216 |
if letter_count < len(target):
|
| 217 |
write_bot(f'The next word starts with {st.session_state.results_context["results_context"][word_count][:letter_count]}.', remember=False)
|
|
|
|
| 218 |
st.session_state.is_helpful_context['ask'] = True
|
| 219 |
else:
|
| 220 |
write_bot(f'This is my predicted word: "{target}". Does this match your query?')
|
|
|
|
| 221 |
st.session_state.is_helpful_context['ask'] = True
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
elif b4:
|
| 224 |
write_bot(f"Here are all my guesses about your word: {st.session_state.results_context['results_context_print']}")
|
| 225 |
st.session_state.is_helpful_context['ask'] = True
|