HelenaXH commited on
Commit
2323ed0
·
verified ·
1 Parent(s): e6b96b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -128
app.py CHANGED
@@ -9,7 +9,8 @@ user_profile = {
9
  "bg_info": None,
10
  "work_value": None,
11
  "personality_summary": None,
12
- "dream_day": None
 
13
  }
14
 
15
  # ============================ 基础问题 ============================
@@ -50,6 +51,7 @@ forward_index = 0
50
  backward_index = 0
51
  forward_done = False
52
  backward_done = False
 
53
 
54
  # ============================ 模型设置 ============================
55
  model_default = "gpt-4o"
@@ -65,6 +67,7 @@ def generate_system_prompt():
65
  work_value = user_profile["work_value"]
66
  personality_summary = user_profile["personality_summary"]
67
  dream_day = user_profile["dream_day"]
 
68
 
69
  if "是" in (mode or ""):
70
  return f"""
@@ -83,17 +86,18 @@ def generate_system_prompt():
83
  else:
84
  return f"""
85
  你是一位专业的职业规划顾问,使用Forward Design方法帮助学生探索合适的职业路径。
 
86
  学生背景:
87
  - 学术背景: {bg_info}
88
  - 工作意义: {work_value}
89
  - 性格总结: {personality_summary}
90
  - 理想一天: {dream_day}
 
 
91
  请输出:
92
  1. 学生的优势、性格、价值观分析
93
- 2. 推荐6个职业方向说明理由
94
- 3. 等学生选择后,再推荐3个具体职业,每个说明日常内容、适配性、要求、准备路径
95
- 最后请用Markdown格式输出职业路径图:
96
- 📍 现在 → 🎓 学习建议 → 💼 实践建议 → 📄 认证建议 → 🚀 求职建议
97
  """
98
 
99
  # ============================ 主逻辑函数 ============================
@@ -101,19 +105,17 @@ def predict(message, history):
101
  global current_q_index, questions
102
  global in_forward_flow, in_backward_flow
103
  global forward_index, backward_index
104
- global forward_done, backward_done
105
 
106
  if not history:
107
  current_q_index = 0
108
  questions = base_questions[:]
109
  for key in user_profile:
110
  user_profile[key] = None
111
- in_forward_flow = False
112
- in_backward_flow = False
113
- forward_index = 0
114
- backward_index = 0
115
- forward_done = False
116
- backward_done = False
117
 
118
  if current_q_index > 0 and current_q_index <= len(questions):
119
  key = questions[current_q_index - 1][0]
@@ -144,7 +146,38 @@ def predict(message, history):
144
  key, q_text = forward_additional_questions[forward_index]
145
  forward_index += 1
146
  return q_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  else:
 
148
  forward_done = True
149
 
150
  if in_backward_flow and backward_index > 0 and not backward_done:
@@ -184,119 +217,3 @@ def predict(message, history):
184
 
185
  return "信息收集完毕,若尚未得到最终回复,请输入任意文字以继续。"
186
 
187
-
188
- # ============================ Gradio UI ============================
189
- with gr.Blocks(css="""
190
- body {
191
- background-color: #1e1e1e;
192
- color: #ffffff;
193
- }
194
- .gradio-container {
195
- font-family: 'Segoe UI', sans-serif;
196
- }
197
- .message.user {
198
- background-color: #cce6ff !important;
199
- color: #000000 !important;
200
- border-radius: 10px !important;
201
- padding: 10px;
202
- margin: 6px;
203
- }
204
- .message.bot {
205
- background-color: #5599ff !important;
206
- color: #000000 !important;
207
- border-radius: 10px !important;
208
- padding: 10px;
209
- margin: 6px;
210
- }
211
- .gradio-container .chat-msg.bot-msg .message.bot p {
212
- color: #000000 !important;
213
- }
214
- .gr-button {
215
- border-radius: 8px;
216
- }
217
- #custom-send {
218
- background-color: #ec4899 !important;
219
- color: white !important;
220
- border-radius: 999px !important;
221
- padding: 10px 24px !important;
222
- font-weight: bold;
223
- box-shadow: 0 0 10px #ec4899;
224
- transition: all 0.3s ease-in-out;
225
- }
226
- #custom-send:hover {
227
- background-color: #d63384 !important;
228
- box-shadow: 0 0 12px #ec4899;
229
- }
230
- textarea, input {
231
- background-color: #ffe4f1 !important;
232
- color: #5e2c49 !important;
233
- border: 1px solid #ec4899 !important;
234
- }
235
- footer {
236
- display: none !important;
237
- }
238
- """) as demo:
239
- with gr.Row():
240
- gr.HTML("""
241
- <div style='display: flex; align-items: center; justify-content: center; gap: 20px; margin-bottom: 10px;'>
242
- <img src='https://media3.giphy.com/media/v1.Y2lkPTc5MGI3NjExMmFucGwxbmNsd3J5NXV0Y282NXNtMzNsZW5jMm4wNWh6c2dqbXIwdiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/l41m18LjqpzxUr2WA/giphy.gif' width='200' style='border-radius: 12px; box-shadow: 0 0 10px #ec4899;'>
243
- <div style='text-align: left;'>
244
- <h1 style='color:white; font-size: 36px; margin-bottom: 6px;'>🎓 AI职业规划助手(升级版)</h1>
245
- <p style='font-size: 18px; font-weight:bold; color:#ec4899; margin-top: 0;margin-left: 150px'>让梦想照进现实 💖</p>
246
- </div>
247
- </div>
248
- """)
249
- gr.Markdown("""
250
- **📝 个性化职业规划助手:Forward / Backward 多轮交互示例**
251
-
252
- - 如果你已确定了职业目标,选择 **Backward** 模式(回答“是”)。
253
- - 如果你需要探索适合的职业方向,选择 **Forward** 模式(回答“否”)。
254
- - 我会收集你的基础信息、学术背景,并根据你的回答做多轮引导,最后给出职业规划建议。
255
- """)
256
- chatbot = gr.Chatbot(height=500, show_label=False, show_copy_button=True, type="messages")
257
- with gr.Row():
258
- with gr.Column(scale=8):
259
- msg = gr.Textbox(placeholder="请在这里输入你的回答...", show_label=False, container=False)
260
- with gr.Column(scale=1):
261
- submit_btn = gr.Button("🚀 发送", elem_id="custom-send")
262
- with gr.Row():
263
- reset_btn = gr.Button("🔄 重新开始")
264
- def add_message(message, history):
265
- history = history or []
266
- history.append({"role": "user", "content": message})
267
- return "", history
268
- def bot_response(history):
269
- history = history or []
270
- user_message = history[-1]["content"]
271
- bot_message = predict(user_message, history[:-1] if len(history) > 1 else [])
272
- history.append({"role": "assistant", "content": bot_message})
273
- return history
274
- submit_btn.click(fn=add_message, inputs=[msg, chatbot], outputs=[msg, chatbot]).then(
275
- fn=bot_response, inputs=[chatbot], outputs=[chatbot]
276
- )
277
- msg.submit(fn=add_message, inputs=[msg, chatbot], outputs=[msg, chatbot]).then(
278
- fn=bot_response, inputs=[chatbot], outputs=[chatbot]
279
- )
280
- def reset_conversation():
281
- global current_q_index, questions
282
- global in_forward_flow, in_backward_flow
283
- global forward_index, backward_index
284
- global forward_done, backward_done
285
- current_q_index = 0
286
- questions = base_questions[:]
287
- for key in user_profile:
288
- user_profile[key] = None
289
- in_forward_flow = False
290
- in_backward_flow = False
291
- forward_index = 0
292
- backward_index = 0
293
- forward_done = False
294
- backward_done = False
295
- return []
296
- reset_btn.click(fn=reset_conversation, inputs=None, outputs=chatbot, queue=False)
297
- def auto_first_question():
298
- return [{"role": "assistant", "content": questions[0][1]}]
299
- demo.load(auto_first_question, inputs=None, outputs=chatbot)
300
- demo.launch()
301
-
302
-
 
9
  "bg_info": None,
10
  "work_value": None,
11
  "personality_summary": None,
12
+ "dream_day": None,
13
+ "forward_direction_choice": None
14
  }
15
 
16
  # ============================ 基础问题 ============================
 
51
  backward_index = 0
52
  forward_done = False
53
  backward_done = False
54
+ forward_recommendation_given = False
55
 
56
  # ============================ 模型设置 ============================
57
  model_default = "gpt-4o"
 
67
  work_value = user_profile["work_value"]
68
  personality_summary = user_profile["personality_summary"]
69
  dream_day = user_profile["dream_day"]
70
+ forward_direction_choice = user_profile["forward_direction_choice"]
71
 
72
  if "是" in (mode or ""):
73
  return f"""
 
86
  else:
87
  return f"""
88
  你是一位专业的职业规划顾问,使用Forward Design方法帮助学生探索合适的职业路径。
89
+
90
  学生背景:
91
  - 学术背景: {bg_info}
92
  - 工作意义: {work_value}
93
  - 性格总结: {personality_summary}
94
  - 理想一天: {dream_day}
95
+ - 选择方向: {forward_direction_choice}
96
+
97
  请输出:
98
  1. 学生的优势、性格、价值观分析
99
+ 2. 推荐具体职业(3个),说明日常内容、适性、要求、准备路径
100
+ 3. 输出职业路径图:📍 现在 → 🎓 → 💼 → 📄 → 🚀
 
 
101
  """
102
 
103
  # ============================ 主逻辑函数 ============================
 
105
  global current_q_index, questions
106
  global in_forward_flow, in_backward_flow
107
  global forward_index, backward_index
108
+ global forward_done, backward_done, forward_recommendation_given
109
 
110
  if not history:
111
  current_q_index = 0
112
  questions = base_questions[:]
113
  for key in user_profile:
114
  user_profile[key] = None
115
+ in_forward_flow = in_backward_flow = False
116
+ forward_index = backward_index = 0
117
+ forward_done = backward_done = False
118
+ forward_recommendation_given = False
 
 
119
 
120
  if current_q_index > 0 and current_q_index <= len(questions):
121
  key = questions[current_q_index - 1][0]
 
146
  key, q_text = forward_additional_questions[forward_index]
147
  forward_index += 1
148
  return q_text
149
+ elif not forward_recommendation_given:
150
+ forward_recommendation_given = True
151
+ try:
152
+ api_key = os.environ.get("API_TOKEN")
153
+ if not api_key:
154
+ return "错误:API_TOKEN 未设置"
155
+ client = OpenAI(api_key=api_key)
156
+ recommendation_prompt = f"""
157
+ 根据以下信息,总结学生的特点,并推荐3个职业方向(大类)。最后请以如下格式结尾:\n
158
+ 1. xxx\n2. xxx\n3. xxx\n\n请学生从中选择一个方向继续:
159
+ - 学术背景: {user_profile['bg_info']}
160
+ - 工作意义: {user_profile['work_value']}
161
+ - 性格总结: {user_profile['personality_summary']}
162
+ - 理想一天: {user_profile['dream_day']}
163
+ """
164
+ messages = [
165
+ {"role": "system", "content": "你是一位职业顾问,善于总结和推荐方向。"},
166
+ {"role": "user", "content": recommendation_prompt}
167
+ ]
168
+ response = client.chat.completions.create(
169
+ model=model_default,
170
+ messages=messages,
171
+ max_tokens=token_default,
172
+ temperature=temp_default,
173
+ top_p=top_p_default,
174
+ stream=False
175
+ )
176
+ return response.choices[0].message.content
177
+ except Exception as e:
178
+ return f"生成推荐方向时出错: {str(e)}"
179
  else:
180
+ user_profile["forward_direction_choice"] = message.strip()
181
  forward_done = True
182
 
183
  if in_backward_flow and backward_index > 0 and not backward_done:
 
217
 
218
  return "信息收集完毕,若尚未得到最终回复,请输入任意文字以继续。"
219