File size: 5,295 Bytes
b4251f4 8697be1 b4251f4 8697be1 77eed2b b4251f4 8697be1 77eed2b 8697be1 77eed2b b4251f4 8697be1 77eed2b b4251f4 8697be1 b4251f4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
## chatGPT with Gradio 起手式
## 在你的資料夾新增 .env 檔案,並在裡面寫入 API_KEY=你的API金鑰
import os
import openai
import gradio as gr
## AI 建議
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, # this is the degree of randomness of the model's output
)
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, # this is the degree of randomness of the model's output
)
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], "重度肥胖"
# 建立 components
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() # 顯示BMI值結果
output_state.render() # 顯示BMI診斷結果
btn.render() # 顯示計算BMI值按鈕
btn.click(fn=BMI,
inputs=[year, month],
outputs=[output_bmi,output_state])
advice.render() # 顯示AI建議結果的文字框
with gr.Row():
key_box.render() # 顯示API金鑰輸入框
btn_advice.render() # 顯示AI建議按鈕
btn_advice.click(fn=get_advice, inputs=[output_bmi,temperature,key_box], outputs=advice)
btn_gym.render() # 顯示AI健身計畫按鈕
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()
|