HelenaXH commited on
Commit
2a75c72
·
verified ·
1 Parent(s): c6753f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -108
app.py CHANGED
@@ -4,7 +4,7 @@ import os
4
 
5
  # ============================ 用户信息结构 ============================
6
  user_profile = {
7
- "mode": None, # 只在回答第一个问题时设置
8
  "specific_career": None,
9
  "bg_info": None,
10
  "work_value": None,
@@ -43,13 +43,14 @@ forward_done = False
43
  backward_done = False
44
  forward_recommendation_given = False
45
 
 
46
  recommendation_round = 0
47
  forward_deep_dive_done = False
48
  roadmap_offered = False
49
  roadmap_done = False
50
 
51
  # ============================ 模型设置 ============================
52
- model_default = "gpt-4o"
53
  token_default = 2000
54
  temp_default = 0.7
55
  top_p_default = 0.95
@@ -86,8 +87,8 @@ def do_time_roadmap():
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,
@@ -101,7 +102,7 @@ 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 "未知背景"
@@ -132,8 +133,8 @@ def do_deep_analysis():
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,
@@ -175,8 +176,8 @@ def recommend_3directions():
175
  return "错误:API_TOKEN 未设置"
176
  client = OpenAI(api_key=api_key)
177
  msgs = [
178
- {"role":"system","content":"你是一位职业顾问,擅长根据用户资料推荐3大方向"},
179
- {"role":"user","content": rec_prompt}
180
  ]
181
  resp = client.chat.completions.create(
182
  model=model_default,
@@ -241,7 +242,6 @@ def predict(message, history):
241
  global forward_deep_dive_done
242
  global roadmap_offered, roadmap_done
243
 
244
- # 若是第一次
245
  if not history:
246
  current_q_index = 0
247
  questions = base_questions[:]
@@ -250,7 +250,8 @@ def predict(message, history):
250
 
251
  in_forward_flow = False
252
  in_backward_flow = False
253
- forward_index = backward_index = 0
 
254
  forward_done = backward_done = False
255
  forward_recommendation_given = False
256
  recommendation_round = 0
@@ -258,11 +259,17 @@ def predict(message, history):
258
  roadmap_offered = False
259
  roadmap_done = False
260
 
261
- # ===== 如果在base_questions环节 =====
262
  if 0 < current_q_index <= len(questions):
263
  key = questions[current_q_index - 1][0]
264
  if key == "mode" and current_q_index == 1:
265
- user_profile["mode"] = message.strip() # 只这里写 mode
 
 
 
 
 
 
266
  else:
267
  user_profile[key] = message.strip()
268
 
@@ -273,13 +280,12 @@ def predict(message, history):
273
 
274
  mode = user_profile.get("mode") or ""
275
 
276
- # ~~~~~~~~~~~~ Backward模式 ~~~~~~~~~~~~
277
  if mode == "是":
278
  if in_backward_flow and not backward_done:
279
  if backward_index > 0 and backward_index <= len(backward_additional_questions):
280
  prev_key = backward_additional_questions[backward_index - 1][0]
281
  user_profile[prev_key] = message.strip()
282
-
283
  if backward_index < len(backward_additional_questions):
284
  k, prompt_text = backward_additional_questions[backward_index]
285
  backward_index += 1
@@ -295,8 +301,8 @@ def predict(message, history):
295
  client = OpenAI(api_key=api_key)
296
  sprompt = generate_system_prompt()
297
  msgs = [
298
- {"role":"system","content": sprompt},
299
- {"role":"user","content": f"请根据{user_profile}给出最终规划"}
300
  ]
301
  resp = client.chat.completions.create(
302
  model=model_default,
@@ -310,7 +316,7 @@ def predict(message, history):
310
  except Exception as e:
311
  return f"发生错误: {str(e)}"
312
 
313
- # ~~~~~~~~~~~~ Forward模式 ~~~~~~~~~~~~
314
  else:
315
  if in_forward_flow and not forward_done and not forward_deep_dive_done and not roadmap_done:
316
  if forward_index > 0 and forward_index <= len(forward_additional_questions):
@@ -334,13 +340,14 @@ def predict(message, history):
334
  recommendation_round += 1
335
  if recommendation_round > 2:
336
  forward_done = True
337
- return "已多次换方向,结束推荐了~"
338
  else:
339
  return recommend_3directions()
340
  else:
341
- return "请回复1/2/3选择方向,或输入'换'来重"
342
 
343
  elif forward_deep_dive_done and not roadmap_offered:
 
344
  roadmap_offered = True
345
  return "需要一个时间轴式职业路线图吗?如果是,请回复“是”,否则回复“否”。"
346
 
@@ -353,7 +360,7 @@ def predict(message, history):
353
  else:
354
  roadmap_done = True
355
  forward_done = True
356
- return "好的,不生成时间轴,本次就到这里。"
357
 
358
  if forward_done:
359
  try:
@@ -364,7 +371,7 @@ def predict(message, history):
364
  sprompt = generate_system_prompt()
365
  msgs = [
366
  {"role":"system","content":sprompt},
367
- {"role":"user","content": f"最终资料: {user_profile}, 如果还需要帮助请提问。"}
368
  ]
369
  resp = client.chat.completions.create(
370
  model=model_default,
@@ -378,105 +385,108 @@ def predict(message, history):
378
  except Exception as e:
379
  return f"发生错误: {str(e)}"
380
 
381
- return "信息收集完毕,如果尚未得到最终回复,请再输入任意文字继续。"
382
 
383
- # ============================ Gradio UI ============================
384
- with gr.Blocks(css="""
385
- body {
386
- background-color: #1e1e1e;
387
- color: #ffffff;
388
- }
389
- .gradio-container {
390
- font-family: 'Segoe UI', sans-serif;
391
- }
392
- .message.user {
393
- background-color: #cce6ff !important;
394
- color: #000000 !important;
 
 
 
 
 
395
  border-radius: 10px !important;
396
  padding: 10px;
397
  margin: 6px;
398
- }
399
- .message.bot {
400
- background-color: #5599ff !important;
401
- color: #000000 !important;
 
402
  border-radius: 10px !important;
403
  padding: 10px;
404
  margin: 6px;
405
- }
406
- .gradio-container .chat-msg.bot-msg .message.bot p {
407
- color: #000000 !important;
408
- }
409
- .gr-button {
410
- border-radius: 8px;
411
- }
412
- #custom-send {
413
- background-color: #ec4899 !important;
414
- color: white !important;
415
- border-radius: 999px !important;
416
- padding: 10px 24px !important;
417
- font-weight: bold;
418
- box-shadow: 0 0 10px #ec4899;
419
- transition: all 0.3s ease-in-out;
420
- }
421
- #custom-send:hover {
422
- background-color: #d63384 !important;
423
- box-shadow: 0 0 12px #ec4899;
424
- }
425
- textarea, input {
426
- background-color: #ffe4f1 !important;
427
- color: #5e2c49 !important;
428
- border: 1px solid #ec4899 !important;
429
- }
430
- footer {
431
- display: none !important;
432
- }
433
- """) as demo:
 
 
 
434
 
435
  with gr.Row():
 
 
 
436
  gr.HTML("""
437
  <div style='display: flex; align-items: center; justify-content: center; gap: 20px; margin-bottom: 10px;'>
438
- <img src='https://media3.giphy.com/media/v1.Y2lkPTc5MGI3NjExMmFucGwxbmNsd3J5NXV0Y282NXNtMzNsZW5jMm4wNWh6c2dqbXIwdiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/l41m18LjqpzxUr2WA/giphy.gif' width='200' style='border-radius: 12px; box-shadow: 0 0 10px #ec4899;'>
439
- <div style='text-align: left;'>
440
- <h1 style='color:white; font-size: 36px; margin-bottom: 6px;'>🎓 多步职业规划助手</h1>
441
- <p style='font-size: 18px; font-weight:bold; color:#ec4899; margin-top: 0;'>Forward三步:推荐大方向→深度分析→可选时间轴</p>
442
- </div>
443
- </div>
 
 
 
444
  """)
445
- gr.Markdown("""
446
- **Forward / Backward** 多轮交互;Forward可换方向+深度分析+时间轴;Backward直接问目标职业+背景
447
- """)
448
-
449
- chatbot = gr.Chatbot(
450
- height=500,
451
- show_label=False,
452
- show_copy_button=True,
453
- type="messages"
454
- )
455
- with gr.Row():
456
- with gr.Column(scale=8):
457
- msg = gr.Textbox(placeholder="请在这里输入你的回答...", show_label=False, container=False)
458
- with gr.Column(scale=1):
459
- submit_btn = gr.Button("🚀 发送", elem_id="custom-send")
460
-
461
- with gr.Row():
462
- reset_btn = gr.Button("🔄 重新开始")
463
-
464
- def add_message(m, hist):
465
  hist = hist or []
466
  hist.append({"role":"user","content":m})
467
  return "", hist
468
 
469
- def bot_response(hist):
470
- hist = hist or []
471
  user_m = hist[-1]["content"]
472
  bot_m = predict(user_m, hist[:-1])
473
  hist.append({"role":"assistant","content":bot_m})
474
  return hist
475
 
476
- submit_btn.click(add_message, [msg, chatbot], [msg, chatbot]) \
477
- .then(bot_response, chatbot, chatbot)
478
- msg.submit(add_message, [msg, chatbot], [msg, chatbot]) \
479
- .then(bot_response, chatbot, chatbot)
 
480
 
481
  def reset_state():
482
  global current_q_index, questions
@@ -490,15 +500,14 @@ footer {
490
 
491
  current_q_index = 0
492
  questions = base_questions[:]
493
- for k in user_profile:
494
- user_profile[k] = None
495
 
496
  in_forward_flow = False
497
  in_backward_flow = False
498
  forward_index = 0
499
  backward_index = 0
500
- forward_done = False
501
- backward_done = False
502
  forward_recommendation_given = False
503
  recommendation_round = 0
504
  forward_deep_dive_done = False
@@ -506,10 +515,11 @@ footer {
506
  roadmap_done = False
507
 
508
  return []
509
- reset_btn.click(reset_state, outputs=chatbot)
510
 
511
- def auto_first():
512
- return [{"role": "assistant", "content": questions[0][1]}]
 
 
513
 
514
- demo.load(auto_first, outputs=chatbot)
515
  demo.launch()
 
4
 
5
  # ============================ 用户信息结构 ============================
6
  user_profile = {
7
+ "mode": None, # 只在回答第一个问题时设置(是/否)
8
  "specific_career": None,
9
  "bg_info": None,
10
  "work_value": None,
 
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
50
  roadmap_done = False
51
 
52
  # ============================ 模型设置 ============================
53
+ model_default = "gpt-4o" # 你可改成 "gpt-3.5-turbo" 等
54
  token_default = 2000
55
  temp_default = 0.7
56
  top_p_default = 0.95
 
87
  client = OpenAI(api_key=api_key)
88
 
89
  msgs = [
90
+ {"role": "system", "content": "你是一位专业的职业规划顾问,会生成时间轴式路线图"},
91
+ {"role": "user", "content": prompt_roadmap}
92
  ]
93
  resp = client.chat.completions.create(
94
  model=model_default,
 
102
  except Exception as e:
103
  return f"生成路线图时出错: {str(e)}"
104
 
105
+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 深度分析函数(3个具体职业) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106
  def do_deep_analysis():
107
  direction = user_profile["forward_direction_choice"] or "尚未选择"
108
  bg_info = user_profile["bg_info"] or "未知背景"
 
133
  return "错误:API_TOKEN 未设置"
134
  client = OpenAI(api_key=api_key)
135
  msgs = [
136
+ {"role": "system", "content": "你是一位专业职业顾问,会深度分析并列出3个具体职业。"},
137
+ {"role": "user", "content": deep_prompt}
138
  ]
139
  resp = client.chat.completions.create(
140
  model=model_default,
 
176
  return "错误:API_TOKEN 未设置"
177
  client = OpenAI(api_key=api_key)
178
  msgs = [
179
+ {"role": "system", "content": "你是一位职业顾问,擅长根据用户资料推荐3大方向"},
180
+ {"role": "user", "content": rec_prompt}
181
  ]
182
  resp = client.chat.completions.create(
183
  model=model_default,
 
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[:]
 
250
 
251
  in_forward_flow = False
252
  in_backward_flow = False
253
+ forward_index = 0
254
+ backward_index = 0
255
  forward_done = backward_done = False
256
  forward_recommendation_given = False
257
  recommendation_round = 0
 
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
  if key == "mode" and current_q_index == 1:
266
+ # 只有回答第一个问题时 => mode
267
+ ans = message.strip()
268
+ user_profile["mode"] = ans
269
+ if "是" in ans:
270
+ in_backward_flow = True
271
+ else:
272
+ in_forward_flow = True
273
  else:
274
  user_profile[key] = message.strip()
275
 
 
280
 
281
  mode = user_profile.get("mode") or ""
282
 
283
+ # ========== Backward ==========
284
  if mode == "是":
285
  if in_backward_flow and not backward_done:
286
  if backward_index > 0 and backward_index <= len(backward_additional_questions):
287
  prev_key = backward_additional_questions[backward_index - 1][0]
288
  user_profile[prev_key] = message.strip()
 
289
  if backward_index < len(backward_additional_questions):
290
  k, prompt_text = backward_additional_questions[backward_index]
291
  backward_index += 1
 
301
  client = OpenAI(api_key=api_key)
302
  sprompt = generate_system_prompt()
303
  msgs = [
304
+ {"role":"system","content":sprompt},
305
+ {"role":"user","content": f"请根据{user_profile}给出完整规划"}
306
  ]
307
  resp = client.chat.completions.create(
308
  model=model_default,
 
316
  except Exception as e:
317
  return f"发生错误: {str(e)}"
318
 
319
+ # ========== Forward ==========
320
  else:
321
  if in_forward_flow and not forward_done and not forward_deep_dive_done and not roadmap_done:
322
  if forward_index > 0 and forward_index <= len(forward_additional_questions):
 
340
  recommendation_round += 1
341
  if recommendation_round > 2:
342
  forward_done = True
343
+ return "已多次换方向,结束推荐"
344
  else:
345
  return recommend_3directions()
346
  else:
347
+ return "请回复1/2/3选择方向,或输入'换'来重新推荐"
348
 
349
  elif forward_deep_dive_done and not roadmap_offered:
350
+ # 深度分析结束 -> 问路线图
351
  roadmap_offered = True
352
  return "需要一个时间轴式职业路线图吗?如果是,请回复“是”,否则回复“否”。"
353
 
 
360
  else:
361
  roadmap_done = True
362
  forward_done = True
363
+ return "好的,不生成时间轴,本次规划结束。"
364
 
365
  if forward_done:
366
  try:
 
371
  sprompt = generate_system_prompt()
372
  msgs = [
373
  {"role":"system","content":sprompt},
374
+ {"role":"user","content": f"以下是资料: {user_profile}. 需要更多咨询可再次输入"}
375
  ]
376
  resp = client.chat.completions.create(
377
  model=model_default,
 
385
  except Exception as e:
386
  return f"发生错误: {str(e)}"
387
 
388
+ return "信息收集完毕,如未得到最终回复,请再输入任意文字继续。"
389
 
390
+ with gr.Blocks(css="""
391
+ body {
392
+ background-color: #1e1e1e;
393
+ color: #ffffff;
394
+ }
395
+
396
+ .gradio-container {
397
+ font-family: 'Segoe UI', sans-serif;
398
+ }
399
+
400
+ .gr-chatbot {
401
+ background-color: transparent !important;
402
+ }
403
+
404
+ .message.user {
405
+ background-color: #cce6ff !important; /* Very light blue */
406
+ color: #000000 !important; /* <-- Black text */
407
  border-radius: 10px !important;
408
  padding: 10px;
409
  margin: 6px;
410
+ }
411
+
412
+ .message.bot {
413
+ background-color: #99ccff !important; /* Light blue */
414
+ color: #000000 !important; /* <-- Black text */
415
  border-radius: 10px !important;
416
  padding: 10px;
417
  margin: 6px;
418
+ }
419
+
420
+ .gr-button {
421
+ border-radius: 8px;
422
+ }
423
+
424
+ #custom-send {
425
+ background-color: #ec4899 !important;
426
+ color: white !important;
427
+ border-radius: 999px !important;
428
+ padding: 10px 24px !important;
429
+ font-weight: bold;
430
+ box-shadow: 0 0 10px #ec4899;
431
+ transition: all 0.3s ease-in-out;
432
+ }
433
+
434
+ #custom-send:hover {
435
+ background-color: #d63384 !important;
436
+ box-shadow: 0 0 12px #ec4899;
437
+ }
438
+
439
+ textarea, input {
440
+ background-color: #ffe4f1 !important;
441
+ color: #5e2c49 !important;
442
+ border: 1px solid #ec4899 !important;
443
+ }
444
+
445
+ footer {
446
+ display: none !important;
447
+ }
448
+
449
+ """) as demo:
450
 
451
  with gr.Row():
452
+
453
+
454
+
455
  gr.HTML("""
456
  <div style='display: flex; align-items: center; justify-content: center; gap: 20px; margin-bottom: 10px;'>
457
+ <!-- 左侧动图 -->
458
+ <img src='https://media3.giphy.com/media/v1.Y2lkPTc5MGI3NjExMmFucGwxbmNsd3J5NXV0Y282NXNtMzNsZW5jMm4wNWh6c2dqbXIwdiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/l41m18LjqpzxUr2WA/giphy.gif' width='200' style='border-radius: 12px; box-shadow: 0 0 10px #ec4899;'>
459
+
460
+ <!-- 右侧标题 -->
461
+ <div style='text-align: left;'>
462
+ <h1 style='color:Black; font-size: 36px; margin-bottom: 6px;'>🎓 AI Career Exploration Assistant</h1>
463
+ <p style='font-size: 18px; font-weight:bold; color:#ec4899; margin-top: 0;margin-left: 150px'>Hi! Lets Make the Dream Comes True 💖</p>
464
+ </div>
465
+ </div>
466
  """)
467
+ gr.Markdown("**Let's chat! 💬** Ask me anything you want")
468
+
469
+ chatbot = gr.Chatbot(height=500, show_label=False, show_copy_button=True, type="messages")
470
+ msg = gr.Textbox(placeholder="请输入你的回答...")
471
+ send = gr.Button("发送 🚀", elem_id="custom-send")
472
+ reset = gr.Button("🔄 重新开始")
473
+
474
+ def add_user_message(m, hist):
 
 
 
 
 
 
 
 
 
 
 
 
475
  hist = hist or []
476
  hist.append({"role":"user","content":m})
477
  return "", hist
478
 
479
+ def add_bot_response(hist):
 
480
  user_m = hist[-1]["content"]
481
  bot_m = predict(user_m, hist[:-1])
482
  hist.append({"role":"assistant","content":bot_m})
483
  return hist
484
 
485
+ send.click(add_user_message, [msg, chatbot], [msg, chatbot]) \
486
+ .then(add_bot_response, chatbot, chatbot)
487
+
488
+ msg.submit(add_user_message, [msg, chatbot], [msg, chatbot]) \
489
+ .then(add_bot_response, chatbot, chatbot)
490
 
491
  def reset_state():
492
  global current_q_index, questions
 
500
 
501
  current_q_index = 0
502
  questions = base_questions[:]
503
+ for key in user_profile:
504
+ user_profile[key] = None
505
 
506
  in_forward_flow = False
507
  in_backward_flow = False
508
  forward_index = 0
509
  backward_index = 0
510
+ forward_done = backward_done = False
 
511
  forward_recommendation_given = False
512
  recommendation_round = 0
513
  forward_deep_dive_done = False
 
515
  roadmap_done = False
516
 
517
  return []
 
518
 
519
+ reset.click(reset_state, outputs=chatbot)
520
+
521
+ def first_q():
522
+ return [{"role":"assistant","content": questions[0][1]}]
523
 
524
+ demo.load(first_q, outputs=chatbot)
525
  demo.launch()