coding6 commited on
Commit
3d429a8
·
verified ·
1 Parent(s): 7aff856

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +174 -0
  2. recipe_model.py +54 -0
  3. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #import streamlit as st
2
+ #from PIL import Image
3
+ #from recipe_model import predict_dish, generate_recipe
4
+
5
+ #st.set_page_config(page_title="AI Recipe Chef", layout="centered", page_icon="🍳")
6
+ #st.title("👨‍🍳 AI Recipe Chef")
7
+ #st.write("Upload any food image and get a real AI-generated recipe with filters.")
8
+ #st.markdown(
9
+ # "<h1 style='text-align: center; color: #F63366;'>👨‍🍳 AI Recipe Chef</h1>",
10
+ # unsafe_allow_html=True
11
+ #)
12
+ #st.markdown("<p style='text-align: center;'>Upload a food image and get a personalized recipe with filters!</p>", unsafe_allow_html=True)
13
+ #uploaded_image = st.file_uploader("Upload food image", type=["jpg", "jpeg", "png"])
14
+ #st.sidebar.title("🔍History")
15
+ #diet = st.selectbox("Dietary Preference", [
16
+ # "Any", "Vegetarian", "Non-Vegetarian", "Keto", "Gluten-Free", "Paleo"])
17
+
18
+ #cuisine = st.selectbox("Cuisine", ["Any","Indian", "Chinese", "Italian", "Mexican", "Meditarrean"])
19
+ #cook_time = st.selectbox("Cook Time", ["Any","<15 mins", "15-30 mins", ">30 mins","1 hour"])
20
+
21
+ #if uploaded_image:
22
+ # st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
23
+ # with st.spinner("Detecting dish..."):
24
+ # dish = predict_dish(uploaded_image)
25
+ # st.success(f"Detected Dish: {dish}")
26
+
27
+ # with st.spinner("Generating recipe..."):
28
+ # import time
29
+ # time.sleep(0.8)
30
+ # recipe = generate_recipe(dish, diet, cuisine, cook_time)
31
+
32
+ # st.subheader("📋 Ingredients & Instructions")
33
+ # st.markdown(recipe, unsafe_allow_html=True)
34
+ from __future__ import annotations
35
+ import streamlit as st
36
+ from PIL import Image
37
+ from typing import Dict, List
38
+
39
+
40
+
41
+ # ---------------- Dummy backend ----------------
42
+ def generate_recipes(img: Image.Image, filters: Dict[str, str]) -> List[Dict]:
43
+ """Return hard-coded recipes; swap with real model later."""
44
+ return [
45
+ {
46
+ "title": "Cheesy Veg Sandwich",
47
+ "diet": filters["diet"],
48
+ "cuisine": filters["cuisine"],
49
+ "cook_time": filters["cook_time"],
50
+ "ingredients": [
51
+ "2 bread slices",
52
+ "1 slice cheese",
53
+ "Chopped tomato",
54
+ "Chopped onion",
55
+ "Butter",
56
+ ],
57
+ "instructions": [
58
+ "Spread butter on bread.",
59
+ "Add cheese and veggies.",
60
+ "Grill or toast till golden.",
61
+ "Serve hot with ketchup.",
62
+ ],
63
+ },
64
+ {
65
+ "title": "Quick Egg Fried Rice",
66
+ "diet": filters["diet"],
67
+ "cuisine": "Chinese",
68
+ "cook_time": "<30 min",
69
+ "ingredients": [
70
+ "1 cup cooked rice",
71
+ "2 eggs",
72
+ "Soy sauce",
73
+ "Chopped spring onion",
74
+ "Salt & pepper",
75
+ ],
76
+ "instructions": [
77
+ "Scramble eggs in a pan.",
78
+ "Add rice and soy sauce.",
79
+ "Mix well with veggies.",
80
+ "Garnish with spring onion.",
81
+ ],
82
+ },
83
+ ]
84
+
85
+ # ---------------- UI ----------------
86
+ def main() -> None:
87
+
88
+ st.set_page_config(page_title="AI Recipe Generator", page_icon="🍳", layout="centered")
89
+ st.title("📸🍽️ AI-Powered Recipe Generator")
90
+
91
+ uploaded = st.file_uploader("Upload your fridge / pantry photo", type=["jpg", "jpeg", "png"])
92
+ if uploaded:
93
+ img = Image.open(uploaded)
94
+ st.image(img, caption="Uploaded image", use_column_width=True)
95
+
96
+ st.subheader("Filters")
97
+ col1, col2, col3 = st.columns(3)
98
+ with col1:
99
+ diet = st.selectbox("Diet", ["Any", "Vegetarian", "Vegan", "Keto", "Pescatarian", "Gluten-Free"])
100
+ with col2:
101
+ cuisine = st.selectbox("Cuisine", ["Any", "Indian", "Italian", "Mexican", "Chinese", "Mediterranean"])
102
+ with col3:
103
+ cook_time = st.selectbox("Cook time", ["Any", "<15 min", "<30 min", "<45 min", "1 hour+"])
104
+
105
+ if st.button("Generate Recipes", type="primary"):
106
+ with st.spinner("Chef-bot is thinking…"):
107
+ recs = generate_recipes(img, {"diet": diet, "cuisine": cuisine, "cook_time": cook_time})
108
+
109
+ if not recs:
110
+ st.warning("No recipes found — try relaxing a filter.")
111
+ else:
112
+ st.success(f"{len(recs)} recipe{'s' if len(recs)>1 else ''} found!")
113
+ show_recipes(recs)
114
+
115
+ if st.session_state.get("favourites"):
116
+ st.divider()
117
+ st.subheader("⭐ Saved favourites")
118
+ show_recipes(st.session_state["favourites"], favourite=True)
119
+ else:
120
+ st.info("👆 Upload an image to start.")
121
+
122
+ def show_recipes(recs: List[Dict], favourite: bool = False) -> None:
123
+ for i, r in enumerate(recs):
124
+ with st.expander(f"🍽️ {r['title']}"):
125
+ c1, c2, c3 = st.columns([2, 1, 1])
126
+ with c1:
127
+ st.markdown(f"**Diet:** {r['diet']}")
128
+ st.markdown(f"**Cuisine:** {r['cuisine']}")
129
+ st.markdown(f"**Time:** {r['cook_time']}")
130
+ with c2:
131
+ rating = st.slider("Rate", 1, 5, 3, key=f"rating_{i}_{favourite}")
132
+ st.session_state.setdefault("ratings", {})[r["title"]] = rating
133
+ with c3:
134
+ fav_key = f"fav_{i}_{favourite}"
135
+ if st.checkbox("❤️ Save", key=fav_key):
136
+ st.session_state.setdefault("favourites", []).append(r)
137
+
138
+ st.markdown("**Ingredients**")
139
+ st.markdown("\n".join(f"- {ing}" for ing in r["ingredients"]))
140
+ st.markdown("**Instructions**")
141
+ st.markdown("\n".join(f"{j+1}. {step}" for j, step in enumerate(r["instructions"])))
142
+
143
+ if __name__ == "__main__":
144
+ main()
145
+
146
+
147
+
148
+
149
+
150
+ # ---------- tiny helper to colour the diet tag ----------
151
+ def diet_badge(diet: str) -> str:
152
+ colors = {
153
+ "Vegetarian": "#34c759", # green
154
+ "Vegan": "#0a84ff", # blue
155
+ "Keto": "#ff9f0a", # orange
156
+ "Gluten-Free": "#ff375f", # pink
157
+ "Any": "#8e8e93", # grey
158
+ }
159
+ col = colors.get(diet, "#8e8e93")
160
+ return (
161
+ f"<span style='background:{col};color:white;"
162
+ "border-radius:4px;padding:2px 6px;font-size:0.85rem;'>"
163
+ f"{diet}</span>"
164
+ )
165
+ #if file:
166
+ # img = Image.open(file)
167
+ # st.image(img, caption="Your Image", use_column_width=True)
168
+ # with st.spinner("Thinking like a chef..."):
169
+ # dish = predict_dish(img)
170
+ # st.success(f"Detected Dish: **{dish}**")
171
+ # recipe = generate_recipe(dish, diet, cuisine, cook_time)
172
+ # st.subheader("📝 Recipe:")
173
+ # st.write(recipe)
174
+
recipe_model.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoProcessor, AutoModelForImageClassification, pipeline
2
+ from PIL import Image
3
+ import torch
4
+
5
+
6
+ def load_classification_model():
7
+ processor = AutoProcessor.from_pretrained("Shresthadev403/food-image-classification")
8
+ model = AutoModelForImageClassification.from_pretrained("Shresthadev403/food-image-classification")
9
+ return processor, model
10
+ def load_text_generator():
11
+ return pipeline( "text2text-generation",model="distilgpt2")
12
+ processor, model = load_classification_model()
13
+ text_generator = load_text_generator()
14
+
15
+
16
+ def predict_dish(image: Image.Image):
17
+ try:
18
+ image = Image.open(image).convert("RGB")
19
+ inputs = processor(images=image, return_tensors="pt")
20
+ with torch.no_grad():
21
+ outputs = model(**inputs)
22
+ logits = outputs.logits
23
+ predicted_class_idx = logits.argmax(-1).item()
24
+ label = model.config.id2label[predicted_class_idx]
25
+ return label.lower().replace(" ", "_")
26
+ except Exception as e:
27
+ print(f"❌ Error in dish prediction: {e}")
28
+ return "unknown_dish"
29
+
30
+
31
+
32
+ def generate_recipe(dish, diet=None, cuisine=None, cook_time=None):
33
+
34
+ filters = []
35
+ if diet and diet != "Any":
36
+ filters.append(f"{diet} diet")
37
+ if cuisine and cuisine != "Any":
38
+ filters.append(f"{cuisine} cuisine")
39
+ if cook_time and cook_time != "Any":
40
+ filters.append(f"ready in {cook_time}")
41
+ filter_text = ", ".join(filters)
42
+
43
+ prompt = f"""
44
+ Create a step-by-step recipe for {dish}.
45
+ Include:
46
+ - Ingredients with quantities
47
+ - Step-by-step instructions cooking steps
48
+ Make sure it's a {filter_text} recipe."""
49
+ try:
50
+ result = text_generator(prompt.strip(), max_length=282, do_sample=False)
51
+ return result[0]['generated_text']
52
+ except Exception as e:
53
+ print(f"❌ Error generating recipe: {e}")
54
+ return "Sorry, couldn't generate a recipe at the moment."
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ torch
3
+ torchvision
4
+ transformers
5
+ pillow