Spaces:
Sleeping
Sleeping
Commit
·
e103d2f
1
Parent(s):
9c0b0d5
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,57 +12,54 @@ def calculate_total_calories(user_input):
|
|
| 12 |
results = []
|
| 13 |
|
| 14 |
for item in menu_items:
|
| 15 |
-
# Split the menu item into quantity and item name
|
| 16 |
parts = item.strip().split(' ', 1)
|
| 17 |
|
| 18 |
if len(parts) == 2:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
quantity =
|
| 25 |
-
item_name =
|
| 26 |
-
except ValueError:
|
| 27 |
-
quantity = 1.0 # Assume a default quantity of 1 if not specified
|
| 28 |
-
unit = first_part
|
| 29 |
-
item_name = second_part
|
| 30 |
else:
|
| 31 |
-
quantity = 1
|
| 32 |
item_name = item.strip()
|
| 33 |
-
|
| 34 |
# Calculate the similarity scores between the item name and menu item names
|
| 35 |
similarity_scores = df_menu['food'].apply(lambda x: fuzz.token_set_ratio(x.lower(), item_name.lower()))
|
| 36 |
-
|
| 37 |
# Find the closest match with the highest similarity score
|
| 38 |
closest_match_index = similarity_scores.idxmax()
|
| 39 |
closest_match_score = similarity_scores[closest_match_index]
|
| 40 |
-
|
| 41 |
# Check if the similarity score is above a certain threshold
|
| 42 |
threshold = 60
|
| 43 |
if closest_match_score < threshold:
|
| 44 |
results.append("Không tìm thấy thông tin thức ăn: " + item_name)
|
| 45 |
continue
|
| 46 |
-
|
| 47 |
# Get the closest match menu item details
|
| 48 |
closest_match = df_menu.loc[closest_match_index]
|
| 49 |
menu_name = closest_match['food']
|
| 50 |
-
|
| 51 |
calories = closest_match['calo']
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
# Check if the quantity is in grams or milliliters
|
| 55 |
-
if item_unit.lower() == 'g' or item_unit.lower() == 'gram':
|
| 56 |
-
quantity = float(quantity)
|
| 57 |
-
elif item_unit.lower() == 'l' or item_unit.lower() == 'lit':
|
| 58 |
-
quantity = float(quantity) * 1000
|
| 59 |
-
|
| 60 |
# Calculate the total calories for the current menu item
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
results.append("Tên món ăn: " + menu_name)
|
| 64 |
-
results.append("
|
| 65 |
-
results.append("
|
|
|
|
|
|
|
| 66 |
results.append("") # Add an empty entry for spacing
|
| 67 |
|
| 68 |
results.append(str(total_calories) + " Kcals")
|
|
|
|
| 12 |
results = []
|
| 13 |
|
| 14 |
for item in menu_items:
|
| 15 |
+
# Split the menu item into quantity and item name
|
| 16 |
parts = item.strip().split(' ', 1)
|
| 17 |
|
| 18 |
if len(parts) == 2:
|
| 19 |
+
quantity_str = parts[0]
|
| 20 |
+
if quantity_str.isdigit():
|
| 21 |
+
quantity = int(quantity_str)
|
| 22 |
+
item_name = parts[1]
|
| 23 |
+
else:
|
| 24 |
+
quantity = 1
|
| 25 |
+
item_name = item.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
else:
|
| 27 |
+
quantity = 1
|
| 28 |
item_name = item.strip()
|
| 29 |
+
|
| 30 |
# Calculate the similarity scores between the item name and menu item names
|
| 31 |
similarity_scores = df_menu['food'].apply(lambda x: fuzz.token_set_ratio(x.lower(), item_name.lower()))
|
| 32 |
+
|
| 33 |
# Find the closest match with the highest similarity score
|
| 34 |
closest_match_index = similarity_scores.idxmax()
|
| 35 |
closest_match_score = similarity_scores[closest_match_index]
|
| 36 |
+
|
| 37 |
# Check if the similarity score is above a certain threshold
|
| 38 |
threshold = 60
|
| 39 |
if closest_match_score < threshold:
|
| 40 |
results.append("Không tìm thấy thông tin thức ăn: " + item_name)
|
| 41 |
continue
|
| 42 |
+
|
| 43 |
# Get the closest match menu item details
|
| 44 |
closest_match = df_menu.loc[closest_match_index]
|
| 45 |
menu_name = closest_match['food']
|
| 46 |
+
unit = closest_match['unit']
|
| 47 |
calories = closest_match['calo']
|
| 48 |
+
calories_per_unit = closest_match['calo_per_unit']
|
| 49 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
# Calculate the total calories for the current menu item
|
| 51 |
+
unit_spec = ['ml','g','gram']
|
| 52 |
+
if unit in unit_spec:
|
| 53 |
+
item_calories = calories_per_unit * quantity
|
| 54 |
+
total_calories += item_calories
|
| 55 |
+
else:
|
| 56 |
+
item_calories = calories * quantity
|
| 57 |
+
total_calories += item_calories
|
| 58 |
results.append("Tên món ăn: " + menu_name)
|
| 59 |
+
results.append("Số lượng: " + str(quantity))
|
| 60 |
+
results.append("Đơn vị: " + unit)
|
| 61 |
+
results.append("Lượng calories trong mỗi đơn vị: " + str(calories)+ " Kcals")
|
| 62 |
+
results.append("Tổng lượng calories của " + menu_name + ": " + str(item_calories)+ " Kcals")
|
| 63 |
results.append("") # Add an empty entry for spacing
|
| 64 |
|
| 65 |
results.append(str(total_calories) + " Kcals")
|