Spaces:
Sleeping
Sleeping
File size: 1,895 Bytes
00554d3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import gradio as gr
# Function to calculate BMI
def calculate_bmi(weight, height, unit, precision):
try:
if weight <= 0 or height <= 0:
return "โ Please enter positive numbers."
# Convert height and weight depending on unit system
if unit == "Metric (kg, cm)":
weight_kg = weight
height_m = height / 100 # convert cm โ m
else: # Imperial (lbs, inches)
weight_kg = weight * 0.45359237
height_m = height * 0.0254
bmi = weight_kg / (height_m ** 2)
bmi = round(bmi, precision)
# WHO classification
if bmi < 18.5:
category = "Underweight ๐ก"
advice = "You may need to gain some healthy weight."
elif 18.5 <= bmi < 25:
category = "Normal โ
"
advice = "Great! Maintain your healthy lifestyle."
elif 25 <= bmi < 30:
category = "Overweight ๐ "
advice = "Consider balanced diet and exercise."
else:
category = "Obese ๐ด"
advice = "Seek medical advice for weight management."
return f"๐ BMI: {bmi}\n๐ Category: {category}\n๐ก Advice: {advice}"
except Exception as e:
return f"Error: {str(e)}"
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# ๐งฎ BMI Calculator")
with gr.Row():
unit = gr.Radio(["Metric (kg, cm)", "Imperial (lbs, inches)"], value="Metric (kg, cm)", label="Unit System")
with gr.Row():
weight = gr.Number(label="Weight (kg or lbs)")
height = gr.Number(label="Height (cm or inches)")
precision = gr.Slider(0, 4, value=2, step=1, label="Decimal Precision")
calc_btn = gr.Button("Calculate BMI")
result = gr.Textbox(label="Result", lines=4)
calc_btn.click(calculate_bmi, [weight, height, unit, precision], result)
# Run
demo.launch()
|