NursNurs commited on
Commit
4b446a4
Β·
1 Parent(s): 1dd66ad

Upload 5 files

Browse files
Files changed (4) hide show
  1. README.md +2 -2
  2. app.py +384 -0
  3. requirements.txt +6 -0
  4. simon.jpg +0 -0
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: YouNameIt Chatbot
3
- emoji: πŸŒ–
4
  colorFrom: green
5
- colorTo: gray
6
  sdk: streamlit
7
  sdk_version: 1.27.0
8
  app_file: app.py
 
1
  ---
2
  title: YouNameIt Chatbot
3
+ emoji: πŸ“‰
4
  colorFrom: green
5
+ colorTo: yellow
6
  sdk: streamlit
7
  sdk_version: 1.27.0
8
  app_file: app.py
app.py ADDED
@@ -0,0 +1,384 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
9
+
10
+ @st.cache_resource
11
+ def get_models():
12
+ st.write('Loading the model...')
13
+ config = PeftConfig.from_pretrained("NursNurs/T5ForReverseDictionary")
14
+ model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large")
15
+ model = PeftModel.from_pretrained(model, "NursNurs/T5ForReverseDictionary")
16
+
17
+ tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
18
+
19
+ st.write("The assistant is loaded and ready to use!")
20
+ return model, tokenizer
21
+
22
+ model, tokenizer = get_models()
23
+
24
+ def return_top_k(sentence, k=10):
25
+
26
+ if sentence[-1] != ".":
27
+ sentence = sentence + "."
28
+
29
+ inputs = [f"Descripton : {sentence}. Word : "]
30
+
31
+ inputs = tokenizer(
32
+ inputs,
33
+ padding=True, truncation=True,
34
+ return_tensors="pt",
35
+ )
36
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
37
+ model.to(device)
38
+
39
+ with torch.no_grad():
40
+ inputs = {k: v.to(device) for k, v in inputs.items()}
41
+ output_sequences = model.generate(input_ids=inputs["input_ids"], max_new_tokens=10, num_beams=k+5, num_return_sequences=k+5, #max_length=3,
42
+ top_p = 50, output_scores=True, return_dict_in_generate=True) #repetition_penalty=10000.0
43
+
44
+ logits = output_sequences['sequences_scores'].clone().detach()
45
+ decoded_probabilities = torch.softmax(logits, dim=0)
46
+
47
+
48
+ #all word predictions
49
+ predictions = [tokenizer.decode(tokens, skip_special_tokens=True) for tokens in output_sequences['sequences']]
50
+ probabilities = [round(float(prob), 2) for prob in decoded_probabilities]
51
+
52
+ for pred in predictions:
53
+ if (len(pred) < 2) | (pred in sentence.split()):
54
+ predictions.pop(predictions.index(pred))
55
+
56
+ return predictions[:10]
57
+
58
+
59
+ if 'messages' not in st.session_state:
60
+ st.session_state.messages = []
61
+
62
+ if 'results' not in st.session_state:
63
+ st.session_state.results = {'results': False, 'results_print': False}
64
+
65
+ if 'actions' not in st.session_state:
66
+ st.session_state.actions = [""]
67
+
68
+ if 'counters' not in st.session_state:
69
+ st.session_state.counters = {"letter_count": 1, "word_count": 0}
70
+
71
+ if 'is_helpful' not in st.session_state:
72
+ st.session_state.is_helpful = {'ask':False}
73
+
74
+ if 'descriptions' not in st.session_state:
75
+ st.session_state.descriptions = []
76
+
77
+ st.title("You name it!")
78
+
79
+ with st.chat_message('user', avatar='simon.jpg'):
80
+ st.write("Hey assistant!")
81
+
82
+ bot = st.chat_message('assistant')
83
+ bot.write("Hello human! Wanna practice naming some words?")
84
+
85
+ #for showing history of messages
86
+ for message in st.session_state.messages:
87
+ if message['role'] == 'user':
88
+ with st.chat_message(message['role'], avatar='simon.jpg'):
89
+ st.markdown(message['content'])
90
+ else:
91
+ with st.chat_message(message['role']):
92
+ st.markdown(message['content'])
93
+
94
+ def get_text():
95
+ input_text = st.chat_input()
96
+ return input_text
97
+
98
+ def write_bot(input, remember=True, blink=True):
99
+ with st.chat_message('assistant'):
100
+ message_placeholder = st.empty()
101
+ full_response = input
102
+ if blink == True:
103
+ response = ''
104
+ for chunk in full_response.split():
105
+ response += chunk + " "
106
+ time.sleep(0.05)
107
+ # Add a blinking cursor to simulate typing
108
+ message_placeholder.markdown(response + "β–Œ")
109
+ time.sleep(0.5)
110
+ message_placeholder.markdown(full_response)
111
+ if remember == True:
112
+ st.session_state.messages.append({'role': 'assistant', 'content': full_response})
113
+
114
+ def ask_if_helped():
115
+ y = st.button('Yes!', key=60)
116
+ n = st.button('No...', key=61)
117
+ new = st.button('I have a new word', key=62)
118
+ if y:
119
+ write_bot("I am happy to help!")
120
+ elif n:
121
+ st.session_state.actions.append('cue')
122
+ elif new:
123
+ write_bot("Please describe your word!")
124
+ # st.session_state.is_helpful['ask'] = False
125
+
126
+ if st.session_state.actions[-1] == "result":
127
+ a1 = st.button('Results', key=10)
128
+ a2 = st.button('Cue', key=11)
129
+ if a1:
130
+ write_bot("Here are my guesses about your word:")
131
+ st.write(st.session_state.results['results_print'])
132
+ time.sleep(1)
133
+ write_bot('Does it help you remember the word?', remember=False)
134
+ st.session_state.is_helpful['ask'] = True
135
+ elif a2:
136
+ write_bot(f'The first letter is {st.session_state.results["results"][0][0]}.')
137
+ time.sleep(1)
138
+ write_bot('Does it help you remember the word?', remember=False)
139
+ st.session_state.is_helpful['ask'] = True
140
+
141
+ if st.session_state.is_helpful['ask'] == True:
142
+ ask_if_helped()
143
+
144
+
145
+ if st.session_state.actions[-1] == 'cue':
146
+ guessed = False
147
+ write_bot('What do you want to see?', remember=False, blink=False)
148
+ b1 = st.button("Next letter", key="1")
149
+ b2 = st.button("Next word", key="2")
150
+ b3 = st.button("All words", key="3")
151
+ b4 = st.button("I remembered the word!", key="4", type='primary')
152
+ b5 = st.button("Exit", key="5", type='primary')
153
+ while guessed == False:
154
+ if b1:
155
+ st.session_state.counters["letter_count"] += 1
156
+ word_count = st.session_state.counters["word_count"]
157
+ letter_count = st.session_state.counters["letter_count"]
158
+ write_bot(f'The word starts with {st.session_state.results["results"][word_count][:letter_count]}', remember=False)
159
+
160
+ elif b2:
161
+ st.session_state.counters["letter_count"] = 1
162
+ letter_count = st.session_state.counters["letter_count"]
163
+ st.session_state.counters["word_count"] += 1
164
+ word_count = st.session_state.counters["word_count"]
165
+ write_bot(f'The next word starts with {st.session_state.results["results"][word_count][:letter_count]}', remember=False)
166
+
167
+ elif b3:
168
+ write_bot(f"Here are all my guesses about your word: {st.session_state.results['results_print']}")
169
+
170
+ elif b4:
171
+ write_bot("Yay! I am happy I could be of help!")
172
+ new = st.button('Play again', key=63)
173
+ if new:
174
+ write_bot("Please describe your word!")
175
+ guessed = True
176
+
177
+ break
178
+
179
+ elif b5:
180
+ write_bot("I am sorry I couldn't help you this time. See you soon!")
181
+ st.session_state.actions.append('cue')
182
+ new = st.button('Play again', key=64)
183
+ if new:
184
+ write_bot("Please describe your word!")
185
+ break
186
+
187
+ #display user message in chat message container
188
+ prompt = get_text()
189
+ if prompt:
190
+ with st.chat_message('user', avatar='simon.jpg'):
191
+ st.markdown(prompt)
192
+ #add to history
193
+ st.session_state.messages.append({'role': 'user', 'content': prompt})
194
+ yes = ['yes', 'again', 'Yes', 'sure', 'new word', 'yes!', 'yep', 'yeah']
195
+ if prompt in yes:
196
+ write_bot("Please describe your word!")
197
+ elif prompt == 'It is similar to the best place on earth':
198
+ write_bot("Great! Let me think what it could be...")
199
+ time.sleep(3)
200
+ write_bot("Do you mean Saarland?")
201
+ #if previously we asked to give a prompt
202
+ elif (st.session_state.messages[-2]['content'] == "Please describe your word!") & (st.session_state.messages[-1]['content'] != "no"):
203
+ write_bot("Great! Let me think what it could be...")
204
+ st.session_state.descriptions.append(prompt)
205
+ st.session_state.results['results'] = return_top_k(st.session_state.descriptions[-1])
206
+ st.session_state.results['results_print'] = dict(zip(range(1, 11), st.session_state.results['results']))
207
+ write_bot("I think I have some ideas. Do you want to see my guesses or do you want a cue?")
208
+ st.session_state.actions.append("result")
209
+
210
+
211
+
212
+ # elif prompt == 'results':
213
+ # st.text("results")
214
+ # st.write("results")
215
+ # st.session_state.actions.append({'result': True})
216
+ # st.write(st.session_state.actions)
217
+ # with st.chat_message('user'):
218
+ # custom_response = "Results"
219
+ # st.markdown(custom_response)
220
+ # st.session_state.messages.append({'role': 'user', 'content': custom_response})
221
+
222
+ # with st.chat_message('assistant'):
223
+ # message_placeholder = st.empty()
224
+ # response = f"Here are my guesses about your word: {result_print}"
225
+ # message_placeholder.markdown(response + "|")
226
+ # st.session_state.messages.append({'role': 'assistant', 'content': response})
227
+ # elif st.button('Cue'):
228
+ # response = "Cue"
229
+ # with st.chat_message('user'):
230
+ # st.markdown(response)
231
+ # st.session_state.messages.append({'role': 'user', 'content': response})
232
+ # text = f'The first letter is {result[0][0]}.'
233
+ # bot.write(text)
234
+ # st.session_state.messages.append({'role': 'assistant', 'content': text})
235
+ # letter_count = 1
236
+ # word_count = 0
237
+ # elif prompt == 'Results':
238
+ # with st.chat_message('assistant'):
239
+ # message_placeholder = st.empty()
240
+ # response = f"Here are my guesses about your word: {result_print}"
241
+ # message_placeholder.markdown(response + "|")
242
+ # st.session_state.messages.append({'role': 'assistant', 'content': response})
243
+
244
+ # #if you don't wanna practice word naming
245
+ # else:
246
+ # with st.chat_message('assistant'):
247
+ # message_placeholder = st.empty()
248
+ # response = "See you next time!"
249
+ # message_placeholder.markdown(response + "|")
250
+ # st.session_state.messages.append({'role': 'assistant', 'content': response})
251
+
252
+
253
+
254
+ # if st.button('Results'):
255
+ # bot.write("Here are my guesses about your word:")
256
+ # bot.write(result_print)
257
+ # elif st.button('Cue'):
258
+ # bot.write(f'The first letter is {result[0][0]}.')
259
+ # letter_count = 1
260
+ # word_count = 0
261
+ # answer = st.chat_input('Does it help you remember the word? Type yes or no')
262
+ # if answer == "no":
263
+ # bot.write("What do you want to see?")
264
+ # if st.button('Next letter'):
265
+ # letter_count += 1
266
+ # bot.write(f'The word starts with {result[word_count][:letter_count]}')
267
+ # elif st.button('Next word'):
268
+ # letter_count = 1
269
+ # bot.write(f'The next word starts with {result[word_count][:letter_count]}')
270
+ # word_count += 1
271
+ # elif st.button('All words'):
272
+ # bot.write("Here are all my guesses about your word:")
273
+ # bot.write(result_print)
274
+ # bot.write("Does this help you remember your word?")
275
+ # answer = st.chat_input('Type yes/no/exit')
276
+ # if answer == 'Exit':
277
+ # st.write("I am sorry I couldn't help you. See you next time!")
278
+
279
+
280
+ #write down assistant's responses
281
+ #response = f'Echo: {prompt}' #echoes prompt
282
+ # with st.chat_message('assistant'):
283
+ # message_placeholder = st.empty()
284
+ # full_response = "yeee"
285
+ # #here insert the loop with the model answers (for response in...)
286
+ # #this to imitate a cursor
287
+ # message_placeholder.markdown(full_response + "|")
288
+
289
+ # #add to history
290
+ # st.session_state.messages.append({'role': 'assistant', 'content': full_response})
291
+
292
+
293
+
294
+ ##TODO: a button to delete history
295
+ # if prompt == 'Yes':
296
+ # bot.write("Great! Please describe the word you have in mind.")
297
+ # sent = st.chat_input('Description of your word')
298
+
299
+
300
+ # # adding the text that will show in the text box as default
301
+ # default_value = "Type the description of the word you have in mind!"
302
+
303
+ # sent = st.text_area("Text", default_value, height = 50)
304
+ # result = return_top_k(sent)
305
+ # result = ['animal', 'monster', 'creature', 'bird', 'cat', 'human', 'dog', 'spider', 'alien', 'meow']
306
+ # result = return_top_k(sent)
307
+ # result_print = dict(zip(range(1, 11), result))
308
+
309
+ # if st.button('Results'):
310
+ # st.write("Here are my guesses about your word:")
311
+ # st.write(result_print)
312
+ # elif st.button('Cue'):
313
+ # st.write(f'The first letter is {result[0][0]}.')
314
+ # letter_count = 1
315
+ # word_count = 0
316
+ # answer = st.text_area("Text", 'Does it help you remember the word? Type yes or no', height = 50)
317
+ # if answer == 'No':
318
+ # while answer == 'No':
319
+ # option = st.selectbox(
320
+ # 'What do you want to see?',
321
+ # ('Next letter', 'Next word', 'All words'))
322
+ # if option == 'Next letter':
323
+ # letter_count += 1
324
+ # st.write(f'The word starts with {result[word_count][:letter_count]}')
325
+ # elif option == 'Next word':
326
+ # letter_count = 1
327
+ # st.write(f'The next word starts with {result[word_count][:letter_count]}')
328
+ # word_count += 1
329
+ # else:
330
+ # st.write("Here are all my guesses about your word:")
331
+ # st.write(result_print)
332
+ # answer = st.selectbox(
333
+ # 'Does it help you remember the word??',
334
+ # ('Yes', 'No', 'Exit'))
335
+ # if answer == 'Exit':
336
+ # st.write("I am sorry I couldn't help you. See you next time!")
337
+ # break
338
+ # else:
339
+ # st.write("I am happy I could be of help!")
340
+ # else:
341
+ # st.write('Do you want to see my guesses or do you want a cue?')
342
+
343
+
344
+ #2
345
+
346
+ # option = st.selectbox(
347
+ # 'Do you want to see my guesses or do you want a cue?',
348
+ # ('Results', 'Cue'))
349
+
350
+ # st.write('You selected:', option)
351
+
352
+ # if option == 'Results':
353
+ # st.write("Here are my guesses about your word:")
354
+ # st.write(result_print)
355
+ # elif option == 'Cue':
356
+ # st.write(f'The first letter is {result[0][0]}.')
357
+ # letter_count = 1
358
+ # word_count = 0
359
+ # answer = st.selectbox(
360
+ # 'Does it help you remember the word??',
361
+ # ('Yes', 'No'))
362
+ # if answer == 'No':
363
+ # while answer == 'No':
364
+ # option = st.selectbox(
365
+ # 'What do you want to see?',
366
+ # ('Next letter', 'Next word', 'All words'))
367
+ # if option == 'Next letter':
368
+ # letter_count += 1
369
+ # st.write(f'The word starts with {result[word_count][:letter_count]}')
370
+ # elif option == 'Next word':
371
+ # letter_count = 1
372
+ # st.write(f'The next word starts with {result[word_count][:letter_count]}')
373
+ # word_count += 1
374
+ # else:
375
+ # st.write("Here are all my guesses about your word:")
376
+ # st.write(result_print)
377
+ # answer = st.selectbox(
378
+ # 'Does it help you remember the word??',
379
+ # ('Yes', 'No', 'Exit'))
380
+ # if answer == 'Exit':
381
+ # st.write("I am sorry I couldn't help you. See you next time!")
382
+ # break
383
+ # else:
384
+ # st.write("I am happy I could be of help!")
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ numpy==1.23.5
2
+ peft==0.5.0
3
+ streamlit==1.26.0
4
+ torch==1.13.1
5
+ tqdm==4.64.1
6
+ transformers==4.33.2
simon.jpg ADDED