OmPrakashSingh1704 commited on
Commit
c910c88
1 Parent(s): 580dd3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -58
app.py CHANGED
@@ -31,29 +31,31 @@ def get_recommendations(user_description, count_vectorizer, count_matrix):
31
  similar_indices = cosine_similarities.argsort()[::-1]
32
  return similar_indices
33
 
 
34
  def show_recipe(recipe):
35
- name_and_dis = f'# {recipe["name"]}\n\n'
36
- name_and_dis += f'{recipe["description"]}\n\n'
37
- ingredients = '## Ingredients:\n'
38
- instructions = '\n## Instructions:\n'
39
- for instruction in recipe["instructions"]:
40
- instructions += f"{instruction['step_number']}. {instruction['instruction']}\n"
41
-
42
- st.write(name_and_dis)
43
- col01, col02 = st.columns(2)
44
- with col01:
45
- cont = st.container(border=True, height=500)
46
- cont.write(ingredients)
47
- for j, i in enumerate(recipe["ingredients"]):
48
- cont.selectbox(i['name'],
49
- options=items_dict.iloc[get_recommendations(i['name'], cv, vectors)]["PRODUCT_NAME"].values,
50
- key=f"selectbox_{j}_{i['name']}{random.random()*100}")
51
- with col02:
52
- cont = st.container(border=True, height=500)
53
- cont.write(instructions)
54
-
55
 
56
  # Initialize the inference client for the Mixtral model
 
57
  if not mode:
58
  cook,saved=st.tabs(['COOK','SAVED'])
59
  with cook:
@@ -187,6 +189,7 @@ if not mode:
187
  st.session_state.exclusions = None
188
  st.session_state.serving_size = 2
189
  st.session_state.selected_difficulty = "Quick & Easy"
 
190
 
191
 
192
  st.title("Let's get cooking")
@@ -303,47 +306,47 @@ if not mode:
303
  st.success("Recipe Saved!")
304
  with saved:
305
  st.title("Saved Recipes")
306
- def load_saved_recipes_from_pickle(directory_path):
307
- os.makedirs('data', exist_ok=True)
308
- recipes = []
309
- # Iterate through all files in the directory
310
- for filename in os.listdir(directory_path):
311
- if filename.endswith('.pkl'):
312
- file_path = os.path.join(directory_path, filename)
313
- with open(file_path, 'rb') as file:
314
- recipe = pickle.load(file)
315
- recipes.append(recipe)
316
- return recipes
317
-
318
-
319
- # get all saved files
320
- directory_path = 'data'
321
- recipes = load_saved_recipes_from_pickle(directory_path)
322
- print(recipes)
323
-
324
- cols = st.columns([4, 1])
325
- with cols[1]:
326
- user_sort = st.selectbox("Sort", ('Recent', 'Oldest', 'A-Z', 'Z-A', 'Random'))
327
- if user_sort == 'Recent':
328
- recipes.sort(key=lambda x: x['timestamp'], reverse=True)
329
- elif user_sort == 'Oldest':
330
- recipes.sort(key=lambda x: x['timestamp'])
331
- elif user_sort == 'A-Z':
332
- recipes.sort(key=lambda x: x['name'])
333
- elif user_sort == 'Z-A':
334
- recipes.sort(key=lambda x: x['name'], reverse=True)
335
- elif user_sort == 'Random':
336
- recipes.sort(key=lambda x: x['file'])
337
- with cols[0]:
338
- user_search = st.selectbox("Search Recipes", [""] + [recipe['name'] for recipe in recipes])
339
- st.write("") # just some space
340
-
341
- if user_search!="":
342
- with st.spinner('LOADING YOUR RECIPE...'):
343
  st.divider()
344
  filtered_recipes = [recipe for recipe in recipes if recipe['name'] == user_search]
345
  if filtered_recipes:
346
  show_recipe(filtered_recipes[0])
347
  else:
348
  st.write("No recipe found.")
349
-
 
31
  similar_indices = cosine_similarities.argsort()[::-1]
32
  return similar_indices
33
 
34
+ @st.cache_data(show_spinner=True)
35
  def show_recipe(recipe):
36
+ with st.spinner("HANG TIGHT, RECIPE INCOMING..."):
37
+ name_and_dis = f'# {recipe["name"]}\n\n'
38
+ name_and_dis += f'{recipe["description"]}\n\n'
39
+ ingredients = '## Ingredients:\n'
40
+ instructions = '\n## Instructions:\n'
41
+ for instruction in recipe["instructions"]:
42
+ instructions += f"{instruction['step_number']}. {instruction['instruction']}\n"
43
+
44
+ st.write(name_and_dis)
45
+ col01, col02 = st.columns(2)
46
+ with col01:
47
+ cont = st.container(border=True, height=500)
48
+ cont.write(ingredients)
49
+ for j, i in enumerate(recipe["ingredients"]):
50
+ cont.selectbox(i['name'],
51
+ options=items_dict.iloc[get_recommendations(i['name'], cv, vectors)]["PRODUCT_NAME"].values,
52
+ key=f"selectbox_{j}_{i['name']}{random.random()*100}")
53
+ with col02:
54
+ cont = st.container(border=True, height=500)
55
+ cont.write(instructions)
56
 
57
  # Initialize the inference client for the Mixtral model
58
+
59
  if not mode:
60
  cook,saved=st.tabs(['COOK','SAVED'])
61
  with cook:
 
189
  st.session_state.exclusions = None
190
  st.session_state.serving_size = 2
191
  st.session_state.selected_difficulty = "Quick & Easy"
192
+ st.session_state.recipe=None
193
 
194
 
195
  st.title("Let's get cooking")
 
306
  st.success("Recipe Saved!")
307
  with saved:
308
  st.title("Saved Recipes")
309
+ with st.spinner('LOADING YOUR RECIPE...'):
310
+ def load_saved_recipes_from_pickle(directory_path):
311
+ os.makedirs('data', exist_ok=True)
312
+ recipes = []
313
+ # Iterate through all files in the directory
314
+ for filename in os.listdir(directory_path):
315
+ if filename.endswith('.pkl'):
316
+ file_path = os.path.join(directory_path, filename)
317
+ with open(file_path, 'rb') as file:
318
+ recipe = pickle.load(file)
319
+ recipes.append(recipe)
320
+ return recipes
321
+
322
+
323
+ # get all saved files
324
+ directory_path = 'data'
325
+ recipes = load_saved_recipes_from_pickle(directory_path)
326
+ print(recipes)
327
+
328
+ cols = st.columns([4, 1])
329
+ with cols[1]:
330
+ user_sort = st.selectbox("Sort", ('Recent', 'Oldest', 'A-Z', 'Z-A', 'Random'))
331
+ if user_sort == 'Recent':
332
+ recipes.sort(key=lambda x: x['timestamp'], reverse=True)
333
+ elif user_sort == 'Oldest':
334
+ recipes.sort(key=lambda x: x['timestamp'])
335
+ elif user_sort == 'A-Z':
336
+ recipes.sort(key=lambda x: x['name'])
337
+ elif user_sort == 'Z-A':
338
+ recipes.sort(key=lambda x: x['name'], reverse=True)
339
+ elif user_sort == 'Random':
340
+ recipes.sort(key=lambda x: x['file'])
341
+ with cols[0]:
342
+ user_search = st.selectbox("Search Recipes", [""] + [recipe['name'] for recipe in recipes])
343
+ st.write("") # just some space
344
+
345
+ if user_search!="":
346
  st.divider()
347
  filtered_recipes = [recipe for recipe in recipes if recipe['name'] == user_search]
348
  if filtered_recipes:
349
  show_recipe(filtered_recipes[0])
350
  else:
351
  st.write("No recipe found.")
352
+