Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,7 @@ import os
|
|
11 |
import tempfile
|
12 |
from diffusers import StableDiffusionPipeline
|
13 |
import torch
|
14 |
-
|
15 |
|
16 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
17 |
|
@@ -51,43 +51,42 @@ def generate_dishes(ingredients, n=3, max_tokens=150, temperature=0.7):
|
|
51 |
dishes = [choice.text.strip() for choice in response.choices]
|
52 |
return dishes
|
53 |
|
54 |
-
def
|
55 |
-
truncated_dishes = [dish.split(':')[0] for dish in dishes[:3]]
|
56 |
-
|
57 |
model_id = "runwayml/stable-diffusion-v1-5"
|
58 |
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
59 |
-
pipe = pipe.to("cuda")
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
prompt = f"a photo of {dish}"
|
64 |
-
generated_image = pipe(prompt).images[0]
|
65 |
-
image = Image.fromarray((generated_image.permute(1, 2, 0).cpu().numpy() * 255).astype(np.uint8))
|
66 |
-
images.append(image)
|
67 |
|
68 |
-
|
|
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
st.title("Fridge to Dish App")
|
72 |
st.write("Upload an image of food ingredients in your fridge and get recipe suggestions!")
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
if
|
77 |
-
image = Image.open(
|
78 |
-
st.image(image, caption=
|
79 |
-
|
80 |
ingredients = extract_ingredients(image)
|
81 |
-
st.write(f"Ingredients detected: {', '.join(ingredients)}")
|
82 |
|
|
|
83 |
suggested_dishes = generate_dishes(ingredients)
|
84 |
-
st.write("Suggested dishes:")
|
85 |
-
st.write(suggested_dishes)
|
86 |
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
st.write("Generated images:")
|
92 |
-
for i in range(3):
|
93 |
-
st.image("placeholder.jpg", caption=f"Dish {i+1}")
|
|
|
11 |
import tempfile
|
12 |
from diffusers import StableDiffusionPipeline
|
13 |
import torch
|
14 |
+
import base64
|
15 |
|
16 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
17 |
|
|
|
51 |
dishes = [choice.text.strip() for choice in response.choices]
|
52 |
return dishes
|
53 |
|
54 |
+
def generate_image(prompt):
|
|
|
|
|
55 |
model_id = "runwayml/stable-diffusion-v1-5"
|
56 |
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
|
|
57 |
|
58 |
+
# If you have a GPU available, uncomment the following line
|
59 |
+
# pipe = pipe.to("cuda")
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
image = pipe(prompt).images[0]
|
62 |
+
return image
|
63 |
|
64 |
+
def get_image_download_link(image, filename, text):
|
65 |
+
buffered = BytesIO()
|
66 |
+
image.save(buffered, format="JPEG")
|
67 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
68 |
+
href = f'<a download="{filename}" href="data:image/jpeg;base64,{img_str}" target="_blank">{text}</a>'
|
69 |
+
return href
|
70 |
|
71 |
st.title("Fridge to Dish App")
|
72 |
st.write("Upload an image of food ingredients in your fridge and get recipe suggestions!")
|
73 |
|
74 |
+
# Upload the image and extract ingredients (use the appropriate function)
|
75 |
+
uploaded_image = st.file_uploader("Upload an image of your fridge", type=['jpg', 'jpeg'])
|
76 |
+
if uploaded_image:
|
77 |
+
image = Image.open(uploaded_image)
|
78 |
+
st.image(image, caption='Uploaded Image.', use_column_width=True)
|
|
|
79 |
ingredients = extract_ingredients(image)
|
|
|
80 |
|
81 |
+
# Generate dish suggestions
|
82 |
suggested_dishes = generate_dishes(ingredients)
|
|
|
|
|
83 |
|
84 |
+
for i, dish in enumerate(suggested_dishes):
|
85 |
+
st.write(f"Suggested Dish {i + 1}: {dish}")
|
86 |
+
|
87 |
+
if st.button(f"Generate Image for Dish {i + 1}"):
|
88 |
+
dish_image = generate_image(dish)
|
89 |
+
st.image(dish_image, caption=f'Generated Image for {dish}.', use_column_width=True)
|
90 |
|
91 |
+
download_link = get_image_download_link(dish_image, f"{dish}.jpg", f"Download {dish} Image")
|
92 |
+
st.markdown(download_link, unsafe_allow_html=True)
|
|
|
|
|
|