HelenaXH commited on
Commit
925113b
·
verified ·
1 Parent(s): f925b72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -20
app.py CHANGED
@@ -10,7 +10,10 @@ user_profile = {
10
  "work_value": None,
11
  "personality_summary": None,
12
  "dream_day": None,
13
- "forward_direction_choice": None
 
 
 
14
  }
15
 
16
  # ============================ 基础问题 ============================
@@ -54,9 +57,56 @@ token_default = 2000
54
  temp_default = 0.7
55
  top_p_default = 0.95
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 路线图生成函数 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58
  def do_time_roadmap():
59
- direction = user_profile["forward_direction_choice"] or user_profile["specific_career"]
 
60
  bg_info = user_profile["bg_info"] or "未知专业"
61
  wv = user_profile["work_value"] or "暂无"
62
  ps = user_profile["personality_summary"] or "暂无"
@@ -69,14 +119,12 @@ def do_time_roadmap():
69
  - 工作价值:{wv}
70
  - 性格:{ps}
71
  - 理想工作:{dd}
72
-
73
  请你使用时间轴方式,给出一个职业路线图,如:
74
  📍 Now: [当前阶段做什么]
75
  🎓 Year X: [需要选的课/要做的项目]
76
  💼 下个暑假: [申请什么实习、参加什么活动]
77
  📄 毕业前: [考什么证书、准备材料]
78
  🚀 毕业后: [目标岗位、如何申请]
79
-
80
  请用Markdown分段写作,结合学生当前背景合理推断要点,写得具体些。
81
  """
82
  try:
@@ -101,7 +149,8 @@ def do_time_roadmap():
101
  except Exception as e:
102
  return f"生成路线图时出错: {str(e)}"
103
 
104
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 深度分析函数 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
105
  def do_deep_analysis():
106
  direction = user_profile["forward_direction_choice"] or "尚未选择"
107
  bg_info = user_profile["bg_info"] or "未知背景"
@@ -115,7 +164,6 @@ def do_deep_analysis():
115
  价值观: {wv}
116
  性格: {ps}
117
  理想工作: {dd}
118
-
119
  请你再深入分析,为学生推荐3个更具体的职业,并说明:
120
  1. 这些职业对学生背景的契合度
121
  2. 日常工作内容
@@ -123,7 +171,6 @@ def do_deep_analysis():
123
  4. 本校资源如何利用
124
  5. 实习 & 经验积累建议
125
  6. 简历优化思路
126
-
127
  用中文分段写作,字数500+。
128
  """
129
  try:
@@ -147,6 +194,7 @@ def do_deep_analysis():
147
  except Exception as e:
148
  return f"深度分析出错: {str(e)}"
149
 
 
150
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 推荐大方向函数 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
151
  def recommend_3directions():
152
  bg_info = user_profile["bg_info"] or "未知"
@@ -160,11 +208,9 @@ def recommend_3directions():
160
  - 工作意义: {wv}
161
  - 性格: {ps}
162
  - 理想工作: {dd}
163
-
164
  分两部分:
165
  [一、人物画像]:1) 学术背景 2) 价值观 3) 性格 4) 理想工作
166
  [二、推荐3个大方向]:每个方向(1~2段文字)
167
-
168
  最后用如下结尾:
169
  1. XXX方向
170
  2. XXX方向
@@ -188,10 +234,24 @@ def recommend_3directions():
188
  top_p=top_p_default,
189
  stream=False
190
  )
191
- return resp.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
192
  except Exception as e:
193
  return f"多方向推荐出错: {str(e)}"
194
 
 
195
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ generate_system_prompt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
196
  def generate_system_prompt():
197
  mode = user_profile["mode"]
@@ -231,6 +291,7 @@ def generate_system_prompt():
231
  📍 现在 → 🎓 学习建议 → 💼 实践建议 → 📄 认证建议 → 🚀 求职建议
232
  """
233
 
 
234
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 主逻辑函数 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
235
  def predict(message, history):
236
  global current_q_index, questions
@@ -242,6 +303,7 @@ def predict(message, history):
242
  global forward_deep_dive_done
243
  global roadmap_offered, roadmap_done
244
 
 
245
  if not history:
246
  current_q_index = 0
247
  questions[:] = base_questions
@@ -259,10 +321,10 @@ def predict(message, history):
259
  roadmap_offered = False
260
  roadmap_done = False
261
 
262
- # 如果还在base_questions
263
  if 0 < current_q_index <= len(questions):
264
  key = questions[current_q_index - 1][0]
265
- # 只有回答第一个问题的时候 -> 决定是/否
266
  if key == "mode" and current_q_index == 1:
267
  ans = message.strip()
268
  user_profile["mode"] = ans
@@ -278,12 +340,15 @@ def predict(message, history):
278
  current_q_index += 1
279
  return nxt
280
 
 
281
  mode = user_profile.get("mode") or ""
282
 
283
- # 如果回答“是” => Backward
 
 
284
  if mode == "是":
285
  if in_backward_flow and not backward_done:
286
- # 录入回答
287
  if backward_index > 0 and backward_index <= len(backward_additional_questions):
288
  prev_key = backward_additional_questions[backward_index - 1][0]
289
  user_profile[prev_key] = message.strip()
@@ -295,6 +360,7 @@ def predict(message, history):
295
  else:
296
  backward_done = True
297
 
 
298
  if backward_done:
299
  try:
300
  api_key = os.environ.get("API_TOKEN")
@@ -318,20 +384,23 @@ def predict(message, history):
318
  except Exception as e:
319
  return f"发生错误: {str(e)}"
320
 
321
- # 如果回答“否” => Forward
 
 
322
  else:
323
  if in_forward_flow and not forward_done and not forward_deep_dive_done and not roadmap_done:
 
324
  if forward_index > 0 and forward_index <= len(forward_additional_questions):
325
  prev_key = forward_additional_questions[forward_index - 1][0]
326
  user_profile[prev_key] = message.strip()
327
 
328
- # 如果还有 Forward附加问题没问完
329
  if forward_index < len(forward_additional_questions):
330
  k, prompt_text = forward_additional_questions[forward_index]
331
  forward_index += 1
332
  return prompt_text
333
 
334
- # 问完4个问题 -> 推荐3方向
335
  elif not forward_recommendation_given:
336
  forward_recommendation_given = True
337
  return recommend_3directions()
@@ -340,9 +409,33 @@ def predict(message, history):
340
  # 学生选1/2/3 or '换'
341
  choice = message.strip().lower()
342
  if choice in ["1","2","3"]:
 
343
  user_profile["forward_direction_choice"] = choice
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
344
  forward_deep_dive_done = True
345
- return do_deep_analysis()
 
346
  elif choice == "换":
347
  recommendation_round += 1
348
  if recommendation_round > 2:
@@ -353,8 +446,8 @@ def predict(message, history):
353
  else:
354
  return "请回复1/2/3选择方向,或输入'换'来重新推荐"
355
 
 
356
  elif forward_deep_dive_done and not roadmap_offered:
357
- # 深度分析结束 -> 问路线图
358
  roadmap_offered = True
359
  return "需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
360
 
@@ -369,6 +462,7 @@ def predict(message, history):
369
  forward_done = True
370
  return "好的,不生成时间轴,本次规划结束。"
371
 
 
372
  if forward_done:
373
  try:
374
  api_key = os.environ.get("API_TOKEN")
@@ -450,7 +544,6 @@ footer {
450
  """) as demo:
451
  gr.Markdown("""
452
  # 🎓 职业规划助手
453
-
454
  如果你有明确职业目标 => 回答“是”,否则 => 回答“否”
455
  """)
456
 
 
10
  "work_value": None,
11
  "personality_summary": None,
12
  "dream_day": None,
13
+ "forward_direction_choice": None,
14
+ # 新增:存储3个推荐方向 + 最终选择的方向
15
+ "recommended_directions": [],
16
+ "final_choice": None
17
  }
18
 
19
  # ============================ 基础问题 ============================
 
57
  temp_default = 0.7
58
  top_p_default = 0.95
59
 
60
+
61
+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 新增:选择后更详细介绍的函数 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62
+ def do_selected_career_detail(selected_direction, bg_info, wv, ps, dd):
63
+ """
64
+ 当用户在3大方向中选择了某一个之后,自动生成更详细的分析:
65
+ 包括薪资水平、工作环境、晋升难度、关键技能、工作节奏等。
66
+ """
67
+ prompt = f"""
68
+ 你是一位专业的职业规划顾问。
69
+ 用户选定的具体方向: {selected_direction}
70
+ 学生背景: {bg_info}
71
+ 工作价值: {wv}
72
+ 性格: {ps}
73
+ 理想工作: {dd}
74
+
75
+ 请写一份更详细的方向说明,至少包含:
76
+ 1. 行业平均薪资(初级/中级/高级)
77
+ 2. 工作环境(远程/混合/办公室,团队规模等)
78
+ 3. 晋升难度(需要什么资历或条件)
79
+ 4. 关键技能和考证
80
+ 5. 日常工作节奏
81
+ 请用分段Markdown格式,字数不少于300字。
82
+ """
83
+ try:
84
+ api_key = os.environ.get("API_TOKEN")
85
+ if not api_key:
86
+ return "错误:API_TOKEN 未设置"
87
+ client = OpenAI(api_key=api_key)
88
+
89
+ msgs = [
90
+ {"role": "system", "content": "你是一位专业职业顾问,会为用户选定方向提供更详细的分析。"},
91
+ {"role": "user", "content": prompt}
92
+ ]
93
+ resp = client.chat.completions.create(
94
+ model=model_default,
95
+ messages=msgs,
96
+ max_tokens=token_default,
97
+ temperature=temp_default,
98
+ top_p=top_p_default,
99
+ stream=False
100
+ )
101
+ return resp.choices[0].message.content
102
+ except Exception as e:
103
+ return f"生成职业详细信息时出错: {str(e)}"
104
+
105
+
106
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 路线图生成函数 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107
  def do_time_roadmap():
108
+ # 修改:优先使用 final_choice
109
+ direction = user_profile.get("final_choice") or user_profile.get("forward_direction_choice") or user_profile.get("specific_career")
110
  bg_info = user_profile["bg_info"] or "未知专业"
111
  wv = user_profile["work_value"] or "暂无"
112
  ps = user_profile["personality_summary"] or "暂无"
 
119
  - 工作价值:{wv}
120
  - 性格:{ps}
121
  - 理想工作:{dd}
 
122
  请你使用时间轴方式,给出一个职业路线图,如:
123
  📍 Now: [当前阶段做什么]
124
  🎓 Year X: [需要选的课/要做的项目]
125
  💼 下个暑假: [申请什么实习、参加什么活动]
126
  📄 毕业前: [考什么证书、准备材料]
127
  🚀 毕业后: [目标岗位、如何申请]
 
128
  请用Markdown分段写作,结合学生当前背景合理推断要点,写得具体些。
129
  """
130
  try:
 
149
  except Exception as e:
150
  return f"生成路线图时出错: {str(e)}"
151
 
152
+
153
+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 深度分析函数 (可保留备用) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
154
  def do_deep_analysis():
155
  direction = user_profile["forward_direction_choice"] or "尚未选择"
156
  bg_info = user_profile["bg_info"] or "未知背景"
 
164
  价值观: {wv}
165
  性格: {ps}
166
  理想工作: {dd}
 
167
  请你再深入分析,为学生推荐3个更具体的职业,并说明:
168
  1. 这些职业对学生背景的契合度
169
  2. 日常工作内容
 
171
  4. 本校资源如何利用
172
  5. 实习 & 经验积累建议
173
  6. 简历优化思路
 
174
  用中文分段写作,字数500+。
175
  """
176
  try:
 
194
  except Exception as e:
195
  return f"深度分析出错: {str(e)}"
196
 
197
+
198
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 推荐大方向函数 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
199
  def recommend_3directions():
200
  bg_info = user_profile["bg_info"] or "未知"
 
208
  - 工作意义: {wv}
209
  - 性格: {ps}
210
  - 理想工作: {dd}
 
211
  分两部分:
212
  [一、人物画像]:1) 学术背景 2) 价值观 3) 性格 4) 理想工作
213
  [二、推荐3个大方向]:每个方向(1~2段文字)
 
214
  最后用如下结尾:
215
  1. XXX方向
216
  2. XXX方向
 
234
  top_p=top_p_default,
235
  stream=False
236
  )
237
+
238
+ # 这里是真实的大模型返回文本
239
+ output_text = resp.choices[0].message.content
240
+
241
+ # ---- 关键:给 user_profile["recommended_directions"] 赋值 ----
242
+ # 如果你想自动化解析返回的方向,可用正则或提取逻辑。这里演示写死3个示例:
243
+ user_profile["recommended_directions"] = [
244
+ "远程工程咨询",
245
+ "在线教育和辅导",
246
+ "远程研发项目管理"
247
+ ]
248
+
249
+ return output_text
250
+
251
  except Exception as e:
252
  return f"多方向推荐出错: {str(e)}"
253
 
254
+
255
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ generate_system_prompt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
256
  def generate_system_prompt():
257
  mode = user_profile["mode"]
 
291
  📍 现在 → 🎓 学习建议 → 💼 实践建议 → 📄 认证建议 → 🚀 求职建议
292
  """
293
 
294
+
295
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 主逻辑函数 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
296
  def predict(message, history):
297
  global current_q_index, questions
 
303
  global forward_deep_dive_done
304
  global roadmap_offered, roadmap_done
305
 
306
+ # ========== 初始化 ==========
307
  if not history:
308
  current_q_index = 0
309
  questions[:] = base_questions
 
321
  roadmap_offered = False
322
  roadmap_done = False
323
 
324
+ # ========== 先处理基础 ==========
325
  if 0 < current_q_index <= len(questions):
326
  key = questions[current_q_index - 1][0]
327
+ # 第一个问题决定是 /
328
  if key == "mode" and current_q_index == 1:
329
  ans = message.strip()
330
  user_profile["mode"] = ans
 
340
  current_q_index += 1
341
  return nxt
342
 
343
+ # ========== 分模式处理 ==========
344
  mode = user_profile.get("mode") or ""
345
 
346
+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
347
+ # Backward模式处理
348
+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
349
  if mode == "是":
350
  if in_backward_flow and not backward_done:
351
+ # 补充回答
352
  if backward_index > 0 and backward_index <= len(backward_additional_questions):
353
  prev_key = backward_additional_questions[backward_index - 1][0]
354
  user_profile[prev_key] = message.strip()
 
360
  else:
361
  backward_done = True
362
 
363
+ # backward_done 表示可以给最终回复
364
  if backward_done:
365
  try:
366
  api_key = os.environ.get("API_TOKEN")
 
384
  except Exception as e:
385
  return f"发生错误: {str(e)}"
386
 
387
+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
388
+ # Forward模式处理
389
+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
390
  else:
391
  if in_forward_flow and not forward_done and not forward_deep_dive_done and not roadmap_done:
392
+ # 收集 Forward 问题回答
393
  if forward_index > 0 and forward_index <= len(forward_additional_questions):
394
  prev_key = forward_additional_questions[forward_index - 1][0]
395
  user_profile[prev_key] = message.strip()
396
 
397
+ # 如果还有 Forward问题没问完
398
  if forward_index < len(forward_additional_questions):
399
  k, prompt_text = forward_additional_questions[forward_index]
400
  forward_index += 1
401
  return prompt_text
402
 
403
+ # 如果 4 个问题都问完 -> 推荐3方向
404
  elif not forward_recommendation_given:
405
  forward_recommendation_given = True
406
  return recommend_3directions()
 
409
  # 学生选1/2/3 or '换'
410
  choice = message.strip().lower()
411
  if choice in ["1","2","3"]:
412
+ # 将数字保存在 forward_direction_choice
413
  user_profile["forward_direction_choice"] = choice
414
+
415
+ # 先找到选的是哪一个实际方向
416
+ idx = int(choice) - 1 # 0,1,2
417
+ # 如果 recommended_directions 有存储,拿到
418
+ # 如果为空,就简单用 '未知方向'
419
+ final_dir = "未知方向"
420
+ if user_profile["recommended_directions"]:
421
+ if 0 <= idx < len(user_profile["recommended_directions"]):
422
+ final_dir = user_profile["recommended_directions"][idx]
423
+
424
+ # 存下来,后续生成路线图用
425
+ user_profile["final_choice"] = final_dir
426
+
427
+ # 生成更详细的分析
428
+ detail_msg = do_selected_career_detail(
429
+ selected_direction=final_dir,
430
+ bg_info=user_profile["bg_info"] or "",
431
+ wv=user_profile["work_value"] or "",
432
+ ps=user_profile["personality_summary"] or "",
433
+ dd=user_profile["dream_day"] or ""
434
+ )
435
+
436
  forward_deep_dive_done = True
437
+ return detail_msg
438
+
439
  elif choice == "换":
440
  recommendation_round += 1
441
  if recommendation_round > 2:
 
446
  else:
447
  return "请回复1/2/3选择方向,或输入'换'来重新推荐"
448
 
449
+ # 详细方向介绍结束后 -> 问时间轴
450
  elif forward_deep_dive_done and not roadmap_offered:
 
451
  roadmap_offered = True
452
  return "需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
453
 
 
462
  forward_done = True
463
  return "好的,不生成时间轴,本次规划结束。"
464
 
465
+ # 全流程结束后,再给一个额外结果
466
  if forward_done:
467
  try:
468
  api_key = os.environ.get("API_TOKEN")
 
544
  """) as demo:
545
  gr.Markdown("""
546
  # 🎓 职业规划助手
 
547
  如果你有明确职业目标 => 回答“是”,否则 => 回答“否”
548
  """)
549