NursNurs commited on
Commit
823d6c0
·
1 Parent(s): 962fc4e

Adjusted search algorithm

Browse files

filtering and promotion are now done only for pre-defined categories

Files changed (1) hide show
  1. app.py +95 -63
app.py CHANGED
@@ -49,7 +49,16 @@ def get_bert_embeddings(sentence, model, tokenizer):
49
  return embeddings
50
 
51
  # a function that return top-K best restaurants
52
- def compute_cos_sim(query):
 
 
 
 
 
 
 
 
 
53
  embedded_query = get_bert_embeddings(query, model, tokenizer)
54
  embedded_query = embedded_query.numpy()
55
  top_similar = np.array([])
@@ -171,28 +180,26 @@ def promote_places(preferences):
171
  descr = [word.lower() for word in st.session_state.df['Strings'][i].split()]
172
  name = st.session_state.df['Names'][i]
173
  for pref in preferences:
174
- if pref in descr:
175
  st.session_state.df['Weights'][i] = 2 * st.session_state.df['Weights'][i]
176
 
177
  return st.session_state.df
178
 
179
  def generate_results(sort_by):
180
  if sort_by == 'Price':
181
- with st.spinner("Sorting your results by price..."):
182
- st.write("Sorting your results by price...")
183
- results = sort_by_price(10)
184
  elif sort_by == 'Rating':
185
- with st.spinner("Sorting your results by rating..."):
186
- st.write("Sorting your results by rating...")
187
- results = sort_by_rating(10)
188
  elif sort_by == 'Relevancy (default)':
189
- with st.spinner("Sorting your results by relevancy..."):
190
- st.write("Sorting your results by relevancy...")
191
- results = sort_by_relevancy(10)
192
  else:
193
- st.write("Sorry, we are still working on this option. For now, the results are sorted by relevance")
194
- with st.spinner("Sorting your results by relevancy..."):
195
- results = sort_by_relevancy(10)
196
  return results
197
 
198
  if 'preferences_1' not in st.session_state:
@@ -201,6 +208,12 @@ if 'preferences_1' not in st.session_state:
201
  if 'preferences_2' not in st.session_state:
202
  st.session_state.preferences_2 = []
203
 
 
 
 
 
 
 
204
  if 'food' not in st.session_state:
205
  st.session_state.food = ['Coffee', 'Italian', 'Mexican', 'Chinese', 'Indian', 'Asian', 'Fast food', 'Other']
206
 
@@ -224,9 +237,6 @@ if 'df' not in st.session_state:
224
 
225
  if 'precalculated_df' not in st.session_state:
226
  st.session_state.precalculated_df = pd.DataFrame()
227
-
228
- if 'stop_search' not in st.session_state:
229
- st.session_state.stop_search = False
230
 
231
  # Configure Streamlit page and state
232
  st.title("GoTogether!")
@@ -299,7 +309,7 @@ if food_1 == 'Other':
299
  ambiance_1 = st.selectbox('What describes your occasion the best?', st.session_state.ambiance, key=2)
300
  if ambiance_1 == 'Other':
301
  ambiance_1 = st.text_input(label="Your description", placeholder="How would you describe your meeting?", key=11)
302
-
303
  options_food_1 = st.multiselect(
304
  'Do you have any dietary restrictions?',
305
  ['Vegan', 'Vegetarian', 'Halal'], key=100)
@@ -329,16 +339,33 @@ with_kids_2 = st.checkbox('I will come with kids', key=201)
329
 
330
  if len(st.session_state.preferences_1) == 0:
331
  st.session_state.preferences_1.append(food_1)
 
 
 
 
332
  st.session_state.preferences_1.append(ambiance_1)
 
 
 
 
 
333
  st.session_state.restrictions.extend(options_food_1)
334
- if additional_1:
335
- st.session_state.preferences_1.append(additional_1)
336
  if with_kids:
337
  st.session_state.restrictions.append('kids')
338
-
 
 
339
  if len(st.session_state.preferences_2) == 0:
340
  st.session_state.preferences_2.append(food_2)
 
 
 
 
341
  st.session_state.preferences_2.append(ambiance_2)
 
 
 
 
342
  st.session_state.restrictions.extend(options_food_2)
343
  if additional_2:
344
  st.session_state.preferences_2.append(additional_2)
@@ -348,8 +375,9 @@ if len(st.session_state.preferences_2) == 0:
348
  submitted = st.button('Submit!')
349
 
350
  if submitted:
351
- st.markdown("Thanks, we received your preferences!")
352
- st.session_state.stop_search = False
 
353
 
354
  else:
355
  st.write('☝️ Describe your preferences!')
@@ -371,48 +399,51 @@ if submit or (not st.session_state.precalculated_df.empty):
371
  index=st.session_state.options.index('Relevancy (default)'))
372
  if sort_by:
373
  st.session_state.sort_by = sort_by
374
- results = generate_results(st.session_state.sort_by)
375
- k = 10
376
- st.write(f"Here are the best {k} matches to your preferences:")
377
- i = 1
378
- nums = list(range(1, 11))
379
- words = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'one: :zero']
380
- nums_emojis = dict(zip(nums, words))
381
- for name, score in results.items():
382
-
383
- condition = st.session_state.precalculated_df['Names'] == name
384
- rating = st.session_state.precalculated_df.loc[condition, 'Rating'].values[0]
385
- with st.expander(f":{nums_emojis[i]}: **{name}** **({str(rating)}**:star:): match score: {score}"):
386
 
387
- #f":{nums_emojis[i]}: **{name}** **({str(rating)}**:star:) :", 'match score:', score
388
- try:
389
- if type(st.session_state.precalculated_df.loc[condition, 'Price'].values[0]) == str:
390
- st.write("Price category:", st.session_state.precalculated_df.loc[condition, 'Price'].values[0])
391
- except:
392
- pass
393
 
394
- # Use the condition to extract the value(s)
395
- # description = st.session_state.precalculated_df.loc[condition, 'Strings']
396
- # st.write(description)
397
-
398
- type = [item for item in eval(st.session_state.precalculated_df.loc[condition, 'Category'].values[0])]
399
- # Display HTML with the custom styles
400
- for word in type:
401
- st.markdown(css, unsafe_allow_html=True)
402
- st.markdown(f'<div class="blue-box">{word}</div>', unsafe_allow_html=True)
403
- # st.write("Restaurant type:", str(type))
404
-
405
- keywords = [item[0] for item in eval(st.session_state.precalculated_df.loc[condition, 'Keywords'].values[0])]
406
- for pair in keywords[:3]:
407
- st.markdown(css, unsafe_allow_html=True)
408
- st.markdown(f'<div class="orange-box">{pair[0]} {pair[1]}</div>', unsafe_allow_html=True)
409
- # st.write("Restaurant type:", str(type))
410
-
411
-
412
- url = st.session_state.precalculated_df.loc[condition, 'URL'].values[0]
413
- st.write(f"_Check on the_ [_map_]({url})")
414
-
415
- i+=1
 
 
 
 
 
 
 
 
 
416
 
417
  # st.markdown("This is a text with <span style='font-size: 20px;'>bigger</span> and <i>italic</i> text.", unsafe_allow_html=True)
418
  # st.markdown("<span style='font-size: 24px;'>This is larger text</span>", unsafe_allow_html=True)
@@ -425,6 +456,7 @@ if stop:
425
  st.write("New search is launched. Please specify your preferences in the form!")
426
  st.session_state.preferences_1, st.session_state.preferences_2 = [], []
427
  st.session_state.restrictions = []
 
428
  st.session_state.sort_by = ""
429
  st.session_state.df = init_df
430
  st.session_state.precalculated_df = pd.DataFrame()
 
49
  return embeddings
50
 
51
  # a function that return top-K best restaurants
52
+ def compute_cos_sim(input):
53
+ query = ""
54
+ query += input
55
+
56
+ # for el in st.session_state.preferences_1:
57
+ # query += el
58
+ # for el in st.session_state.preferences_2:
59
+ # query += el
60
+
61
+ st.write("Your query is", query)
62
  embedded_query = get_bert_embeddings(query, model, tokenizer)
63
  embedded_query = embedded_query.numpy()
64
  top_similar = np.array([])
 
180
  descr = [word.lower() for word in st.session_state.df['Strings'][i].split()]
181
  name = st.session_state.df['Names'][i]
182
  for pref in preferences:
183
+ if (pref in descr) & ((pref in st.session_state.food) or (pref in st.session_state.ambiance)):
184
  st.session_state.df['Weights'][i] = 2 * st.session_state.df['Weights'][i]
185
 
186
  return st.session_state.df
187
 
188
  def generate_results(sort_by):
189
  if sort_by == 'Price':
190
+ results = sort_by_price(10)
 
 
191
  elif sort_by == 'Rating':
192
+ # with st.spinner("Sorting your results by rating..."):
193
+ # st.write("Sorting your results by rating...")
194
+ results = sort_by_rating(10)
195
  elif sort_by == 'Relevancy (default)':
196
+ # with st.spinner("Sorting your results by relevancy..."):
197
+ # st.write("Sorting your results by relevancy...")
198
+ results = sort_by_relevancy(10)
199
  else:
200
+ st.write(":pensive: Sorry, we are still working on this option. For now, the results are sorted by relevance")
201
+ # with st.spinner("Sorting your results by relevancy..."):
202
+ results = sort_by_relevancy(10)
203
  return results
204
 
205
  if 'preferences_1' not in st.session_state:
 
208
  if 'preferences_2' not in st.session_state:
209
  st.session_state.preferences_2 = []
210
 
211
+ if 'additional_1' not in st.session_state:
212
+ st.session_state.additional_1 = []
213
+
214
+ if 'additional_2' not in st.session_state:
215
+ st.session_state.additional_2 = []
216
+
217
  if 'food' not in st.session_state:
218
  st.session_state.food = ['Coffee', 'Italian', 'Mexican', 'Chinese', 'Indian', 'Asian', 'Fast food', 'Other']
219
 
 
237
 
238
  if 'precalculated_df' not in st.session_state:
239
  st.session_state.precalculated_df = pd.DataFrame()
 
 
 
240
 
241
  # Configure Streamlit page and state
242
  st.title("GoTogether!")
 
309
  ambiance_1 = st.selectbox('What describes your occasion the best?', st.session_state.ambiance, key=2)
310
  if ambiance_1 == 'Other':
311
  ambiance_1 = st.text_input(label="Your description", placeholder="How would you describe your meeting?", key=11)
312
+
313
  options_food_1 = st.multiselect(
314
  'Do you have any dietary restrictions?',
315
  ['Vegan', 'Vegetarian', 'Halal'], key=100)
 
339
 
340
  if len(st.session_state.preferences_1) == 0:
341
  st.session_state.preferences_1.append(food_1)
342
+ # if food_1 in st.session_state.food:
343
+ # st.session_state.preferences_1.append(food_1)
344
+ # else:
345
+ # st.session_state.additional_1.append(food_1_o)
346
  st.session_state.preferences_1.append(ambiance_1)
347
+
348
+ # if ambiance_1 in st.session_state.ambiance:
349
+ # st.session_state.preferences_1.append(ambiance_1)
350
+ # else:
351
+ # st.session_state.additional_1.append(ambiance_1_o)
352
  st.session_state.restrictions.extend(options_food_1)
 
 
353
  if with_kids:
354
  st.session_state.restrictions.append('kids')
355
+ if additional_1:
356
+ st.session_state.preferences_1.append(additional_1)
357
+
358
  if len(st.session_state.preferences_2) == 0:
359
  st.session_state.preferences_2.append(food_2)
360
+ # if food_2 in st.session_state.food:
361
+ # st.session_state.preferences_2.append(food_2)
362
+ # else:
363
+ # st.session_state.additional_2.append(food_2_o)
364
  st.session_state.preferences_2.append(ambiance_2)
365
+ # if ambiance_2 in st.session_state.ambiance:
366
+ # st.session_state.preferences_2.append(ambiance_2)
367
+ # else:
368
+ # st.session_state.additional_2.append(ambiance_2_o)
369
  st.session_state.restrictions.extend(options_food_2)
370
  if additional_2:
371
  st.session_state.preferences_2.append(additional_2)
 
375
  submitted = st.button('Submit!')
376
 
377
  if submitted:
378
+ with st.spinner('Processing your request...'):
379
+ time.sleep(1)
380
+ st.success("Thanks, we received your preferences!")
381
 
382
  else:
383
  st.write('☝️ Describe your preferences!')
 
399
  index=st.session_state.options.index('Relevancy (default)'))
400
  if sort_by:
401
  st.session_state.sort_by = sort_by
402
+ with st.spinner(f"Sorting your results by {sort_by.lower()}..."):
403
+ results = generate_results(st.session_state.sort_by)
404
+ k = 10
405
+ st.write(f"Here are the best {k} matches to your preferences:")
406
+ i = 1
407
+ nums = list(range(1, 11))
408
+ words = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'one: :zero']
409
+ nums_emojis = dict(zip(nums, words))
410
+ for name, score in results.items():
 
 
 
411
 
412
+ condition = st.session_state.precalculated_df['Names'] == name
413
+ rating = st.session_state.precalculated_df.loc[condition, 'Rating'].values[0]
414
+ with st.expander(f":{nums_emojis[i]}: **{name}** **({str(rating)}**:star:): match score: {score}"):
 
 
 
415
 
416
+ #f":{nums_emojis[i]}: **{name}** **({str(rating)}**:star:) :", 'match score:', score
417
+ try:
418
+ if type(st.session_state.precalculated_df.loc[condition, 'Price'].values[0]) == str:
419
+ st.write("Price category:", st.session_state.precalculated_df.loc[condition, 'Price'].values[0])
420
+ except:
421
+ pass
422
+
423
+ # Use the condition to extract the value(s)
424
+ # description = st.session_state.precalculated_df.loc[condition, 'Strings']
425
+ # st.write(description)
426
+
427
+ type = [item for item in eval(st.session_state.precalculated_df.loc[condition, 'Category'].values[0])]
428
+ # Display HTML with the custom styles
429
+ for word in type:
430
+ st.markdown(css, unsafe_allow_html=True)
431
+ st.markdown(f'<div class="blue-box">{word}</div>', unsafe_allow_html=True)
432
+ # st.write("Restaurant type:", str(type))
433
+
434
+ keywords = [item[0] for item in eval(st.session_state.precalculated_df.loc[condition, 'Keywords'].values[0]) if item[1] > 2]
435
+ for pair in keywords[:3]:
436
+ st.markdown(css, unsafe_allow_html=True)
437
+ st.markdown(f'<div class="orange-box">{pair[0]} {pair[1]}</div>', unsafe_allow_html=True)
438
+ # st.write("Restaurant type:", str(type))
439
+
440
+
441
+ url = st.session_state.precalculated_df.loc[condition, 'URL'].values[0]
442
+ st.write(f"_Check on the_ [_map_]({url})")
443
+
444
+ st.write(st.session_state.precalculated_df.loc[condition, 'Strings'].values[0])
445
+
446
+ i+=1
447
 
448
  # st.markdown("This is a text with <span style='font-size: 20px;'>bigger</span> and <i>italic</i> text.", unsafe_allow_html=True)
449
  # st.markdown("<span style='font-size: 24px;'>This is larger text</span>", unsafe_allow_html=True)
 
456
  st.write("New search is launched. Please specify your preferences in the form!")
457
  st.session_state.preferences_1, st.session_state.preferences_2 = [], []
458
  st.session_state.restrictions = []
459
+ st.session_state.additional_1, st.session_state.additional_2 = [], []
460
  st.session_state.sort_by = ""
461
  st.session_state.df = init_df
462
  st.session_state.precalculated_df = pd.DataFrame()