HelenaXH commited on
Commit
f611a7c
·
verified ·
1 Parent(s): 3f4d656

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -83
app.py CHANGED
@@ -9,8 +9,7 @@ user_profile = {
9
  "bg_info": None,
10
  "work_value": None,
11
  "personality_summary": None,
12
- "dream_day": None,
13
- "forward_direction_choice": None
14
  }
15
 
16
  # ============================ 基础问题 ============================
@@ -51,7 +50,6 @@ forward_index = 0
51
  backward_index = 0
52
  forward_done = False
53
  backward_done = False
54
- forward_recommendation_given = False
55
 
56
  # ============================ 模型设置 ============================
57
  model_default = "gpt-4o"
@@ -67,7 +65,6 @@ def generate_system_prompt():
67
  work_value = user_profile["work_value"]
68
  personality_summary = user_profile["personality_summary"]
69
  dream_day = user_profile["dream_day"]
70
- forward_direction_choice = user_profile["forward_direction_choice"]
71
 
72
  if "是" in (mode or ""):
73
  return f"""
@@ -86,18 +83,17 @@ def generate_system_prompt():
86
  else:
87
  return f"""
88
  你是一位专业的职业规划顾问,使用Forward Design方法帮助学生探索合适的职业路径。
89
-
90
  学生背景:
91
  - 学术背景: {bg_info}
92
  - 工作意义: {work_value}
93
  - 性格总结: {personality_summary}
94
  - 理想一天: {dream_day}
95
- - 选择方向: {forward_direction_choice}
96
-
97
  请输出:
98
  1. 学生的优势、性格、价值观分析
99
- 2. 推荐具体职业(3个),说明日常内容、适性、要求、准备路径
100
- 3. 输出职业路径图:📍 现在 → 🎓 → 💼 → 📄 → 🚀
 
 
101
  """
102
 
103
  # ============================ 主逻辑函数 ============================
@@ -105,17 +101,19 @@ def predict(message, history):
105
  global current_q_index, questions
106
  global in_forward_flow, in_backward_flow
107
  global forward_index, backward_index
108
- global forward_done, backward_done, forward_recommendation_given
109
 
110
  if not history:
111
  current_q_index = 0
112
  questions = base_questions[:]
113
  for key in user_profile:
114
  user_profile[key] = None
115
- in_forward_flow = in_backward_flow = False
116
- forward_index = backward_index = 0
117
- forward_done = backward_done = False
118
- forward_recommendation_given = False
 
 
119
 
120
  if current_q_index > 0 and current_q_index <= len(questions):
121
  key = questions[current_q_index - 1][0]
@@ -146,38 +144,7 @@ def predict(message, history):
146
  key, q_text = forward_additional_questions[forward_index]
147
  forward_index += 1
148
  return q_text
149
- elif not forward_recommendation_given:
150
- forward_recommendation_given = True
151
- try:
152
- api_key = os.environ.get("API_TOKEN")
153
- if not api_key:
154
- return "错误:API_TOKEN 未设置"
155
- client = OpenAI(api_key=api_key)
156
- recommendation_prompt = f"""
157
- 根据以下信息,总结学生的特点,并推荐3个职业方向(大类)。最后请以如下格式结尾:\n
158
- 1. xxx\n2. xxx\n3. xxx\n\n请学生从中选择一个方向继续:
159
- - 学术背景: {user_profile['bg_info']}
160
- - 工作意义: {user_profile['work_value']}
161
- - 性格总结: {user_profile['personality_summary']}
162
- - 理想一天: {user_profile['dream_day']}
163
- """
164
- messages = [
165
- {"role": "system", "content": "你是一位职业顾问,善于总结和推荐方向。"},
166
- {"role": "user", "content": recommendation_prompt}
167
- ]
168
- response = client.chat.completions.create(
169
- model=model_default,
170
- messages=messages,
171
- max_tokens=token_default,
172
- temperature=temp_default,
173
- top_p=top_p_default,
174
- stream=False
175
- )
176
- return response.choices[0].message.content
177
- except Exception as e:
178
- return f"生成推荐方向时出错: {str(e)}"
179
  else:
180
- user_profile["forward_direction_choice"] = message.strip()
181
  forward_done = True
182
 
183
  if in_backward_flow and backward_index > 0 and not backward_done:
@@ -217,58 +184,116 @@ def predict(message, history):
217
 
218
  return "信息收集完毕,若尚未得到最终回复,请输入任意文字以继续。"
219
 
 
220
  # ============================ Gradio UI ============================
221
- with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
  gr.Markdown("""
223
- # 🎓 AI 职业规划助手
224
- 欢迎!我是职业发展向导~请回答几个问题,我将为你定制专属的职业探索建议!
 
 
225
  """)
226
-
227
- chatbot = gr.Chatbot()
228
- msg = gr.Textbox(placeholder="请输入你的回答...", label=None)
229
- send = gr.Button("发送 🚀")
230
- reset = gr.Button("🔄 重新开始")
231
-
232
- def add_user_message(message, history):
 
 
233
  history = history or []
234
  history.append({"role": "user", "content": message})
235
  return "", history
236
-
237
- def add_bot_response(history):
238
- message = history[-1]["content"]
239
- reply = predict(message, history[:-1])
240
- history.append({"role": "assistant", "content": reply})
241
  return history
242
-
243
- send.click(add_user_message, [msg, chatbot], [msg, chatbot]).then(
244
- add_bot_response, chatbot, chatbot
245
  )
246
- msg.submit(add_user_message, [msg, chatbot], [msg, chatbot]).then(
247
- add_bot_response, chatbot, chatbot
248
  )
249
-
250
- def reset_state():
251
  global current_q_index, questions
252
  global in_forward_flow, in_backward_flow
253
  global forward_index, backward_index
254
- global forward_done, backward_done, forward_recommendation_given
255
  current_q_index = 0
256
  questions = base_questions[:]
257
  for key in user_profile:
258
  user_profile[key] = None
259
- in_forward_flow = in_backward_flow = False
260
- forward_index = backward_index = 0
261
- forward_done = backward_done = False
262
- forward_recommendation_given = False
 
 
263
  return []
264
-
265
- reset.click(reset_state, outputs=chatbot)
266
-
267
- def auto_intro():
268
- return [{"role": "assistant", "content": base_questions[0][1]}]
269
-
270
- demo.load(auto_intro, outputs=chatbot)
271
-
272
- demo.launch()
273
-
274
-
 
9
  "bg_info": None,
10
  "work_value": None,
11
  "personality_summary": None,
12
+ "dream_day": None
 
13
  }
14
 
15
  # ============================ 基础问题 ============================
 
50
  backward_index = 0
51
  forward_done = False
52
  backward_done = False
 
53
 
54
  # ============================ 模型设置 ============================
55
  model_default = "gpt-4o"
 
65
  work_value = user_profile["work_value"]
66
  personality_summary = user_profile["personality_summary"]
67
  dream_day = user_profile["dream_day"]
 
68
 
69
  if "是" in (mode or ""):
70
  return f"""
 
83
  else:
84
  return f"""
85
  你是一位专业的职业规划顾问,使用Forward Design方法帮助学生探索合适的职业路径。
 
86
  学生背景:
87
  - 学术背景: {bg_info}
88
  - 工作意义: {work_value}
89
  - 性格总结: {personality_summary}
90
  - 理想一天: {dream_day}
 
 
91
  请输出:
92
  1. 学生的优势、性格、价值观分析
93
+ 2. 推荐6个职业方向说明理由
94
+ 3. 等学生选择后,再推荐3个具体职业,每个说明日常内容、适配性、要求、准备路径
95
+ 最后请用Markdown格式输出职业路径图:
96
+ 📍 现在 → 🎓 学习建议 → 💼 实践建议 → 📄 认证建议 → 🚀 求职建议
97
  """
98
 
99
  # ============================ 主逻辑函数 ============================
 
101
  global current_q_index, questions
102
  global in_forward_flow, in_backward_flow
103
  global forward_index, backward_index
104
+ global forward_done, backward_done
105
 
106
  if not history:
107
  current_q_index = 0
108
  questions = base_questions[:]
109
  for key in user_profile:
110
  user_profile[key] = None
111
+ in_forward_flow = False
112
+ in_backward_flow = False
113
+ forward_index = 0
114
+ backward_index = 0
115
+ forward_done = False
116
+ backward_done = False
117
 
118
  if current_q_index > 0 and current_q_index <= len(questions):
119
  key = questions[current_q_index - 1][0]
 
144
  key, q_text = forward_additional_questions[forward_index]
145
  forward_index += 1
146
  return q_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  else:
 
148
  forward_done = True
149
 
150
  if in_backward_flow and backward_index > 0 and not backward_done:
 
184
 
185
  return "信息收集完毕,若尚未得到最终回复,请输入任意文字以继续。"
186
 
187
+
188
  # ============================ Gradio UI ============================
189
+ with gr.Blocks(css="""
190
+ body {
191
+ background-color: #1e1e1e;
192
+ color: #ffffff;
193
+ }
194
+ .gradio-container {
195
+ font-family: 'Segoe UI', sans-serif;
196
+ }
197
+ .message.user {
198
+ background-color: #cce6ff !important;
199
+ color: #000000 !important;
200
+ border-radius: 10px !important;
201
+ padding: 10px;
202
+ margin: 6px;
203
+ }
204
+ .message.bot {
205
+ background-color: #5599ff !important;
206
+ color: #000000 !important;
207
+ border-radius: 10px !important;
208
+ padding: 10px;
209
+ margin: 6px;
210
+ }
211
+ .gradio-container .chat-msg.bot-msg .message.bot p {
212
+ color: #000000 !important;
213
+ }
214
+ .gr-button {
215
+ border-radius: 8px;
216
+ }
217
+ #custom-send {
218
+ background-color: #ec4899 !important;
219
+ color: white !important;
220
+ border-radius: 999px !important;
221
+ padding: 10px 24px !important;
222
+ font-weight: bold;
223
+ box-shadow: 0 0 10px #ec4899;
224
+ transition: all 0.3s ease-in-out;
225
+ }
226
+ #custom-send:hover {
227
+ background-color: #d63384 !important;
228
+ box-shadow: 0 0 12px #ec4899;
229
+ }
230
+ textarea, input {
231
+ background-color: #ffe4f1 !important;
232
+ color: #5e2c49 !important;
233
+ border: 1px solid #ec4899 !important;
234
+ }
235
+ footer {
236
+ display: none !important;
237
+ }
238
+ """) as demo:
239
+ with gr.Row():
240
+ gr.HTML("""
241
+ <div style='display: flex; align-items: center; justify-content: center; gap: 20px; margin-bottom: 10px;'>
242
+ <img src='https://media3.giphy.com/media/v1.Y2lkPTc5MGI3NjExMmFucGwxbmNsd3J5NXV0Y282NXNtMzNsZW5jMm4wNWh6c2dqbXIwdiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/l41m18LjqpzxUr2WA/giphy.gif' width='200' style='border-radius: 12px; box-shadow: 0 0 10px #ec4899;'>
243
+ <div style='text-align: left;'>
244
+ <h1 style='color:white; font-size: 36px; margin-bottom: 6px;'>🎓 AI职业规划助手(升级版)</h1>
245
+ <p style='font-size: 18px; font-weight:bold; color:#ec4899; margin-top: 0;margin-left: 150px'>让梦想照进现实 💖</p>
246
+ </div>
247
+ </div>
248
+ """)
249
  gr.Markdown("""
250
+ **📝 个性化职业规划助手:Forward / Backward 多轮交互示例**
251
+ - 如果已确定了职业目标,选择 **Backward** 模式(回答“是”)。
252
+ - 如果你需要探索适合的职业方向,选择 **Forward** 模式(回答“否”)。
253
+ - 我会收集你的基础信息、学术背景,并根据你的回答做多轮引导,最后给出职业规划建议。
254
  """)
255
+ chatbot = gr.Chatbot(height=500, show_label=False, show_copy_button=True, type="messages")
256
+ with gr.Row():
257
+ with gr.Column(scale=8):
258
+ msg = gr.Textbox(placeholder="请在这里输入你的回答...", show_label=False, container=False)
259
+ with gr.Column(scale=1):
260
+ submit_btn = gr.Button("🚀 发送", elem_id="custom-send")
261
+ with gr.Row():
262
+ reset_btn = gr.Button("🔄 重新开始")
263
+ def add_message(message, history):
264
  history = history or []
265
  history.append({"role": "user", "content": message})
266
  return "", history
267
+ def bot_response(history):
268
+ history = history or []
269
+ user_message = history[-1]["content"]
270
+ bot_message = predict(user_message, history[:-1] if len(history) > 1 else [])
271
+ history.append({"role": "assistant", "content": bot_message})
272
  return history
273
+ submit_btn.click(fn=add_message, inputs=[msg, chatbot], outputs=[msg, chatbot]).then(
274
+ fn=bot_response, inputs=[chatbot], outputs=[chatbot]
 
275
  )
276
+ msg.submit(fn=add_message, inputs=[msg, chatbot], outputs=[msg, chatbot]).then(
277
+ fn=bot_response, inputs=[chatbot], outputs=[chatbot]
278
  )
279
+ def reset_conversation():
 
280
  global current_q_index, questions
281
  global in_forward_flow, in_backward_flow
282
  global forward_index, backward_index
283
+ global forward_done, backward_done
284
  current_q_index = 0
285
  questions = base_questions[:]
286
  for key in user_profile:
287
  user_profile[key] = None
288
+ in_forward_flow = False
289
+ in_backward_flow = False
290
+ forward_index = 0
291
+ backward_index = 0
292
+ forward_done = False
293
+ backward_done = False
294
  return []
295
+ reset_btn.click(fn=reset_conversation, inputs=None, outputs=chatbot, queue=False)
296
+ def auto_first_question():
297
+ return [{"role": "assistant", "content": questions[0][1]}]
298
+ demo.load(auto_first_question, inputs=None, outputs=chatbot)
299
+ demo.launch()