Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| def bmi(height, weight): | |
| try: | |
| h = float(height) | |
| w = float(weight) | |
| if h > 10: | |
| h = h / 100 | |
| bmi = w / (h**2) | |
| except: | |
| return "身高或體重的輸入格式錯誤!" | |
| return f"BMI = {bmi:.2f}!" | |
| # 建立 Gradio 介面 | |
| demo = gr.Interface(fn=bmi, | |
| inputs=[ | |
| gr.Textbox(label="身高", placeholder="這裡輸入身高(單位:公尺或公分)"), | |
| gr.Textbox(label="體重", placeholder="這裡輸入體重(單位:公斤)") | |
| ], #使用gr.Textbox物件的方式可以設定更多的細節參數 | |
| outputs=gr.Textbox(label="BMI", value=""), | |
| title="BMI計算機", | |
| description="輸入身高跟體重,可以幫你計算BMI值") | |
| # 啟動 Web 應用 | |
| demo.launch() | |
| # %% | |