Bargerya commited on
Commit
0121f83
1 Parent(s): 80f4b08

加入星象的測試

Browse files
Files changed (1) hide show
  1. app.py +38 -17
app.py CHANGED
@@ -6,6 +6,16 @@ import gradio as gr
6
  from zhdate import ZhDate
7
  from datetime import datetime
8
 
 
 
 
 
 
 
 
 
 
 
9
  # 定義地支對應的時間區間
10
  z_mapping = {
11
  "1": "子",
@@ -25,13 +35,20 @@ z_mapping = {
25
 
26
  ## AI 建議
27
  def get_advice(bmi,temp, API_KEY, model="gpt-3.5-turbo"):
28
- openai.api_key = API_KEY
29
- messages = [{"role": "system", "content": "You can provide some dietary advice based on \
30
- the user's BMI value. You can only give up to 3 suggestions"},
31
- {"role": "user", "content": f'My BMI is {bmi}. What can I do to be healthier?'},]
 
 
 
 
 
 
 
32
  response = openai.chat.completions.create(
33
  model=model,
34
- max_tokens=200,
35
  messages=messages,
36
  temperature=temp, # this is the degree of randomness of the model's output
37
  )
@@ -39,7 +56,11 @@ def get_advice(bmi,temp, API_KEY, model="gpt-3.5-turbo"):
39
 
40
  ## 健身計畫
41
  def get_gym(bmi,slide, temp, API_KEY, model="gpt-3.5-turbo"):
42
- openai.api_key = API_KEY
 
 
 
 
43
  messages = [{"role": "system", "content": "You are a great fitness coach and \
44
  you will give users great fitness plans."},
45
  {"role": "user", "content": f'My BMI is {bmi}. I want a {slide}-point weight\
@@ -52,7 +73,7 @@ def get_gym(bmi,slide, temp, API_KEY, model="gpt-3.5-turbo"):
52
  )
53
  return response.choices[0].message.content
54
 
55
- def BMI(year, month, day, btime) -> datetime:
56
  dt_date1 = datetime(int(year), int(month), int(day))
57
  date_lunar = ZhDate.from_datetime(dt_date1)
58
  bstr = str(date_lunar)
@@ -93,7 +114,7 @@ sex = gr.Textbox(
93
  info="輸入你是男生或女生",
94
  placeholder="Input you are male or female here...",)
95
 
96
- output_bmi = gr.Textbox(
97
  value="",
98
  label="你的農曆生日及出生時辰",
99
  info="這是從西元換算出的農曆生日及出生時辰",
@@ -110,15 +131,15 @@ btn = gr.Button(
110
  variant="primary", scale=1)
111
 
112
  btn_advice = gr.Button(
113
- value="AI 建議",
114
  variant="primary", scale=2)
115
 
116
  btn_gym = gr.Button(
117
- value="AI 健身計畫",
118
  variant="primary", scale=1)
119
 
120
  key_box = gr.Textbox(
121
- label="Enter your API key",
122
  info="You have to provide your own OPENAI_API_KEY for this app to function properly",
123
  placeholder="Type OpenAI API KEY here...",
124
  type="password")
@@ -160,21 +181,21 @@ with gr.Blocks() as demo:
160
  sex.render() # 顯示性別
161
 
162
  with gr.Row():
163
- output_bmi.render() # 顯示BMI值結果
164
 
165
- btn.render() # 顯示計算BMI值按鈕
166
- btn.click(fn=BMI,
167
  inputs=[year, month, day, btime],
168
- outputs=[output_bmi])
169
 
170
  advice.render() # 顯示AI建議結果的文字框
171
 
172
  with gr.Row():
173
  key_box.render() # 顯示API金鑰輸入框
174
  btn_advice.render() # 顯示AI建議按鈕
175
- btn_advice.click(fn=get_advice, inputs=[output_bmi,temperature,key_box], outputs=advice)
176
  btn_gym.render() # 顯示AI健身計畫按鈕
177
- btn_gym.click(fn=get_gym, inputs=[output_bmi,slider,temperature, key_box], outputs=advice)
178
 
179
  with gr.Accordion("settings", open=True):
180
  slider.render()
 
6
  from zhdate import ZhDate
7
  from datetime import datetime
8
 
9
+
10
+ from dotenv import load_dotenv, find_dotenv
11
+ _ = load_dotenv(find_dotenv()) # read local .env file
12
+ openai.api_key = os.environ['OPENAI_API_KEY']
13
+ print(openai.api_key)
14
+ if openai.api_key:
15
+ DESKTOP_KEY = openai.api_key
16
+ print(DESKTOP_KEY)
17
+
18
+
19
  # 定義地支對應的時間區間
20
  z_mapping = {
21
  "1": "子",
 
35
 
36
  ## AI 建議
37
  def get_advice(bmi,temp, API_KEY, model="gpt-3.5-turbo"):
38
+ if API_KEY:
39
+ openai.api_key = API_KEY
40
+ else:
41
+ openai.api_key = DESKTOP_KEY
42
+ print(openai.api_key)
43
+ bmi = '' #未來再說
44
+ gw_main_star = '像一個獨裁霸道的君王,無法聽進別人的意見'
45
+ gw_main_star = '像一個足智多謀的軍師,ㄧ有時候會考慮太多而失去前進的勇氣'
46
+
47
+ messages = [{"role": "system", "content": "你是一個心理權威,你會根據用戶的個性,提供至少3個,不超過5個建議。"},
48
+ {"role": "user", "content": f'我的個性是 {gw_main_star}. 針對這樣的個性,妳可否給我一些待人處事的建議?'},]
49
  response = openai.chat.completions.create(
50
  model=model,
51
+ max_tokens=1000,
52
  messages=messages,
53
  temperature=temp, # this is the degree of randomness of the model's output
54
  )
 
56
 
57
  ## 健身計畫
58
  def get_gym(bmi,slide, temp, API_KEY, model="gpt-3.5-turbo"):
59
+ if API_KEY:
60
+ openai.api_key = API_KEY
61
+ else:
62
+ openai.api_key = DESKTOP_KEY
63
+ print(openai.api_key)
64
  messages = [{"role": "system", "content": "You are a great fitness coach and \
65
  you will give users great fitness plans."},
66
  {"role": "user", "content": f'My BMI is {bmi}. I want a {slide}-point weight\
 
73
  )
74
  return response.choices[0].message.content
75
 
76
+ def GWAI(year, month, day, btime) -> datetime:
77
  dt_date1 = datetime(int(year), int(month), int(day))
78
  date_lunar = ZhDate.from_datetime(dt_date1)
79
  bstr = str(date_lunar)
 
114
  info="輸入你是男生或女生",
115
  placeholder="Input you are male or female here...",)
116
 
117
+ output_gwai = gr.Textbox(
118
  value="",
119
  label="你的農曆生日及出生時辰",
120
  info="這是從西元換算出的農曆生日及出生時辰",
 
131
  variant="primary", scale=1)
132
 
133
  btn_advice = gr.Button(
134
+ value="AI 紫微的人生建議",
135
  variant="primary", scale=2)
136
 
137
  btn_gym = gr.Button(
138
+ value="AI 紫微聊天室",
139
  variant="primary", scale=1)
140
 
141
  key_box = gr.Textbox(
142
+ label="輸入你的 API 金鑰",
143
  info="You have to provide your own OPENAI_API_KEY for this app to function properly",
144
  placeholder="Type OpenAI API KEY here...",
145
  type="password")
 
181
  sex.render() # 顯示性別
182
 
183
  with gr.Row():
184
+ output_gwai.render() # 顯示農曆生日欄
185
 
186
+ btn.render() # 顯示農曆生日值結果
187
+ btn.click(fn=GWAI,
188
  inputs=[year, month, day, btime],
189
+ outputs=[output_gwai])
190
 
191
  advice.render() # 顯示AI建議結果的文字框
192
 
193
  with gr.Row():
194
  key_box.render() # 顯示API金鑰輸入框
195
  btn_advice.render() # 顯示AI建議按鈕
196
+ btn_advice.click(fn=get_advice, inputs=[output_gwai,temperature,key_box], outputs=advice)
197
  btn_gym.render() # 顯示AI健身計畫按鈕
198
+ btn_gym.click(fn=get_gym, inputs=[output_gwai,slider,temperature, key_box], outputs=advice)
199
 
200
  with gr.Accordion("settings", open=True):
201
  slider.render()