Spaces:
Running
Running
David Chuan-En Lin
commited on
Commit
Β·
28f7cf3
1
Parent(s):
b77f2ea
Another minor UI update
Browse files- foodnet.py +7 -22
foodnet.py
CHANGED
@@ -62,6 +62,7 @@ def recommend_ingredients(yum, leftovers, n=10):
|
|
62 |
leftovers_embedding = leftovers_embedding_sum / len(leftovers) # Embedding for leftovers
|
63 |
top_matches = yum.similar_by_vector(leftovers_embedding, topn=100)
|
64 |
top_matches = [(x[0].replace('_',' '), x[1]) for x in top_matches]
|
|
|
65 |
output = [x for x in top_matches if not any(ignore in x[0] for ignore in leftovers)] # Remove boring same item matches, e.g. "romaine lettuce" if leftovers already contain "lettuce".
|
66 |
return output[:n]
|
67 |
|
@@ -248,7 +249,7 @@ def load_image(image_file):
|
|
248 |
img = Image.open(image_file)
|
249 |
return img
|
250 |
|
251 |
-
st.set_page_config(page_title="FoodNet", page_icon = "π", layout = "centered", initial_sidebar_state = "
|
252 |
|
253 |
##### UI/UX #####
|
254 |
## Sidebar ##
|
@@ -261,10 +262,10 @@ if add_selectbox == "FoodNet Recommender":
|
|
261 |
st.write("Search for similar food ingredients. Select two or more ingredients to find complementary ingredients.")
|
262 |
ingredients = list(yum.key_to_index.keys())
|
263 |
ingredients = [x.replace('_',' ') for x in ingredients]
|
264 |
-
st.multiselect("Type or select food ingredients", ingredients, default=['
|
265 |
|
266 |
## Slider ##
|
267 |
-
st.slider("
|
268 |
|
269 |
## Show Images ##
|
270 |
# search = py_un.search(type_="photos", query="cookie")
|
@@ -302,45 +303,29 @@ if add_selectbox == "FoodNet Recommender":
|
|
302 |
|
303 |
# ## Show Plot ##
|
304 |
# st.pyplot(fig)
|
305 |
-
st.selectbox(label="
|
306 |
if st.session_state.diet != 'None':
|
307 |
if st.session_state.diet == 'Vegitarian':
|
308 |
out = [o for o in out if not any(ignore in o[0] for ignore in blacklists.vegitarian)]
|
309 |
-
if st.session_state.diet ==
|
310 |
out = [o for o in out if not any(ignore in o[0] for ignore in blacklists.kosher)]
|
311 |
names = [o[0] for o in out]
|
312 |
probs = [o[1] for o in out]
|
313 |
|
314 |
col1, col2, col3 = st.columns(3)
|
315 |
|
316 |
-
# st.checkbox(label="Show model score", value=False, key="probs")
|
317 |
-
# if st.session_state.probs:
|
318 |
-
# col1.table(data=out)
|
319 |
-
# else:
|
320 |
-
# col1.table(data=names)
|
321 |
-
|
322 |
for i, name in enumerate(names):
|
323 |
search = py_un.search(type_='photos', query=name)
|
324 |
for photo in search.entries:
|
325 |
-
col_id = i%3
|
326 |
if col_id == 0:
|
327 |
col1.image(photo.link_download, caption=name, use_column_width=True)
|
328 |
elif col_id == 1:
|
329 |
col2.image(photo.link_download, caption=name, use_column_width=True)
|
330 |
elif col_id == 2:
|
331 |
col3.image(photo.link_download, caption=name, use_column_width=True)
|
332 |
-
# print(photo.id, photo.link_download)
|
333 |
-
|
334 |
break
|
335 |
|
336 |
-
## Plot Results ##
|
337 |
-
# st.checkbox(label="Show model bar chart", value=False, key="plot")
|
338 |
-
# if st.session_state.plot:
|
339 |
-
# fig = plot_results(names, probs, st.session_state.top_n)
|
340 |
-
|
341 |
-
# ## Show Plot ##
|
342 |
-
# col2.pyplot(fig)
|
343 |
-
|
344 |
elif add_selectbox == "Food Donation Resources":
|
345 |
st.title('Food Donation Resources')
|
346 |
st.subheader('Pittsburgh Food Bank:')
|
|
|
62 |
leftovers_embedding = leftovers_embedding_sum / len(leftovers) # Embedding for leftovers
|
63 |
top_matches = yum.similar_by_vector(leftovers_embedding, topn=100)
|
64 |
top_matches = [(x[0].replace('_',' '), x[1]) for x in top_matches]
|
65 |
+
leftovers = [x.replace('_',' ') for x in leftovers]
|
66 |
output = [x for x in top_matches if not any(ignore in x[0] for ignore in leftovers)] # Remove boring same item matches, e.g. "romaine lettuce" if leftovers already contain "lettuce".
|
67 |
return output[:n]
|
68 |
|
|
|
249 |
img = Image.open(image_file)
|
250 |
return img
|
251 |
|
252 |
+
st.set_page_config(page_title="FoodNet", page_icon = "π", layout = "centered", initial_sidebar_state = "collapsed")
|
253 |
|
254 |
##### UI/UX #####
|
255 |
## Sidebar ##
|
|
|
262 |
st.write("Search for similar food ingredients. Select two or more ingredients to find complementary ingredients.")
|
263 |
ingredients = list(yum.key_to_index.keys())
|
264 |
ingredients = [x.replace('_',' ') for x in ingredients]
|
265 |
+
st.multiselect("Type or select food ingredients", ingredients, default=['tomato', 'parsley'], key="leftovers")
|
266 |
|
267 |
## Slider ##
|
268 |
+
st.slider("Select number of recommendations to show", min_value=1, max_value=100, value=5, step=1, key='top_n')
|
269 |
|
270 |
## Show Images ##
|
271 |
# search = py_un.search(type_="photos", query="cookie")
|
|
|
303 |
|
304 |
# ## Show Plot ##
|
305 |
# st.pyplot(fig)
|
306 |
+
st.selectbox(label="Select dietary restriction", options=('None', 'Kosher', 'Vegitarian'), key="diet")
|
307 |
if st.session_state.diet != 'None':
|
308 |
if st.session_state.diet == 'Vegitarian':
|
309 |
out = [o for o in out if not any(ignore in o[0] for ignore in blacklists.vegitarian)]
|
310 |
+
if st.session_state.diet == 'Kosher':
|
311 |
out = [o for o in out if not any(ignore in o[0] for ignore in blacklists.kosher)]
|
312 |
names = [o[0] for o in out]
|
313 |
probs = [o[1] for o in out]
|
314 |
|
315 |
col1, col2, col3 = st.columns(3)
|
316 |
|
|
|
|
|
|
|
|
|
|
|
|
|
317 |
for i, name in enumerate(names):
|
318 |
search = py_un.search(type_='photos', query=name)
|
319 |
for photo in search.entries:
|
320 |
+
col_id = i % 3
|
321 |
if col_id == 0:
|
322 |
col1.image(photo.link_download, caption=name, use_column_width=True)
|
323 |
elif col_id == 1:
|
324 |
col2.image(photo.link_download, caption=name, use_column_width=True)
|
325 |
elif col_id == 2:
|
326 |
col3.image(photo.link_download, caption=name, use_column_width=True)
|
|
|
|
|
327 |
break
|
328 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
329 |
elif add_selectbox == "Food Donation Resources":
|
330 |
st.title('Food Donation Resources')
|
331 |
st.subheader('Pittsburgh Food Bank:')
|