HelenaXH commited on
Commit
f56bf7c
·
verified ·
1 Parent(s): 8f59b9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -46
app.py CHANGED
@@ -2,9 +2,9 @@ import gradio as gr
2
  from openai import OpenAI
3
  import os
4
 
5
- # ============================ 用户信息结构 ============================
6
  user_profile = {
7
- "mode": None, # 只在回答第一个问题时设置(是/否)
8
  "specific_career": None,
9
  "bg_info": None,
10
  "work_value": None,
@@ -12,33 +12,35 @@ user_profile = {
12
  "dream_day": None,
13
  "forward_direction_choice": None,
14
 
15
- # Forward模式下更多状态
16
- "recommended_directions": [], # 3个大方向
17
- "selected_direction": None, # 学生最终选定的大方向
18
- "recommended_jobs": [], # 3个具体职业
19
- "final_choice": None # 学生最终选定的具体职业
20
  }
21
 
22
- # ============================ 基础问题 ============================
23
  base_questions = [
24
- ("mode", "你是否有心仪的职业方向?\n- 如果有,请回复''(Backward模式)\n- 如果没有,请回复''(Forward模式)")
25
  ]
26
 
27
- # ============================ Forward 模式问题 ============================
 
 
 
28
  forward_additional_questions = [
29
- ("bg_info", """那么接下来我会需要你提供一些你的资料,并分享一些你的性格和喜好。请先告诉我,你的学校、年级、专业,和主修课程是什么?如果能提供你的选课表或resume就更好啦。"""),
30
- ("work_value", """非常好!那你认为"工作"的意义是什么?你觉得一份理想的工作,应该带来哪些价值或满足感?(例如:帮助他人、赚大钱、自由时间、个人成长、创意空间等)"""),
31
- ("personality_summary", "用几句话总结你的性格:比如外向/内向?喜欢挑战?注重细节?讨厌重复吗?"),
32
- ("dream_day", "再描述一下你理想工作的一天是什么样:在哪工作?做什么?和谁合作?忙还是闲?更自由还是更有秩序?")
33
  ]
34
 
35
- # ============================ Backward 模式问题 ============================
36
  backward_additional_questions = [
37
- ("specific_career", "请具体描述你想要从事的职业方向。"),
38
- ("bg_info", "请提供你的学校、年级、专业以及已有的实习或项目经历,以便更好地帮你规划如何达成目标。")
39
  ]
40
 
41
- # ============================ 状态控制变量 ============================
42
  current_q_index = 0
43
  questions = base_questions[:]
44
  in_forward_flow = False
@@ -53,44 +55,45 @@ forward_deep_dive_done = False
53
  roadmap_offered = False
54
  roadmap_done = False
55
 
56
- direction_chosen = False # 是否已经选定大方向
57
- jobs_recommended = False # 是否已给出3个具体职业
58
- job_chosen = False # 是否选定了具体职业
 
 
 
 
59
 
60
- # ========== 用于 A/B/C 专项问题的标记 ==========
61
- post_career_detail_asked = False # 是否已询问用户要看A/B/C
62
- post_career_detail_done = False # 是否已回答完A/B/C
63
 
64
- # ============================ 模型设置 ============================
65
  model_default = "gpt-4o"
66
  token_default = 2000
67
  temp_default = 0.7
68
  top_p_default = 0.95
69
 
70
 
71
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72
- # 动态函数:根据用户选择的 A/B/C,让 OpenAI 生成相应分析
73
  def backward_strategy_plan(specific_career, bg_info):
74
  prompt = f"""
75
- 你是一位专业的职业规划顾问。
76
- 现在有一位学生,他的目标职业是:{specific_career}
77
- 他的当前背景是:{bg_info}
78
- 请根据这个学生的背景和目标,帮他做一个清晰的达成路径规划,内容要包括:
79
- 1. 是否需要考虑转专业或辅修?
80
- 2. 是否需要补哪些课?课程关键词有哪些?
81
- 3. 推荐的证书/考试(如CFA等)和学习建议
82
- 4. 推荐的实习方向(基于他的背景)
83
- 5. 如何利用本校资源(career center、club、networking等)
84
- 6. 如果他背景不匹配(比如机械去金融),如何填补Gap?
85
- 请使用 Markdown 分段格式,不少于300字,内容务必紧扣他目前的背景和目标。
 
 
86
  """
87
  try:
88
  api_key = os.environ.get("API_TOKEN")
89
  if not api_key:
90
- return "错误:API_TOKEN 未设置"
91
  client = OpenAI(api_key=api_key)
92
  msgs = [
93
- {"role": "system", "content": "你是一位经验丰富的职业顾问,擅长根据用户背景制定路径规划"},
94
  {"role": "user", "content": prompt}
95
  ]
96
  resp = client.chat.completions.create(
@@ -103,7 +106,7 @@ def backward_strategy_plan(specific_career, bg_info):
103
  )
104
  return resp.choices[0].message.content
105
  except Exception as e:
106
- return f"生成职业路径建议时出错: {str(e)}"
107
 
108
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109
  def answer_abc_questions(selected, bg_info, wv, ps, dd):
@@ -719,8 +722,8 @@ def predict(message, history):
719
  except Exception as e:
720
  return f"发生错误: {str(e)}"
721
 
722
- # 兜底
723
- return "信息收集完毕,如还未得到最终回复,请再输入任意文字继续。"
724
 
725
  # ============================ Gradio UI ============================
726
  import gradio as gr
@@ -791,9 +794,9 @@ footer {
791
  """)
792
 
793
  chatbot = gr.Chatbot(height=500, show_label=False, show_copy_button=True, type="messages")
794
- msg = gr.Textbox(placeholder="请输入你的回答...")
795
- send = gr.Button("发送 🚀", elem_id="custom-send")
796
- reset = gr.Button("🔄 重新开始")
797
 
798
  def add_user_message(m, hist):
799
  hist = hist or []
 
2
  from openai import OpenAI
3
  import os
4
 
5
+
6
  user_profile = {
7
+ "mode": None,
8
  "specific_career": None,
9
  "bg_info": None,
10
  "work_value": None,
 
12
  "dream_day": None,
13
  "forward_direction_choice": None,
14
 
15
+ "recommended_directions": [],
16
+ "selected_direction": None,
17
+ "recommended_jobs": [],
18
+ "final_choice": None
 
19
  }
20
 
21
+
22
  base_questions = [
23
+ ("mode", "Do you have any preferred future plans?\n- if yes, reply'Y'(Backward Mode)\n- if NO,reply'NO'(Forward Mode)")
24
  ]
25
 
26
+
27
+
28
+
29
+ # ============================ Forward Mode Questions ============================
30
  forward_additional_questions = [
31
+ ("bg_info", """To get started, please tell me a bit about your academic background and interests. What's your school, year, major, and core courses? If you have a transcript or resume, that would be even better!"""),
32
+ ("work_value", """Great! What do you think is the meaning or value of 'work'? What should an ideal job provide for you? (e.g., helping others, high income, free time, personal growth, creative space, etc.)"""),
33
+ ("personality_summary", "Briefly describe your personality: Are you introverted or extroverted? Do you enjoy challenges? Detail-oriented? Do you dislike repetitive work?"),
34
+ ("dream_day", "Now describe what your ideal workday looks like: Where are you working? What are you doing? Who are you collaborating with? Is it busy or relaxed? More freedom or more structure?")
35
  ]
36
 
37
+ # ============================ Backward Mode Questions ============================
38
  backward_additional_questions = [
39
+ ("specific_career", "Please describe the specific career path you want to pursue."),
40
+ ("bg_info", "Please provide your school, year, major, and any internship or project experience you already have, so I can help you build a clearer path to your goal.")
41
  ]
42
 
43
+
44
  current_q_index = 0
45
  questions = base_questions[:]
46
  in_forward_flow = False
 
55
  roadmap_offered = False
56
  roadmap_done = False
57
 
58
+ direction_chosen = False
59
+ jobs_recommended = False
60
+ job_chosen = False
61
+
62
+
63
+ post_career_detail_asked = False
64
+ post_career_detail_done = False
65
 
 
 
 
66
 
 
67
  model_default = "gpt-4o"
68
  token_default = 2000
69
  temp_default = 0.7
70
  top_p_default = 0.95
71
 
72
 
73
+
 
74
  def backward_strategy_plan(specific_career, bg_info):
75
  prompt = f"""
76
+ You are a professional career planning advisor.
77
+ A student has the target career: {specific_career}
78
+ Their current background is: {bg_info}
79
+
80
+ Please help the student build a clear plan to reach their goal, including:
81
+ 1. Should they consider changing majors or taking a minor?
82
+ 2. Are there any courses they should take? What are the key course topics?
83
+ 3. Recommended certificates/exams (e.g., CFA) and study tips
84
+ 4. Suggested internship directions (based on their background)
85
+ 5. How to make use of university resources (career center, clubs, networking, etc.)
86
+ 6. If their background is mismatched (e.g., mechanical engineering to finance), how to bridge the gap?
87
+
88
+ Use Markdown formatting in sections, with a minimum of 300 words. The content must align closely with the student's current background and goals.
89
  """
90
  try:
91
  api_key = os.environ.get("API_TOKEN")
92
  if not api_key:
93
+ return "Wrong: API_TOKEN not set"
94
  client = OpenAI(api_key=api_key)
95
  msgs = [
96
+ {"role": "system", "content": "You are an experienced career advisor, good at creating roadmaps based on user background. "},
97
  {"role": "user", "content": prompt}
98
  ]
99
  resp = client.chat.completions.create(
 
106
  )
107
  return resp.choices[0].message.content
108
  except Exception as e:
109
+ return f"Wrong path: {str(e)}"
110
 
111
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112
  def answer_abc_questions(selected, bg_info, wv, ps, dd):
 
722
  except Exception as e:
723
  return f"发生错误: {str(e)}"
724
 
725
+
726
+ return "Information down"
727
 
728
  # ============================ Gradio UI ============================
729
  import gradio as gr
 
794
  """)
795
 
796
  chatbot = gr.Chatbot(height=500, show_label=False, show_copy_button=True, type="messages")
797
+ msg = gr.Textbox(placeholder="enter your answer...")
798
+ send = gr.Button("Send 🚀", elem_id="custom-send")
799
+ reset = gr.Button("🔄 Restart")
800
 
801
  def add_user_message(m, hist):
802
  hist = hist or []