Update app.py
Browse files
app.py
CHANGED
|
@@ -107,6 +107,7 @@ def predict(message, history):
|
|
| 107 |
global forward_done, backward_done
|
| 108 |
global forward_recommendation_given
|
| 109 |
|
|
|
|
| 110 |
if not history:
|
| 111 |
current_q_index = 0
|
| 112 |
questions = base_questions[:]
|
|
@@ -121,47 +122,65 @@ def predict(message, history):
|
|
| 121 |
backward_done = False
|
| 122 |
forward_recommendation_given = False
|
| 123 |
|
| 124 |
-
#
|
|
|
|
| 125 |
if 0 < current_q_index <= len(questions):
|
| 126 |
key = questions[current_q_index - 1][0]
|
| 127 |
user_profile[key] = message.strip()
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
| 134 |
if current_q_index < len(questions):
|
| 135 |
nxt = questions[current_q_index][1]
|
| 136 |
current_q_index += 1
|
| 137 |
return nxt
|
| 138 |
|
|
|
|
| 139 |
mode = user_profile.get("mode") or ""
|
| 140 |
|
| 141 |
-
#
|
| 142 |
if "是" in mode:
|
| 143 |
if in_backward_flow and not backward_done:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
if backward_index < len(backward_additional_questions):
|
| 145 |
k, prompt_text = backward_additional_questions[backward_index]
|
| 146 |
backward_index += 1
|
| 147 |
return prompt_text
|
| 148 |
else:
|
|
|
|
| 149 |
backward_done = True
|
| 150 |
|
| 151 |
-
#
|
| 152 |
else:
|
| 153 |
if in_forward_flow and not forward_done:
|
| 154 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
if forward_index < len(forward_additional_questions):
|
| 156 |
k, prompt_text = forward_additional_questions[forward_index]
|
| 157 |
forward_index += 1
|
| 158 |
return prompt_text
|
| 159 |
|
| 160 |
-
# 如果
|
| 161 |
elif not forward_recommendation_given:
|
| 162 |
forward_recommendation_given = True
|
| 163 |
|
| 164 |
-
# ================== 调试打印 user_profile ==================
|
| 165 |
print("===== Debug user_profile =====")
|
| 166 |
print(user_profile)
|
| 167 |
|
|
@@ -173,7 +192,7 @@ def predict(message, history):
|
|
| 173 |
|
| 174 |
recommendation_prompt = f"""
|
| 175 |
请根据以下信息,为这位学生撰写一份结构化、条理清晰的“人物画像分析”:
|
| 176 |
-
- 学术背景: {user_profile['bg_info']}
|
| 177 |
- 工作意义: {user_profile['work_value']}
|
| 178 |
- 性格总结: {user_profile['personality_summary']}
|
| 179 |
- 理想工作状态: {user_profile['dream_day']}
|
|
@@ -199,7 +218,6 @@ def predict(message, history):
|
|
| 199 |
如果信息不足,请基于通用逻辑合理推断。请使用中文分段写作,字数不少于500字。
|
| 200 |
"""
|
| 201 |
|
| 202 |
-
# ================== 调试打印 recommendation_prompt ==================
|
| 203 |
print("===== Debug recommendation_prompt =====")
|
| 204 |
print(recommendation_prompt)
|
| 205 |
|
|
@@ -221,11 +239,11 @@ def predict(message, history):
|
|
| 221 |
return f"生成推荐方向时出错: {str(e)}"
|
| 222 |
|
| 223 |
else:
|
| 224 |
-
#
|
| 225 |
user_profile["forward_direction_choice"] = message.strip()
|
| 226 |
forward_done = True
|
| 227 |
|
| 228 |
-
#
|
| 229 |
if ("是" in mode and backward_done) or ("否" in mode and forward_done):
|
| 230 |
try:
|
| 231 |
api_key = os.environ.get("API_TOKEN")
|
|
@@ -312,24 +330,32 @@ footer {
|
|
| 312 |
<div style='display: flex; align-items: center; justify-content: center; gap: 20px; margin-bottom: 10px;'>
|
| 313 |
<img src='https://media3.giphy.com/media/v1.Y2lkPTc5MGI3NjExMmFucGwxbmNsd3J5NXV0Y282NXNtMzNsZW5jMm4wNWh6c2dqbXIwdiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/l41m18LjqpzxUr2WA/giphy.gif' width='200' style='border-radius: 12px; box-shadow: 0 0 10px #ec4899;'>
|
| 314 |
<div style='text-align: left;'>
|
| 315 |
-
<h1 style='color:white; font-size: 36px; margin-bottom: 6px;'>🎓 AI职业规划助手(
|
| 316 |
<p style='font-size: 18px; font-weight:bold; color:#ec4899; margin-top: 0;margin-left: 150px'>让梦想照进现实 💖</p>
|
| 317 |
</div>
|
| 318 |
</div>
|
| 319 |
""")
|
| 320 |
|
| 321 |
gr.Markdown("""
|
| 322 |
-
**📝 Forward / Backward
|
| 323 |
-
-
|
| 324 |
-
-
|
| 325 |
-
- Forward模式会在你回答完四大问题后,自动给出一个简短“学生画像”+3个大方向供你选择
|
| 326 |
""")
|
| 327 |
|
| 328 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
|
| 330 |
with gr.Row():
|
| 331 |
with gr.Column(scale=8):
|
| 332 |
-
msg = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
with gr.Column(scale=1):
|
| 334 |
submit_btn = gr.Button("🚀 发送", elem_id="custom-send")
|
| 335 |
|
|
@@ -348,11 +374,25 @@ footer {
|
|
| 348 |
history.append({"role": "assistant", "content": bot_message})
|
| 349 |
return history
|
| 350 |
|
| 351 |
-
submit_btn.click(
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
|
| 357 |
def reset_conversation():
|
| 358 |
global current_q_index, questions
|
|
@@ -375,7 +415,12 @@ footer {
|
|
| 375 |
forward_recommendation_given = False
|
| 376 |
return []
|
| 377 |
|
| 378 |
-
reset_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
|
| 380 |
def auto_first_question():
|
| 381 |
return [{"role": "assistant", "content": questions[0][1]}]
|
|
|
|
| 107 |
global forward_done, backward_done
|
| 108 |
global forward_recommendation_given
|
| 109 |
|
| 110 |
+
# 第一次对话,重置
|
| 111 |
if not history:
|
| 112 |
current_q_index = 0
|
| 113 |
questions = base_questions[:]
|
|
|
|
| 122 |
backward_done = False
|
| 123 |
forward_recommendation_given = False
|
| 124 |
|
| 125 |
+
# ========== 存储 base_questions 回答 ==========
|
| 126 |
+
# 如果还在问 base_questions(只有1题,就1个循环)
|
| 127 |
if 0 < current_q_index <= len(questions):
|
| 128 |
key = questions[current_q_index - 1][0]
|
| 129 |
user_profile[key] = message.strip()
|
| 130 |
+
# 如果回答了 mode 问题
|
| 131 |
+
if key == "mode":
|
| 132 |
+
if "是" in user_profile["mode"]:
|
| 133 |
+
questions.insert(1, ("specific_career", "请具体描述你想要从事的职业方向。"))
|
| 134 |
+
in_backward_flow = True
|
| 135 |
+
elif "否" in user_profile["mode"]:
|
| 136 |
+
in_forward_flow = True
|
| 137 |
+
|
| 138 |
+
# 如果 base_questions 还没问完,就继续问
|
| 139 |
if current_q_index < len(questions):
|
| 140 |
nxt = questions[current_q_index][1]
|
| 141 |
current_q_index += 1
|
| 142 |
return nxt
|
| 143 |
|
| 144 |
+
# ========== 进入 Forward / Backward 的额外问题 ==========
|
| 145 |
mode = user_profile.get("mode") or ""
|
| 146 |
|
| 147 |
+
# --- Backward 模式 ---
|
| 148 |
if "是" in mode:
|
| 149 |
if in_backward_flow and not backward_done:
|
| 150 |
+
# 存储上一次回答到 user_profile
|
| 151 |
+
# backward_index > 0 才说明至少问过1个 backward问题
|
| 152 |
+
if backward_index > 0 and backward_index <= len(backward_additional_questions):
|
| 153 |
+
prev_key = backward_additional_questions[backward_index - 1][0]
|
| 154 |
+
user_profile[prev_key] = message.strip()
|
| 155 |
+
|
| 156 |
+
# 是否还有下一个问题
|
| 157 |
if backward_index < len(backward_additional_questions):
|
| 158 |
k, prompt_text = backward_additional_questions[backward_index]
|
| 159 |
backward_index += 1
|
| 160 |
return prompt_text
|
| 161 |
else:
|
| 162 |
+
# 问完了
|
| 163 |
backward_done = True
|
| 164 |
|
| 165 |
+
# --- Forward 模式 ---
|
| 166 |
else:
|
| 167 |
if in_forward_flow and not forward_done:
|
| 168 |
+
# 存储上一次回答到 user_profile
|
| 169 |
+
# forward_index > 0 才说明至少问过1个 forward问题
|
| 170 |
+
if forward_index > 0 and forward_index <= len(forward_additional_questions):
|
| 171 |
+
prev_key = forward_additional_questions[forward_index - 1][0]
|
| 172 |
+
user_profile[prev_key] = message.strip()
|
| 173 |
+
|
| 174 |
+
# 如果还有 Forward问题没问完
|
| 175 |
if forward_index < len(forward_additional_questions):
|
| 176 |
k, prompt_text = forward_additional_questions[forward_index]
|
| 177 |
forward_index += 1
|
| 178 |
return prompt_text
|
| 179 |
|
| 180 |
+
# 如果 4 个 Forward问题都问完,但还没推荐过方向
|
| 181 |
elif not forward_recommendation_given:
|
| 182 |
forward_recommendation_given = True
|
| 183 |
|
|
|
|
| 184 |
print("===== Debug user_profile =====")
|
| 185 |
print(user_profile)
|
| 186 |
|
|
|
|
| 192 |
|
| 193 |
recommendation_prompt = f"""
|
| 194 |
请根据以下信息,为这位学生撰写一份结构化、条理清晰的“人物画像分析”:
|
| 195 |
+
- 学术背景: {user_profile['bg_info']}
|
| 196 |
- 工作意义: {user_profile['work_value']}
|
| 197 |
- 性格总结: {user_profile['personality_summary']}
|
| 198 |
- 理想工作状态: {user_profile['dream_day']}
|
|
|
|
| 218 |
如果信息不足,请基于通用逻辑合理推断。请使用中文分段写作,字数不少于500字。
|
| 219 |
"""
|
| 220 |
|
|
|
|
| 221 |
print("===== Debug recommendation_prompt =====")
|
| 222 |
print(recommendation_prompt)
|
| 223 |
|
|
|
|
| 239 |
return f"生成推荐方向时出错: {str(e)}"
|
| 240 |
|
| 241 |
else:
|
| 242 |
+
# 已推荐过方向,现在用户输入了方向选择
|
| 243 |
user_profile["forward_direction_choice"] = message.strip()
|
| 244 |
forward_done = True
|
| 245 |
|
| 246 |
+
# 如果走到这里,说明 Backward_done / Forward_done
|
| 247 |
if ("是" in mode and backward_done) or ("否" in mode and forward_done):
|
| 248 |
try:
|
| 249 |
api_key = os.environ.get("API_TOKEN")
|
|
|
|
| 330 |
<div style='display: flex; align-items: center; justify-content: center; gap: 20px; margin-bottom: 10px;'>
|
| 331 |
<img src='https://media3.giphy.com/media/v1.Y2lkPTc5MGI3NjExMmFucGwxbmNsd3J5NXV0Y282NXNtMzNsZW5jMm4wNWh6c2dqbXIwdiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/l41m18LjqpzxUr2WA/giphy.gif' width='200' style='border-radius: 12px; box-shadow: 0 0 10px #ec4899;'>
|
| 332 |
<div style='text-align: left;'>
|
| 333 |
+
<h1 style='color:white; font-size: 36px; margin-bottom: 6px;'>🎓 AI职业规划助手(修正版)</h1>
|
| 334 |
<p style='font-size: 18px; font-weight:bold; color:#ec4899; margin-top: 0;margin-left: 150px'>让梦想照进现实 💖</p>
|
| 335 |
</div>
|
| 336 |
</div>
|
| 337 |
""")
|
| 338 |
|
| 339 |
gr.Markdown("""
|
| 340 |
+
**📝 修正版:确保Forward / Backward问题回答也存入 user_profile**
|
| 341 |
+
- 逐题逐答,不要一次性粘贴所有回答
|
| 342 |
+
- 这样每个回答都会写进 user_profile,再进行下一题
|
|
|
|
| 343 |
""")
|
| 344 |
|
| 345 |
+
chatbot = gr.Chatbot(
|
| 346 |
+
height=500,
|
| 347 |
+
show_label=False,
|
| 348 |
+
show_copy_button=True,
|
| 349 |
+
type="messages"
|
| 350 |
+
)
|
| 351 |
|
| 352 |
with gr.Row():
|
| 353 |
with gr.Column(scale=8):
|
| 354 |
+
msg = gr.Textbox(
|
| 355 |
+
placeholder="请在这里输入你的回答...",
|
| 356 |
+
show_label=False,
|
| 357 |
+
container=False
|
| 358 |
+
)
|
| 359 |
with gr.Column(scale=1):
|
| 360 |
submit_btn = gr.Button("🚀 发送", elem_id="custom-send")
|
| 361 |
|
|
|
|
| 374 |
history.append({"role": "assistant", "content": bot_message})
|
| 375 |
return history
|
| 376 |
|
| 377 |
+
submit_btn.click(
|
| 378 |
+
fn=add_message,
|
| 379 |
+
inputs=[msg, chatbot],
|
| 380 |
+
outputs=[msg, chatbot]
|
| 381 |
+
).then(
|
| 382 |
+
fn=bot_response,
|
| 383 |
+
inputs=[chatbot],
|
| 384 |
+
outputs=[chatbot]
|
| 385 |
+
)
|
| 386 |
+
|
| 387 |
+
msg.submit(
|
| 388 |
+
fn=add_message,
|
| 389 |
+
inputs=[msg, chatbot],
|
| 390 |
+
outputs=[msg, chatbot]
|
| 391 |
+
).then(
|
| 392 |
+
fn=bot_response,
|
| 393 |
+
inputs=[chatbot],
|
| 394 |
+
outputs=[chatbot]
|
| 395 |
+
)
|
| 396 |
|
| 397 |
def reset_conversation():
|
| 398 |
global current_q_index, questions
|
|
|
|
| 415 |
forward_recommendation_given = False
|
| 416 |
return []
|
| 417 |
|
| 418 |
+
reset_btn.click(
|
| 419 |
+
fn=reset_conversation,
|
| 420 |
+
inputs=None,
|
| 421 |
+
outputs=chatbot,
|
| 422 |
+
queue=False
|
| 423 |
+
)
|
| 424 |
|
| 425 |
def auto_first_question():
|
| 426 |
return [{"role": "assistant", "content": questions[0][1]}]
|