Spaces:
Sleeping
Sleeping
| # app.py | |
| # Simple BMI Calculator - Hugging Face Space (Gradio) | |
| # No pip installs here — just define your Gradio app. | |
| import gradio as gr | |
| def weight_to_kg(weight, unit): | |
| if unit == "kg": | |
| return weight | |
| elif unit == "lb": | |
| return weight * 0.45359237 | |
| else: | |
| return None | |
| def height_to_m(height, unit): | |
| if unit == "m": | |
| return height | |
| elif unit == "cm": | |
| return height / 100.0 | |
| elif unit == "in": | |
| return height * 0.0254 | |
| elif unit == "ft": | |
| return height * 0.3048 | |
| else: | |
| return None | |
| def bmi_category(bmi): | |
| if bmi < 18.5: | |
| return "Underweight" | |
| elif bmi < 25: | |
| return "Normal weight" | |
| elif bmi < 30: | |
| return "Overweight" | |
| else: | |
| return "Obesity" | |
| def calculate_bmi(weight, weight_unit, height, height_unit, decimals): | |
| # Basic validation | |
| if weight is None or height is None: | |
| return "⚠️ Please enter both weight and height." | |
| try: | |
| weight = float(weight) | |
| height = float(height) | |
| except: | |
| return "⚠️ Please enter numeric values for weight and height." | |
| if weight <= 0: | |
| return "⚠️ Weight must be greater than zero." | |
| if height <= 0: | |
| return "⚠️ Height must be greater than zero." | |
| kg = weight_to_kg(weight, weight_unit) | |
| m = height_to_m(height, height_unit) | |
| if kg is None or m is None: | |
| return "⚠️ Invalid unit selected." | |
| if m <= 0: | |
| return "⚠️ Converted height must be greater than zero." | |
| bmi = kg / (m * m) | |
| try: | |
| decimals = int(decimals) | |
| except: | |
| decimals = 2 | |
| bmi_str = f"{bmi:.{decimals}f}" | |
| category = bmi_category(bmi) | |
| note = ( | |
| "ℹ️ Note: BMI is a rough estimate and may be inaccurate for athletes, " | |
| "children, elderly people, or pregnant individuals. For medical advice, consult a professional." | |
| ) | |
| return f"✅ BMI = {bmi_str} kg/m² — {category}\n\n{note}" | |
| with gr.Blocks(title="Simple BMI Calculator") as demo: | |
| gr.Markdown("# 🧮 Simple BMI Calculator\nEnter weight and height, choose units, and click **Calculate**.") | |
| with gr.Row(): | |
| weight = gr.Number(label="Weight", value=70) | |
| weight_unit = gr.Dropdown(["kg", "lb"], value="kg", label="Weight unit") | |
| with gr.Row(): | |
| height = gr.Number(label="Height", value=170) | |
| height_unit = gr.Dropdown(["cm", "m", "in", "ft"], value="cm", label="Height unit") | |
| decimals = gr.Slider(0, 5, value=2, step=1, label="Decimal precision") | |
| calc_btn = gr.Button("Calculate BMI") | |
| output = gr.Textbox(label="Result", lines=6) | |
| calc_btn.click(fn=calculate_bmi, inputs=[weight, weight_unit, height, height_unit, decimals], outputs=output) | |
| gr.Examples( | |
| examples=[ | |
| [70, "kg", 170, "cm", 2], | |
| [50, "kg", 160, "cm", 2], | |
| [200, "lb", 72, "in", 2], | |
| [90, "kg", 1.8, "m", 2], | |
| ], | |
| inputs=[weight, weight_unit, height, height_unit, decimals], | |
| label="Examples (click to fill)" | |
| ) | |
| # When Spaces runs this file it will serve the Gradio app automatically. | |
| if __name__ == "__main__": | |
| demo.launch() | |