HelenaXH commited on
Commit
f925b72
·
verified ·
1 Parent(s): 7b9b608

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -29
app.py CHANGED
@@ -43,7 +43,6 @@ forward_done = False
43
  backward_done = False
44
  forward_recommendation_given = False
45
 
46
- # 允许多次换方向、深度分析、路线图
47
  recommendation_round = 0
48
  forward_deep_dive_done = False
49
  roadmap_offered = False
@@ -70,12 +69,14 @@ def do_time_roadmap():
70
  - 工作价值:{wv}
71
  - 性格:{ps}
72
  - 理想工作:{dd}
 
73
  请你使用时间轴方式,给出一个职业路线图,如:
74
  📍 Now: [当前阶段做什么]
75
  🎓 Year X: [需要选的课/要做的项目]
76
  💼 下个暑假: [申请什么实习、参加什么活动]
77
  📄 毕业前: [考什么证书、准备材料]
78
  🚀 毕业后: [目标岗位、如何申请]
 
79
  请用Markdown分段写作,结合学生当前背景合理推断要点,写得具体些。
80
  """
81
  try:
@@ -85,8 +86,8 @@ def do_time_roadmap():
85
  client = OpenAI(api_key=api_key)
86
 
87
  msgs = [
88
- {"role": "system", "content": "你是一位专业的职业规划顾问,会生成时间轴式路线图"},
89
- {"role": "user", "content": prompt_roadmap}
90
  ]
91
  resp = client.chat.completions.create(
92
  model=model_default,
@@ -114,6 +115,7 @@ def do_deep_analysis():
114
  价值观: {wv}
115
  性格: {ps}
116
  理想工作: {dd}
 
117
  请你再深入分析,为学生推荐3个更具体的职业,并说明:
118
  1. 这些职业对学生背景的契合度
119
  2. 日常工作内容
@@ -121,7 +123,8 @@ def do_deep_analysis():
121
  4. 本校资源如何利用
122
  5. 实习 & 经验积累建议
123
  6. 简历优化思路
124
- 用英文分段写作,字数500+。
 
125
  """
126
  try:
127
  api_key = os.environ.get("API_TOKEN")
@@ -129,8 +132,8 @@ def do_deep_analysis():
129
  return "错误:API_TOKEN 未设置"
130
  client = OpenAI(api_key=api_key)
131
  msgs = [
132
- {"role": "system", "content": "你是一位专业职业顾问,会深度分析并列出3个具体职业。"},
133
- {"role": "user", "content": deep_prompt}
134
  ]
135
  resp = client.chat.completions.create(
136
  model=model_default,
@@ -157,9 +160,11 @@ def recommend_3directions():
157
  - 工作意义: {wv}
158
  - 性格: {ps}
159
  - 理想工作: {dd}
 
160
  分两部分:
161
  [一、人物画像]:1) 学术背景 2) 价值观 3) 性格 4) 理想工作
162
  [二、推荐3个大方向]:每个方向(1~2段文字)
 
163
  最后用如下结尾:
164
  1. XXX方向
165
  2. XXX方向
@@ -172,8 +177,8 @@ def recommend_3directions():
172
  return "错误:API_TOKEN 未设置"
173
  client = OpenAI(api_key=api_key)
174
  msgs = [
175
- {"role": "system", "content": "你是一位职业顾问,擅长根据用户资料推荐3大方向"},
176
- {"role": "user", "content": rec_prompt}
177
  ]
178
  resp = client.chat.completions.create(
179
  model=model_default,
@@ -219,7 +224,6 @@ def generate_system_prompt():
219
  性格总结: {ps}
220
  理想工作: {dd}
221
  学生选择方向: {fwd}
222
-
223
  请输出:
224
  1. 学生的优势、性格、价值观分析
225
  2. 推荐3个具体职业,并说明日常工作内容、适配性、要求、准备路径
@@ -240,7 +244,7 @@ def predict(message, history):
240
 
241
  if not history:
242
  current_q_index = 0
243
- questions[:] = base_questions # 用 [:] 保持原列表
244
  for k in user_profile:
245
  user_profile[k] = None
246
 
@@ -276,9 +280,10 @@ def predict(message, history):
276
 
277
  mode = user_profile.get("mode") or ""
278
 
279
- # Backward
280
  if mode == "是":
281
  if in_backward_flow and not backward_done:
 
282
  if backward_index > 0 and backward_index <= len(backward_additional_questions):
283
  prev_key = backward_additional_questions[backward_index - 1][0]
284
  user_profile[prev_key] = message.strip()
@@ -313,21 +318,26 @@ def predict(message, history):
313
  except Exception as e:
314
  return f"发生错误: {str(e)}"
315
 
316
- # Forward
317
  else:
318
  if in_forward_flow and not forward_done and not forward_deep_dive_done and not roadmap_done:
319
  if forward_index > 0 and forward_index <= len(forward_additional_questions):
320
  prev_key = forward_additional_questions[forward_index - 1][0]
321
  user_profile[prev_key] = message.strip()
322
 
 
323
  if forward_index < len(forward_additional_questions):
324
  k, prompt_text = forward_additional_questions[forward_index]
325
  forward_index += 1
326
  return prompt_text
 
 
327
  elif not forward_recommendation_given:
328
  forward_recommendation_given = True
329
  return recommend_3directions()
 
330
  else:
 
331
  choice = message.strip().lower()
332
  if choice in ["1","2","3"]:
333
  user_profile["forward_direction_choice"] = choice
@@ -346,11 +356,11 @@ def predict(message, history):
346
  elif forward_deep_dive_done and not roadmap_offered:
347
  # 深度分析结束 -> 问路线图
348
  roadmap_offered = True
349
- return "需要一个时间轴式职业路线图吗?如果是回复“OK”,否则回复“NO Thanks”。"
350
 
351
  elif roadmap_offered and not roadmap_done:
352
  ans = message.strip().lower()
353
- if ans == "OK":
354
  roadmap_done = True
355
  forward_done = True
356
  return do_time_roadmap()
@@ -385,7 +395,7 @@ def predict(message, history):
385
  return "信息收集完毕,如还未得到最终回复,请再输入任意文字继续。"
386
 
387
 
388
- ### ------ Gradio UI ------
389
  import gradio as gr
390
 
391
  with gr.Blocks(css="""
@@ -438,20 +448,11 @@ footer {
438
  display: none !important;
439
  }
440
  """) as demo:
441
- with gr.Row():
442
- gr.HTML("""
443
- <div style='display: flex; align-items: center; justify-content: center; gap: 20px; margin-bottom: 10px;'>
444
- <!-- 左侧动图 -->
445
- <img src='https://media3.giphy.com/media/v1.Y2lkPTc5MGI3NjExMmFucGwxbmNsd3J5NXV0Y282NXNtMzNsZW5jMm4wNWh6c2dqbXIwdiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/l41m18LjqpzxUr2WA/giphy.gif' width='200' style='border-radius: 12px; box-shadow: 0 0 10px #ec4899;'>
446
-
447
- <!-- 右侧标题 -->
448
- <div style='text-align: left;'>
449
- <h1 style='color:Black; font-size: 36px; margin-bottom: 6px;'>🎓 AI Career Exploration Assistant</h1>
450
- <p style='font-size: 18px; font-weight:bold; color:#ec4899; margin-top: 0;margin-left: 150px'>Hi! Lets Make the Dream Comes True 💖</p>
451
- </div>
452
- </div>
453
- """)
454
- gr.Markdown("## 🎓 职业规划助手\n如果你有明确职业目标 => 回答“是”;否则 => 回答“否”")
455
 
456
  chatbot = gr.Chatbot(height=500, show_label=False, show_copy_button=True, type="messages")
457
  msg = gr.Textbox(placeholder="请输入你的回答...")
 
43
  backward_done = False
44
  forward_recommendation_given = False
45
 
 
46
  recommendation_round = 0
47
  forward_deep_dive_done = False
48
  roadmap_offered = False
 
69
  - 工作价值:{wv}
70
  - 性格:{ps}
71
  - 理想工作:{dd}
72
+
73
  请你使用时间轴方式,给出一个职业路线图,如:
74
  📍 Now: [当前阶段做什么]
75
  🎓 Year X: [需要选的课/要做的项目]
76
  💼 下个暑假: [申请什么实习、参加什么活动]
77
  📄 毕业前: [考什么证书、准备材料]
78
  🚀 毕业后: [目标岗位、如何申请]
79
+
80
  请用Markdown分段写作,结合学生当前背景合理推断要点,写得具体些。
81
  """
82
  try:
 
86
  client = OpenAI(api_key=api_key)
87
 
88
  msgs = [
89
+ {"role":"system","content":"你是一位专业的职业规划顾问,会生成时间轴式路线图"},
90
+ {"role":"user","content": prompt_roadmap}
91
  ]
92
  resp = client.chat.completions.create(
93
  model=model_default,
 
115
  价值观: {wv}
116
  性格: {ps}
117
  理想工作: {dd}
118
+
119
  请你再深入分析,为学生推荐3个更具体的职业,并说明:
120
  1. 这些职业对学生背景的契合度
121
  2. 日常工作内容
 
123
  4. 本校资源如何利用
124
  5. 实习 & 经验积累建议
125
  6. 简历优化思路
126
+
127
+ 用中文分段写作,字数500+。
128
  """
129
  try:
130
  api_key = os.environ.get("API_TOKEN")
 
132
  return "错误:API_TOKEN 未设置"
133
  client = OpenAI(api_key=api_key)
134
  msgs = [
135
+ {"role":"system","content":"你是一位专业职业顾问,会深度分析并列出3个具体职业。"},
136
+ {"role":"user","content":deep_prompt}
137
  ]
138
  resp = client.chat.completions.create(
139
  model=model_default,
 
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方向
 
177
  return "错误:API_TOKEN 未设置"
178
  client = OpenAI(api_key=api_key)
179
  msgs = [
180
+ {"role":"system","content":"你是一位职业顾问,擅长根据用户资料推荐3大方向"},
181
+ {"role":"user","content": rec_prompt}
182
  ]
183
  resp = client.chat.completions.create(
184
  model=model_default,
 
224
  性格总结: {ps}
225
  理想工作: {dd}
226
  学生选择方向: {fwd}
 
227
  请输出:
228
  1. 学生的优势、性格、价值观分析
229
  2. 推荐3个具体职业,并说明日常工作内容、适配性、要求、准备路径
 
244
 
245
  if not history:
246
  current_q_index = 0
247
+ questions[:] = base_questions
248
  for k in user_profile:
249
  user_profile[k] = None
250
 
 
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()
 
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()
338
+
339
  else:
340
+ # 学生选1/2/3 or '换'
341
  choice = message.strip().lower()
342
  if choice in ["1","2","3"]:
343
  user_profile["forward_direction_choice"] = choice
 
356
  elif forward_deep_dive_done and not roadmap_offered:
357
  # 深度分析结束 -> 问路线图
358
  roadmap_offered = True
359
+ return "需要一个时间轴式职业路线图吗?如回复【要】,否则回复【不要】。"
360
 
361
  elif roadmap_offered and not roadmap_done:
362
  ans = message.strip().lower()
363
+ if ans == "":
364
  roadmap_done = True
365
  forward_done = True
366
  return do_time_roadmap()
 
395
  return "信息收集完毕,如还未得到最终回复,请再输入任意文字继续。"
396
 
397
 
398
+ # ============================ Gradio UI ============================
399
  import gradio as gr
400
 
401
  with gr.Blocks(css="""
 
448
  display: none !important;
449
  }
450
  """) as demo:
451
+ gr.Markdown("""
452
+ # 🎓 职业规划助手
453
+
454
+ 如果你有明确职业目标 => 回答“是”,否则 => 回答“否”
455
+ """)
 
 
 
 
 
 
 
 
 
456
 
457
  chatbot = gr.Chatbot(height=500, show_label=False, show_copy_button=True, type="messages")
458
  msg = gr.Textbox(placeholder="请输入你的回答...")