HelenaXH commited on
Commit
c0ccc4f
·
verified ·
1 Parent(s): fa97226

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -295
app.py CHANGED
@@ -3,55 +3,99 @@ from openai import OpenAI
3
  import os
4
  import time
5
 
6
- # 存储用户回答的全局字典
7
  user_profile = {
8
- "mode": None,
9
- "age": None,
10
- "degree": None,
11
- "interests": None,
12
- "mbti": None,
13
- "specific_career": None # 用于Backward模式时存储具体职业方向
 
 
 
 
 
14
  }
15
 
16
- # 问题列表
17
  base_questions = [
18
- ("mode", "你是否有心仪的职业方向?\n- 如果有,请回复'是'(Backward模式:我们将帮你设计实现该目标的路径)\n- 如果没有,请回复'否'(Forward模式:我们将根据你的背景推荐合适的职业)"),
19
  ("age", "你的年龄范围是?(例如:'18岁以下','18-21岁','21-25岁','25-29岁','30岁以上')"),
20
  ("degree", "你目前的学历是什么?(例如:'本科在读','本科毕业','硕士在读','博士')"),
21
  ("interests", "请列出几个你的兴趣爱好,用逗号分隔。(例如:编程,心理学,设计)"),
22
  ("mbti", "你的MBTI人格类型是什么?(例如:INFP,INTJ,ENFP等)如果不知道,可以回复'不知道'")
23
  ]
24
 
25
- # 全局变量存储当前问题索引和问题列表
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  current_q_index = 0
27
- questions = base_questions.copy()
28
 
29
- # 默认值
 
 
 
 
 
 
 
 
30
  system_prompt_default = "你是一位专业的职业规划顾问,帮助学生规划他们的职业路径。"
31
  model_default = "gpt-4o"
32
  temp_default = 0.7
33
  top_p_default = 0.95
34
  token_default = 2000
35
 
36
- # 函数:生成系统提示
37
  def generate_system_prompt():
 
 
 
38
  mode = user_profile["mode"]
39
- age = user_profile["age"]
40
  degree = user_profile["degree"]
41
  interests = user_profile["interests"]
42
  mbti = user_profile["mbti"]
43
- specific_career = user_profile.get("specific_career")
44
-
45
- if mode and "" in mode: # Backward 模式
 
 
 
46
  prompt = f"""
47
  你是一位专业的职业规划顾问,使用"Backward Design"方法帮助学生规划他们的职业路径。
48
-
49
  学生个人资料:
50
  - 年龄: {age}
51
  - 学历: {degree}
52
  - 兴趣爱好: {interests}
53
  - MBTI类型: {mbti}
54
  - 目标职业: {specific_career}
 
55
 
56
  请分析学生提出的职业方向:
57
  1. 提供该职业的简要分析
@@ -59,7 +103,7 @@ def generate_system_prompt():
59
  3. 分析该职业所需的技能和能力
60
  4. 描述职业发展和上升空间
61
 
62
- 然后,基于学生的学术背景��请提供以下方面的具体建议:
63
  1. 专业选择建议(是否需要转专业、双学位等)
64
  2. 推荐的选课方向及理由
65
  3. 需要学习的关键技能
@@ -69,279 +113,3 @@ def generate_system_prompt():
69
  7. 简历优化建议
70
 
71
  最后,请用Markdown格式提供清晰的职业路线图,格式如下:
72
- ```
73
- 📍 现在: [当前状态]
74
- 🎓 [时间点]: [建议行动,如选修特定课程]
75
- 💼 [时间点]: [建议行动,如申请实习]
76
- 📄 [时间点]: [建议行动,如获取认证]
77
- 🚀 [时间点]: [建议行动,如目标职位申请]
78
- ```
79
- """
80
- else: # Forward 模式
81
- prompt = f"""
82
- 你是一位专业的职业规划顾问,使用"Forward Design"方法帮助学生探索适合的职业路径。
83
-
84
- 学生个人资料:
85
- - 年龄: {age}
86
- - 学历: {degree}
87
- - 兴趣爱好: {interests}
88
- - MBTI类型: {mbti}
89
-
90
- 请根据学生的背景和个人资料进行分析:
91
- 1. 总结学生的优势和劣势
92
- 2. 分析学生的性格特点和职业适配性
93
- 3. 基于学生的学历背景、兴趣爱好和性格特点,推荐6个可能适合的职业大方向
94
-
95
- 请以对话形式呈现,让学生从这6个方向中选择1-2个他们最感兴趣的方向。
96
-
97
- 当学生选择了特定方向后,请进一步推荐3个具体职业,并针对每个职业提供:
98
- 1. 职业描述和日常工作内容
99
- 2. 该职业对学生的适合度分析
100
- 3. 入行门槛和要求
101
- 4. 详细的准备步骤,包括:
102
- - 推荐的选课方向
103
- - 必要的技能培训
104
- - 资格认证或考试
105
- - 如何利用学校资源
106
- - 实习和经验积累建议
107
- - 简历优化方向
108
-
109
- 最后,为学生选择的职业方向提供一个清晰的Markdown格式职业路线图:
110
- ```
111
- 📍 现在: [当前状态]
112
- 🎓 [时间点]: [建议行动,如选修特定课程]
113
- 💼 [时间点]: [建议行动,如申请实习]
114
- 📄 [时间点]: [建议行动,如获取认证]
115
- 🚀 [时间点]: [建议行动,如目标职位申请]
116
- ```
117
- """
118
- return prompt
119
-
120
- # 预测函数
121
- def predict(message, history):
122
- global current_q_index, questions
123
-
124
- # 如果是第一次交互,重置问题索引和用户资料
125
- if not history:
126
- current_q_index = 0
127
- for key in user_profile:
128
- user_profile[key] = None
129
- questions = base_questions.copy()
130
-
131
- # 处理当前问题的回答
132
- if current_q_index > 0 and current_q_index <= len(questions):
133
- key = questions[current_q_index - 1][0]
134
- user_profile[key] = message.strip()
135
-
136
- # 如果模式是Backward(是)且刚刚回答了模式问题,添加额外的具体职业方向问题
137
- if key == "mode" and "是" in user_profile["mode"]:
138
- questions.insert(1, ("specific_career", "请具体描述你想要从事的职业方向。"))
139
-
140
- # 如果还有问题要问
141
- if current_q_index < len(questions):
142
- question = questions[current_q_index][1]
143
- current_q_index += 1
144
- return question
145
-
146
- # 所有问题都回答完了,生成个性化回复
147
- try:
148
- # 初始化OpenAI客户端
149
- api_key = os.environ.get("API_TOKEN")
150
- if not api_key:
151
- return "错误:API密钥未设置,请在Space设置中添加名为API_TOKEN的Secret。"
152
-
153
- client = OpenAI(api_key=api_key)
154
-
155
- # 生成系统提示
156
- system_prompt = generate_system_prompt()
157
-
158
- # 构建消息
159
- messages = [
160
- {"role": "system", "content": system_prompt},
161
- {"role": "user", "content": f"根据我提供的信息,请给我职业规划建议。我的回答是:{user_profile}. {message}"}
162
- ]
163
-
164
- # 调用API
165
- response = client.chat.completions.create(
166
- model=model_default,
167
- messages=messages,
168
- max_tokens=token_default,
169
- temperature=temp_default,
170
- top_p=top_p_default,
171
- stream=False
172
- )
173
-
174
- return response.choices[0].message.content
175
- except Exception as e:
176
- return f"发生错误: {str(e)}"
177
-
178
- # 创建自定义界面
179
- with gr.Blocks(css="""
180
- body {
181
- background-color: #1e1e1e;
182
- color: #ffffff;
183
- }
184
-
185
- .gradio-container {
186
- font-family: 'Segoe UI', sans-serif;
187
- }
188
-
189
- .message.user {
190
- background-color: #cce6ff !important; /* Much lighter blue */
191
- color: #000000 !important;
192
- border-radius: 10px !important;
193
- padding: 10px;
194
- margin: 6px;
195
- }
196
-
197
- .message.bot {
198
- background-color: #5599ff !important; /* Lighter blue */
199
- color: #000000 !important;
200
- border-radius: 10px !important;
201
- padding: 10px;
202
- margin: 6px;
203
- }
204
-
205
- .gradio-container .chat-msg.bot-msg .message.bot p {
206
- color: #000000 !important;
207
- }
208
-
209
- .gr-button {
210
- border-radius: 8px;
211
- }
212
-
213
- #custom-send {
214
- background-color: #ec4899 !important;
215
- color: white !important;
216
- border-radius: 999px !important;
217
- padding: 10px 24px !important;
218
- font-weight: bold;
219
- box-shadow: 0 0 10px #ec4899;
220
- transition: all 0.3s ease-in-out;
221
- }
222
-
223
- #custom-send:hover {
224
- background-color: #d63384 !important;
225
- box-shadow: 0 0 12px #ec4899;
226
- }
227
-
228
- textarea, input {
229
- background-color: #ffe4f1 !important;
230
- color: #5e2c49 !important;
231
- border: 1px solid #ec4899 !important;
232
- }
233
-
234
- footer {
235
- display: none !important;
236
- }
237
- """) as demo:
238
-
239
- with gr.Row():
240
- gr.HTML("""
241
- <div style='display: flex; align-items: center; justify-content: center; gap: 20px; margin-bottom: 10px;'>
242
- <!-- 左侧动图 -->
243
- <img src='https://media3.giphy.com/media/v1.Y2lkPTc5MGI3NjExMmFucGwxbmNsd3J5NXV0Y282NXNtMzNsZW5jMm4wNWh6c2dqbXIwdiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/l41m18LjqpzxUr2WA/giphy.gif' width='200' style='border-radius: 12px; box-shadow: 0 0 10px #ec4899;'>
244
-
245
- <!-- 右侧标题 -->
246
- <div style='text-align: left;'>
247
- <h1 style='color:white; font-size: 36px; margin-bottom: 6px;'>🎓 AI职业规划助手</h1>
248
- <p style='font-size: 18px; font-weight:bold; color:#ec4899; margin-top: 0;margin-left: 150px'>让梦想照进现实 💖</p>
249
- </div>
250
- </div>
251
- """)
252
-
253
- # 添加说明文本
254
- gr.Markdown("""
255
- **📝 个性化职业规划助手**
256
-
257
- 我将通过5个简单问题了解你的背景,然后为你提供量身定制的职业规划建议。
258
-
259
- **两种模式:**
260
- - **Forward模式**:如果你还不确定职业方向,我会根据你的背景推荐合适的职业
261
- - **Backward模式**:如果你已有明确职业目标,我会帮你规划实现该目标的路径
262
-
263
- 请回答下面的问题,开始你的职业规划之旅!
264
- """)
265
-
266
- # 创建聊天界面 - 修复类型和属性
267
- chatbot = gr.Chatbot(
268
- height=500,
269
- show_label=False,
270
- show_copy_button=True,
271
- type="messages" # 指定类型为messages
272
- )
273
-
274
- # 输入框和按钮
275
- with gr.Row():
276
- with gr.Column(scale=8):
277
- msg = gr.Textbox(
278
- placeholder="请在这里输入你的回答...",
279
- show_label=False,
280
- container=False
281
- )
282
-
283
- with gr.Column(scale=1):
284
- submit_btn = gr.Button("🚀 发送", elem_id="custom-send")
285
-
286
- # 重置按钮
287
- with gr.Row():
288
- reset_btn = gr.Button("🔄 重新开始")
289
-
290
- # 修复事件处理
291
- def add_message(message, history):
292
- history = history or []
293
- history.append({"role": "user", "content": message})
294
- return "", history
295
-
296
- def bot_response(history):
297
- history = history or []
298
- user_message = history[-1]["content"]
299
- bot_message = predict(user_message, history[:-1] if len(history) > 1 else [])
300
- history.append({"role": "assistant", "content": bot_message})
301
- return history
302
-
303
- # 提交按钮点击事件
304
- submit_btn.click(
305
- fn=add_message,
306
- inputs=[msg, chatbot],
307
- outputs=[msg, chatbot]
308
- ).then(
309
- fn=bot_response,
310
- inputs=[chatbot],
311
- outputs=[chatbot]
312
- )
313
-
314
- # 文本框提交事件
315
- msg.submit(
316
- fn=add_message,
317
- inputs=[msg, chatbot],
318
- outputs=[msg, chatbot]
319
- ).then(
320
- fn=bot_response,
321
- inputs=[chatbot],
322
- outputs=[chatbot]
323
- )
324
-
325
- # 重置功能
326
- def reset_conversation():
327
- global current_q_index, questions
328
- current_q_index = 0
329
- questions = base_questions.copy()
330
- for key in user_profile:
331
- user_profile[key] = None
332
- return []
333
-
334
- reset_btn.click(
335
- fn=reset_conversation,
336
- inputs=None,
337
- outputs=chatbot,
338
- queue=False
339
- )
340
-
341
- # 当页面加载时自动触发第一个问题
342
- def auto_first_question():
343
- return [{"role": "assistant", "content": questions[0][1]}]
344
-
345
- demo.load(auto_first_question, inputs=None, outputs=chatbot)
346
-
347
- demo.launch()
 
3
  import os
4
  import time
5
 
6
+ # ========== 全局变量与基础问题 ==========
7
  user_profile = {
8
+ "mode": None, # 是否有明确职业目标:是/否
9
+ "age": None, # 年龄范围
10
+ "degree": None, # 学历
11
+ "interests": None, # 兴趣爱好
12
+ "mbti": None, # MBTI类型
13
+ "specific_career": None, # Backward模式下的目标职业
14
+
15
+ # 以下是为增强多轮交互新增的字段:
16
+ "bg_info": None, # 学术背景(学校、年级、专业、主修课程、Resume)
17
+ "test_choice": None, # 是否做职业测评或小游戏(Forward模式中示例)
18
+ "forward_career_choice": None, # Forward模式下,用户从多个推荐方向中最终选择的方向
19
  }
20
 
21
+ # 原始基础问题(5个)
22
  base_questions = [
23
+ ("mode", "你是否有心仪的职业方向?\n- 如果有,请回复'是'(Backward模式)\n- 如果没有,请回复'否'(Forward模式)"),
24
  ("age", "你的年龄范围是?(例如:'18岁以下','18-21岁','21-25岁','25-29岁','30岁以上')"),
25
  ("degree", "你目前的学历是什么?(例如:'本科在读','本科毕业','硕士在读','博士')"),
26
  ("interests", "请列出几个你的兴趣爱好,用逗号分隔。(例如:编程,心理学,设计)"),
27
  ("mbti", "你的MBTI人格类型是什么?(例如:INFP,INTJ,ENFP等)如果不知道,可以回复'不知道'")
28
  ]
29
 
30
+ # Forward模式下的补充问题:示例
31
+ forward_additional_questions = [
32
+ (
33
+ "bg_info",
34
+ "请提供你的具体学术背景:你的学校、年级、专业、主修课程。如果能简单介绍一下你目前的选课或实习经历就更好啦。"
35
+ ),
36
+ (
37
+ "test_choice",
38
+ "我可以帮你做一个简单的职业性格测评,或玩一个小心理游戏,让你更清晰地认识自己。你想试试看吗?(请输入'是'或'否')"
39
+ ),
40
+ (
41
+ "forward_career_choice",
42
+ "基于你的背景和性格,我会给出几个职业大方向,请在这里输入你最感兴趣的1~2个方向,以便深入讨论。"
43
+ ),
44
+ ]
45
+
46
+ # Backward模式下的补充问题:示例
47
+ backward_additional_questions = [
48
+ # 注意:original code中会在第1个问题之后自动插入("specific_career", "请具体描述你的职业方向")
49
+ (
50
+ "bg_info",
51
+ "请提供你的学校、年级、专业以及已有的实习或项目经历,以便更好地帮你规划如何达成目标。"
52
+ )
53
+ ]
54
+
55
+ # 记录当前问题索引
56
  current_q_index = 0
57
+ questions = base_questions[:] # 基础问题列表的拷贝
58
 
59
+ # 标记流程状态
60
+ in_forward_flow = False
61
+ in_backward_flow = False
62
+ forward_index = 0
63
+ backward_index = 0
64
+ forward_done = False
65
+ backward_done = False
66
+
67
+ # ========== 默认模型参数,可自行修改 ==========
68
  system_prompt_default = "你是一位专业的职业规划顾问,帮助学生规划他们的职业路径。"
69
  model_default = "gpt-4o"
70
  temp_default = 0.7
71
  top_p_default = 0.95
72
  token_default = 2000
73
 
74
+ # ========== 生成系统提示 ==========
75
  def generate_system_prompt():
76
+ """
77
+ 根据 Forward / Backward 模式以及收集的用户信息,动态生成 system prompt
78
+ """
79
  mode = user_profile["mode"]
80
+ age = user_profile["age"]
81
  degree = user_profile["degree"]
82
  interests = user_profile["interests"]
83
  mbti = user_profile["mbti"]
84
+ specific_career = user_profile["specific_career"]
85
+ bg_info = user_profile["bg_info"]
86
+ test_choice = user_profile["test_choice"]
87
+ forward_career_choice = user_profile["forward_career_choice"]
88
+
89
+ if mode and "是" in mode: # Backward模式
90
  prompt = f"""
91
  你是一位专业的职业规划顾问,使用"Backward Design"方法帮助学生规划他们的职业路径。
 
92
  学生个人资料:
93
  - 年龄: {age}
94
  - 学历: {degree}
95
  - 兴趣爱好: {interests}
96
  - MBTI类型: {mbti}
97
  - 目标职业: {specific_career}
98
+ - 学术背景: {bg_info}
99
 
100
  请分析学生提出的职业方向:
101
  1. 提供该职业的简要分析
 
103
  3. 分析该职业所需的技能和能力
104
  4. 描述职业发展和上升空间
105
 
106
+ 然后,基于学生的背景请提供以下方面的具体建议:
107
  1. 专业选择建议(是否需要转专业、双学位等)
108
  2. 推荐的选课方向及理由
109
  3. 需要学习的关键技能
 
113
  7. 简历优化建议
114
 
115
  最后,请用Markdown格式提供清晰的职业路线图,格式如下: