Spaces:
Sleeping
Sleeping
Commit
·
51a7681
1
Parent(s):
94758a9
Update calori.py (#1)
Browse files- Update calori.py (5a8d014491d361d12931bace68313ee1a4731edf)
Co-authored-by: Trần Thanh Tùng <[email protected]>
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import re
|
| 2 |
from fuzzywuzzy import fuzz
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
|
@@ -6,9 +5,6 @@ import gradio as gr
|
|
| 6 |
def calculate_total_calories(user_input):
|
| 7 |
df_menu = pd.read_excel('calories.xlsx')
|
| 8 |
|
| 9 |
-
# Define a regular expression pattern to extract the quantity and item name
|
| 10 |
-
pattern = r'(\d+)\s+(.+)'
|
| 11 |
-
|
| 12 |
# Split the user input into individual menu items
|
| 13 |
menu_items = user_input.split(',')
|
| 14 |
|
|
@@ -16,12 +12,12 @@ def calculate_total_calories(user_input):
|
|
| 16 |
results = []
|
| 17 |
|
| 18 |
for item in menu_items:
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
|
| 22 |
-
if
|
| 23 |
-
quantity = int(
|
| 24 |
-
item_name =
|
| 25 |
else:
|
| 26 |
quantity = 1 # Assume a default quantity of 1 if not specified
|
| 27 |
item_name = item.strip()
|
|
@@ -36,7 +32,7 @@ def calculate_total_calories(user_input):
|
|
| 36 |
# Check if the similarity score is above a certain threshold
|
| 37 |
threshold = 60
|
| 38 |
if closest_match_score < threshold:
|
| 39 |
-
results.append("Không tìm thấy thông tin thức ăn
|
| 40 |
continue
|
| 41 |
|
| 42 |
# Get the closest match menu item details
|
|
@@ -48,24 +44,24 @@ def calculate_total_calories(user_input):
|
|
| 48 |
# Calculate the total calories for the current menu item
|
| 49 |
item_calories = calories * quantity
|
| 50 |
total_calories += item_calories
|
| 51 |
-
results.append("Tên món ăn
|
| 52 |
-
results.append("Số lượng
|
| 53 |
-
results.append("Đơn
|
| 54 |
-
results.append("Lượng calories trong mỗi đơn
|
| 55 |
-
results.append("Tổng lượng calories của " + menu_name + "
|
| 56 |
results.append("") # Add an empty entry for spacing
|
| 57 |
|
| 58 |
results.append(str(total_calories) + " Kcals")
|
| 59 |
-
return "\n".join(results[0:-1])
|
| 60 |
|
| 61 |
output_text = [
|
| 62 |
-
gr.outputs.Textbox(label="Thông tin các thành phần trong bữa ăn
|
| 63 |
-
gr.outputs.Textbox(label="Tổng lượng calories của bữa ăn
|
| 64 |
]
|
| 65 |
|
| 66 |
def gradio_interface():
|
| 67 |
-
input_text = gr.inputs.Textbox(label="Hãy cho tôi biết bữa ăn hôm nay của bạn"
|
| 68 |
-
gr_interface = gr.Interface(fn=calculate_total_calories, inputs=input_text, outputs=output_text, title="Tính Toán Thực Đơn Hằng Ngày"
|
| 69 |
return gr_interface
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 1 |
from fuzzywuzzy import fuzz
|
| 2 |
import pandas as pd
|
| 3 |
import gradio as gr
|
|
|
|
| 5 |
def calculate_total_calories(user_input):
|
| 6 |
df_menu = pd.read_excel('calories.xlsx')
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
# Split the user input into individual menu items
|
| 9 |
menu_items = user_input.split(',')
|
| 10 |
|
|
|
|
| 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 = int(parts[0])
|
| 20 |
+
item_name = parts[1]
|
| 21 |
else:
|
| 22 |
quantity = 1 # Assume a default quantity of 1 if not specified
|
| 23 |
item_name = item.strip()
|
|
|
|
| 32 |
# Check if the similarity score is above a certain threshold
|
| 33 |
threshold = 60
|
| 34 |
if closest_match_score < threshold:
|
| 35 |
+
results.append("Không tìm thấy thông tin thức ăn: " + item_name)
|
| 36 |
continue
|
| 37 |
|
| 38 |
# Get the closest match menu item details
|
|
|
|
| 44 |
# Calculate the total calories for the current menu item
|
| 45 |
item_calories = calories * quantity
|
| 46 |
total_calories += item_calories
|
| 47 |
+
results.append("Tên món ăn: " + menu_name)
|
| 48 |
+
results.append("Số lượng: " + str(quantity))
|
| 49 |
+
results.append("Đơn vị: " + unit)
|
| 50 |
+
results.append("Lượng calories trong mỗi đơn vị: " + str(calories)+ " Kcals")
|
| 51 |
+
results.append("Tổng lượng calories của " + menu_name + ": " + str(item_calories)+ " Kcals")
|
| 52 |
results.append("") # Add an empty entry for spacing
|
| 53 |
|
| 54 |
results.append(str(total_calories) + " Kcals")
|
| 55 |
+
return "\n".join(results[0:-1]), results[-1]
|
| 56 |
|
| 57 |
output_text = [
|
| 58 |
+
gr.outputs.Textbox(label="Thông tin các thành phần trong bữa ăn"),
|
| 59 |
+
gr.outputs.Textbox(label="Tổng lượng calories của bữa ăn")
|
| 60 |
]
|
| 61 |
|
| 62 |
def gradio_interface():
|
| 63 |
+
input_text = gr.inputs.Textbox(label="Hãy cho tôi biết bữa ăn hôm nay của bạn")
|
| 64 |
+
gr_interface = gr.Interface(fn=calculate_total_calories, inputs=input_text, outputs=output_text, title="Tính Toán Thực Đơn Hằng Ngày", examples=["1 phần cơm tấm sườn bì, 2 trái chuối", "1 phần phở bò, 1 phần bánh Flan"])
|
| 65 |
return gr_interface
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|