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()