Bargerya commited on
Commit
b4251f4
1 Parent(s): 99de029

Add application file

Browse files
Files changed (1) hide show
  1. app.py +162 -0
app.py ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## chatGPT with Gradio 起手式
2
+ ## 在你的資料夾新增 .env 檔案,並在裡面寫入 API_KEY=你的API金鑰
3
+ import os
4
+ import openai
5
+ import gradio as gr
6
+ from dotenv import load_dotenv, find_dotenv
7
+ _ = load_dotenv(find_dotenv()) # read local .env file
8
+
9
+ API_KEY = os.environ['OPENAI_API_KEY']
10
+
11
+ print (API_KEY)
12
+
13
+ ## AI 建議
14
+ def get_advice(bmi,temp, API_KEY, model="gpt-3.5-turbo"):
15
+ openai.api_key = API_KEY
16
+ messages = [{"role": "system", "content": "You can provide some dietary advice based on \
17
+ the user's BMI value. You can only give up to 3 suggestions"},
18
+ {"role": "user", "content": f'My BMI is {bmi}. What can I do to be healthier?'},]
19
+ response = openai.chat.completions.create(
20
+ model=model,
21
+ max_tokens=200,
22
+ messages=messages,
23
+ temperature=temp, # this is the degree of randomness of the model's output
24
+ )
25
+ return response.choices[0].message.content
26
+
27
+ ## 健身計畫
28
+ def get_gym(bmi,slide, temp, API_KEY, model="gpt-3.5-turbo"):
29
+ openai.api_key = API_KEY
30
+ messages = [{"role": "system", "content": "You are a great fitness coach and \
31
+ you will give users great fitness plans."},
32
+ {"role": "user", "content": f'My BMI is {bmi}. I want a {slide}-point weight\
33
+ loss plan, from 1 to 10. The higher the number, the faster the weight loss.'},]
34
+ response = openai.chat.completions.create(
35
+ model=model,
36
+ max_tokens=200,
37
+ messages=messages,
38
+ temperature=temp, # this is the degree of randomness of the model's output
39
+ )
40
+ return response.choices[0].message.content
41
+
42
+ def BMI(height, weight) -> int:
43
+ height = int(height) / 100
44
+ bmi = int(weight) / (height * height)
45
+ if bmi < 18.5:
46
+ return str(bmi)[:5], "過輕"
47
+ elif bmi < 24:
48
+ return str(bmi)[:5], "正常"
49
+ elif bmi < 27:
50
+ return str(bmi)[:5], "過重"
51
+ elif bmi < 30:
52
+ return str(bmi)[:5], "輕度肥胖"
53
+ elif bmi < 35:
54
+ return str(bmi)[:5], "中度肥胖"
55
+ else:
56
+ return str(bmi)[:5], "重度肥胖"
57
+
58
+
59
+
60
+ # 建立 components
61
+ height = gr.Textbox(
62
+ label="身高",
63
+ info="輸入你的身高(cm)",
64
+ placeholder="Type your hiegh here...")
65
+
66
+ weight = gr.Textbox(
67
+ label="體重",
68
+ info="輸入你的體重(kg)",
69
+ placeholder="Type your weight here...",)
70
+
71
+ output_bmi = gr.Textbox(
72
+ value="",
73
+ label="BMI 值",
74
+ info="顯示BMI 數字",
75
+ placeholder="BMI")
76
+
77
+ output_state = gr.Textbox(
78
+ value="",
79
+ label="BMI 結果",
80
+ info="診斷",
81
+ placeholder="顯示診斷結果")
82
+
83
+ advice = gr.Textbox(
84
+ label="AI Advice",
85
+ info="請選擇以下按鈕讓AI 根據你的BMI值給予的建議",
86
+ placeholder="Ouput Text here...",
87
+ lines=5,)
88
+
89
+ btn = gr.Button(
90
+ value="計算BMI值",
91
+ variant="primary", scale=1)
92
+
93
+ btn_advice = gr.Button(
94
+ value="AI 建議",
95
+ variant="primary", scale=2)
96
+
97
+ btn_gym = gr.Button(
98
+ value="AI 健身計畫",
99
+ variant="primary", scale=1)
100
+
101
+ key_box = gr.Textbox(
102
+ label="Enter your API key",
103
+ info="You have to provide your own OPENAI_API_KEY for this app to function properly",
104
+ placeholder="Type OpenAI API KEY here...",
105
+ type="password")
106
+
107
+
108
+ slider = gr.Slider(
109
+ minimum=1,
110
+ maximum=10,
111
+ step=1,
112
+ label="減重速度",
113
+ value=5,
114
+ info="請選擇你的減重速度,數字越大,減重越快",
115
+ )
116
+
117
+ temperature = gr.Slider(
118
+ minimum=0,
119
+ maximum=1.0,
120
+ value=0.3,
121
+ step=0.05,
122
+ label="Temperature",
123
+ info=(
124
+ "Temperature controls the degree of randomness in token selection. Lower "
125
+ "temperatures are good for prompts that expect a true or correct response, "
126
+ "while higher temperatures can lead to more diverse or unexpected results. "
127
+ ),
128
+ )
129
+
130
+ with gr.Blocks() as demo:
131
+ gr.Markdown("""
132
+ # BMI 計算器
133
+ 簡易測量你的BMI值
134
+ """)
135
+ with gr.Column():
136
+ with gr.Row():
137
+ height.render() # 顯示身高輸入框
138
+ weight.render() # 顯示體重輸入框
139
+
140
+ with gr.Row():
141
+ output_bmi.render() # 顯示BMI值結果
142
+ output_state.render() # 顯示BMI診斷結果
143
+
144
+ btn.render() # 顯示計算BMI值按鈕
145
+ btn.click(fn=BMI,
146
+ inputs=[height, weight],
147
+ outputs=[output_bmi,output_state])
148
+
149
+ advice.render() # 顯示AI建議結果的文字框
150
+
151
+ with gr.Row():
152
+ key_box.render() # 顯示API金鑰輸入框
153
+ btn_advice.render() # 顯示AI建議按鈕
154
+ btn_advice.click(fn=get_advice, inputs=[output_bmi,temperature,key_box], outputs=advice)
155
+ btn_gym.render() # 顯示AI健身計畫按鈕
156
+ btn_gym.click(fn=get_gym, inputs=[output_bmi,slider,temperature, key_box], outputs=advice)
157
+
158
+ with gr.Accordion("settings", open=True):
159
+ slider.render()
160
+ temperature.render()
161
+
162
+ demo.launch()