agung mofdac commited on
Commit
9e972a3
1 Parent(s): 343181f

Update Classification_And_Recipes.py

Browse files
Files changed (1) hide show
  1. Classification_And_Recipes.py +408 -408
Classification_And_Recipes.py CHANGED
@@ -14,7 +14,7 @@ import re
14
 
15
 
16
  st.set_page_config(layout='wide',
17
- page_title='Food Classification & Recipes Recommender'
18
  )
19
 
20
  # st.sidebar.markdown("<h3 style='text-align: center;'>Project Location:</h3>", unsafe_allow_html=True)
@@ -61,434 +61,434 @@ def main():
61
  # st.image(image, width=260)
62
 
63
  # select_health = st.radio("Select One (Not Functional Yet):", ["Regular", "Low-Calorie"], horizontal=True)
64
- menu = st.sidebar.selectbox('Pilih Menu',['Prediksi Makanan', 'Rekomendasi Resep'])
65
- if menu == 'Prediksi Makanan':
66
 
67
- uploaded_file = st.file_uploader("Upload Files", type=['png','jpeg','jpg'])
68
 
69
- loading_text = st.empty()
70
 
71
- if uploaded_file != None:
72
- loading_text.markdown("Loading...")
73
- img = Image.open(uploaded_file)
74
- extractor = AutoFeatureExtractor.from_pretrained("Kaludi/food-category-classification-v2.0")
75
- model = AutoModelForImageClassification.from_pretrained("Kaludi/food-category-classification-v2.0")
76
- inputs = extractor(img, return_tensors="pt")
77
- outputs = model(**inputs)
78
- # ...
79
- loading_text.empty()
80
- label_num=outputs.logits.softmax(1).argmax(1)
81
- label_num=label_num.item()
82
 
83
 
84
- probs = outputs.logits.softmax(dim=1)
85
- percentage = round(probs[0, label_num].item() * 100, 2)
86
 
87
- st.markdown("### Your Image:")
88
- st.image(img, width=260)
89
 
90
- st.write("The Predicted Classification is:")
91
 
92
- if label_num==0:
93
- st.write("**Roti** (" + f"{percentage}%)")
94
- elif label_num==1:
95
- st.write("**Produk dari Susu** (" + f"{percentage}%)")
96
- elif label_num==2:
97
- st.write("**Kue** (" + f"{percentage}%)")
98
- elif label_num==3:
99
- st.write("**Telur** (" + f"{percentage}%)")
100
- elif label_num==4:
101
- st.write("**Gorengan** (" + f"{percentage}%)")
102
- elif label_num==5:
103
- st.write("**Buah** (" + f"{percentage}%)")
104
- elif label_num==6:
105
- st.write("**Daging** (" + f"{percentage}%)")
106
- elif label_num==7:
107
- st.write("**Mie** (" + f"{percentage}%)")
108
- elif label_num==8:
109
- st.write("**Nasi** (" + f"{percentage}%)")
110
- elif label_num==9:
111
- st.write("**Seafood** (" + f"{percentage}%)")
112
- elif label_num==10:
113
- st.write("**Sup** (" + f"{percentage}%)")
114
- else:
115
- st.write("**Sayuran** (" + f"{percentage}%)")
116
 
117
- # st.write("You Selected **{}** For Diet and **{}** For Cuisine with Max".format(diet, cuisine), calories, "Calories For", ( "**Bread**" if label_num==0 else "**Dairy**" if label_num==1 else "**Dessert**" if label_num==2 else "**Egg**" if label_num==3 else "**Fried Food**" if label_num==4 else "**Fruit**" if label_num==5 else "**Meat**" if label_num==6 else "**Noodles**" if label_num==7 else "**Rice**" if label_num==8 else "**Seafood**" if label_num==9 else "**Soup**" if label_num==10 else "**Vegetable**"))
118
 
119
- # Dropdown for Diet
120
- elif menu == 'Rekomendasi Resep':
121
- diet_options = ['All', 'Gluten-Free', 'Vegan', 'Vegetarian', 'Dairy-Free']
122
- diet = st.selectbox('Diet', diet_options)
123
-
124
- # Dropdown for Cuisine
125
- cuisine_options = ['All', 'African', 'Asian', 'Caribbean', 'Central American', 'Europe', 'Middle Eastern', 'North American', 'Oceanic', 'South American']
126
-
127
- cuisine = st.selectbox('Cuisine', cuisine_options)
 
 
 
128
 
129
- # Slider for Calories
130
- calories = st.slider("Select Max Calories (Per Serving)", 25, 1000, 500)
131
-
132
- # print the calories
133
- st.write("Selected: **{}** Max Calories.".format(calories))
134
 
135
- url = "https://alcksyjrmd.execute-api.us-east-2.amazonaws.com/default/nutrients_response"
136
 
137
- # Dropdown for Cuisine
138
- cuisine_category = ['Bread', 'Dairy', 'Dessert', 'Egg', 'Fried', 'Fruit', 'Meat', 'Noodles', 'Rice','Seafood','Soup','Vegetable']
139
- label_num = st.selectbox('Cuisine Type', cuisine_category)
140
- category = ("Bread" if label_num==0 else "Dairy" if label_num==1 else "Dessert" if label_num==2 else "Egg" if label_num==3 else "Fried" if label_num==4 else "Fruit" if label_num==5 else "Meat" if label_num==6 else "Noodles" if label_num==7 else "Rice" if label_num==8 else "Seafood" if label_num==9 else "**Soup**" if label_num==10 else "Vegetable")
141
 
142
- params = {"f": category, "k": str(calories)}
143
 
144
- if diet != "All":
145
- params["d"] = diet
146
 
147
- if cuisine != "All":
148
- params["c"] = cuisine
149
 
150
- response = requests.get(url, params=params)
151
- response_json = json.loads(response.content)
152
- # Convert response_json to a list
153
- response_json = list(response_json)
154
 
155
- if len(response_json) == 0:
156
- st.markdown("### Tidak ditemukan:")
157
- st.write("**No recipes found. Please adjust your search criteria.**")
158
- else:
159
- if len(response_json) > 1:
160
- random_recipe = random.choice(response_json)
161
- if st.button("Get Recipe"):
162
- response_json.remove(random_recipe)
163
- if len(response_json) == 0:
164
- st.write("No more recipes. Please adjust your search criteria.")
165
- else:
166
- random_recipe = random.choice(response_json)
167
- st.markdown("### Recommended Recipe:")
168
- st.write("**Title:** ", random_recipe['Title'])
169
- if random_recipe['Image Link'].endswith(".jpg") or random_recipe['Image Link'].endswith(".jpeg") or random_recipe['Image Link'].endswith(".png"):
170
- st.image(random_recipe['Image Link'], width=300)
171
- else:
172
- st.write("**Image Link:** ", random_recipe['Image Link'])
173
- st.write("**Rating:** ", random_recipe['Rating'])
174
- if random_recipe['Description'] != "Description not found":
175
- st.write("**Description:** ", random_recipe['Description'])
176
- st.write("**Ingredients:**<br>", random_recipe['Ingredients'].replace('\n', '<br>'), unsafe_allow_html=True)
177
- st.write("**Recipe Facts:**<br>", random_recipe['Recipe Facts'].replace('\n', '<br>'), unsafe_allow_html=True)
178
- st.write("**Directions:**<br>", random_recipe['Directions'].replace('\n', '<br>'), unsafe_allow_html=True)
179
- # extract only numeric values and convert mg to g
180
- values = [
181
- float(re.sub(r'[^\d.]+', '', random_recipe['Total Fat'])),
182
- float(re.sub(r'[^\d.]+', '', random_recipe['Saturated Fat'])),
183
- float(re.sub(r'[^\d.]+', '', random_recipe['Cholesterol'])) / 1000,
184
- float(re.sub(r'[^\d.]+', '', random_recipe['Sodium'])) / 1000,
185
- float(re.sub(r'[^\d.]+', '', random_recipe['Total Carbohydrate'])),
186
- float(re.sub(r'[^\d.]+', '', random_recipe['Dietary Fiber'])),
187
- float(re.sub(r'[^\d.]+', '', random_recipe['Total Sugars'])),
188
- float(re.sub(r'[^\d.]+', '', random_recipe['Protein'])),
189
- float(re.sub(r'[^\d.]+', '', random_recipe['Vitamin C'])) / 1000,
190
- float(re.sub(r'[^\d.]+', '', random_recipe['Calcium'])) / 1000,
191
- float(re.sub(r'[^\d.]+', '', random_recipe['Iron'])) / 1000,
192
- float(re.sub(r'[^\d.]+', '', random_recipe['Potassium'])) / 1000
193
- ]
194
- # Create a list of daily values (DV) for each nutrient based on a 2000 calorie per day diet, all are in grams
195
- dv = [65, 20, 0.3, 2.3, 300, 28, 50, 50, 0.09, 1, 0.018, 4.7]
196
-
197
- # Calculate the percentage of DV for each nutrient
198
- dv_percent = [round(value * 100 / dv[i]) for i, value in enumerate(values)]
199
- nutrition_html = """
200
- <div id="nutrition-info_6-0" class="comp nutrition-info">
201
- <table class="nutrition-info__table">
202
- <thead>
203
- <tr>
204
- <th class="nutrition-info__heading" colspan="3">Number of Servings: <span class="nutrition-info__heading-aside">{servings}</span></th>
205
- </tr>
206
- </thead>
207
- <tbody class="nutrition-info__table--body">
208
- <tr class="nutrition-info__table--row">
209
- <td class="nutrition-info__table--cell">Calories</td>
210
- <td class="nutrition-info__table--cell">{calories}</td>
211
- <td class="nutrition-info__table--cell"></td>
212
- </tr>
213
- <tr class="nutrition-info__table--row">
214
- <td class="nutrition-info__table--cell">Total Fat</td>
215
- <td class="nutrition-info__table--cell">{total_fat}</td>
216
- <td class="nutrition-info__table--cell">{fat_percent}% DV</td>
217
- </tr>
218
- <tr class="nutrition-info__table--row">
219
- <td class="nutrition-info__table--cell">Saturated Fat</td>
220
- <td class="nutrition-info__table--cell">{saturated_fat}</td>
221
- <td class="nutrition-info__table--cell">{sat_fat_percent}% DV</td>
222
- </tr>
223
- <tr class="nutrition-info__table--row">
224
- <td class="nutrition-info__table--cell">Cholesterol</td>
225
- <td class="nutrition-info__table--cell">{cholesterol}</td>
226
- <td class="nutrition-info__table--cell">{chol_percent}% DV</td>
227
- </tr>
228
- <tr class="nutrition-info__table--row">
229
- <td class="nutrition-info__table--cell">Sodium</td>
230
- <td class="nutrition-info__table--cell">{sodium}</td>
231
- <td class="nutrition-info__table--cell">{sodium_percent}% DV</td>
232
- </tr>
233
- <tr class="nutrition-info__table--row">
234
- <td class="nutrition-info__table--cell">Total Carbohydrate</td>
235
- <td class="nutrition-info__table--cell">{total_carbohydrate}</td>
236
- <td class="nutrition-info__table--cell">{carb_percent}% DV</td>
237
- </tr>
238
- <tr class="nutrition-info__table--row">
239
- <td class="nutrition-info__table--cell">Dietary Fiber</td>
240
- <td class="nutrition-info__table--cell">{dietary_fiber}</td>
241
- <td class="nutrition-info__table--cell">{diet_fibe_percent}% DV</td>
242
- </tr>
243
- <tr class="nutrition-info__table--row">
244
- <td class="nutrition-info__table--cell">Total Sugars</td>
245
- <td class="nutrition-info__table--cell">{total_sugars}</td>
246
- <td class="nutrition-info__table--cell">{tot_sugars_percent}% DV</td>
247
- </tr>
248
- <tr class="nutrition-info__table--row">
249
- <td class="nutrition-info__table--cell">Protein</td>
250
- <td class="nutrition-info__table--cell">{protein}</td>
251
- <td class="nutrition-info__table--cell">{protein_percent}% DV</td>
252
- </tr>
253
- <tr class="nutrition-info__table--row">
254
- <td class="nutrition-info__table--cell">Vitamin C</td>
255
- <td class="nutrition-info__table--cell">{vitc}</td>
256
- <td class="nutrition-info__table--cell">{vitc_percent}% DV</td>
257
- </tr>
258
- <tr class="nutrition-info__table--row">
259
- <td class="nutrition-info__table--cell">Calcium</td>
260
- <td class="nutrition-info__table--cell">{calc}</td>
261
- <td class="nutrition-info__table--cell">{calc_percent}% DV</td>
262
- </tr>
263
- <tr class="nutrition-info__table--row">
264
- <td class="nutrition-info__table--cell">Iron</td>
265
- <td class="nutrition-info__table--cell">{iron}</td>
266
- <td class="nutrition-info__table--cell">{iron_percent}% DV</td>
267
- </tr>
268
- <tr class="nutrition-info__table--row">
269
- <td class="nutrition-info__table--cell">Potassium</td>
270
- <td class="nutrition-info__table--cell">{pota}</td>
271
- <td class="nutrition-info__table--cell">{pota_percent}% DV</td>
272
- </tr>
273
- </tbody>
274
- </table>
275
- </div>
276
- """
277
- # Use the nutrition HTML and format it with the values
278
- formatted_html = nutrition_html.format(
279
- calories=random_recipe['Calories'],
280
- total_fat=random_recipe['Total Fat'],
281
- saturated_fat=random_recipe['Saturated Fat'],
282
- cholesterol=random_recipe['Cholesterol'],
283
- sodium=random_recipe['Sodium'],
284
- total_carbohydrate=random_recipe['Total Carbohydrate'],
285
- dietary_fiber=random_recipe['Dietary Fiber'],
286
- total_sugars=random_recipe['Total Sugars'],
287
- servings=random_recipe['Number of Servings'],
288
- vitc=random_recipe['Vitamin C'],
289
- calc=random_recipe['Calcium'],
290
- iron=random_recipe['Iron'],
291
- pota=random_recipe['Potassium'],
292
- protein=random_recipe['Protein'],
293
- fat_percent=dv_percent[0],
294
- sat_fat_percent=dv_percent[1],
295
- chol_percent=dv_percent[2],
296
- sodium_percent=dv_percent[3],
297
- carb_percent=dv_percent[4],
298
- diet_fibe_percent=dv_percent[5],
299
- tot_sugars_percent=dv_percent[6],
300
- protein_percent=dv_percent[7],
301
- vitc_percent=dv_percent[8],
302
- calc_percent=dv_percent[9],
303
- iron_percent=dv_percent[10],
304
- pota_percent=dv_percent[11]
305
-
306
- )
307
-
308
- # Define a function to apply the CSS styles to the table cells
309
- def format_table(val):
310
- return f"background-color: #133350; color: #fff; border: 1px solid #ddd; border-radius: .25rem; padding: .625rem .625rem 0; font-family: Helvetica; font-size: 1rem;"
311
-
312
- with st.container():
313
- # Add the nutrition table to the Streamlit app
314
- st.write("<h2 style='text-align:left;'>Nutrition Facts (per serving)</h2>", unsafe_allow_html=True)
315
- st.write(f"<div style='max-height:none; overflow:auto'>{formatted_html}</div>", unsafe_allow_html=True)
316
- st.write("<p style='text-align:left;'>*The % Daily Value (DV) tells you how much a nutrient in a food serving contributes to a daily diet. 2,000 calories a day is used for general nutrition advice.</p>", unsafe_allow_html=True)
317
- # create pie chart
318
- labels = ['Total Fat', 'Saturated Fat', 'Cholesterol', 'Sodium', 'Total Carbohydrate', 'Dietary Fiber', 'Total Sugars', 'Protein', 'Vitamin C', 'Calcium', 'Iron', 'Potassium']
319
- fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
320
- st.markdown("### Macronutrients Pie Chart ;) (In Grams)")
321
- st.plotly_chart(fig)
322
- st.write("**Tags:** ", random_recipe['Tags'])
323
- st.write("**Recipe URL:** ", random_recipe['Recipe URLs'])
324
- st.write("*To download this recipe as a PDF, open the hamburger menu on the top right and click on Print.*")
325
- st.markdown("### JSON Response:")
326
- st.write(response_json)
327
-
328
  else:
329
- st.markdown("### Recommended Recipe:")
330
- st.write("**Title:** ", response_json[0]['Title'])
331
- if response_json[0]['Image Link'].endswith(".jpg") or response_json[0]['Image Link'].endswith(".jpeg") or response_json[0]['Image Link'].endswith(".png"):
332
- st.image(response_json[0]['Image Link'], width=300)
333
- else:
334
- st.write("**Image Link:** ", response_json[0]['Image Link'])
335
- st.write("**Rating:** ", response_json[0]['Rating'])
336
- if response_json[0]['Description'] != "Description not found":
337
- st.write("**Description:** ", response_json[0]['Description'])
338
- st.write("**Ingredients:**<br>", response_json[0]['Ingredients'].replace('\n', '<br>'), unsafe_allow_html=True)
339
- st.write("**Recipe Facts:**<br>", response_json[0]['Recipe Facts'].replace('\n', '<br>'), unsafe_allow_html=True)
340
- st.write("**Directions:**<br>", response_json[0]['Directions'].replace('\n', '<br>'), unsafe_allow_html=True)
341
- # extract only numeric values and convert mg to g
342
- values = [
343
- float(re.sub(r'[^\d.]+', '', response_json[0]['Total Fat'])),
344
- float(re.sub(r'[^\d.]+', '', response_json[0]['Saturated Fat'])),
345
- float(re.sub(r'[^\d.]+', '', response_json[0]['Cholesterol'])) / 1000,
346
- float(re.sub(r'[^\d.]+', '', response_json[0]['Sodium'])) / 1000,
347
- float(re.sub(r'[^\d.]+', '', response_json[0]['Total Carbohydrate'])),
348
- float(re.sub(r'[^\d.]+', '', response_json[0]['Dietary Fiber'])),
349
- float(re.sub(r'[^\d.]+', '', response_json[0]['Total Sugars'])),
350
- float(re.sub(r'[^\d.]+', '', response_json[0]['Protein'])),
351
- float(re.sub(r'[^\d.]+', '', response_json[0]['Vitamin C'])) / 1000,
352
- float(re.sub(r'[^\d.]+', '', response_json[0]['Calcium'])) / 1000,
353
- float(re.sub(r'[^\d.]+', '', response_json[0]['Iron'])) / 1000,
354
- float(re.sub(r'[^\d.]+', '', response_json[0]['Potassium'])) / 1000
355
- ]
356
- # Create a list of daily values (DV) for each nutrient based on a 2000 calorie per day diet, all are in grams
357
- dv = [65, 20, 0.3, 2.3, 300, 28, 50, 50, 0.09, 1, 0.018, 4.7]
358
-
359
- # Calculate the percentage of DV for each nutrient
360
- dv_percent = [round(value * 100 / dv[i]) for i, value in enumerate(values)]
361
- nutrition_html = """
362
- <div id="nutrition-info_6-0" class="comp nutrition-info">
363
- <table class="nutrition-info__table">
364
- <thead>
365
- <tr>
366
- <th class="nutrition-info__heading" colspan="3">Number of Servings: <span class="nutrition-info__heading-aside">{servings}</span></th>
367
- </tr>
368
- </thead>
369
- <tbody class="nutrition-info__table--body">
370
- <tr class="nutrition-info__table--row">
371
- <td class="nutrition-info__table--cell">Calories</td>
372
- <td class="nutrition-info__table--cell">{calories}</td>
373
- <td class="nutrition-info__table--cell"></td>
374
- </tr>
375
- <tr class="nutrition-info__table--row">
376
- <td class="nutrition-info__table--cell">Total Fat</td>
377
- <td class="nutrition-info__table--cell">{total_fat}</td>
378
- <td class="nutrition-info__table--cell">{fat_percent}% DV</td>
379
- </tr>
380
- <tr class="nutrition-info__table--row">
381
- <td class="nutrition-info__table--cell">Saturated Fat</td>
382
- <td class="nutrition-info__table--cell">{saturated_fat}</td>
383
- <td class="nutrition-info__table--cell">{sat_fat_percent}% DV</td>
384
- </tr>
385
- <tr class="nutrition-info__table--row">
386
- <td class="nutrition-info__table--cell">Cholesterol</td>
387
- <td class="nutrition-info__table--cell">{cholesterol}</td>
388
- <td class="nutrition-info__table--cell">{chol_percent}% DV</td>
389
- </tr>
390
- <tr class="nutrition-info__table--row">
391
- <td class="nutrition-info__table--cell">Sodium</td>
392
- <td class="nutrition-info__table--cell">{sodium}</td>
393
- <td class="nutrition-info__table--cell">{sodium_percent}% DV</td>
394
- </tr>
395
- <tr class="nutrition-info__table--row">
396
- <td class="nutrition-info__table--cell">Total Carbohydrate</td>
397
- <td class="nutrition-info__table--cell">{total_carbohydrate}</td>
398
- <td class="nutrition-info__table--cell">{carb_percent}% DV</td>
399
- </tr>
400
- <tr class="nutrition-info__table--row">
401
- <td class="nutrition-info__table--cell">Dietary Fiber</td>
402
- <td class="nutrition-info__table--cell">{dietary_fiber}</td>
403
- <td class="nutrition-info__table--cell">{diet_fibe_percent}% DV</td>
404
- </tr>
405
- <tr class="nutrition-info__table--row">
406
- <td class="nutrition-info__table--cell">Total Sugars</td>
407
- <td class="nutrition-info__table--cell">{total_sugars}</td>
408
- <td class="nutrition-info__table--cell">{tot_sugars_percent}% DV</td>
409
- </tr>
410
- <tr class="nutrition-info__table--row">
411
- <td class="nutrition-info__table--cell">Protein</td>
412
- <td class="nutrition-info__table--cell">{protein}</td>
413
- <td class="nutrition-info__table--cell">{protein_percent}% DV</td>
414
- </tr>
415
- <tr class="nutrition-info__table--row">
416
- <td class="nutrition-info__table--cell">Vitamin C</td>
417
- <td class="nutrition-info__table--cell">{vitc}</td>
418
- <td class="nutrition-info__table--cell">{vitc_percent}% DV</td>
419
- </tr>
420
- <tr class="nutrition-info__table--row">
421
- <td class="nutrition-info__table--cell">Calcium</td>
422
- <td class="nutrition-info__table--cell">{calc}</td>
423
- <td class="nutrition-info__table--cell">{calc_percent}% DV</td>
424
- </tr>
425
- <tr class="nutrition-info__table--row">
426
- <td class="nutrition-info__table--cell">Iron</td>
427
- <td class="nutrition-info__table--cell">{iron}</td>
428
- <td class="nutrition-info__table--cell">{iron_percent}% DV</td>
429
- </tr>
430
- <tr class="nutrition-info__table--row">
431
- <td class="nutrition-info__table--cell">Potassium</td>
432
- <td class="nutrition-info__table--cell">{pota}</td>
433
- <td class="nutrition-info__table--cell">{pota_percent}% DV</td>
434
- </tr>
435
- </tbody>
436
- </table>
437
- </div>
438
- """
439
- # Use the nutrition HTML and format it with the values
440
- formatted_html = nutrition_html.format(
441
- calories=response_json[0]['Calories'],
442
- total_fat=response_json[0]['Total Fat'],
443
- saturated_fat=response_json[0]['Saturated Fat'],
444
- cholesterol=response_json[0]['Cholesterol'],
445
- sodium=response_json[0]['Sodium'],
446
- total_carbohydrate=response_json[0]['Total Carbohydrate'],
447
- dietary_fiber=response_json[0]['Dietary Fiber'],
448
- total_sugars=response_json[0]['Total Sugars'],
449
- servings=response_json[0]['Number of Servings'],
450
- vitc=response_json[0]['Vitamin C'],
451
- calc=response_json[0]['Calcium'],
452
- iron=response_json[0]['Iron'],
453
- pota=response_json[0]['Potassium'],
454
- protein=response_json[0]['Protein'],
455
- fat_percent=dv_percent[0],
456
- sat_fat_percent=dv_percent[1],
457
- chol_percent=dv_percent[2],
458
- sodium_percent=dv_percent[3],
459
- carb_percent=dv_percent[4],
460
- diet_fibe_percent=dv_percent[5],
461
- tot_sugars_percent=dv_percent[6],
462
- protein_percent=dv_percent[7],
463
- vitc_percent=dv_percent[8],
464
- calc_percent=dv_percent[9],
465
- iron_percent=dv_percent[10],
466
- pota_percent=dv_percent[11]
467
-
468
- )
469
-
470
- # Define a function to apply the CSS styles to the table cells
471
- def format_table(val):
472
- return f"background-color: #133350; color: #fff; border: 1px solid #ddd; border-radius: .25rem; padding: .625rem .625rem 0; font-family: Helvetica; font-size: 1rem;"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473
 
474
- with st.container():
475
- # Add the nutrition table to the Streamlit app
476
- st.write("<h2 style='text-align:left;'>Nutrition Facts (per serving)</h2>", unsafe_allow_html=True)
477
- st.write(f"<div style='max-height:none; overflow:auto'>{formatted_html}</div>", unsafe_allow_html=True)
478
- st.write("<p style='text-align:left;'>*The % Daily Value (DV) tells you how much a nutrient in a food serving contributes to a daily diet. 2,000 calories a day is used for general nutrition advice.</p>", unsafe_allow_html=True)
479
- # create pie chart
480
- labels = ['Total Fat', 'Saturated Fat', 'Cholesterol', 'Sodium', 'Total Carbohydrate', 'Dietary Fiber', 'Total Sugars', 'Protein', 'Vitamin C', 'Calcium', 'Iron', 'Potassium']
481
- fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
482
- st.markdown("### Macronutrients Pie Chart ;) (In Grams)")
483
- st.plotly_chart(fig)
484
- st.write("**Tags:** ", response_json[0]['Tags'])
485
- st.write("**Recipe URL:** ", response_json[0]['Recipe URLs'])
486
- st.write("*To download this recipe as a PDF, open the hamburger menu on the top right and click on Print.*")
487
- st.markdown("### JSON Response:")
488
- st.write(response_json)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489
 
490
- st.markdown("<hr style='text-align: center;'>", unsafe_allow_html=True)
491
- # st.markdown("<p style='text-align: center'><a href='https://github.com/Kaludii'>Github</a> | <a href='https://huggingface.co/Kaludi'>HuggingFace</a></p>", unsafe_allow_html=True)
492
 
493
  if __name__ == '__main__':
494
  main()
 
14
 
15
 
16
  st.set_page_config(layout='wide',
17
+ page_title='Recipes Recommender'
18
  )
19
 
20
  # st.sidebar.markdown("<h3 style='text-align: center;'>Project Location:</h3>", unsafe_allow_html=True)
 
61
  # st.image(image, width=260)
62
 
63
  # select_health = st.radio("Select One (Not Functional Yet):", ["Regular", "Low-Calorie"], horizontal=True)
64
+ # menu = st.sidebar.selectbox('Pilih Menu',['Prediksi Makanan', 'Rekomendasi Resep'])
65
+ # if menu == 'Prediksi Makanan':
66
 
67
+ # uploaded_file = st.file_uploader("Upload Files", type=['png','jpeg','jpg'])
68
 
69
+ # loading_text = st.empty()
70
 
71
+ # if uploaded_file != None:
72
+ # loading_text.markdown("Loading...")
73
+ # img = Image.open(uploaded_file)
74
+ # extractor = AutoFeatureExtractor.from_pretrained("Kaludi/food-category-classification-v2.0")
75
+ # model = AutoModelForImageClassification.from_pretrained("Kaludi/food-category-classification-v2.0")
76
+ # inputs = extractor(img, return_tensors="pt")
77
+ # outputs = model(**inputs)
78
+ # # ...
79
+ # loading_text.empty()
80
+ # label_num=outputs.logits.softmax(1).argmax(1)
81
+ # label_num=label_num.item()
82
 
83
 
84
+ # probs = outputs.logits.softmax(dim=1)
85
+ # percentage = round(probs[0, label_num].item() * 100, 2)
86
 
87
+ # st.markdown("### Your Image:")
88
+ # st.image(img, width=260)
89
 
90
+ # st.write("The Predicted Classification is:")
91
 
92
+ # if label_num==0:
93
+ # st.write("**Roti** (" + f"{percentage}%)")
94
+ # elif label_num==1:
95
+ # st.write("**Produk dari Susu** (" + f"{percentage}%)")
96
+ # elif label_num==2:
97
+ # st.write("**Kue** (" + f"{percentage}%)")
98
+ # elif label_num==3:
99
+ # st.write("**Telur** (" + f"{percentage}%)")
100
+ # elif label_num==4:
101
+ # st.write("**Gorengan** (" + f"{percentage}%)")
102
+ # elif label_num==5:
103
+ # st.write("**Buah** (" + f"{percentage}%)")
104
+ # elif label_num==6:
105
+ # st.write("**Daging** (" + f"{percentage}%)")
106
+ # elif label_num==7:
107
+ # st.write("**Mie** (" + f"{percentage}%)")
108
+ # elif label_num==8:
109
+ # st.write("**Nasi** (" + f"{percentage}%)")
110
+ # elif label_num==9:
111
+ # st.write("**Seafood** (" + f"{percentage}%)")
112
+ # elif label_num==10:
113
+ # st.write("**Sup** (" + f"{percentage}%)")
114
+ # else:
115
+ # st.write("**Sayuran** (" + f"{percentage}%)")
116
 
117
+ # # st.write("You Selected **{}** For Diet and **{}** For Cuisine with Max".format(diet, cuisine), calories, "Calories For", ( "**Bread**" if label_num==0 else "**Dairy**" if label_num==1 else "**Dessert**" if label_num==2 else "**Egg**" if label_num==3 else "**Fried Food**" if label_num==4 else "**Fruit**" if label_num==5 else "**Meat**" if label_num==6 else "**Noodles**" if label_num==7 else "**Rice**" if label_num==8 else "**Seafood**" if label_num==9 else "**Soup**" if label_num==10 else "**Vegetable**"))
118
 
119
+ # # Dropdown for Diet
120
+ # elif menu == 'Rekomendasi Resep':
121
+ diet_options = ['All', 'Gluten-Free', 'Vegan', 'Vegetarian', 'Dairy-Free']
122
+ diet = st.selectbox('Diet', diet_options)
123
+
124
+ # Dropdown for Cuisine
125
+ cuisine_options = ['All', 'African', 'Asian', 'Caribbean', 'Central American', 'Europe', 'Middle Eastern', 'North American', 'Oceanic', 'South American']
126
+
127
+ cuisine = st.selectbox('Cuisine', cuisine_options)
128
+
129
+ # Slider for Calories
130
+ calories = st.slider("Select Max Calories (Per Serving)", 25, 1000, 500)
131
 
132
+ # print the calories
133
+ st.write("Selected: **{}** Max Calories.".format(calories))
 
 
 
134
 
135
+ url = "https://alcksyjrmd.execute-api.us-east-2.amazonaws.com/default/nutrients_response"
136
 
137
+ # Dropdown for Cuisine
138
+ cuisine_category = ['Bread', 'Dairy', 'Dessert', 'Egg', 'Fried', 'Fruit', 'Meat', 'Noodles', 'Rice','Seafood','Soup','Vegetable']
139
+ label_num = st.selectbox('Cuisine Type', cuisine_category)
140
+ category = ("Bread" if label_num==0 else "Dairy" if label_num==1 else "Dessert" if label_num==2 else "Egg" if label_num==3 else "Fried" if label_num==4 else "Fruit" if label_num==5 else "Meat" if label_num==6 else "Noodles" if label_num==7 else "Rice" if label_num==8 else "Seafood" if label_num==9 else "**Soup**" if label_num==10 else "Vegetable")
141
 
142
+ params = {"f": category, "k": str(calories)}
143
 
144
+ if diet != "All":
145
+ params["d"] = diet
146
 
147
+ if cuisine != "All":
148
+ params["c"] = cuisine
149
 
150
+ response = requests.get(url, params=params)
151
+ response_json = json.loads(response.content)
152
+ # Convert response_json to a list
153
+ response_json = list(response_json)
154
 
155
+ if len(response_json) == 0:
156
+ st.markdown("### Tidak ditemukan:")
157
+ st.write("**No recipes found. Please adjust your search criteria.**")
158
+ else:
159
+ if len(response_json) > 1:
160
+ random_recipe = random.choice(response_json)
161
+ if st.button("Get Recipe"):
162
+ response_json.remove(random_recipe)
163
+ if len(response_json) == 0:
164
+ st.write("No more recipes. Please adjust your search criteria.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  else:
166
+ random_recipe = random.choice(response_json)
167
+ st.markdown("### Recommended Recipe:")
168
+ st.write("**Title:** ", random_recipe['Title'])
169
+ if random_recipe['Image Link'].endswith(".jpg") or random_recipe['Image Link'].endswith(".jpeg") or random_recipe['Image Link'].endswith(".png"):
170
+ st.image(random_recipe['Image Link'], width=300)
171
+ else:
172
+ st.write("**Image Link:** ", random_recipe['Image Link'])
173
+ st.write("**Rating:** ", random_recipe['Rating'])
174
+ if random_recipe['Description'] != "Description not found":
175
+ st.write("**Description:** ", random_recipe['Description'])
176
+ st.write("**Ingredients:**<br>", random_recipe['Ingredients'].replace('\n', '<br>'), unsafe_allow_html=True)
177
+ st.write("**Recipe Facts:**<br>", random_recipe['Recipe Facts'].replace('\n', '<br>'), unsafe_allow_html=True)
178
+ st.write("**Directions:**<br>", random_recipe['Directions'].replace('\n', '<br>'), unsafe_allow_html=True)
179
+ # extract only numeric values and convert mg to g
180
+ values = [
181
+ float(re.sub(r'[^\d.]+', '', random_recipe['Total Fat'])),
182
+ float(re.sub(r'[^\d.]+', '', random_recipe['Saturated Fat'])),
183
+ float(re.sub(r'[^\d.]+', '', random_recipe['Cholesterol'])) / 1000,
184
+ float(re.sub(r'[^\d.]+', '', random_recipe['Sodium'])) / 1000,
185
+ float(re.sub(r'[^\d.]+', '', random_recipe['Total Carbohydrate'])),
186
+ float(re.sub(r'[^\d.]+', '', random_recipe['Dietary Fiber'])),
187
+ float(re.sub(r'[^\d.]+', '', random_recipe['Total Sugars'])),
188
+ float(re.sub(r'[^\d.]+', '', random_recipe['Protein'])),
189
+ float(re.sub(r'[^\d.]+', '', random_recipe['Vitamin C'])) / 1000,
190
+ float(re.sub(r'[^\d.]+', '', random_recipe['Calcium'])) / 1000,
191
+ float(re.sub(r'[^\d.]+', '', random_recipe['Iron'])) / 1000,
192
+ float(re.sub(r'[^\d.]+', '', random_recipe['Potassium'])) / 1000
193
+ ]
194
+ # Create a list of daily values (DV) for each nutrient based on a 2000 calorie per day diet, all are in grams
195
+ dv = [65, 20, 0.3, 2.3, 300, 28, 50, 50, 0.09, 1, 0.018, 4.7]
196
+
197
+ # Calculate the percentage of DV for each nutrient
198
+ dv_percent = [round(value * 100 / dv[i]) for i, value in enumerate(values)]
199
+ nutrition_html = """
200
+ <div id="nutrition-info_6-0" class="comp nutrition-info">
201
+ <table class="nutrition-info__table">
202
+ <thead>
203
+ <tr>
204
+ <th class="nutrition-info__heading" colspan="3">Number of Servings: <span class="nutrition-info__heading-aside">{servings}</span></th>
205
+ </tr>
206
+ </thead>
207
+ <tbody class="nutrition-info__table--body">
208
+ <tr class="nutrition-info__table--row">
209
+ <td class="nutrition-info__table--cell">Calories</td>
210
+ <td class="nutrition-info__table--cell">{calories}</td>
211
+ <td class="nutrition-info__table--cell"></td>
212
+ </tr>
213
+ <tr class="nutrition-info__table--row">
214
+ <td class="nutrition-info__table--cell">Total Fat</td>
215
+ <td class="nutrition-info__table--cell">{total_fat}</td>
216
+ <td class="nutrition-info__table--cell">{fat_percent}% DV</td>
217
+ </tr>
218
+ <tr class="nutrition-info__table--row">
219
+ <td class="nutrition-info__table--cell">Saturated Fat</td>
220
+ <td class="nutrition-info__table--cell">{saturated_fat}</td>
221
+ <td class="nutrition-info__table--cell">{sat_fat_percent}% DV</td>
222
+ </tr>
223
+ <tr class="nutrition-info__table--row">
224
+ <td class="nutrition-info__table--cell">Cholesterol</td>
225
+ <td class="nutrition-info__table--cell">{cholesterol}</td>
226
+ <td class="nutrition-info__table--cell">{chol_percent}% DV</td>
227
+ </tr>
228
+ <tr class="nutrition-info__table--row">
229
+ <td class="nutrition-info__table--cell">Sodium</td>
230
+ <td class="nutrition-info__table--cell">{sodium}</td>
231
+ <td class="nutrition-info__table--cell">{sodium_percent}% DV</td>
232
+ </tr>
233
+ <tr class="nutrition-info__table--row">
234
+ <td class="nutrition-info__table--cell">Total Carbohydrate</td>
235
+ <td class="nutrition-info__table--cell">{total_carbohydrate}</td>
236
+ <td class="nutrition-info__table--cell">{carb_percent}% DV</td>
237
+ </tr>
238
+ <tr class="nutrition-info__table--row">
239
+ <td class="nutrition-info__table--cell">Dietary Fiber</td>
240
+ <td class="nutrition-info__table--cell">{dietary_fiber}</td>
241
+ <td class="nutrition-info__table--cell">{diet_fibe_percent}% DV</td>
242
+ </tr>
243
+ <tr class="nutrition-info__table--row">
244
+ <td class="nutrition-info__table--cell">Total Sugars</td>
245
+ <td class="nutrition-info__table--cell">{total_sugars}</td>
246
+ <td class="nutrition-info__table--cell">{tot_sugars_percent}% DV</td>
247
+ </tr>
248
+ <tr class="nutrition-info__table--row">
249
+ <td class="nutrition-info__table--cell">Protein</td>
250
+ <td class="nutrition-info__table--cell">{protein}</td>
251
+ <td class="nutrition-info__table--cell">{protein_percent}% DV</td>
252
+ </tr>
253
+ <tr class="nutrition-info__table--row">
254
+ <td class="nutrition-info__table--cell">Vitamin C</td>
255
+ <td class="nutrition-info__table--cell">{vitc}</td>
256
+ <td class="nutrition-info__table--cell">{vitc_percent}% DV</td>
257
+ </tr>
258
+ <tr class="nutrition-info__table--row">
259
+ <td class="nutrition-info__table--cell">Calcium</td>
260
+ <td class="nutrition-info__table--cell">{calc}</td>
261
+ <td class="nutrition-info__table--cell">{calc_percent}% DV</td>
262
+ </tr>
263
+ <tr class="nutrition-info__table--row">
264
+ <td class="nutrition-info__table--cell">Iron</td>
265
+ <td class="nutrition-info__table--cell">{iron}</td>
266
+ <td class="nutrition-info__table--cell">{iron_percent}% DV</td>
267
+ </tr>
268
+ <tr class="nutrition-info__table--row">
269
+ <td class="nutrition-info__table--cell">Potassium</td>
270
+ <td class="nutrition-info__table--cell">{pota}</td>
271
+ <td class="nutrition-info__table--cell">{pota_percent}% DV</td>
272
+ </tr>
273
+ </tbody>
274
+ </table>
275
+ </div>
276
+ """
277
+ # Use the nutrition HTML and format it with the values
278
+ formatted_html = nutrition_html.format(
279
+ calories=random_recipe['Calories'],
280
+ total_fat=random_recipe['Total Fat'],
281
+ saturated_fat=random_recipe['Saturated Fat'],
282
+ cholesterol=random_recipe['Cholesterol'],
283
+ sodium=random_recipe['Sodium'],
284
+ total_carbohydrate=random_recipe['Total Carbohydrate'],
285
+ dietary_fiber=random_recipe['Dietary Fiber'],
286
+ total_sugars=random_recipe['Total Sugars'],
287
+ servings=random_recipe['Number of Servings'],
288
+ vitc=random_recipe['Vitamin C'],
289
+ calc=random_recipe['Calcium'],
290
+ iron=random_recipe['Iron'],
291
+ pota=random_recipe['Potassium'],
292
+ protein=random_recipe['Protein'],
293
+ fat_percent=dv_percent[0],
294
+ sat_fat_percent=dv_percent[1],
295
+ chol_percent=dv_percent[2],
296
+ sodium_percent=dv_percent[3],
297
+ carb_percent=dv_percent[4],
298
+ diet_fibe_percent=dv_percent[5],
299
+ tot_sugars_percent=dv_percent[6],
300
+ protein_percent=dv_percent[7],
301
+ vitc_percent=dv_percent[8],
302
+ calc_percent=dv_percent[9],
303
+ iron_percent=dv_percent[10],
304
+ pota_percent=dv_percent[11]
305
+
306
+ )
307
+
308
+ # Define a function to apply the CSS styles to the table cells
309
+ def format_table(val):
310
+ return f"background-color: #133350; color: #fff; border: 1px solid #ddd; border-radius: .25rem; padding: .625rem .625rem 0; font-family: Helvetica; font-size: 1rem;"
311
+
312
+ with st.container():
313
+ # Add the nutrition table to the Streamlit app
314
+ st.write("<h2 style='text-align:left;'>Nutrition Facts (per serving)</h2>", unsafe_allow_html=True)
315
+ st.write(f"<div style='max-height:none; overflow:auto'>{formatted_html}</div>", unsafe_allow_html=True)
316
+ st.write("<p style='text-align:left;'>*The % Daily Value (DV) tells you how much a nutrient in a food serving contributes to a daily diet. 2,000 calories a day is used for general nutrition advice.</p>", unsafe_allow_html=True)
317
+ # create pie chart
318
+ labels = ['Total Fat', 'Saturated Fat', 'Cholesterol', 'Sodium', 'Total Carbohydrate', 'Dietary Fiber', 'Total Sugars', 'Protein', 'Vitamin C', 'Calcium', 'Iron', 'Potassium']
319
+ fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
320
+ st.markdown("### Macronutrients Pie Chart ;) (In Grams)")
321
+ st.plotly_chart(fig)
322
+ st.write("**Tags:** ", random_recipe['Tags'])
323
+ st.write("**Recipe URL:** ", random_recipe['Recipe URLs'])
324
+ st.write("*To download this recipe as a PDF, open the hamburger menu on the top right and click on Print.*")
325
+ st.markdown("### JSON Response:")
326
+ st.write(response_json)
327
 
328
+ else:
329
+ st.markdown("### Recommended Recipe:")
330
+ st.write("**Title:** ", response_json[0]['Title'])
331
+ if response_json[0]['Image Link'].endswith(".jpg") or response_json[0]['Image Link'].endswith(".jpeg") or response_json[0]['Image Link'].endswith(".png"):
332
+ st.image(response_json[0]['Image Link'], width=300)
333
+ else:
334
+ st.write("**Image Link:** ", response_json[0]['Image Link'])
335
+ st.write("**Rating:** ", response_json[0]['Rating'])
336
+ if response_json[0]['Description'] != "Description not found":
337
+ st.write("**Description:** ", response_json[0]['Description'])
338
+ st.write("**Ingredients:**<br>", response_json[0]['Ingredients'].replace('\n', '<br>'), unsafe_allow_html=True)
339
+ st.write("**Recipe Facts:**<br>", response_json[0]['Recipe Facts'].replace('\n', '<br>'), unsafe_allow_html=True)
340
+ st.write("**Directions:**<br>", response_json[0]['Directions'].replace('\n', '<br>'), unsafe_allow_html=True)
341
+ # extract only numeric values and convert mg to g
342
+ values = [
343
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Total Fat'])),
344
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Saturated Fat'])),
345
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Cholesterol'])) / 1000,
346
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Sodium'])) / 1000,
347
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Total Carbohydrate'])),
348
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Dietary Fiber'])),
349
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Total Sugars'])),
350
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Protein'])),
351
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Vitamin C'])) / 1000,
352
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Calcium'])) / 1000,
353
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Iron'])) / 1000,
354
+ float(re.sub(r'[^\d.]+', '', response_json[0]['Potassium'])) / 1000
355
+ ]
356
+ # Create a list of daily values (DV) for each nutrient based on a 2000 calorie per day diet, all are in grams
357
+ dv = [65, 20, 0.3, 2.3, 300, 28, 50, 50, 0.09, 1, 0.018, 4.7]
358
+
359
+ # Calculate the percentage of DV for each nutrient
360
+ dv_percent = [round(value * 100 / dv[i]) for i, value in enumerate(values)]
361
+ nutrition_html = """
362
+ <div id="nutrition-info_6-0" class="comp nutrition-info">
363
+ <table class="nutrition-info__table">
364
+ <thead>
365
+ <tr>
366
+ <th class="nutrition-info__heading" colspan="3">Number of Servings: <span class="nutrition-info__heading-aside">{servings}</span></th>
367
+ </tr>
368
+ </thead>
369
+ <tbody class="nutrition-info__table--body">
370
+ <tr class="nutrition-info__table--row">
371
+ <td class="nutrition-info__table--cell">Calories</td>
372
+ <td class="nutrition-info__table--cell">{calories}</td>
373
+ <td class="nutrition-info__table--cell"></td>
374
+ </tr>
375
+ <tr class="nutrition-info__table--row">
376
+ <td class="nutrition-info__table--cell">Total Fat</td>
377
+ <td class="nutrition-info__table--cell">{total_fat}</td>
378
+ <td class="nutrition-info__table--cell">{fat_percent}% DV</td>
379
+ </tr>
380
+ <tr class="nutrition-info__table--row">
381
+ <td class="nutrition-info__table--cell">Saturated Fat</td>
382
+ <td class="nutrition-info__table--cell">{saturated_fat}</td>
383
+ <td class="nutrition-info__table--cell">{sat_fat_percent}% DV</td>
384
+ </tr>
385
+ <tr class="nutrition-info__table--row">
386
+ <td class="nutrition-info__table--cell">Cholesterol</td>
387
+ <td class="nutrition-info__table--cell">{cholesterol}</td>
388
+ <td class="nutrition-info__table--cell">{chol_percent}% DV</td>
389
+ </tr>
390
+ <tr class="nutrition-info__table--row">
391
+ <td class="nutrition-info__table--cell">Sodium</td>
392
+ <td class="nutrition-info__table--cell">{sodium}</td>
393
+ <td class="nutrition-info__table--cell">{sodium_percent}% DV</td>
394
+ </tr>
395
+ <tr class="nutrition-info__table--row">
396
+ <td class="nutrition-info__table--cell">Total Carbohydrate</td>
397
+ <td class="nutrition-info__table--cell">{total_carbohydrate}</td>
398
+ <td class="nutrition-info__table--cell">{carb_percent}% DV</td>
399
+ </tr>
400
+ <tr class="nutrition-info__table--row">
401
+ <td class="nutrition-info__table--cell">Dietary Fiber</td>
402
+ <td class="nutrition-info__table--cell">{dietary_fiber}</td>
403
+ <td class="nutrition-info__table--cell">{diet_fibe_percent}% DV</td>
404
+ </tr>
405
+ <tr class="nutrition-info__table--row">
406
+ <td class="nutrition-info__table--cell">Total Sugars</td>
407
+ <td class="nutrition-info__table--cell">{total_sugars}</td>
408
+ <td class="nutrition-info__table--cell">{tot_sugars_percent}% DV</td>
409
+ </tr>
410
+ <tr class="nutrition-info__table--row">
411
+ <td class="nutrition-info__table--cell">Protein</td>
412
+ <td class="nutrition-info__table--cell">{protein}</td>
413
+ <td class="nutrition-info__table--cell">{protein_percent}% DV</td>
414
+ </tr>
415
+ <tr class="nutrition-info__table--row">
416
+ <td class="nutrition-info__table--cell">Vitamin C</td>
417
+ <td class="nutrition-info__table--cell">{vitc}</td>
418
+ <td class="nutrition-info__table--cell">{vitc_percent}% DV</td>
419
+ </tr>
420
+ <tr class="nutrition-info__table--row">
421
+ <td class="nutrition-info__table--cell">Calcium</td>
422
+ <td class="nutrition-info__table--cell">{calc}</td>
423
+ <td class="nutrition-info__table--cell">{calc_percent}% DV</td>
424
+ </tr>
425
+ <tr class="nutrition-info__table--row">
426
+ <td class="nutrition-info__table--cell">Iron</td>
427
+ <td class="nutrition-info__table--cell">{iron}</td>
428
+ <td class="nutrition-info__table--cell">{iron_percent}% DV</td>
429
+ </tr>
430
+ <tr class="nutrition-info__table--row">
431
+ <td class="nutrition-info__table--cell">Potassium</td>
432
+ <td class="nutrition-info__table--cell">{pota}</td>
433
+ <td class="nutrition-info__table--cell">{pota_percent}% DV</td>
434
+ </tr>
435
+ </tbody>
436
+ </table>
437
+ </div>
438
+ """
439
+ # Use the nutrition HTML and format it with the values
440
+ formatted_html = nutrition_html.format(
441
+ calories=response_json[0]['Calories'],
442
+ total_fat=response_json[0]['Total Fat'],
443
+ saturated_fat=response_json[0]['Saturated Fat'],
444
+ cholesterol=response_json[0]['Cholesterol'],
445
+ sodium=response_json[0]['Sodium'],
446
+ total_carbohydrate=response_json[0]['Total Carbohydrate'],
447
+ dietary_fiber=response_json[0]['Dietary Fiber'],
448
+ total_sugars=response_json[0]['Total Sugars'],
449
+ servings=response_json[0]['Number of Servings'],
450
+ vitc=response_json[0]['Vitamin C'],
451
+ calc=response_json[0]['Calcium'],
452
+ iron=response_json[0]['Iron'],
453
+ pota=response_json[0]['Potassium'],
454
+ protein=response_json[0]['Protein'],
455
+ fat_percent=dv_percent[0],
456
+ sat_fat_percent=dv_percent[1],
457
+ chol_percent=dv_percent[2],
458
+ sodium_percent=dv_percent[3],
459
+ carb_percent=dv_percent[4],
460
+ diet_fibe_percent=dv_percent[5],
461
+ tot_sugars_percent=dv_percent[6],
462
+ protein_percent=dv_percent[7],
463
+ vitc_percent=dv_percent[8],
464
+ calc_percent=dv_percent[9],
465
+ iron_percent=dv_percent[10],
466
+ pota_percent=dv_percent[11]
467
+
468
+ )
469
+
470
+ # Define a function to apply the CSS styles to the table cells
471
+ def format_table(val):
472
+ return f"background-color: #133350; color: #fff; border: 1px solid #ddd; border-radius: .25rem; padding: .625rem .625rem 0; font-family: Helvetica; font-size: 1rem;"
473
+
474
+ with st.container():
475
+ # Add the nutrition table to the Streamlit app
476
+ st.write("<h2 style='text-align:left;'>Nutrition Facts (per serving)</h2>", unsafe_allow_html=True)
477
+ st.write(f"<div style='max-height:none; overflow:auto'>{formatted_html}</div>", unsafe_allow_html=True)
478
+ st.write("<p style='text-align:left;'>*The % Daily Value (DV) tells you how much a nutrient in a food serving contributes to a daily diet. 2,000 calories a day is used for general nutrition advice.</p>", unsafe_allow_html=True)
479
+ # create pie chart
480
+ labels = ['Total Fat', 'Saturated Fat', 'Cholesterol', 'Sodium', 'Total Carbohydrate', 'Dietary Fiber', 'Total Sugars', 'Protein', 'Vitamin C', 'Calcium', 'Iron', 'Potassium']
481
+ fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
482
+ st.markdown("### Macronutrients Pie Chart ;) (In Grams)")
483
+ st.plotly_chart(fig)
484
+ st.write("**Tags:** ", response_json[0]['Tags'])
485
+ st.write("**Recipe URL:** ", response_json[0]['Recipe URLs'])
486
+ st.write("*To download this recipe as a PDF, open the hamburger menu on the top right and click on Print.*")
487
+ st.markdown("### JSON Response:")
488
+ st.write(response_json)
489
 
490
+ st.markdown("<hr style='text-align: center;'>", unsafe_allow_html=True)
491
+ # st.markdown("<p style='text-align: center'><a href='https://github.com/Kaludii'>Github</a> | <a href='https://huggingface.co/Kaludi'>HuggingFace</a></p>", unsafe_allow_html=True)
492
 
493
  if __name__ == '__main__':
494
  main()