Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
from torchvision.transforms import Compose, Resize, CenterCrop, ToTensor, Normalize
|
6 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
7 |
+
from transformers import pipeline
|
8 |
+
import openai
|
9 |
+
from io import BytesIO
|
10 |
+
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 |
+
|
18 |
+
# Load models and set up GPT-3 pipeline
|
19 |
+
extractor = AutoFeatureExtractor.from_pretrained("stchakman/Fridge_Items_Model")
|
20 |
+
model = AutoModelForImageClassification.from_pretrained("stchakman/Fridge_Items_Model")
|
21 |
+
#gpt3 = pipeline("text-davinci-003", api_key="your_openai_api_key")
|
22 |
+
|
23 |
+
# Map indices to ingredient names
|
24 |
+
term_variables = { "Apples", "Asparagus", "Avocado", "Bananas", "BBQ sauce", "Beans", "Beef", "Beer", "Berries", "Bison", "Bread", "Broccoli", "Cauliflower", "Celery", "Cheese", "Chicken", "Chocolate", "Citrus fruits", "Clams", "Cold cuts", "Corn", "Cottage cheese", "Crab", "Cream", "Cream cheese", "Cucumbers", "Duck", "Eggs", "Energy drinks", "Fish", "Frozen vegetables", "Frozen meals", "Garlic", "Grapes", "Ground beef", "Ground chicken", "Ham", "Hot sauce", "Hummus", "Ice cream", "Jams", "Jerky", "Kiwi", "Lamb", "Lemons", "Lobster", "Mangoes", "Mayonnaise", "Melons", "Milk", "Mussels", "Mustard", "Nectarines", "Onions", "Oranges", "Peaches", "Peas", "Peppers", "Pineapple", "Pizza", "Plums", "Pork", "Potatoes", "Salad dressings", "Salmon", "Shrimp", "Sour cream", "Soy sauce", "Spinach", "Squash", "Steak", "Sweet potatoes", "Frozen Fruits", "Tilapia", "Tomatoes", "Tuna", "Turkey", "Venison", "Water bottles", "Wine", "Yogurt", "Zucchini" }
|
25 |
+
ingredient_names = list(term_variables)
|
26 |
+
|
27 |
+
classifier = pipeline("image-classification", model="stchakman/Fridge_Items_Model")
|
28 |
+
|
29 |
+
def extract_ingredients(image):
|
30 |
+
# Save the PIL Image as a temporary file
|
31 |
+
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
|
32 |
+
image.save(temp_file, format="JPEG")
|
33 |
+
temp_file_path = temp_file.name
|
34 |
+
|
35 |
+
preds = classifier(temp_file_path)
|
36 |
+
predictions = [pred["label"] for pred in preds]
|
37 |
+
return [prediction for prediction in predictions if prediction in ingredient_names]
|
38 |
+
|
39 |
+
def generate_dishes(ingredients, n=3, max_tokens=150, temperature=0.7):
|
40 |
+
ingredients_str = ', '.join(ingredients)
|
41 |
+
prompt = f"I have {ingredients_str} Please return the name of a dish I can make followed by intructions on how to prepare that dish "
|
42 |
+
|
43 |
+
response = openai.Completion.create(
|
44 |
+
model="text-davinci-003",
|
45 |
+
prompt=prompt,
|
46 |
+
max_tokens=max_tokens,
|
47 |
+
temperature=temperature,
|
48 |
+
n=n
|
49 |
+
)
|
50 |
+
|
51 |
+
dishes = [choice.text.strip() for choice in response.choices]
|
52 |
+
return dishes
|
53 |
+
|
54 |
+
def generate_images(dishes):
|
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 |
+
images = []
|
62 |
+
for dish in truncated_dishes:
|
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 |
+
return images
|
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 |
+
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
75 |
+
|
76 |
+
if uploaded_file is not None:
|
77 |
+
image = Image.open(uploaded_file)
|
78 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
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 |
+
dish_images = generate_images(suggested_dishes)
|
88 |
+
|
89 |
+
# Display dish images in a grid
|
90 |
+
# Replace the following lines with code to display generated images
|
91 |
+
st.write("Generated images:")
|
92 |
+
for i in range(3):
|
93 |
+
st.image("placeholder.jpg", caption=f"Dish {i+1}")
|