stchakman commited on
Commit
ae3548a
·
1 Parent(s): 92f3574

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -33,12 +33,13 @@ def extract_ingredients(uploaded_image):
33
 
34
  image = Image.open(temp_file.name)
35
  preds = classifier(temp_file.name)
36
- ingredients = [label for score, label in preds]
37
 
38
  temp_file.close()
39
  os.unlink(temp_file.name)
40
  return ingredients
41
 
 
42
  def generate_dishes(ingredients, n=3, max_tokens=150, temperature=0.7):
43
  ingredients_str = ', '.join(ingredients)
44
  prompt = f"I have {ingredients_str} Please return the name of a dish I can make followed by instructions on how to prepare that dish"
@@ -55,12 +56,11 @@ def generate_dishes(ingredients, n=3, max_tokens=150, temperature=0.7):
55
  return dishes
56
 
57
  model_id = "runwayml/stable-diffusion-v1-5"
58
-
59
  def generate_image(prompt):
60
  with st.spinner("Generating image..."):
61
  pipe = StableDiffusionPipeline.from_pretrained(model_id)
62
  # If you have a GPU available, uncomment the following line
63
- pipe = pipe.to("cuda")
64
  image = pipe(prompt).images[0]
65
  return image
66
 
@@ -73,20 +73,23 @@ def get_image_download_link(image, filename, text):
73
 
74
  st.title("Fridge to Dish App")
75
 
76
- uploaded_image = st.file_uploader("Upload an image of your fridge", type=["jpg", "jpeg"])
77
-
78
- if uploaded_image is not None:
79
- ingredients = extract_ingredients(uploaded_image)
80
- st.write(f"Identified ingredients: {', '.join(ingredients)}")
81
-
82
  suggested_dishes = generate_dishes(ingredients)
83
- st.write("Suggested dishes:")
84
-
85
- for idx, dish in enumerate(suggested_dishes):
86
- st.write(f"{idx + 1}. {dish}")
87
-
88
- selected_dish = st.radio("Select a dish to generate an image", suggested_dishes)
89
 
90
- if selected_dish:
91
- generated_image = generate_image(selected_dish)
92
- st.image(generated_image, caption=selected_dish, use_column_width=True)
 
 
 
 
 
 
 
 
 
 
33
 
34
  image = Image.open(temp_file.name)
35
  preds = classifier(temp_file.name)
36
+ ingredients = [pred["label"] for pred in preds]
37
 
38
  temp_file.close()
39
  os.unlink(temp_file.name)
40
  return ingredients
41
 
42
+
43
  def generate_dishes(ingredients, n=3, max_tokens=150, temperature=0.7):
44
  ingredients_str = ', '.join(ingredients)
45
  prompt = f"I have {ingredients_str} Please return the name of a dish I can make followed by instructions on how to prepare that dish"
 
56
  return dishes
57
 
58
  model_id = "runwayml/stable-diffusion-v1-5"
 
59
  def generate_image(prompt):
60
  with st.spinner("Generating image..."):
61
  pipe = StableDiffusionPipeline.from_pretrained(model_id)
62
  # If you have a GPU available, uncomment the following line
63
+ # pipe = pipe.to("cuda")
64
  image = pipe(prompt).images[0]
65
  return image
66
 
 
73
 
74
  st.title("Fridge to Dish App")
75
 
76
+ uploaded_file = st.file_uploader("Upload an image of your ingredients", type=["jpg", "jpeg", "png"])
77
+ if uploaded_file is not None:
78
+ ingredients = extract_ingredients(uploaded_file)
79
+ st.write("Ingredients found:")
80
+ st.write(", ".join(ingredients))
81
+
82
  suggested_dishes = generate_dishes(ingredients)
 
 
 
 
 
 
83
 
84
+ if len(suggested_dishes) > 0:
85
+ st.write("Suggested dishes based on the ingredients:")
86
+ for idx, dish in enumerate(suggested_dishes):
87
+ st.write(f"{idx + 1}. {dish['name']}")
88
+
89
+ for idx, dish in enumerate(suggested_dishes[:3]):
90
+ if st.button(f"Generate Image for Dish {idx + 1}"):
91
+ dish_image = generate_image(dish['name'])
92
+ st.image(dish_image, caption=dish['name'], use_column_width=True)
93
+ else:
94
+ st.write("No dishes found for the given ingredients.")
95
+