Spaces:
Sleeping
Sleeping
Bug fix
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import streamlit as st
|
|
2 |
import torch
|
3 |
from tqdm import tqdm
|
4 |
from peft import PeftModel, PeftConfig
|
5 |
-
from transformers import AutoModelForSeq2SeqLM
|
6 |
from transformers import AutoTokenizer
|
7 |
import numpy as np
|
8 |
import time
|
@@ -11,9 +11,10 @@ import time
|
|
11 |
import nltk
|
12 |
nltk.download('wordnet')
|
13 |
from nltk.corpus import wordnet as wn
|
|
|
14 |
|
15 |
@st.cache_resource
|
16 |
-
def get_models():
|
17 |
st.write('Loading the model...')
|
18 |
config = PeftConfig.from_pretrained("NursNurs/T5ForReverseDictionary")
|
19 |
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large")
|
@@ -21,8 +22,16 @@ def get_models():
|
|
21 |
|
22 |
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
st.write("The assistant is loaded and ready to use!")
|
25 |
-
|
|
|
26 |
|
27 |
model, tokenizer = get_models()
|
28 |
|
@@ -124,6 +133,7 @@ def ask_if_helped():
|
|
124 |
write_bot("I am happy to help!")
|
125 |
elif n:
|
126 |
st.session_state.actions.append('cue')
|
|
|
127 |
#cue_generation()
|
128 |
elif new:
|
129 |
write_bot("Please describe your word!")
|
@@ -132,6 +142,20 @@ def ask_if_helped():
|
|
132 |
## removed: if st.session_state.actions[-1] == "result":
|
133 |
|
134 |
# JS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
def postproc_wn(related_words, syns=False):
|
136 |
if syns:
|
137 |
related_words = [word.split('.')[0] if word[0] != "." else word.split('.')[1] for word in related_words]
|
@@ -174,73 +198,9 @@ def get_available_cues(target):
|
|
174 |
else:
|
175 |
return None
|
176 |
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
write_bot('What do you want to see?', remember=False, blink=False)
|
181 |
-
|
182 |
-
while guessed == False:
|
183 |
-
b1 = st.button("Next letter", key="1")
|
184 |
-
b2 = st.button("Next word", key="2")
|
185 |
-
|
186 |
-
# JS
|
187 |
-
word_count = st.session_state.counters["word_count"]
|
188 |
-
target = st.session_state.results["results"][word_count]
|
189 |
-
if get_available_cues(target):
|
190 |
-
avail_cues = get_available_cues(target)
|
191 |
-
cues_buttons = {}
|
192 |
-
for cue_type, cues in avail_cues.items():
|
193 |
-
#st.button(cue_type)
|
194 |
-
cues_buttons[cue_type] = st.button(cue_type)
|
195 |
-
|
196 |
-
b3 = st.button("All words", key="3")
|
197 |
-
b4 = st.button("I remembered the word!", key="4", type='primary')
|
198 |
-
b5 = st.button("Exit", key="5", type='primary')
|
199 |
-
|
200 |
-
if b1:
|
201 |
-
st.session_state.counters["letter_count"] += 1
|
202 |
-
word_count = st.session_state.counters["word_count"]
|
203 |
-
letter_count = st.session_state.counters["letter_count"]
|
204 |
-
write_bot(f'The word starts with {st.session_state.results["results"][word_count][:letter_count]}', remember=False)
|
205 |
-
|
206 |
-
elif b2:
|
207 |
-
st.session_state.counters["letter_count"] = 1
|
208 |
-
letter_count = st.session_state.counters["letter_count"]
|
209 |
-
st.session_state.counters["word_count"] += 1
|
210 |
-
word_count = st.session_state.counters["word_count"]
|
211 |
-
write_bot(f'The next word starts with {st.session_state.results["results"][word_count][:letter_count]}', remember=False)
|
212 |
-
|
213 |
-
elif "Synonyms" in cues_buttons and cues_buttons['Synonyms']:
|
214 |
-
write_bot(f'Here are synonyms for the current word: {", ".join(avail_cues["Synonyms"])}', remember=False)
|
215 |
-
|
216 |
-
elif "Hypernyms" in cues_buttons and cues_buttons['Hypernyms']:
|
217 |
-
write_bot(f'Here are hypernyms for the current word: {", ".join(avail_cues["Hypernyms"])}', remember=False)
|
218 |
-
|
219 |
-
elif "Hyponyms" in cues_buttons and cues_buttons['Hyponyms']:
|
220 |
-
write_bot(f'Here are hyponyms for the current word: {", ".join(avail_cues["Hyponyms"])}', remember=False)
|
221 |
-
|
222 |
-
elif "Examples" in cues_buttons and cues_buttons['Examples']:
|
223 |
-
write_bot(f'Here are example contexts for the current word: {", ".join(avail_cues["Examples"])}', remember=False)
|
224 |
-
|
225 |
-
elif b3:
|
226 |
-
write_bot(f"Here are all my guesses about your word: {st.session_state.results['results_print']}")
|
227 |
-
|
228 |
-
elif b4:
|
229 |
-
write_bot("Yay! I am happy I could be of help!")
|
230 |
-
new = st.button('Play again', key=63)
|
231 |
-
if new:
|
232 |
-
write_bot("Please describe your word!")
|
233 |
-
guessed = True
|
234 |
-
|
235 |
-
break
|
236 |
-
|
237 |
-
elif b5:
|
238 |
-
write_bot("I am sorry I couldn't help you this time. See you soon!")
|
239 |
-
st.session_state.actions.append('cue')
|
240 |
-
new = st.button('Play again', key=64)
|
241 |
-
if new:
|
242 |
-
write_bot("Please describe your word!")
|
243 |
-
break
|
244 |
|
245 |
#display user message in chat message container
|
246 |
prompt = get_text()
|
@@ -320,16 +280,16 @@ if st.session_state.actions[-1] == 'cue':
|
|
320 |
word_count = st.session_state.counters["word_count"]
|
321 |
write_bot(f'The next word starts with {st.session_state.results["results"][word_count][:letter_count]}', remember=False)
|
322 |
|
323 |
-
elif "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 "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 "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 "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 b3:
|
|
|
2 |
import torch
|
3 |
from tqdm import tqdm
|
4 |
from peft import PeftModel, PeftConfig
|
5 |
+
from transformers import AutoModelForSeq2SeqLM, AutoModelForCausalLM
|
6 |
from transformers import AutoTokenizer
|
7 |
import numpy as np
|
8 |
import time
|
|
|
11 |
import nltk
|
12 |
nltk.download('wordnet')
|
13 |
from nltk.corpus import wordnet as wn
|
14 |
+
from nltk.tokenize import word_tokenize
|
15 |
|
16 |
@st.cache_resource
|
17 |
+
def get_models(llama=False):
|
18 |
st.write('Loading the model...')
|
19 |
config = PeftConfig.from_pretrained("NursNurs/T5ForReverseDictionary")
|
20 |
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large")
|
|
|
22 |
|
23 |
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
|
24 |
|
25 |
+
# JS
|
26 |
+
if llama:
|
27 |
+
model_name = 'meta-llama/Llama-2-7b-chat-hf'
|
28 |
+
access_token = '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'
|
29 |
+
llama_tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=access_token, use_fast=True)#, use_fast=True)
|
30 |
+
llama_model = AutoModelForCausalLM.from_pretrained(model_name, use_auth_token=access_token, device_map={'':0})#, load_in_4bit=True)
|
31 |
+
|
32 |
st.write("The assistant is loaded and ready to use!")
|
33 |
+
|
34 |
+
return model, tokenizer, llama_model, llama_tokenizer if llama else model, tokenizer
|
35 |
|
36 |
model, tokenizer = get_models()
|
37 |
|
|
|
133 |
write_bot("I am happy to help!")
|
134 |
elif n:
|
135 |
st.session_state.actions.append('cue')
|
136 |
+
st.session_state.is_helpful['ask'] = False
|
137 |
#cue_generation()
|
138 |
elif new:
|
139 |
write_bot("Please describe your word!")
|
|
|
142 |
## removed: if st.session_state.actions[-1] == "result":
|
143 |
|
144 |
# JS
|
145 |
+
def get_related_words_llama(relation, target, device, num=5):
|
146 |
+
prompt = 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"
|
147 |
+
|
148 |
+
inputs = tokenizer([prompt], return_tensors='pt').to(device)
|
149 |
+
output = model.generate(
|
150 |
+
**inputs, max_new_tokens=40, temperature=.75, early_stopping=True,
|
151 |
+
)
|
152 |
+
chatbot_response = tokenizer.decode(output[:, inputs['input_ids'].shape[-1]:][0], skip_special_tokens=True).strip()
|
153 |
+
|
154 |
+
postproc = [word for word in word_tokenize(chatbot_response) if len(word)>=3]
|
155 |
+
|
156 |
+
return postproc[-num:] if len(postproc)>=num else postproc
|
157 |
+
|
158 |
+
|
159 |
def postproc_wn(related_words, syns=False):
|
160 |
if syns:
|
161 |
related_words = [word.split('.')[0] if word[0] != "." else word.split('.')[1] for word in related_words]
|
|
|
198 |
else:
|
199 |
return None
|
200 |
|
201 |
+
# JS: moved the cue generation further down
|
202 |
+
#def cue_generation():
|
203 |
+
# if st.session_state.actions[-1] == 'cue':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
|
205 |
#display user message in chat message container
|
206 |
prompt = get_text()
|
|
|
280 |
word_count = st.session_state.counters["word_count"]
|
281 |
write_bot(f'The next word starts with {st.session_state.results["results"][word_count][:letter_count]}', remember=False)
|
282 |
|
283 |
+
elif get_available_cues(target) and "Synonyms" in cues_buttons and cues_buttons['Synonyms']:
|
284 |
write_bot(f'Here are synonyms for the current word: {", ".join(avail_cues["Synonyms"])}', remember=False)
|
285 |
|
286 |
+
elif get_available_cues(target) and "Hypernyms" in cues_buttons and cues_buttons['Hypernyms']:
|
287 |
write_bot(f'Here are hypernyms for the current word: {", ".join(avail_cues["Hypernyms"])}', remember=False)
|
288 |
|
289 |
+
elif get_available_cues(target) and "Hyponyms" in cues_buttons and cues_buttons['Hyponyms']:
|
290 |
write_bot(f'Here are hyponyms for the current word: {", ".join(avail_cues["Hyponyms"])}', remember=False)
|
291 |
|
292 |
+
elif get_available_cues(target) and "Examples" in cues_buttons and cues_buttons['Examples']:
|
293 |
write_bot(f'Here are example contexts for the current word: {", ".join(avail_cues["Examples"])}', remember=False)
|
294 |
|
295 |
elif b3:
|