Spaces:
Build error
Build error
Commit
·
57199ed
1
Parent(s):
007bde0
Upload suggestion.py
Browse files- suggestion.py +132 -0
suggestion.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
def calculate_bmi(height, weight):
|
| 4 |
+
# Calculate BMI using the provided height and weight
|
| 5 |
+
# BMI Formula: weight (kg) / (height (m) ** 2)
|
| 6 |
+
height_m = height / 100 # Convert height from cm to m
|
| 7 |
+
bmi = weight / (height_m ** 2)
|
| 8 |
+
return bmi
|
| 9 |
+
|
| 10 |
+
def calculate_bmr(age, gender, height, weight):
|
| 11 |
+
# Calculate Basal Metabolic Rate (BMR) using the provided age, gender, height, and weight
|
| 12 |
+
if gender == "Nam":
|
| 13 |
+
# BMR Formula for males: 88.362 + (13.397 x weight in kg) + (4.799 x height in cm) - (5.677 x age in years)
|
| 14 |
+
bmr = 88.362 + (13.397 * weight) + (4.799 * height) - (5.677 * age)
|
| 15 |
+
else:
|
| 16 |
+
# BMR Formula for females: 447.593 + (9.247 x weight in kg) + (3.098 x height in cm) - (4.330 x age in years)
|
| 17 |
+
bmr = 447.593 + (9.247 * weight) + (3.098 * height) - (4.330 * age)
|
| 18 |
+
return bmr
|
| 19 |
+
|
| 20 |
+
def calculate_tdee(bmr, activity_level):
|
| 21 |
+
# Calculate Total Daily Energy Expenditure (TDEE) using the provided BMR and activity level
|
| 22 |
+
tdee = bmr * activity_level
|
| 23 |
+
return tdee
|
| 24 |
+
|
| 25 |
+
def calculate_daily_calories_goal(tdee, goal):
|
| 26 |
+
# Calculate the daily calorie goal based on the provided TDEE and goal
|
| 27 |
+
if goal == "Giảm cân":
|
| 28 |
+
calories_goal = tdee - 500 # Aim for a 500 calorie deficit per day for weight loss
|
| 29 |
+
elif goal == "Tăng cân":
|
| 30 |
+
calories_goal = tdee + 500 # Aim for a 500 calorie surplus per day for weight gain
|
| 31 |
+
else:
|
| 32 |
+
calories_goal = tdee # Maintain current weight
|
| 33 |
+
if calories_goal < 0 :
|
| 34 |
+
return 0
|
| 35 |
+
else :
|
| 36 |
+
return calories_goal
|
| 37 |
+
|
| 38 |
+
def get_activity_factor(activity_input):
|
| 39 |
+
"""
|
| 40 |
+
Get the activity factor based on the selected option.
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
activity_input (str): Selected activity option.
|
| 44 |
+
|
| 45 |
+
Returns:
|
| 46 |
+
float: Activity factor based on the selected option.
|
| 47 |
+
"""
|
| 48 |
+
activity_factor_map = {
|
| 49 |
+
'Không': 1.2,
|
| 50 |
+
'Có': 1.55,
|
| 51 |
+
'Thường Xuyên': 1.725
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
return activity_factor_map.get(activity_input, 1.2)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def process(height, weight, age, gender, activities, goal):
|
| 59 |
+
# Determine activity level
|
| 60 |
+
activity_level = get_activity_factor(activities)
|
| 61 |
+
|
| 62 |
+
# Calculate BMR
|
| 63 |
+
bmr = calculate_bmr(age, gender, height, weight)
|
| 64 |
+
|
| 65 |
+
# Calculate TDEE
|
| 66 |
+
tdee = calculate_tdee(bmr, activity_level)
|
| 67 |
+
|
| 68 |
+
# Calculate BMI
|
| 69 |
+
bmi = calculate_bmi(height, weight)
|
| 70 |
+
|
| 71 |
+
# Determine BMI category based on gender
|
| 72 |
+
bmi_category = ""
|
| 73 |
+
if gender == "Nam":
|
| 74 |
+
if bmi < 20:
|
| 75 |
+
bmi_category = "Thiếu cân, cần có chế độ ăn phù hợp để cải thiện tình trạng này"
|
| 76 |
+
elif 20 <= bmi < 25:
|
| 77 |
+
bmi_category = "Bình thường, thậm chí ở trong tình trạng tốt nếu bạn thường xuyên tập thể dục và ăn một chế độ ăn hợp lý"
|
| 78 |
+
elif 25 <= bmi < 30:
|
| 79 |
+
bmi_category = "Thừa cân, cần áp dụng biện pháp để khắc phục tình trạng trên"
|
| 80 |
+
else:
|
| 81 |
+
bmi_category = "Béo phì nặng, nếu không cải thiện sớm có thể gây ra các vấn đề liên quan đến tiêu hóa, hệ tuần hoàn, v.v."
|
| 82 |
+
else:
|
| 83 |
+
if bmi < 18:
|
| 84 |
+
bmi_category = "Thiếu cân, thiếu dinh dưỡng"
|
| 85 |
+
elif 18 <= bmi < 23:
|
| 86 |
+
bmi_category = "Bình thường"
|
| 87 |
+
elif 23 <= bmi < 30:
|
| 88 |
+
bmi_category = "Thừa cân"
|
| 89 |
+
else:
|
| 90 |
+
bmi_category = "Béo phì"
|
| 91 |
+
|
| 92 |
+
# Calculate daily calorie goal
|
| 93 |
+
calo_suggestion = calculate_daily_calories_goal(tdee, goal)
|
| 94 |
+
|
| 95 |
+
return bmi, bmr, tdee, bmi_category, calo_suggestion
|
| 96 |
+
|
| 97 |
+
inputs = [
|
| 98 |
+
gr.inputs.Number(label=" Chiều Cao (cm)"),
|
| 99 |
+
gr.inputs.Number(label=" Cân Nặng (kg)"),
|
| 100 |
+
gr.inputs.Number(label="Tuổi"),
|
| 101 |
+
gr.inputs.Radio(['Nam', 'Nữ'], label="Giới Tính"),
|
| 102 |
+
gr.inputs.Radio(['Không', "Có", 'Thường Xuyên'], label="Hoạt Động Thể Thao", default="Không" ),
|
| 103 |
+
gr.inputs.Radio(['Giảm cân', 'Tăng cân', 'Duy trì'], label="Mục Tiêu", default="Giảm cân")
|
| 104 |
+
]
|
| 105 |
+
|
| 106 |
+
outputs = [
|
| 107 |
+
gr.outputs.Textbox(label="Chỉ số BMI"),
|
| 108 |
+
gr.outputs.Textbox(label="Chỉ số BMR"),
|
| 109 |
+
gr.outputs.Textbox(label="Chỉ số TDEE"),
|
| 110 |
+
gr.outputs.Textbox(label="Lượng Calories mỗi ngày nên là:")
|
| 111 |
+
]
|
| 112 |
+
|
| 113 |
+
def do(height, weight, age, gender, activities, goal):
|
| 114 |
+
bmi, bmr, tdee, bmi_category, calorie_goal = process(height, weight, age, gender, activities, goal)
|
| 115 |
+
|
| 116 |
+
# Format the values with 2 decimal places
|
| 117 |
+
bmi = "{:.1f}".format(bmi)
|
| 118 |
+
bmr = "{:.1f}".format(bmr)
|
| 119 |
+
tdee = "{:.1f}".format(tdee)
|
| 120 |
+
calorie_goal = "{:.1f}".format(calorie_goal)
|
| 121 |
+
|
| 122 |
+
bmr = f"{bmr} / Ngày"
|
| 123 |
+
tdee = f"{tdee} / Ngày"
|
| 124 |
+
calorie_goal = f"{calorie_goal} / Ngày "
|
| 125 |
+
return bmi,bmr,tdee, calorie_goal
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
# Create a Gradio interface
|
| 129 |
+
interface = gr.Interface(fn=do, inputs=inputs, outputs=outputs,allow_flagging="never")
|
| 130 |
+
|
| 131 |
+
# Launch the interface
|
| 132 |
+
interface.launch()
|