HelenaXH commited on
Commit
e6b96b0
·
verified ·
1 Parent(s): 31cf459

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -87
app.py CHANGED
@@ -20,13 +20,10 @@ base_questions = [
20
  # ============================ Forward 模式问题 ============================
21
  forward_additional_questions = [
22
  ("bg_info", """那么接下来我会需要你提供一些你的资料,并分享一些你的性格和喜好,我会分析然后给你推荐定制的可行方向。
23
-
24
  接下来,请你首先提供你的学术背景:你的学校、年级、专业,和主修课程是什么?如果能提供你的选课表或 resume 就最好啦。"""),
25
 
26
  ("work_value", """很好!接下来我想更深入地了解你。
27
-
28
  你认为“工作”的意义是什么?你觉得一份理想的工作,应该带来哪些价值或满足感?
29
-
30
  你可以从以下几个角度思考,也可以自由表达:
31
  - 是可以帮助他人?实现社会影响力?
32
  - 还是可以赚大钱?获得物质回报?
@@ -72,41 +69,34 @@ def generate_system_prompt():
72
  if "是" in (mode or ""):
73
  return f"""
74
  你是一位专业的职业规划顾问,使用Backward Design方法帮助学生达成他们的职业目标。
75
-
76
  目标职业: {specific_career}
77
  背景信息: {bg_info}
78
-
79
  请提供:
80
  1. 该职业的简要分析
81
  2. 排名前列的组织或公司
82
  3. 所需技能和能力
83
  4. 职业发展路径
84
  5. 学术/课程建议、技能培养、资源使用、实习规划、简历优化等
85
-
86
  最后请用Markdown格式输出职业路径图:
87
  📍 现在 → 🎓 学习建议 → 💼 实践建议 → 📄 认证建议 → 🚀 求职建议
88
  """
89
  else:
90
  return f"""
91
  你是一位专业的职业规划顾问,使用Forward Design方法帮助学生探索合适的职业路径。
92
-
93
  学生背景:
94
  - 学术背景: {bg_info}
95
  - 工作意义: {work_value}
96
  - 性格总结: {personality_summary}
97
  - 理想一天: {dream_day}
98
-
99
  请输出:
100
  1. 学生的优势、性格、价值观分析
101
  2. 推荐6个职业方向,并说明匹配理由
102
  3. 等学生选择后,再推荐3个具体职业,每个说明日常内容、适配性、要求、准备路径
103
-
104
  最后请用Markdown格式输出职业路径图:
105
  📍 现在 → 🎓 学习建议 → 💼 实践建议 → 📄 认证建议 → 🚀 求职建议
106
  """
107
- """
108
- return prompt
109
 
 
110
  def predict(message, history):
111
  global current_q_index, questions
112
  global in_forward_flow, in_backward_flow
@@ -169,21 +159,6 @@ def predict(message, history):
169
  key = forward_additional_questions[idx][0]
170
  user_profile[key] = message.strip()
171
 
172
- if "是" in mode:
173
- if backward_index < len(backward_additional_questions):
174
- key, q_text = backward_additional_questions[backward_index]
175
- backward_index += 1
176
- return q_text
177
- else:
178
- backward_done = True
179
- else:
180
- if forward_index < len(forward_additional_questions):
181
- key, q_text = forward_additional_questions[forward_index]
182
- forward_index += 1
183
- return q_text
184
- else:
185
- forward_done = True
186
-
187
  if ("是" in mode and backward_done) or ("否" in mode and forward_done):
188
  try:
189
  api_key = os.environ.get("API_TOKEN")
@@ -209,55 +184,57 @@ def predict(message, history):
209
 
210
  return "信息收集完毕,若尚未得到最终回复,请输入任意文字以继续。"
211
 
212
- with gr.Blocks(css="""
213
- body {
214
- background-color: #1e1e1e;
215
- color: #ffffff;
216
- }
217
- .gradio-container {
218
- font-family: 'Segoe UI', sans-serif;
219
- }
220
- .message.user {
221
- background-color: #cce6ff !important;
222
- color: #000000 !important;
223
- border-radius: 10px !important;
224
- padding: 10px;
225
- margin: 6px;
226
- }
227
- .message.bot {
228
- background-color: #5599ff !important;
229
- color: #000000 !important;
230
- border-radius: 10px !important;
231
- padding: 10px;
232
- margin: 6px;
233
- }
234
- .gradio-container .chat-msg.bot-msg .message.bot p {
235
- color: #000000 !important;
236
- }
237
- .gr-button {
238
- border-radius: 8px;
239
- }
240
- #custom-send {
241
- background-color: #ec4899 !important;
242
- color: white !important;
243
- border-radius: 999px !important;
244
- padding: 10px 24px !important;
245
- font-weight: bold;
246
- box-shadow: 0 0 10px #ec4899;
247
- transition: all 0.3s ease-in-out;
248
- }
249
- #custom-send:hover {
250
- background-color: #d63384 !important;
251
- box-shadow: 0 0 12px #ec4899;
252
- }
253
- textarea, input {
254
- background-color: #ffe4f1 !important;
255
- color: #5e2c49 !important;
256
- border: 1px solid #ec4899 !important;
257
- }
258
- footer {
259
- display: none !important;
260
- }
 
 
261
  """) as demo:
262
  with gr.Row():
263
  gr.HTML("""
@@ -269,7 +246,6 @@ def predict(message, history):
269
  </div>
270
  </div>
271
  """)
272
-
273
  gr.Markdown("""
274
  **📝 个性化职业规划助手:Forward / Backward 多轮交互示例**
275
 
@@ -277,38 +253,30 @@ def predict(message, history):
277
  - 如果你需要探索适合的职业方向,选择 **Forward** 模式(回答“否”)。
278
  - 我会收集你的基础信息、学术背景,并根据你的回答做多轮引导,最后给出职业规划建议。
279
  """)
280
-
281
  chatbot = gr.Chatbot(height=500, show_label=False, show_copy_button=True, type="messages")
282
-
283
  with gr.Row():
284
  with gr.Column(scale=8):
285
  msg = gr.Textbox(placeholder="请在这里输入你的回答...", show_label=False, container=False)
286
  with gr.Column(scale=1):
287
  submit_btn = gr.Button("🚀 发送", elem_id="custom-send")
288
-
289
  with gr.Row():
290
  reset_btn = gr.Button("🔄 重新开始")
291
-
292
  def add_message(message, history):
293
  history = history or []
294
  history.append({"role": "user", "content": message})
295
  return "", history
296
-
297
  def bot_response(history):
298
  history = history or []
299
  user_message = history[-1]["content"]
300
  bot_message = predict(user_message, history[:-1] if len(history) > 1 else [])
301
  history.append({"role": "assistant", "content": bot_message})
302
  return history
303
-
304
  submit_btn.click(fn=add_message, inputs=[msg, chatbot], outputs=[msg, chatbot]).then(
305
  fn=bot_response, inputs=[chatbot], outputs=[chatbot]
306
  )
307
-
308
  msg.submit(fn=add_message, inputs=[msg, chatbot], outputs=[msg, chatbot]).then(
309
  fn=bot_response, inputs=[chatbot], outputs=[chatbot]
310
  )
311
-
312
  def reset_conversation():
313
  global current_q_index, questions
314
  global in_forward_flow, in_backward_flow
@@ -325,12 +293,9 @@ def predict(message, history):
325
  forward_done = False
326
  backward_done = False
327
  return []
328
-
329
  reset_btn.click(fn=reset_conversation, inputs=None, outputs=chatbot, queue=False)
330
-
331
  def auto_first_question():
332
  return [{"role": "assistant", "content": questions[0][1]}]
333
-
334
  demo.load(auto_first_question, inputs=None, outputs=chatbot)
335
  demo.launch()
336
 
 
20
  # ============================ Forward 模式问题 ============================
21
  forward_additional_questions = [
22
  ("bg_info", """那么接下来我会需要你提供一些你的资料,并分享一些你的性格和喜好,我会分析然后给你推荐定制的可行方向。
 
23
  接下来,请你首先提供你的学术背景:你的学校、年级、专业,和主修课程是什么?如果能提供你的选课表或 resume 就最好啦。"""),
24
 
25
  ("work_value", """很好!接下来我想更深入地了解你。
 
26
  你认为“工作”的意义是什么?你觉得一份理想的工作,应该带来哪些价值或满足感?
 
27
  你可以从以下几个角度思考,也可以自由表达:
28
  - 是可以帮助他人?实现社会影响力?
29
  - 还是可以赚大钱?获得物质回报?
 
69
  if "是" in (mode or ""):
70
  return f"""
71
  你是一位专业的职业规划顾问,使用Backward Design方法帮助学生达成他们的职业目标。
 
72
  目标职业: {specific_career}
73
  背景信息: {bg_info}
 
74
  请提供:
75
  1. 该职业的简要分析
76
  2. 排名前列的组织或公司
77
  3. 所需技能和能力
78
  4. 职业发展路径
79
  5. 学术/课程建议、技能培养、资源使用、实习规划、简历优化等
 
80
  最后请用Markdown格式输出职业路径图:
81
  📍 现在 → 🎓 学习建议 → 💼 实践建议 → 📄 认证建议 → 🚀 求职建议
82
  """
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
+ # ============================ 主逻辑函数 ============================
100
  def predict(message, history):
101
  global current_q_index, questions
102
  global in_forward_flow, in_backward_flow
 
159
  key = forward_additional_questions[idx][0]
160
  user_profile[key] = message.strip()
161
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  if ("是" in mode and backward_done) or ("否" in mode and forward_done):
163
  try:
164
  api_key = os.environ.get("API_TOKEN")
 
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("""
 
246
  </div>
247
  </div>
248
  """)
 
249
  gr.Markdown("""
250
  **📝 个性化职业规划助手:Forward / Backward 多轮交互示例**
251
 
 
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
 
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