update result format
Browse files
app.py
CHANGED
|
@@ -48,11 +48,15 @@ def predict_bmi(image, height_in_inches):
|
|
| 48 |
# Use the midpoint BMI for the predicted class
|
| 49 |
bmi_prediction = get_midpoint_bmi(predicted_bmi_class)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
# Create the JSON output
|
| 52 |
result = {
|
| 53 |
-
"weightCategory": f"{predicted_bmi_class} - {bmi_prediction}",
|
| 54 |
"bmiPrediction": f"{bmi_prediction:.2f}",
|
| 55 |
"height": str(height_in_inches),
|
|
|
|
| 56 |
}
|
| 57 |
|
| 58 |
return result
|
|
@@ -65,6 +69,21 @@ def get_midpoint_bmi(weight_category):
|
|
| 65 |
return mid # Return the first midpoint found in the given range
|
| 66 |
return None
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# Create Gradio interface with updated components
|
| 69 |
interface = gr.Interface(
|
| 70 |
fn=predict_bmi,
|
|
|
|
| 48 |
# Use the midpoint BMI for the predicted class
|
| 49 |
bmi_prediction = get_midpoint_bmi(predicted_bmi_class)
|
| 50 |
|
| 51 |
+
# Calculate the weight based on the predicted BMI and user-provided height
|
| 52 |
+
predicted_weight = calculate_weight(bmi_prediction, height_in_inches)
|
| 53 |
+
|
| 54 |
# Create the JSON output
|
| 55 |
result = {
|
| 56 |
+
"weightCategory": f"{predicted_bmi_class} - ({get_bmi_label(bmi_prediction)})",
|
| 57 |
"bmiPrediction": f"{bmi_prediction:.2f}",
|
| 58 |
"height": str(height_in_inches),
|
| 59 |
+
"predictedWeight": f"{predicted_weight:.2f} lbs"
|
| 60 |
}
|
| 61 |
|
| 62 |
return result
|
|
|
|
| 69 |
return mid # Return the first midpoint found in the given range
|
| 70 |
return None
|
| 71 |
|
| 72 |
+
def get_bmi_label(bmi_value):
|
| 73 |
+
"""Return the BMI range label corresponding to the predicted BMI."""
|
| 74 |
+
for category, ranges in bmi_ranges.items():
|
| 75 |
+
for label, (low, high, mid) in ranges.items():
|
| 76 |
+
if low <= bmi_value <= high:
|
| 77 |
+
return label
|
| 78 |
+
return "Unknown"
|
| 79 |
+
|
| 80 |
+
def calculate_weight(bmi, height_in_inches):
|
| 81 |
+
"""Calculate the weight from BMI and height (in inches)."""
|
| 82 |
+
height_in_meters = height_in_inches * 0.0254 # Convert height to meters
|
| 83 |
+
weight_kg = bmi * (height_in_meters ** 2) # BMI formula to calculate weight
|
| 84 |
+
weight_lbs = weight_kg * 2.20462 # Convert kg to lbs
|
| 85 |
+
return weight_lbs
|
| 86 |
+
|
| 87 |
# Create Gradio interface with updated components
|
| 88 |
interface = gr.Interface(
|
| 89 |
fn=predict_bmi,
|