Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| def bmi_calculator(weight, height): | |
| try: | |
| height_m = height / 100 # 公分轉換成公尺 | |
| bmi = weight / (height_m ** 2) | |
| if bmi < 18.5: | |
| category = "體重過輕" | |
| elif 18.5 <= bmi < 24: | |
| category = "正常範圍" | |
| elif 24 <= bmi < 27: | |
| category = "過重" | |
| elif 27 <= bmi < 30: | |
| category = "輕度肥胖" | |
| elif 30 <= bmi < 35: | |
| category = "中度肥胖" | |
| else: | |
| category = "重度肥胖" | |
| return round(bmi, 2), category | |
| except Exception as e: | |
| return None, f"錯誤: {e}" | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🧮 BMI 計算器") | |
| with gr.Row(): | |
| weight = gr.Number(label="體重 (kg)") | |
| height = gr.Number(label="身高 (cm)") | |
| calc_btn = gr.Button("計算 BMI") | |
| output_bmi = gr.Number(label="BMI 值", interactive=False) | |
| output_cat = gr.Textbox(label="結果說明", interactive=False) | |
| calc_btn.click(fn=bmi_calculator, inputs=[weight, height], outputs=[output_bmi, output_cat]) | |
| demo.launch() | |