shukdevdatta123 commited on
Commit
1fde736
Β·
verified Β·
1 Parent(s): 7a23368

Update v2.txt

Browse files
Files changed (1) hide show
  1. v2.txt +55 -95
v2.txt CHANGED
@@ -14,25 +14,6 @@ def encode_image_to_base64(image_path):
14
  with open(image_path, "rb") as image_file:
15
  return base64.b64encode(image_file.read()).decode('utf-8')
16
 
17
- def save_uploaded_image(image):
18
- """Save uploaded image to a temporary file and return the path"""
19
- if image is None:
20
- return None
21
-
22
- with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:
23
- if isinstance(image, dict) and "path" in image:
24
- with open(image["path"], "rb") as img_file:
25
- temp_file.write(img_file.read())
26
- elif isinstance(image, Image.Image):
27
- image.save(temp_file.name, format="JPEG")
28
- else:
29
- try:
30
- Image.open(image).save(temp_file.name, format="JPEG")
31
- except Exception:
32
- return None
33
-
34
- return temp_file.name
35
-
36
  def analyze_single_image(client, img_path):
37
  """Analyze a single image to identify ingredients"""
38
  system_prompt = """You are a culinary expert AI assistant that specializes in identifying ingredients in images.
@@ -69,28 +50,14 @@ def analyze_single_image(client, img_path):
69
  except Exception as e:
70
  return f"Error analyzing image: {str(e)}"
71
 
72
- def get_recipe_suggestions(api_key, images, num_recipes=3, dietary_restrictions="None", cuisine_preference="Any"):
73
  """Get recipe suggestions based on the uploaded images of ingredients"""
74
  if not api_key:
75
  return "Please provide your Together API key."
76
 
77
- if not images or len(images) == 0 or all(img is None for img in images):
78
  return "Please upload at least one image of ingredients."
79
 
80
- valid_images = [img for img in images if img is not None]
81
-
82
- if len(valid_images) == 0:
83
- return "No valid images were uploaded. Please try again."
84
-
85
- image_paths = []
86
- for img in valid_images:
87
- img_path = save_uploaded_image(img)
88
- if img_path:
89
- image_paths.append(img_path)
90
-
91
- if not image_paths:
92
- return "Failed to process the uploaded images."
93
-
94
  try:
95
  client = Together(api_key=api_key)
96
 
@@ -136,12 +103,6 @@ def get_recipe_suggestions(api_key, images, num_recipes=3, dietary_restrictions=
136
  temperature=0.7
137
  )
138
 
139
- for img_path in image_paths:
140
- try:
141
- os.unlink(img_path)
142
- except:
143
- pass
144
-
145
  result = "## πŸ“‹ Ingredients Identified\n\n"
146
  result += combined_ingredients
147
  result += "\n\n---\n\n"
@@ -149,27 +110,34 @@ def get_recipe_suggestions(api_key, images, num_recipes=3, dietary_restrictions=
149
  result += response.choices[0].message.content
150
 
151
  return result
152
-
153
  except Exception as e:
154
- for img_path in image_paths:
155
- try:
156
- os.unlink(img_path)
157
- except:
158
- pass
159
  return f"Error: {str(e)}"
160
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  custom_css = """
162
  @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');
163
  :root {
164
- --primary-color: #E67E22;
165
- --primary-dark: #D35400;
166
- --secondary-color: #2ECC71;
167
- --accent-color: #F1C40F;
168
- --background-color: #FFFFFF;
169
- --text-color: #4A4A4A;
170
- --card-background: #F8F9FA;
171
- --border-radius: 10px;
172
  --font-family: 'Poppins', sans-serif;
 
 
173
  }
174
  body {
175
  font-family: var(--font-family);
@@ -186,31 +154,31 @@ body {
186
  .app-header {
187
  background-color: var(--primary-color);
188
  color: white;
189
- padding: 40px 20px;
190
  text-align: center;
191
- border-radius: 0 0 20px 20px;
192
- box-shadow: 0 4px 10px rgba(0,0,0,0.1);
193
  }
194
  .app-title {
195
- font-size: 2.5em;
196
- margin-bottom: 10px;
197
  font-weight: 700;
 
198
  }
199
  .app-subtitle {
200
- font-size: 1.2em;
201
  font-weight: 300;
202
- max-width: 600px;
203
  margin: 0 auto;
204
  }
205
  .input-section, .output-section {
206
  background-color: var(--card-background);
207
  border-radius: var(--border-radius);
208
  padding: 30px;
209
- box-shadow: 0 4px 15px rgba(0,0,0,0.05);
210
  margin-bottom: 30px;
211
  }
212
  .section-header {
213
- font-size: 1.5em;
214
  font-weight: 600;
215
  color: var(--text-color);
216
  margin-bottom: 20px;
@@ -222,60 +190,63 @@ body {
222
  border-radius: var(--border-radius);
223
  padding: 40px;
224
  text-align: center;
225
- background-color: rgba(46, 204, 113, 0.05);
226
  transition: all 0.3s ease;
227
  }
228
  .image-upload-container:hover {
229
  border-color: var(--primary-color);
230
- background-color: rgba(230, 126, 34, 0.05);
231
  }
232
  button.primary-button {
233
  background-color: var(--primary-color);
234
  color: white;
235
  border: none;
236
- padding: 15px 30px;
237
- border-radius: 5px;
238
  font-size: 1.1em;
239
  cursor: pointer;
240
  transition: all 0.3s ease;
241
- box-shadow: 0 2px 5px rgba(0,0,0,0.1);
242
  width: 100%;
 
243
  }
244
  button.primary-button:hover {
245
- background-color: var(--primary-dark);
246
- box-shadow: 0 4px 10px rgba(0,0,0,0.15);
247
  }
248
  .gallery-container {
249
  display: grid;
250
- grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
251
- gap: 15px;
252
- margin-top: 20px;
253
  }
254
  .gallery-item {
255
- border-radius: 8px;
256
  overflow: hidden;
257
- box-shadow: 0 2px 5px rgba(0,0,0,0.1);
258
  transition: transform 0.3s ease;
 
 
259
  }
260
  .gallery-item:hover {
261
  transform: scale(1.05);
 
262
  }
263
  .recipe-output {
264
- font-size: 1.1em;
265
- line-height: 1.6;
266
  color: var(--text-color);
267
  max-height: 600px;
268
  overflow-y: auto;
269
- padding-right: 10px;
270
  }
271
  .recipe-output h2 {
272
  color: var(--primary-color);
273
  margin-top: 30px;
274
- font-size: 1.8em;
275
  }
276
  .recipe-output h3 {
277
  color: var(--secondary-color);
278
- font-size: 1.4em;
279
  margin-top: 20px;
280
  }
281
  .loading-container {
@@ -312,7 +283,7 @@ button.primary-button:hover {
312
  }
313
  .loading-text {
314
  color: white;
315
- font-size: 1.2em;
316
  margin-top: 20px;
317
  }
318
  .footer {
@@ -320,8 +291,8 @@ button.primary-button:hover {
320
  padding: 40px 20px;
321
  text-align: center;
322
  color: var(--text-color);
323
- font-size: 1em;
324
- box-shadow: 0 -2px 10px rgba(0,0,0,0.05);
325
  }
326
  .footer-content {
327
  max-width: 800px;
@@ -472,19 +443,8 @@ with gr.Blocks(css=custom_css) as app:
472
 
473
  gr.HTML(html_footer)
474
 
475
- def update_gallery(files):
476
- if not files:
477
- return gr.Gallery.update(visible=False)
478
- return gr.Gallery.update(value=[file.name for file in files], visible=True)
479
-
480
  file_upload.change(fn=update_gallery, inputs=file_upload, outputs=image_input)
481
 
482
- def process_recipe_request(api_key, files, num_recipes, dietary_restrictions, cuisine_preference):
483
- if not files:
484
- return "Please upload at least one image of ingredients."
485
- images = [file.name for file in files]
486
- return get_recipe_suggestions(api_key, images, num_recipes, dietary_restrictions, cuisine_preference)
487
-
488
  submit_button.click(
489
  fn=process_recipe_request,
490
  inputs=[api_key_input, file_upload, num_recipes, dietary_restrictions, cuisine_preference],
 
14
  with open(image_path, "rb") as image_file:
15
  return base64.b64encode(image_file.read()).decode('utf-8')
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def analyze_single_image(client, img_path):
18
  """Analyze a single image to identify ingredients"""
19
  system_prompt = """You are a culinary expert AI assistant that specializes in identifying ingredients in images.
 
50
  except Exception as e:
51
  return f"Error analyzing image: {str(e)}"
52
 
53
+ def get_recipe_suggestions(api_key, image_paths, num_recipes=3, dietary_restrictions="None", cuisine_preference="Any"):
54
  """Get recipe suggestions based on the uploaded images of ingredients"""
55
  if not api_key:
56
  return "Please provide your Together API key."
57
 
58
+ if not image_paths or len(image_paths) == 0:
59
  return "Please upload at least one image of ingredients."
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  try:
62
  client = Together(api_key=api_key)
63
 
 
103
  temperature=0.7
104
  )
105
 
 
 
 
 
 
 
106
  result = "## πŸ“‹ Ingredients Identified\n\n"
107
  result += combined_ingredients
108
  result += "\n\n---\n\n"
 
110
  result += response.choices[0].message.content
111
 
112
  return result
 
113
  except Exception as e:
 
 
 
 
 
114
  return f"Error: {str(e)}"
115
 
116
+ def update_gallery(files):
117
+ """Update the gallery with uploaded image paths"""
118
+ if not files or len(files) == 0:
119
+ return gr.update(visible=False)
120
+ return gr.update(value=files, visible=True)
121
+
122
+ def process_recipe_request(api_key, files, num_recipes, dietary_restrictions, cuisine_preference):
123
+ """Process the recipe request with uploaded files"""
124
+ if not files:
125
+ return "Please upload at least one image of ingredients."
126
+ return get_recipe_suggestions(api_key, files, num_recipes, dietary_restrictions, cuisine_preference)
127
+
128
  custom_css = """
129
  @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');
130
  :root {
131
+ --primary-color: #FF6F61; /* Warm coral */
132
+ --secondary-color: #4BB543; /* Fresh green */
133
+ --accent-color: #F0A500; /* Golden yellow */
134
+ --background-color: #F4F4F9; /* Light grayish background */
135
+ --text-color: #333333; /* Dark gray text */
136
+ --card-background: #FFFFFF; /* White for cards */
137
+ --border-radius: 12px;
 
138
  --font-family: 'Poppins', sans-serif;
139
+ --box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 20px;
140
+ --hover-shadow: rgba(0, 0, 0, 0.15) 0px 8px 30px;
141
  }
142
  body {
143
  font-family: var(--font-family);
 
154
  .app-header {
155
  background-color: var(--primary-color);
156
  color: white;
157
+ padding: 60px 20px;
158
  text-align: center;
159
+ border-radius: 0 0 30px 30px;
160
+ box-shadow: var(--box-shadow);
161
  }
162
  .app-title {
163
+ font-size: 2.8em;
 
164
  font-weight: 700;
165
+ margin-bottom: 10px;
166
  }
167
  .app-subtitle {
168
+ font-size: 1.3em;
169
  font-weight: 300;
170
+ max-width: 800px;
171
  margin: 0 auto;
172
  }
173
  .input-section, .output-section {
174
  background-color: var(--card-background);
175
  border-radius: var(--border-radius);
176
  padding: 30px;
177
+ box-shadow: var(--box-shadow);
178
  margin-bottom: 30px;
179
  }
180
  .section-header {
181
+ font-size: 1.6em;
182
  font-weight: 600;
183
  color: var(--text-color);
184
  margin-bottom: 20px;
 
190
  border-radius: var(--border-radius);
191
  padding: 40px;
192
  text-align: center;
193
+ background-color: rgba(75, 181, 67, 0.1);
194
  transition: all 0.3s ease;
195
  }
196
  .image-upload-container:hover {
197
  border-color: var(--primary-color);
198
+ background-color: rgba(255, 111, 97, 0.1);
199
  }
200
  button.primary-button {
201
  background-color: var(--primary-color);
202
  color: white;
203
  border: none;
204
+ padding: 16px 32px;
205
+ border-radius: 6px;
206
  font-size: 1.1em;
207
  cursor: pointer;
208
  transition: all 0.3s ease;
 
209
  width: 100%;
210
+ box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
211
  }
212
  button.primary-button:hover {
213
+ background-color: #E15F52;
214
+ box-shadow: var(--hover-shadow);
215
  }
216
  .gallery-container {
217
  display: grid;
218
+ grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
219
+ gap: 20px;
220
+ margin-top: 30px;
221
  }
222
  .gallery-item {
223
+ border-radius: var(--border-radius);
224
  overflow: hidden;
225
+ box-shadow: var(--box-shadow);
226
  transition: transform 0.3s ease;
227
+ aspect-ratio: 1 / 1;
228
+ object-fit: cover;
229
  }
230
  .gallery-item:hover {
231
  transform: scale(1.05);
232
+ box-shadow: var(--hover-shadow);
233
  }
234
  .recipe-output {
235
+ font-size: 1.2em;
236
+ line-height: 1.7;
237
  color: var(--text-color);
238
  max-height: 600px;
239
  overflow-y: auto;
240
+ padding-right: 15px;
241
  }
242
  .recipe-output h2 {
243
  color: var(--primary-color);
244
  margin-top: 30px;
245
+ font-size: 2em;
246
  }
247
  .recipe-output h3 {
248
  color: var(--secondary-color);
249
+ font-size: 1.5em;
250
  margin-top: 20px;
251
  }
252
  .loading-container {
 
283
  }
284
  .loading-text {
285
  color: white;
286
+ font-size: 1.3em;
287
  margin-top: 20px;
288
  }
289
  .footer {
 
291
  padding: 40px 20px;
292
  text-align: center;
293
  color: var(--text-color);
294
+ font-size: 1.1em;
295
+ box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
296
  }
297
  .footer-content {
298
  max-width: 800px;
 
443
 
444
  gr.HTML(html_footer)
445
 
 
 
 
 
 
446
  file_upload.change(fn=update_gallery, inputs=file_upload, outputs=image_input)
447
 
 
 
 
 
 
 
448
  submit_button.click(
449
  fn=process_recipe_request,
450
  inputs=[api_key_input, file_upload, num_recipes, dietary_restrictions, cuisine_preference],