Spaces:
Sleeping
Sleeping
TuanScientist
commited on
Commit
•
22e17e4
1
Parent(s):
eae39b8
Upload 2 files
Browse files- calories.xlsx +0 -0
- calories_compare.py +73 -0
calories.xlsx
ADDED
Binary file (18.1 kB). View file
|
|
calories_compare.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
from fuzzywuzzy import fuzz
|
3 |
+
import pandas as pd
|
4 |
+
import gradio as gr
|
5 |
+
|
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 |
+
|
15 |
+
total_calories = 0
|
16 |
+
results = []
|
17 |
+
|
18 |
+
for item in menu_items:
|
19 |
+
# Extract the quantity and item name from each menu item using regular expressions
|
20 |
+
match = re.match(pattern, item.strip())
|
21 |
+
|
22 |
+
if match:
|
23 |
+
quantity = int(match.group(1))
|
24 |
+
item_name = match.group(2)
|
25 |
+
else:
|
26 |
+
quantity = 1 # Assume a default quantity of 1 if not specified
|
27 |
+
item_name = item.strip()
|
28 |
+
|
29 |
+
# Calculate the similarity scores between the item name and menu item names
|
30 |
+
similarity_scores = df_menu['food'].apply(lambda x: fuzz.token_set_ratio(x.lower(), item_name.lower()))
|
31 |
+
|
32 |
+
# Find the closest match with the highest similarity score
|
33 |
+
closest_match_index = similarity_scores.idxmax()
|
34 |
+
closest_match_score = similarity_scores[closest_match_index]
|
35 |
+
|
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 : " + item_name)
|
40 |
+
continue
|
41 |
+
|
42 |
+
# Get the closest match menu item details
|
43 |
+
closest_match = df_menu.loc[closest_match_index]
|
44 |
+
menu_name = closest_match['food']
|
45 |
+
unit = closest_match['detail']
|
46 |
+
calories = closest_match['calo']
|
47 |
+
|
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 : " + menu_name)
|
52 |
+
results.append("Số lượng : " + str(quantity))
|
53 |
+
results.append("Đơn vị : " + unit)
|
54 |
+
results.append("Lượng calories trong mỗi đơn vị : " + str(calories)+ " Kcals")
|
55 |
+
results.append("Tổng lượng calories của " + menu_name + " : " + str(item_calories)+ " Kcals")
|
56 |
+
results.append("") # Add an empty entry for spacing
|
57 |
+
|
58 |
+
results.append(str(total_calories) + " Kcals")
|
59 |
+
return "\n".join(results[0:-1]) , results[-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" , 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"])
|
69 |
+
return gr_interface
|
70 |
+
|
71 |
+
if __name__ == "__main__":
|
72 |
+
gr_interface = gradio_interface()
|
73 |
+
gr_interface.launch()
|