Spaces:
Running
Running
Ashhar
commited on
Commit
Β·
80ff985
1
Parent(s):
15d3a17
first push
Browse files- .gitignore +10 -0
- app.py +245 -0
- requirements.txt +9 -0
.gitignore
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.env
|
2 |
+
.venv
|
3 |
+
__pycache__/
|
4 |
+
.gitattributes
|
5 |
+
gradio_cached_examples/
|
6 |
+
app_*.py
|
7 |
+
soup_dump*.html
|
8 |
+
soup_dump.html
|
9 |
+
system_prompt.txt
|
10 |
+
scratch.py
|
app.py
ADDED
@@ -0,0 +1,245 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import streamlit as st
|
3 |
+
from openai import OpenAI
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Load environment variables
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
11 |
+
|
12 |
+
# Set up page configuration
|
13 |
+
st.set_page_config(
|
14 |
+
page_title="Magic Recipe Decoder π½οΈ",
|
15 |
+
page_icon="π₯",
|
16 |
+
layout="wide",
|
17 |
+
initial_sidebar_state="expanded"
|
18 |
+
)
|
19 |
+
# Custom CSS for styling
|
20 |
+
st.markdown("""
|
21 |
+
<style>
|
22 |
+
.big-font {
|
23 |
+
font-size:20px !important;
|
24 |
+
color: #2C3E50;
|
25 |
+
}
|
26 |
+
.highlight-box {
|
27 |
+
background-color: #F5F5F5; /* Soft light gray */
|
28 |
+
border-radius: 10px;
|
29 |
+
padding: 20px;
|
30 |
+
margin-bottom: 20px;
|
31 |
+
color: #333333; /* Very dark gray for text */
|
32 |
+
border: 1px solid #E0E0E0; /* Subtle border */
|
33 |
+
}
|
34 |
+
.highlight-box h1 {
|
35 |
+
color: #1A5F7A; /* Deep teal for main heading */
|
36 |
+
font-size: 24px;
|
37 |
+
margin-bottom: 15px;
|
38 |
+
}
|
39 |
+
.highlight-box h2 {
|
40 |
+
color: #2C7DA0; /* Slightly lighter teal for subheadings */
|
41 |
+
font-size: 20px;
|
42 |
+
margin-top: 15px;
|
43 |
+
margin-bottom: 10px;
|
44 |
+
}
|
45 |
+
.highlight-box h3 {
|
46 |
+
color: #468FAF; /* Even lighter teal for smaller headings */
|
47 |
+
font-size: 18px;
|
48 |
+
margin-top: 10px;
|
49 |
+
margin-bottom: 8px;
|
50 |
+
}
|
51 |
+
.highlight-box p {
|
52 |
+
color: #333333; /* Dark gray for paragraphs */
|
53 |
+
line-height: 1.6;
|
54 |
+
}
|
55 |
+
</style>
|
56 |
+
""", unsafe_allow_html=True)
|
57 |
+
|
58 |
+
# Initialize session state
|
59 |
+
if 'cooking_equipment' not in st.session_state:
|
60 |
+
st.session_state.cooking_equipment = {
|
61 |
+
'Stove': True,
|
62 |
+
'Oven': False,
|
63 |
+
'Microwave': False,
|
64 |
+
'Blender': False,
|
65 |
+
'Pressure Cooker': False
|
66 |
+
}
|
67 |
+
if 'original_recipe' not in st.session_state:
|
68 |
+
st.session_state.original_recipe = None
|
69 |
+
|
70 |
+
|
71 |
+
def google_image_search(query):
|
72 |
+
"""Placeholder for image search - you'll need to implement actual image search API"""
|
73 |
+
return "https://via.placeholder.com/300x200.png?text=" + query.replace(" ", "+")
|
74 |
+
|
75 |
+
|
76 |
+
def analyze_and_generate_recipe(uploaded_image, available_equipment=None):
|
77 |
+
"""Analyze food image and generate recipe in a single LLM call"""
|
78 |
+
with st.spinner('π³ Analyzing your delicious image and generating recipe...'):
|
79 |
+
try:
|
80 |
+
# Prepare the system and user messages
|
81 |
+
messages = [
|
82 |
+
{
|
83 |
+
"role": "system",
|
84 |
+
"content": """You are a professional chef and food analyst.
|
85 |
+
When analyzing a food image, provide a comprehensive recipe that considers:
|
86 |
+
1. Detailed food description
|
87 |
+
2. Complete ingredient list
|
88 |
+
3. Cooking method
|
89 |
+
4. Step-by-step instructions"""
|
90 |
+
},
|
91 |
+
{
|
92 |
+
"role": "user",
|
93 |
+
"content": [
|
94 |
+
{
|
95 |
+
"type": "image_url",
|
96 |
+
"image_url": {"url": f"data:image/jpeg;base64,{uploaded_image}"}
|
97 |
+
},
|
98 |
+
{
|
99 |
+
"type": "text",
|
100 |
+
"text": f"""Analyze this food image and generate a detailed recipe.
|
101 |
+
|
102 |
+
{'Available cooking equipment: ' + ', '.join(available_equipment) if available_equipment else 'No equipment restrictions'}
|
103 |
+
|
104 |
+
If specific equipment is available, prioritize cooking methods that use those tools.
|
105 |
+
|
106 |
+
Provide:
|
107 |
+
- Detailed food description
|
108 |
+
- Ingredient list
|
109 |
+
- Cooking method adapted to available equipment
|
110 |
+
- Precise, step-by-step instructions
|
111 |
+
- Difficulty level
|
112 |
+
- Estimated cooking time
|
113 |
+
|
114 |
+
Use markdown formatting for clear presentation."""
|
115 |
+
}
|
116 |
+
]
|
117 |
+
}
|
118 |
+
]
|
119 |
+
|
120 |
+
# Make the LLM call
|
121 |
+
response = client.chat.completions.create(
|
122 |
+
model="gpt-4o-mini",
|
123 |
+
messages=messages
|
124 |
+
)
|
125 |
+
|
126 |
+
return response.choices[0].message.content
|
127 |
+
except Exception as e:
|
128 |
+
st.error(f"Error analyzing image and generating recipe: {e}")
|
129 |
+
return None
|
130 |
+
|
131 |
+
|
132 |
+
def refine_recipe(original_recipe, user_refinement):
|
133 |
+
"""Refine the recipe based on user input"""
|
134 |
+
try:
|
135 |
+
response = client.chat.completions.create(
|
136 |
+
model="gpt-4o-mini",
|
137 |
+
messages=[
|
138 |
+
{
|
139 |
+
"role": "system",
|
140 |
+
"content": "You are a professional chef who can modify recipes based on specific user preferences."
|
141 |
+
},
|
142 |
+
{
|
143 |
+
"role": "user",
|
144 |
+
"content": f"""Original Recipe:
|
145 |
+
{original_recipe}
|
146 |
+
|
147 |
+
User Refinement Request: {user_refinement}
|
148 |
+
|
149 |
+
Please modify the recipe according to the user's preferences.
|
150 |
+
Provide the updated recipe with clear instructions,
|
151 |
+
maintaining the original recipe's core structure."""
|
152 |
+
}
|
153 |
+
]
|
154 |
+
)
|
155 |
+
|
156 |
+
return response.choices[0].message.content
|
157 |
+
except Exception as e:
|
158 |
+
st.error(f"Error refining recipe: {e}")
|
159 |
+
return None
|
160 |
+
|
161 |
+
|
162 |
+
# Main Streamlit App
|
163 |
+
st.title("π₯ Magic Recipe Decoder")
|
164 |
+
st.markdown("*Discover the secrets behind your favorite dishes!*", unsafe_allow_html=True)
|
165 |
+
|
166 |
+
# Sidebar for Cooking Equipment
|
167 |
+
st.sidebar.header("π§ Cooking Equipment")
|
168 |
+
st.sidebar.markdown("Check the equipment you have available:")
|
169 |
+
|
170 |
+
for equipment, available in st.session_state.cooking_equipment.items():
|
171 |
+
st.session_state.cooking_equipment[equipment] = st.sidebar.checkbox(equipment, value=available)
|
172 |
+
|
173 |
+
# Image Upload and Analysis Section
|
174 |
+
st.markdown("### πΈ Upload Your Food Image", unsafe_allow_html=True)
|
175 |
+
uploaded_file = st.file_uploader("Choose an image...", type=['jpg', 'jpeg', 'png'])
|
176 |
+
|
177 |
+
# Food Analysis and Recipe Generation
|
178 |
+
if uploaded_file is not None:
|
179 |
+
# Display uploaded image
|
180 |
+
col1, col2 = st.columns(2)
|
181 |
+
|
182 |
+
with col1:
|
183 |
+
st.image(uploaded_file, caption='Uploaded Image', use_column_width=True)
|
184 |
+
|
185 |
+
with col2:
|
186 |
+
# Checkbox to use available cooking equipment
|
187 |
+
use_available_equipment = st.checkbox("Use only available cooking equipment", value=False)
|
188 |
+
|
189 |
+
# Prepare available equipment list if checkbox is selected
|
190 |
+
available_equipment = []
|
191 |
+
if use_available_equipment:
|
192 |
+
available_equipment = [
|
193 |
+
equip for equip, available in st.session_state.cooking_equipment.items()
|
194 |
+
if available
|
195 |
+
]
|
196 |
+
|
197 |
+
# Analyze and Generate Recipe
|
198 |
+
if st.button("Generate Recipe"):
|
199 |
+
# Analyze image and generate recipe
|
200 |
+
image_base64 = base64.b64encode(uploaded_file.getvalue()).decode('utf-8')
|
201 |
+
recipe = analyze_and_generate_recipe(
|
202 |
+
image_base64,
|
203 |
+
available_equipment if use_available_equipment else None
|
204 |
+
)
|
205 |
+
|
206 |
+
if recipe:
|
207 |
+
# Store original recipe in session state
|
208 |
+
st.session_state.original_recipe = recipe
|
209 |
+
|
210 |
+
# Display the generated recipe
|
211 |
+
st.markdown("### π³ Generated Recipe", unsafe_allow_html=True)
|
212 |
+
st.markdown(f"<div class='highlight-box'>{recipe}</div>", unsafe_allow_html=True)
|
213 |
+
|
214 |
+
# Recipe Refinement Section
|
215 |
+
if st.session_state.original_recipe:
|
216 |
+
st.markdown("### π§βπ³ Refine Your Recipe", unsafe_allow_html=True)
|
217 |
+
|
218 |
+
# Refinement Prompt
|
219 |
+
user_refinement = st.text_input("Want to modify the recipe? Add your preferences here:")
|
220 |
+
|
221 |
+
if st.button("Refine Recipe"):
|
222 |
+
if user_refinement:
|
223 |
+
# Refine the recipe
|
224 |
+
with st.spinner('πͺ Refining your recipe...'):
|
225 |
+
refined_recipe = refine_recipe(
|
226 |
+
st.session_state.original_recipe,
|
227 |
+
user_refinement
|
228 |
+
)
|
229 |
+
|
230 |
+
if refined_recipe:
|
231 |
+
# Display the refined recipe
|
232 |
+
st.markdown("### π½οΈ Refined Recipe", unsafe_allow_html=True)
|
233 |
+
st.markdown(f"<div class='highlight-box'>{refined_recipe}</div>", unsafe_allow_html=True)
|
234 |
+
else:
|
235 |
+
st.warning("Please enter refinement preferences.")
|
236 |
+
|
237 |
+
# Visual References
|
238 |
+
st.markdown("### πΌοΈ Visual References", unsafe_allow_html=True)
|
239 |
+
if st.session_state.original_recipe:
|
240 |
+
food_name = st.session_state.original_recipe.split('\n')[0]
|
241 |
+
image_urls = [google_image_search(food_name) for _ in range(3)]
|
242 |
+
|
243 |
+
cols = st.columns(3)
|
244 |
+
for i, url in enumerate(image_urls):
|
245 |
+
cols[i].image(url, use_column_width=True)
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# streamlit
|
2 |
+
# pandas
|
3 |
+
|
4 |
+
python-dotenv
|
5 |
+
# groq
|
6 |
+
openai
|
7 |
+
transformers
|
8 |
+
# praw
|
9 |
+
# anthropic
|