Spaces:
Sleeping
Sleeping
Changes in result output + algorithm
Browse filesRemoved promotion of the words, left only restriction
Normalized values from 0 to 100
app.py
CHANGED
|
@@ -10,6 +10,7 @@ from collections import defaultdict, Counter
|
|
| 10 |
from tqdm.auto import tqdm
|
| 11 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 12 |
import time
|
|
|
|
| 13 |
|
| 14 |
#Loading the model
|
| 15 |
@st.cache_resource
|
|
@@ -29,7 +30,7 @@ def str_to_numpy(array_string):
|
|
| 29 |
|
| 30 |
@st.cache_data # 👈 Add the caching decorator
|
| 31 |
def load_data():
|
| 32 |
-
vectors_df = pd.read_csv('
|
| 33 |
embeds = dict(enumerate(vectors_df['Embeddings']))
|
| 34 |
rest_names = list(vectors_df['Names'])
|
| 35 |
vectors_df['Weights'] = [1]*len(vectors_df)
|
|
@@ -58,7 +59,7 @@ def compute_cos_sim(input):
|
|
| 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([])
|
|
@@ -136,7 +137,7 @@ def sort_by_rating(k):
|
|
| 136 |
|
| 137 |
return result
|
| 138 |
|
| 139 |
-
#combines 2 users preferences into 1 string
|
| 140 |
def get_combined_preferences(user1, user2):
|
| 141 |
#TODO: optimize for more users
|
| 142 |
shared_pref = ''
|
|
@@ -150,6 +151,12 @@ def get_combined_preferences(user1, user2):
|
|
| 150 |
|
| 151 |
freq_words = Counter(shared_pref.split())
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
return shared_pref, freq_words
|
| 154 |
|
| 155 |
def filter_places(restrictions):
|
|
@@ -167,40 +174,49 @@ def filter_places(restrictions):
|
|
| 167 |
|
| 168 |
return st.session_state.df
|
| 169 |
|
| 170 |
-
def promote_places(
|
| 171 |
'''
|
| 172 |
input type: dict()
|
| 173 |
a function that takes most common words, checks if descriptions fit them, increases their weight if they do
|
| 174 |
'''
|
| 175 |
#punish the weight of places that don't fit restrictions
|
| 176 |
-
|
| 177 |
-
|
|
|
|
|
|
|
| 178 |
|
| 179 |
for i in range(len(st.session_state.df)):
|
| 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
|
| 184 |
-
st.session_state.df['Weights'][i] =
|
| 185 |
|
| 186 |
return st.session_state.df
|
| 187 |
|
| 188 |
-
def generate_results(
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
#
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
if 'preferences_1' not in st.session_state:
|
| 206 |
st.session_state.preferences_1 = []
|
|
@@ -208,6 +224,9 @@ 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 |
|
|
@@ -224,7 +243,7 @@ if 'restrictions' not in st.session_state:
|
|
| 224 |
st.session_state.restrictions = []
|
| 225 |
|
| 226 |
if 'price' not in st.session_state:
|
| 227 |
-
|
| 228 |
|
| 229 |
if 'sort_by' not in st.session_state:
|
| 230 |
st.session_state.sort_by = ''
|
|
@@ -237,6 +256,9 @@ if 'df' not in st.session_state:
|
|
| 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!")
|
|
@@ -253,20 +275,22 @@ css = """
|
|
| 253 |
border-radius: 10px;
|
| 254 |
display: inline-block;
|
| 255 |
padding: 5px 10px;
|
|
|
|
| 256 |
}
|
| 257 |
|
| 258 |
.blue-box {
|
| 259 |
-
background-color:
|
| 260 |
border: 2px solid navy;
|
| 261 |
border-radius: 10px;
|
| 262 |
display: inline-block;
|
| 263 |
padding: 5px 10px;
|
|
|
|
| 264 |
}
|
| 265 |
|
| 266 |
-
.
|
| 267 |
-
border: 2px solid #004d00; /* Dark
|
| 268 |
border-radius: 10px;
|
| 269 |
-
background-color: #4CAF50; /*
|
| 270 |
display: inline-block;
|
| 271 |
padding: 5px 10px;
|
| 272 |
color: #FFFFFF; /* White text color */
|
|
@@ -283,6 +307,16 @@ css = """
|
|
| 283 |
</style>
|
| 284 |
"""
|
| 285 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
# options_disability_1 = st.multiselect(
|
| 288 |
# 'Do you need a wheelchair?',
|
|
@@ -290,14 +324,6 @@ css = """
|
|
| 290 |
|
| 291 |
# if options_disability_1 == 'Yes':
|
| 292 |
# st.session_state.restrictions.append('Wheelchair')
|
| 293 |
-
|
| 294 |
-
# price_1 = st.select_slider("Your preferred price range", options=('$', '$$', '$$$', '$$$$'), key=3)
|
| 295 |
-
|
| 296 |
-
# st.session_state.preferences_1.append(ambiance_1)
|
| 297 |
-
|
| 298 |
-
# Komplettes Beispiel für die Verwendung der 'with'-Notation
|
| 299 |
-
# with st.form('my_form_1'):
|
| 300 |
-
# st.subheader('**User 1**')
|
| 301 |
|
| 302 |
st.markdown(css, unsafe_allow_html=True)
|
| 303 |
st.markdown(f'<div class="violet-box">User 1</div>', unsafe_allow_html=True)
|
|
@@ -337,46 +363,46 @@ additional_2 = st.text_input(label="Your description", placeholder="Anything els
|
|
| 337 |
|
| 338 |
with_kids_2 = st.checkbox('I will come with kids', key=201)
|
| 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)
|
| 372 |
-
if with_kids_2:
|
| 373 |
-
st.session_state.restrictions.append('kids')
|
| 374 |
-
|
| 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:
|
|
@@ -389,29 +415,33 @@ if submit or (not st.session_state.precalculated_df.empty):
|
|
| 389 |
with st.spinner("Please wait while we are finding the best solution..."):
|
| 390 |
if st.session_state.precalculated_df.empty:
|
| 391 |
query = get_combined_preferences(st.session_state.preferences_1, st.session_state.preferences_2)
|
| 392 |
-
st.write("Your query is:", query[0])
|
| 393 |
#sort places based on restrictions
|
| 394 |
st.session_state.precalculated_df = filter_places(st.session_state.restrictions)
|
| 395 |
#sort places by elevating preferrences
|
| 396 |
-
|
|
|
|
| 397 |
st.session_state.precalculated_df = compute_cos_sim(query[0])
|
| 398 |
sort_by = st.selectbox(('Sort by:'), st.session_state.options, key=400,
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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:
|
|
@@ -419,29 +449,36 @@ if submit or (not st.session_state.precalculated_df.empty):
|
|
| 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 |
-
|
| 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(
|
| 445 |
|
| 446 |
i+=1
|
| 447 |
|
|
@@ -449,7 +486,8 @@ if submit or (not st.session_state.precalculated_df.empty):
|
|
| 449 |
# st.markdown("<span style='font-size: 24px;'>This is larger text</span>", unsafe_allow_html=True)
|
| 450 |
|
| 451 |
|
| 452 |
-
|
|
|
|
| 453 |
|
| 454 |
stop = st.button("New search!", type='primary', key=500)
|
| 455 |
if stop:
|
|
@@ -460,8 +498,6 @@ if stop:
|
|
| 460 |
st.session_state.sort_by = ""
|
| 461 |
st.session_state.df = init_df
|
| 462 |
st.session_state.precalculated_df = pd.DataFrame()
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
# Propose URLs
|
| 467 |
-
# Show keywords instead of whole strings
|
|
|
|
| 10 |
from tqdm.auto import tqdm
|
| 11 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 12 |
import time
|
| 13 |
+
import random
|
| 14 |
|
| 15 |
#Loading the model
|
| 16 |
@st.cache_resource
|
|
|
|
| 30 |
|
| 31 |
@st.cache_data # 👈 Add the caching decorator
|
| 32 |
def load_data():
|
| 33 |
+
vectors_df = pd.read_csv('filtered_restaurants_dataframe_with_embeddings.csv', encoding="utf-8")
|
| 34 |
embeds = dict(enumerate(vectors_df['Embeddings']))
|
| 35 |
rest_names = list(vectors_df['Names'])
|
| 36 |
vectors_df['Weights'] = [1]*len(vectors_df)
|
|
|
|
| 59 |
# for el in st.session_state.preferences_2:
|
| 60 |
# query += el
|
| 61 |
|
| 62 |
+
st.write("Your query for cos sim calculation is", query)
|
| 63 |
embedded_query = get_bert_embeddings(query, model, tokenizer)
|
| 64 |
embedded_query = embedded_query.numpy()
|
| 65 |
top_similar = np.array([])
|
|
|
|
| 137 |
|
| 138 |
return result
|
| 139 |
|
| 140 |
+
#combines 2 users preferences into 1 string
|
| 141 |
def get_combined_preferences(user1, user2):
|
| 142 |
#TODO: optimize for more users
|
| 143 |
shared_pref = ''
|
|
|
|
| 151 |
|
| 152 |
freq_words = Counter(shared_pref.split())
|
| 153 |
|
| 154 |
+
preferences = [pref for pref in st.session_state.preferences_1 if ((pref.capitalize() in st.session_state.food) or (pref in st.session_state.ambiance))]
|
| 155 |
+
preferences.extend([pref for pref in st.session_state.preferences_2 if ((pref.capitalize() in st.session_state.food) or (pref in st.session_state.ambiance))])
|
| 156 |
+
translator = str.maketrans('', '', string.punctuation)
|
| 157 |
+
preferences = [word.translate(translator) for phrase in preferences for word in phrase.split() if len(word) > 0]
|
| 158 |
+
st.session_state.fixed_preferences = [word.lower() for word in preferences]
|
| 159 |
+
|
| 160 |
return shared_pref, freq_words
|
| 161 |
|
| 162 |
def filter_places(restrictions):
|
|
|
|
| 174 |
|
| 175 |
return st.session_state.df
|
| 176 |
|
| 177 |
+
def promote_places():
|
| 178 |
'''
|
| 179 |
input type: dict()
|
| 180 |
a function that takes most common words, checks if descriptions fit them, increases their weight if they do
|
| 181 |
'''
|
| 182 |
#punish the weight of places that don't fit restrictions
|
| 183 |
+
st.write("Here are the most common preferences you provided:")
|
| 184 |
+
st.write(st.session_state.fixed_preferences)
|
| 185 |
+
|
| 186 |
+
preferences = st.session_state.fixed_preferences
|
| 187 |
|
| 188 |
for i in range(len(st.session_state.df)):
|
| 189 |
descr = [word.lower() for word in st.session_state.df['Strings'][i].split()]
|
| 190 |
name = st.session_state.df['Names'][i]
|
| 191 |
for pref in preferences:
|
| 192 |
+
if pref.lower() in descr:
|
| 193 |
+
st.session_state.df['Weights'][i] = 1.1 * st.session_state.df['Weights'][i]
|
| 194 |
|
| 195 |
return st.session_state.df
|
| 196 |
|
| 197 |
+
def generate_results():
|
| 198 |
+
st.session_state.results['Price'] = sort_by_price(10)
|
| 199 |
+
st.session_state.results['Rating'] = sort_by_rating(10)
|
| 200 |
+
st.session_state.results['Relevancy (default)'] = sort_by_relevancy(10)
|
| 201 |
+
st.session_state.results['Distance'] = sort_by_relevancy(10)
|
| 202 |
+
# with st.spinner("Sorting your results by relevancy..."):
|
| 203 |
+
|
| 204 |
+
def get_normalized_val(values):
|
| 205 |
+
if st.session_state.sort_by == 'Relevancy (default)' or st.session_state.sort_by == 'Distance':
|
| 206 |
+
# Find the minimum and maximum values
|
| 207 |
+
min_value = min(st.session_state.precalculated_df['Relevancy'])
|
| 208 |
+
max_value = max(st.session_state.precalculated_df['Relevancy'])
|
| 209 |
+
elif st.session_state.sort_by == 'Rating':
|
| 210 |
+
min_value = min(st.session_state.precalculated_df['Sort_rating'])
|
| 211 |
+
max_value = max(st.session_state.precalculated_df['Sort_rating'])
|
| 212 |
+
elif st.session_state.sort_by == 'Price':
|
| 213 |
+
min_value = min(st.session_state.precalculated_df['Sort_price'])
|
| 214 |
+
max_value = max(st.session_state.precalculated_df['Sort_price'])
|
| 215 |
+
# Define a lambda function for normalization
|
| 216 |
+
normalize = lambda x: 100 * round((x - min_value) / (max_value - min_value), 3)
|
| 217 |
+
# Use the map function to apply the lambda function to all values
|
| 218 |
+
normalized_results = dict(map(lambda item: (item[0], normalize(item[1])), values.items()))
|
| 219 |
+
return normalized_results
|
| 220 |
|
| 221 |
if 'preferences_1' not in st.session_state:
|
| 222 |
st.session_state.preferences_1 = []
|
|
|
|
| 224 |
if 'preferences_2' not in st.session_state:
|
| 225 |
st.session_state.preferences_2 = []
|
| 226 |
|
| 227 |
+
if 'fixed_preferences' not in st.session_state:
|
| 228 |
+
st.session_state.fixed_preferences = []
|
| 229 |
+
|
| 230 |
if 'additional_1' not in st.session_state:
|
| 231 |
st.session_state.additional_1 = []
|
| 232 |
|
|
|
|
| 243 |
st.session_state.restrictions = []
|
| 244 |
|
| 245 |
if 'price' not in st.session_state:
|
| 246 |
+
st.session_state.price = {'$': 2, '₩': 2, '$$': 1, '₩₩': 1, '$$$': 0.5, '$$$$': 0.1, "nan": 1}
|
| 247 |
|
| 248 |
if 'sort_by' not in st.session_state:
|
| 249 |
st.session_state.sort_by = ''
|
|
|
|
| 256 |
|
| 257 |
if 'precalculated_df' not in st.session_state:
|
| 258 |
st.session_state.precalculated_df = pd.DataFrame()
|
| 259 |
+
|
| 260 |
+
if 'results' not in st.session_state:
|
| 261 |
+
st.session_state.results = {}
|
| 262 |
|
| 263 |
# Configure Streamlit page and state
|
| 264 |
st.title("GoTogether!")
|
|
|
|
| 275 |
border-radius: 10px;
|
| 276 |
display: inline-block;
|
| 277 |
padding: 5px 10px;
|
| 278 |
+
margin: 0px;
|
| 279 |
}
|
| 280 |
|
| 281 |
.blue-box {
|
| 282 |
+
background-color: #0077b6;
|
| 283 |
border: 2px solid navy;
|
| 284 |
border-radius: 10px;
|
| 285 |
display: inline-block;
|
| 286 |
padding: 5px 10px;
|
| 287 |
+
color: white;
|
| 288 |
}
|
| 289 |
|
| 290 |
+
.green-box {
|
| 291 |
+
border: 2px solid #004d00; /* Dark green contour */
|
| 292 |
border-radius: 10px;
|
| 293 |
+
background-color: #4CAF50; /* green background */
|
| 294 |
display: inline-block;
|
| 295 |
padding: 5px 10px;
|
| 296 |
color: #FFFFFF; /* White text color */
|
|
|
|
| 307 |
</style>
|
| 308 |
"""
|
| 309 |
|
| 310 |
+
text_css = """
|
| 311 |
+
<style>
|
| 312 |
+
.text {
|
| 313 |
+
font-weight: bold;
|
| 314 |
+
color: #0077b6; /* Sea-blue text color */
|
| 315 |
+
margin-right: 1px;
|
| 316 |
+
}
|
| 317 |
+
</style>
|
| 318 |
+
"""
|
| 319 |
+
|
| 320 |
|
| 321 |
# options_disability_1 = st.multiselect(
|
| 322 |
# 'Do you need a wheelchair?',
|
|
|
|
| 324 |
|
| 325 |
# if options_disability_1 == 'Yes':
|
| 326 |
# st.session_state.restrictions.append('Wheelchair')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
|
| 328 |
st.markdown(css, unsafe_allow_html=True)
|
| 329 |
st.markdown(f'<div class="violet-box">User 1</div>', unsafe_allow_html=True)
|
|
|
|
| 363 |
|
| 364 |
with_kids_2 = st.checkbox('I will come with kids', key=201)
|
| 365 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
submitted = st.button('Submit!')
|
| 367 |
|
| 368 |
if submitted:
|
| 369 |
with st.spinner('Processing your request...'):
|
| 370 |
time.sleep(1)
|
| 371 |
+
if len(st.session_state.preferences_1) == 0:
|
| 372 |
+
st.session_state.preferences_1.append(food_1)
|
| 373 |
+
# if food_1 in st.session_state.food:
|
| 374 |
+
# st.session_state.preferences_1.append(food_1)
|
| 375 |
+
# else:
|
| 376 |
+
# st.session_state.additional_1.append(food_1_o)
|
| 377 |
+
st.session_state.preferences_1.append(ambiance_1)
|
| 378 |
+
|
| 379 |
+
# if ambiance_1 in st.session_state.ambiance:
|
| 380 |
+
# st.session_state.preferences_1.append(ambiance_1)
|
| 381 |
+
# else:
|
| 382 |
+
# st.session_state.additional_1.append(ambiance_1_o)
|
| 383 |
+
st.session_state.restrictions.extend(options_food_1)
|
| 384 |
+
if with_kids:
|
| 385 |
+
st.session_state.restrictions.append('kids')
|
| 386 |
+
if additional_1:
|
| 387 |
+
st.session_state.preferences_1.append(additional_1)
|
| 388 |
+
|
| 389 |
+
if len(st.session_state.preferences_2) == 0:
|
| 390 |
+
st.session_state.preferences_2.append(food_2)
|
| 391 |
+
# if food_2 in st.session_state.food:
|
| 392 |
+
# st.session_state.preferences_2.append(food_2)
|
| 393 |
+
# else:
|
| 394 |
+
# st.session_state.additional_2.append(food_2_o)
|
| 395 |
+
st.session_state.preferences_2.append(ambiance_2)
|
| 396 |
+
# if ambiance_2 in st.session_state.ambiance:
|
| 397 |
+
# st.session_state.preferences_2.append(ambiance_2)
|
| 398 |
+
# else:
|
| 399 |
+
# st.session_state.additional_2.append(ambiance_2_o)
|
| 400 |
+
st.session_state.restrictions.extend(options_food_2)
|
| 401 |
+
if additional_2:
|
| 402 |
+
st.session_state.preferences_2.append(additional_2)
|
| 403 |
+
if with_kids_2:
|
| 404 |
+
st.session_state.restrictions.append('kids')
|
| 405 |
+
|
| 406 |
st.success("Thanks, we received your preferences!")
|
| 407 |
|
| 408 |
else:
|
|
|
|
| 415 |
with st.spinner("Please wait while we are finding the best solution..."):
|
| 416 |
if st.session_state.precalculated_df.empty:
|
| 417 |
query = get_combined_preferences(st.session_state.preferences_1, st.session_state.preferences_2)
|
|
|
|
| 418 |
#sort places based on restrictions
|
| 419 |
st.session_state.precalculated_df = filter_places(st.session_state.restrictions)
|
| 420 |
#sort places by elevating preferrences
|
| 421 |
+
|
| 422 |
+
# st.session_state.precalculated_df = promote_places()
|
| 423 |
st.session_state.precalculated_df = compute_cos_sim(query[0])
|
| 424 |
sort_by = st.selectbox(('Sort by:'), st.session_state.options, key=400,
|
| 425 |
index=st.session_state.options.index('Relevancy (default)'))
|
| 426 |
if sort_by:
|
| 427 |
st.session_state.sort_by = sort_by
|
| 428 |
with st.spinner(f"Sorting your results by {sort_by.lower()}..."):
|
| 429 |
+
if len(st.session_state.results) == 0:
|
| 430 |
+
generate_results()
|
| 431 |
+
results = st.session_state.results[sort_by]
|
| 432 |
+
if sort_by == 'Distance':
|
| 433 |
+
st.write(":pensive: Sorry, we are still working on this option. For now, the results are sorted by relevance")
|
| 434 |
k = 10
|
| 435 |
st.write(f"Here are the best {k} matches to your preferences:")
|
| 436 |
i = 1
|
| 437 |
nums = list(range(1, 11))
|
| 438 |
words = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'one: :zero']
|
| 439 |
nums_emojis = dict(zip(nums, words))
|
| 440 |
+
results = get_normalized_val(results)
|
| 441 |
+
for name, score in results.items():
|
| 442 |
condition = st.session_state.precalculated_df['Names'] == name
|
| 443 |
rating = st.session_state.precalculated_df.loc[condition, 'Rating'].values[0]
|
| 444 |
+
with st.expander(f":{nums_emojis[i]}: **{name}** **({str(rating)}**:star:): match score: {score}%"):
|
| 445 |
|
| 446 |
#f":{nums_emojis[i]}: **{name}** **({str(rating)}**:star:) :", 'match score:', score
|
| 447 |
try:
|
|
|
|
| 449 |
st.write("Price category:", st.session_state.precalculated_df.loc[condition, 'Price'].values[0])
|
| 450 |
except:
|
| 451 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
|
| 453 |
+
descr = st.session_state.precalculated_df.loc[condition, 'Strings'].values[0]
|
| 454 |
+
for word in set([word.lower() for word in descr.split()]):
|
| 455 |
+
if word in st.session_state.fixed_preferences:
|
| 456 |
+
st.markdown(f'✅{word.capitalize()}')
|
| 457 |
+
|
| 458 |
+
|
| 459 |
+
#Restaurant category
|
| 460 |
type = [item for item in eval(st.session_state.precalculated_df.loc[condition, 'Category'].values[0])]
|
| 461 |
+
st.markdown(text_css, unsafe_allow_html=True)
|
| 462 |
+
st.markdown('<div class="text">Category</div>', unsafe_allow_html=True)
|
| 463 |
+
|
| 464 |
# Display HTML with the custom styles
|
| 465 |
for word in type:
|
| 466 |
st.markdown(css, unsafe_allow_html=True)
|
| 467 |
st.markdown(f'<div class="blue-box">{word}</div>', unsafe_allow_html=True)
|
| 468 |
+
|
|
|
|
| 469 |
keywords = [item[0] for item in eval(st.session_state.precalculated_df.loc[condition, 'Keywords'].values[0]) if item[1] > 2]
|
| 470 |
+
if len(keywords) > 0:
|
| 471 |
+
st.markdown(text_css, unsafe_allow_html=True)
|
| 472 |
+
st.markdown('<div class="text">Other users say:</div>', unsafe_allow_html=True)
|
| 473 |
+
|
| 474 |
for pair in keywords[:3]:
|
| 475 |
st.markdown(css, unsafe_allow_html=True)
|
| 476 |
+
st.markdown(f'<div class="orange-box">{pair[0]} {pair[1]}</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
| 477 |
|
| 478 |
url = st.session_state.precalculated_df.loc[condition, 'URL'].values[0]
|
| 479 |
st.write(f"_Check on the_ [_map_]({url})")
|
| 480 |
+
|
| 481 |
+
st.write(descr)
|
| 482 |
|
| 483 |
i+=1
|
| 484 |
|
|
|
|
| 486 |
# st.markdown("<span style='font-size: 24px;'>This is larger text</span>", unsafe_allow_html=True)
|
| 487 |
|
| 488 |
|
| 489 |
+
st.session_state.preferences_1, st.session_state.preferences_2 = [], []
|
| 490 |
+
st.session_state.restrictions = []
|
| 491 |
|
| 492 |
stop = st.button("New search!", type='primary', key=500)
|
| 493 |
if stop:
|
|
|
|
| 498 |
st.session_state.sort_by = ""
|
| 499 |
st.session_state.df = init_df
|
| 500 |
st.session_state.precalculated_df = pd.DataFrame()
|
| 501 |
+
st.session_state.results = {}
|
| 502 |
+
st.session_state.fixed_preferences = []
|
| 503 |
+
|
|
|
|
|
|