|
|
|
|
|
import os |
|
import openai |
|
import gradio as gr |
|
|
|
|
|
def get_advice(bmi,temp, API_KEY, model="gpt-3.5-turbo"): |
|
openai.api_key = API_KEY |
|
messages = [{"role": "system", "content": "You can provide some dietary advice based on \ |
|
the user's BMI value. You can only give up to 3 suggestions"}, |
|
{"role": "user", "content": f'My BMI is {bmi}. What can I do to be healthier?'},] |
|
response = openai.chat.completions.create( |
|
model=model, |
|
max_tokens=200, |
|
messages=messages, |
|
temperature=temp, |
|
) |
|
return response.choices[0].message.content |
|
|
|
|
|
def get_gym(bmi,slide, temp, API_KEY, model="gpt-3.5-turbo"): |
|
openai.api_key = API_KEY |
|
messages = [{"role": "system", "content": "You are a great fitness coach and \ |
|
you will give users great fitness plans."}, |
|
{"role": "user", "content": f'My BMI is {bmi}. I want a {slide}-point weight\ |
|
loss plan, from 1 to 10. The higher the number, the faster the weight loss.'},] |
|
response = openai.chat.completions.create( |
|
model=model, |
|
max_tokens=200, |
|
messages=messages, |
|
temperature=temp, |
|
) |
|
return response.choices[0].message.content |
|
|
|
def BMI(year, month) -> int: |
|
year = int(year) / 100 |
|
bmi = int(month) / (year * year) |
|
if bmi < 18.5: |
|
return str(bmi)[:5], "過輕" |
|
elif bmi < 24: |
|
return str(bmi)[:5], "正常" |
|
elif bmi < 27: |
|
return str(bmi)[:5], "過重" |
|
elif bmi < 30: |
|
return str(bmi)[:5], "輕度肥胖" |
|
elif bmi < 35: |
|
return str(bmi)[:5], "中度肥胖" |
|
else: |
|
return str(bmi)[:5], "重度肥胖" |
|
|
|
|
|
|
|
|
|
year = gr.Textbox( |
|
label="生年", |
|
info="輸入你的西元出生年份", |
|
placeholder="Input your birth year here...") |
|
|
|
month = gr.Textbox( |
|
label="生月", |
|
info="輸入你的出生月份", |
|
placeholder="Input your birthday month here...",) |
|
|
|
day = gr.Textbox( |
|
label="生日", |
|
info="輸入你的出生日子", |
|
placeholder="Input your birthday day here...",) |
|
|
|
time = gr.Textbox( |
|
label="生時", |
|
info="輸入你的出生時間", |
|
placeholder="Input your birth time here...",) |
|
|
|
sex = gr.Textbox( |
|
label="性別", |
|
info="輸入你是男生或女生", |
|
placeholder="Input you are male or female here...",) |
|
|
|
output_bmi = gr.Textbox( |
|
value="", |
|
label="BMI 值", |
|
info="顯示BMI 數字", |
|
placeholder="BMI") |
|
|
|
output_state = gr.Textbox( |
|
value="", |
|
label="BMI 結果", |
|
info="診斷", |
|
placeholder="顯示診斷結果") |
|
|
|
advice = gr.Textbox( |
|
label="AI Advice", |
|
info="請選擇以下按鈕讓AI 根據你的BMI值給予的建議", |
|
placeholder="Ouput Text here...", |
|
lines=5,) |
|
|
|
btn = gr.Button( |
|
value="計算BMI值", |
|
variant="primary", scale=1) |
|
|
|
btn_advice = gr.Button( |
|
value="AI 建議", |
|
variant="primary", scale=2) |
|
|
|
btn_gym = gr.Button( |
|
value="AI 健身計畫", |
|
variant="primary", scale=1) |
|
|
|
key_box = gr.Textbox( |
|
label="Enter your API key", |
|
info="You have to provide your own OPENAI_API_KEY for this app to function properly", |
|
placeholder="Type OpenAI API KEY here...", |
|
type="password") |
|
|
|
|
|
slider = gr.Slider( |
|
minimum=1, |
|
maximum=10, |
|
step=1, |
|
label="減重速度", |
|
value=5, |
|
info="請選擇你的減重速度,數字越大,減重越快", |
|
) |
|
|
|
temperature = gr.Slider( |
|
minimum=0, |
|
maximum=1.0, |
|
value=0.3, |
|
step=0.05, |
|
label="Temperature", |
|
info=( |
|
"Temperature controls the degree of randomness in token selection. Lower " |
|
"temperatures are good for prompts that expect a true or correct response, " |
|
"while higher temperatures can lead to more diverse or unexpected results. " |
|
), |
|
) |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown(""" |
|
# BMI 計算器 |
|
簡易測量你的BMI值 |
|
""") |
|
with gr.Column(): |
|
with gr.Row(): |
|
year.render() |
|
month.render() |
|
day.render() |
|
time.render() |
|
sex.render() |
|
|
|
with gr.Row(): |
|
output_bmi.render() |
|
output_state.render() |
|
|
|
btn.render() |
|
btn.click(fn=BMI, |
|
inputs=[year, month], |
|
outputs=[output_bmi,output_state]) |
|
|
|
advice.render() |
|
|
|
with gr.Row(): |
|
key_box.render() |
|
btn_advice.render() |
|
btn_advice.click(fn=get_advice, inputs=[output_bmi,temperature,key_box], outputs=advice) |
|
btn_gym.render() |
|
btn_gym.click(fn=get_gym, inputs=[output_bmi,slider,temperature, key_box], outputs=advice) |
|
|
|
with gr.Accordion("settings", open=True): |
|
slider.render() |
|
temperature.render() |
|
|
|
demo.launch() |
|
|