Spaces:
Running
Running
update bmi data structure
Browse files
app.py
CHANGED
@@ -37,41 +37,85 @@ bmi_ranges_model2 = {
|
|
37 |
"BMI 40.0 and above"
|
38 |
]
|
39 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
def predict_bmi(image, height_in_inches):
|
42 |
# Prepare inputs for Model 1
|
43 |
inputs_model1 = processor(text=bmi_classes_model1, images=image, return_tensors="pt", padding=True)
|
44 |
outputs_model1 = model(**inputs_model1)
|
45 |
probs_model1 = outputs_model1.logits_per_image.softmax(dim=1)
|
|
|
46 |
|
47 |
# Get the broad category prediction from Model 1
|
48 |
max_prob_index_model1 = probs_model1.argmax().item()
|
49 |
predicted_bmi_class = bmi_classes_model1[max_prob_index_model1]
|
|
|
50 |
|
51 |
# Select class names for Model 2 based on Model 1's prediction
|
52 |
model2_classes = bmi_ranges_model2[predicted_bmi_class.lower()]
|
|
|
53 |
|
54 |
# Prepare inputs for Model 2
|
55 |
inputs_model2 = processor(text=model2_classes, images=image, return_tensors="pt", padding=True)
|
56 |
outputs_model2 = model(**inputs_model2)
|
57 |
probs_model2 = outputs_model2.logits_per_image.softmax(dim=1)
|
|
|
58 |
|
59 |
# Get the finer range prediction from Model 2
|
60 |
max_prob_index_model2 = probs_model2.argmax().item()
|
61 |
finer_bmi_range = model2_classes[max_prob_index_model2]
|
|
|
62 |
|
63 |
# Determine the BMI prediction based on the range
|
64 |
bmi_prediction = get_adjusted_bmi(predicted_bmi_class, finer_bmi_range)
|
|
|
65 |
|
66 |
# Calculate weight using user-provided height
|
67 |
-
predicted_weight = calculate_weight(bmi_prediction, height_in_inches)
|
68 |
|
69 |
# Create the JSON output
|
70 |
result = {
|
71 |
"weightCategory": f"{predicted_bmi_class} - {finer_bmi_range}",
|
72 |
"bmiPrediction": f"{bmi_prediction:.2f}",
|
73 |
"height": str(height_in_inches),
|
74 |
-
"predictedWeight": f"{predicted_weight:.2f} lbs"
|
75 |
}
|
76 |
|
77 |
return result
|
@@ -79,10 +123,12 @@ def predict_bmi(image, height_in_inches):
|
|
79 |
def get_adjusted_bmi(weight_category, finer_range):
|
80 |
"""Return the appropriate BMI value for the given finer range within the weight category."""
|
81 |
category_ranges = bmi_ranges.get(weight_category.lower())
|
|
|
82 |
for range_label, (low, high, mid) in category_ranges.items():
|
|
|
83 |
if "BMI <" in range_label or "BMI ≥" in range_label:
|
84 |
return high if "BMI <" in range_label else low
|
85 |
-
elif range_label == finer_range:
|
86 |
return mid
|
87 |
return None
|
88 |
|
|
|
37 |
"BMI 40.0 and above"
|
38 |
]
|
39 |
}
|
40 |
+
bmi_mapp = {
|
41 |
+
"BMI less than 16.0":"BMI < 16.0",
|
42 |
+
"BMI between 16.0 and 16.99":"16.0 ≤ BMI ≤ 16.99",
|
43 |
+
"BMI between 17.0 and 18.49":"17.0 ≤ BMI ≤ 18.49",
|
44 |
+
"BMI between 18.5 and 20.4":"18.5 ≤ BMI ≤ 20.4",
|
45 |
+
"BMI between 20.5 and 22.4":"20.5 ≤ BMI ≤ 22.4",
|
46 |
+
"BMI between 22.5 and 24.9":"22.5 ≤ BMI ≤ 24.9",
|
47 |
+
"BMI between 25.0 and 26.9":"25.0 ≤ BMI ≤ 26.9",
|
48 |
+
"BMI between 27.0 and 28.9":"27.0 ≤ BMI ≤ 28.9",
|
49 |
+
"BMI between 29.0 and 29.9":"29.0 ≤ BMI ≤ 29.9",
|
50 |
+
"BMI between 30.0 and 34.9": "30.0 ≤ BMI ≤ 34.9",
|
51 |
+
"BMI between 35.0 and 39.9": "35.0 ≤ BMI ≤ 39.9",
|
52 |
+
"BMI 40.0 and above":"BMI ≥ 40.0"
|
53 |
+
}
|
54 |
+
|
55 |
+
# Define BMI ranges with boundaries and midpoints for adjusted BMI calculation
|
56 |
+
bmi_ranges = {
|
57 |
+
"underweight (x < 18.5 bmi)": {
|
58 |
+
"BMI < 16.0": (0, 16.0, 16.0), # Upper boundary 16.0
|
59 |
+
"16.0 ≤ BMI ≤ 16.99": (16.0, 16.99, 16.5), # Midpoint 16.5
|
60 |
+
"17.0 ≤ BMI ≤ 18.49": (17.0, 18.49, 17.75) # Midpoint 17.75
|
61 |
+
},
|
62 |
+
"normal weight (18.5 < x < 25 bmi)": {
|
63 |
+
"18.5 ≤ BMI ≤ 20.4": (18.5, 20.4, 19.45), # Midpoint 19.45
|
64 |
+
"20.5 ≤ BMI ≤ 22.4": (20.5, 22.4, 21.45), # Midpoint 21.45
|
65 |
+
"22.5 ≤ BMI ≤ 24.9": (22.5, 24.9, 23.7) # Midpoint 23.7
|
66 |
+
},
|
67 |
+
"overweight (25 bmi < x < 30)": {
|
68 |
+
"25.0 ≤ BMI ≤ 26.9": (25.0, 26.9, 25.95), # Midpoint 25.95
|
69 |
+
"27.0 ≤ BMI ≤ 28.9": (27.0, 28.9, 27.95), # Midpoint 27.95
|
70 |
+
"29.0 ≤ BMI ≤ 29.9": (29.0, 29.9, 29.45) # Midpoint 29.45
|
71 |
+
},
|
72 |
+
"obesity (x > 30 bmi)": {
|
73 |
+
"30.0 ≤ BMI ≤ 34.9": (30.0, 34.9, 32.5), # Midpoint 32.5
|
74 |
+
"35.0 ≤ BMI ≤ 39.9": (35.0, 39.9, 37.45), # Midpoint 37.45
|
75 |
+
"BMI ≥ 40.0": (40.0, 100, 40.0) # Lower boundary 40.0
|
76 |
+
}
|
77 |
+
}
|
78 |
|
79 |
def predict_bmi(image, height_in_inches):
|
80 |
# Prepare inputs for Model 1
|
81 |
inputs_model1 = processor(text=bmi_classes_model1, images=image, return_tensors="pt", padding=True)
|
82 |
outputs_model1 = model(**inputs_model1)
|
83 |
probs_model1 = outputs_model1.logits_per_image.softmax(dim=1)
|
84 |
+
print(probs_model1,'probs_model1')
|
85 |
|
86 |
# Get the broad category prediction from Model 1
|
87 |
max_prob_index_model1 = probs_model1.argmax().item()
|
88 |
predicted_bmi_class = bmi_classes_model1[max_prob_index_model1]
|
89 |
+
print(predicted_bmi_class,'predicted_bmi_class')
|
90 |
|
91 |
# Select class names for Model 2 based on Model 1's prediction
|
92 |
model2_classes = bmi_ranges_model2[predicted_bmi_class.lower()]
|
93 |
+
print(model2_classes,'model2_classes')
|
94 |
|
95 |
# Prepare inputs for Model 2
|
96 |
inputs_model2 = processor(text=model2_classes, images=image, return_tensors="pt", padding=True)
|
97 |
outputs_model2 = model(**inputs_model2)
|
98 |
probs_model2 = outputs_model2.logits_per_image.softmax(dim=1)
|
99 |
+
print(probs_model2,'probs_model2')
|
100 |
|
101 |
# Get the finer range prediction from Model 2
|
102 |
max_prob_index_model2 = probs_model2.argmax().item()
|
103 |
finer_bmi_range = model2_classes[max_prob_index_model2]
|
104 |
+
print(finer_bmi_range,'finer_bmi_range')
|
105 |
|
106 |
# Determine the BMI prediction based on the range
|
107 |
bmi_prediction = get_adjusted_bmi(predicted_bmi_class, finer_bmi_range)
|
108 |
+
print(bmi_prediction)
|
109 |
|
110 |
# Calculate weight using user-provided height
|
111 |
+
#predicted_weight = calculate_weight(bmi_prediction, height_in_inches)
|
112 |
|
113 |
# Create the JSON output
|
114 |
result = {
|
115 |
"weightCategory": f"{predicted_bmi_class} - {finer_bmi_range}",
|
116 |
"bmiPrediction": f"{bmi_prediction:.2f}",
|
117 |
"height": str(height_in_inches),
|
118 |
+
#"predictedWeight": f"{predicted_weight:.2f} lbs"
|
119 |
}
|
120 |
|
121 |
return result
|
|
|
123 |
def get_adjusted_bmi(weight_category, finer_range):
|
124 |
"""Return the appropriate BMI value for the given finer range within the weight category."""
|
125 |
category_ranges = bmi_ranges.get(weight_category.lower())
|
126 |
+
print(category_ranges,'category_ranges')
|
127 |
for range_label, (low, high, mid) in category_ranges.items():
|
128 |
+
print(range_label,'range_label')
|
129 |
if "BMI <" in range_label or "BMI ≥" in range_label:
|
130 |
return high if "BMI <" in range_label else low
|
131 |
+
elif range_label == bmi_mapp[finer_range]:
|
132 |
return mid
|
133 |
return None
|
134 |
|