Update app.py
Browse files
app.py
CHANGED
|
@@ -50,7 +50,7 @@ roadmap_offered = False
|
|
| 50 |
roadmap_done = False
|
| 51 |
|
| 52 |
# ============================ 模型设置 ============================
|
| 53 |
-
model_default = "gpt-4o"
|
| 54 |
token_default = 2000
|
| 55 |
temp_default = 0.7
|
| 56 |
top_p_default = 0.95
|
|
@@ -70,14 +70,12 @@ def do_time_roadmap():
|
|
| 70 |
- 工作价值:{wv}
|
| 71 |
- 性格:{ps}
|
| 72 |
- 理想工作:{dd}
|
| 73 |
-
|
| 74 |
请你使用时间轴方式,给出一个职业路线图,如:
|
| 75 |
📍 Now: [当前阶段做什么]
|
| 76 |
🎓 Year X: [需要选的课/要做的项目]
|
| 77 |
💼 下个暑假: [申请什么实习、参加什么活动]
|
| 78 |
📄 毕业前: [考什么证书、准备材料]
|
| 79 |
🚀 毕业后: [目标岗位、如何申请]
|
| 80 |
-
|
| 81 |
请用Markdown分段写作,结合学生当前背景合理推断要点,写得具体些。
|
| 82 |
"""
|
| 83 |
try:
|
|
@@ -102,7 +100,7 @@ def do_time_roadmap():
|
|
| 102 |
except Exception as e:
|
| 103 |
return f"生成路线图时出错: {str(e)}"
|
| 104 |
|
| 105 |
-
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 深度分析函数
|
| 106 |
def do_deep_analysis():
|
| 107 |
direction = user_profile["forward_direction_choice"] or "尚未选择"
|
| 108 |
bg_info = user_profile["bg_info"] or "未知背景"
|
|
@@ -116,7 +114,6 @@ def do_deep_analysis():
|
|
| 116 |
价值观: {wv}
|
| 117 |
性格: {ps}
|
| 118 |
理想工作: {dd}
|
| 119 |
-
|
| 120 |
请你再深入分析,为学生推荐3个更具体的职业,并说明:
|
| 121 |
1. 这些职业对学生背景的契合度
|
| 122 |
2. 日常工作内容
|
|
@@ -124,7 +121,6 @@ def do_deep_analysis():
|
|
| 124 |
4. 本校资源如何利用
|
| 125 |
5. 实习 & 经验积累建议
|
| 126 |
6. 简历优化思路
|
| 127 |
-
|
| 128 |
用中文分段写作,字数500+。
|
| 129 |
"""
|
| 130 |
try:
|
|
@@ -244,7 +240,7 @@ def predict(message, history):
|
|
| 244 |
|
| 245 |
if not history:
|
| 246 |
current_q_index = 0
|
| 247 |
-
questions = base_questions[:]
|
| 248 |
for k in user_profile:
|
| 249 |
user_profile[k] = None
|
| 250 |
|
|
@@ -259,11 +255,11 @@ def predict(message, history):
|
|
| 259 |
roadmap_offered = False
|
| 260 |
roadmap_done = False
|
| 261 |
|
| 262 |
-
#
|
| 263 |
if 0 < current_q_index <= len(questions):
|
| 264 |
key = questions[current_q_index - 1][0]
|
|
|
|
| 265 |
if key == "mode" and current_q_index == 1:
|
| 266 |
-
# 只有回答第一个问题时 => mode
|
| 267 |
ans = message.strip()
|
| 268 |
user_profile["mode"] = ans
|
| 269 |
if "是" in ans:
|
|
@@ -280,12 +276,13 @@ def predict(message, history):
|
|
| 280 |
|
| 281 |
mode = user_profile.get("mode") or ""
|
| 282 |
|
| 283 |
-
#
|
| 284 |
if mode == "是":
|
| 285 |
if in_backward_flow and not backward_done:
|
| 286 |
if backward_index > 0 and backward_index <= len(backward_additional_questions):
|
| 287 |
prev_key = backward_additional_questions[backward_index - 1][0]
|
| 288 |
user_profile[prev_key] = message.strip()
|
|
|
|
| 289 |
if backward_index < len(backward_additional_questions):
|
| 290 |
k, prompt_text = backward_additional_questions[backward_index]
|
| 291 |
backward_index += 1
|
|
@@ -316,7 +313,7 @@ def predict(message, history):
|
|
| 316 |
except Exception as e:
|
| 317 |
return f"发生错误: {str(e)}"
|
| 318 |
|
| 319 |
-
#
|
| 320 |
else:
|
| 321 |
if in_forward_flow and not forward_done and not forward_deep_dive_done and not roadmap_done:
|
| 322 |
if forward_index > 0 and forward_index <= len(forward_additional_questions):
|
|
@@ -387,71 +384,61 @@ def predict(message, history):
|
|
| 387 |
|
| 388 |
return "信息收集完毕,如还未得到最终回复,请再输入任意文字继续。"
|
| 389 |
|
| 390 |
-
with gr.Blocks(css="""
|
| 391 |
-
body {
|
| 392 |
-
background-color: #1e1e1e;
|
| 393 |
-
color: #ffffff;
|
| 394 |
-
}
|
| 395 |
-
|
| 396 |
-
.gradio-container {
|
| 397 |
-
font-family: 'Segoe UI', sans-serif;
|
| 398 |
-
}
|
| 399 |
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
}
|
| 403 |
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
color: #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
border-radius: 10px !important;
|
| 408 |
padding: 10px;
|
| 409 |
margin: 6px;
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
color: #000000 !important; /* <-- Black text */
|
| 415 |
border-radius: 10px !important;
|
| 416 |
padding: 10px;
|
| 417 |
margin: 6px;
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
footer {
|
| 446 |
-
display: none !important;
|
| 447 |
-
}
|
| 448 |
-
|
| 449 |
-
""") as demo:
|
| 450 |
-
|
| 451 |
with gr.Row():
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
gr.HTML("""
|
| 456 |
<div style='display: flex; align-items: center; justify-content: center; gap: 20px; margin-bottom: 10px;'>
|
| 457 |
<!-- 左侧动图 -->
|
|
@@ -464,8 +451,8 @@ def predict(message, history):
|
|
| 464 |
</div>
|
| 465 |
</div>
|
| 466 |
""")
|
| 467 |
-
gr.Markdown("
|
| 468 |
-
|
| 469 |
chatbot = gr.Chatbot(height=500, show_label=False, show_copy_button=True, type="messages")
|
| 470 |
msg = gr.Textbox(placeholder="请输入你的回答...")
|
| 471 |
send = gr.Button("发送 🚀", elem_id="custom-send")
|
|
@@ -477,9 +464,9 @@ def predict(message, history):
|
|
| 477 |
return "", hist
|
| 478 |
|
| 479 |
def add_bot_response(hist):
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
hist.append({"role":"assistant","content":
|
| 483 |
return hist
|
| 484 |
|
| 485 |
send.click(add_user_message, [msg, chatbot], [msg, chatbot]) \
|
|
@@ -499,27 +486,27 @@ def predict(message, history):
|
|
| 499 |
global roadmap_offered, roadmap_done
|
| 500 |
|
| 501 |
current_q_index = 0
|
| 502 |
-
questions = base_questions
|
| 503 |
-
for
|
| 504 |
-
user_profile[
|
| 505 |
|
| 506 |
in_forward_flow = False
|
| 507 |
in_backward_flow = False
|
| 508 |
forward_index = 0
|
| 509 |
backward_index = 0
|
| 510 |
-
forward_done =
|
|
|
|
| 511 |
forward_recommendation_given = False
|
| 512 |
recommendation_round = 0
|
| 513 |
forward_deep_dive_done = False
|
| 514 |
roadmap_offered = False
|
| 515 |
roadmap_done = False
|
| 516 |
-
|
| 517 |
return []
|
| 518 |
|
| 519 |
reset.click(reset_state, outputs=chatbot)
|
| 520 |
|
| 521 |
-
def
|
| 522 |
return [{"role":"assistant","content": questions[0][1]}]
|
| 523 |
|
| 524 |
-
demo.load(
|
| 525 |
demo.launch()
|
|
|
|
| 50 |
roadmap_done = False
|
| 51 |
|
| 52 |
# ============================ 模型设置 ============================
|
| 53 |
+
model_default = "gpt-4o"
|
| 54 |
token_default = 2000
|
| 55 |
temp_default = 0.7
|
| 56 |
top_p_default = 0.95
|
|
|
|
| 70 |
- 工作价值:{wv}
|
| 71 |
- 性格:{ps}
|
| 72 |
- 理想工作:{dd}
|
|
|
|
| 73 |
请你使用时间轴方式,给出一个职业路线图,如:
|
| 74 |
📍 Now: [当前阶段做什么]
|
| 75 |
🎓 Year X: [需要选的课/要做的项目]
|
| 76 |
💼 下个暑假: [申请什么实习、参加什么活动]
|
| 77 |
📄 毕业前: [考什么证书、准备材料]
|
| 78 |
🚀 毕业后: [目标岗位、如何申请]
|
|
|
|
| 79 |
请用Markdown分段写作,结合学生当前背景合理推断要点,写得具体些。
|
| 80 |
"""
|
| 81 |
try:
|
|
|
|
| 100 |
except Exception as e:
|
| 101 |
return f"生成路线图时出错: {str(e)}"
|
| 102 |
|
| 103 |
+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 深度分析函数 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 104 |
def do_deep_analysis():
|
| 105 |
direction = user_profile["forward_direction_choice"] or "尚未选择"
|
| 106 |
bg_info = user_profile["bg_info"] or "未知背景"
|
|
|
|
| 114 |
价值观: {wv}
|
| 115 |
性格: {ps}
|
| 116 |
理想工作: {dd}
|
|
|
|
| 117 |
请你再深入分析,为学生推荐3个更具体的职业,并说明:
|
| 118 |
1. 这些职业对学生背景的契合度
|
| 119 |
2. 日常工作内容
|
|
|
|
| 121 |
4. 本校资源如何利用
|
| 122 |
5. 实习 & 经验积累建议
|
| 123 |
6. 简历优化思路
|
|
|
|
| 124 |
用中文分段写作,字数500+。
|
| 125 |
"""
|
| 126 |
try:
|
|
|
|
| 240 |
|
| 241 |
if not history:
|
| 242 |
current_q_index = 0
|
| 243 |
+
questions[:] = base_questions # 用 [:] 保持原列表
|
| 244 |
for k in user_profile:
|
| 245 |
user_profile[k] = None
|
| 246 |
|
|
|
|
| 255 |
roadmap_offered = False
|
| 256 |
roadmap_done = False
|
| 257 |
|
| 258 |
+
# 如果还在问 base_questions
|
| 259 |
if 0 < current_q_index <= len(questions):
|
| 260 |
key = questions[current_q_index - 1][0]
|
| 261 |
+
# 只有回答第一个问题的时候 -> 决定是/否
|
| 262 |
if key == "mode" and current_q_index == 1:
|
|
|
|
| 263 |
ans = message.strip()
|
| 264 |
user_profile["mode"] = ans
|
| 265 |
if "是" in ans:
|
|
|
|
| 276 |
|
| 277 |
mode = user_profile.get("mode") or ""
|
| 278 |
|
| 279 |
+
# Backward
|
| 280 |
if mode == "是":
|
| 281 |
if in_backward_flow and not backward_done:
|
| 282 |
if backward_index > 0 and backward_index <= len(backward_additional_questions):
|
| 283 |
prev_key = backward_additional_questions[backward_index - 1][0]
|
| 284 |
user_profile[prev_key] = message.strip()
|
| 285 |
+
|
| 286 |
if backward_index < len(backward_additional_questions):
|
| 287 |
k, prompt_text = backward_additional_questions[backward_index]
|
| 288 |
backward_index += 1
|
|
|
|
| 313 |
except Exception as e:
|
| 314 |
return f"发生错误: {str(e)}"
|
| 315 |
|
| 316 |
+
# Forward
|
| 317 |
else:
|
| 318 |
if in_forward_flow and not forward_done and not forward_deep_dive_done and not roadmap_done:
|
| 319 |
if forward_index > 0 and forward_index <= len(forward_additional_questions):
|
|
|
|
| 384 |
|
| 385 |
return "信息收集完毕,如还未得到最终回复,请再输入任意文字继续。"
|
| 386 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
|
| 388 |
+
### ------ Gradio UI ------
|
| 389 |
+
import gradio as gr
|
|
|
|
| 390 |
|
| 391 |
+
with gr.Blocks(css="""
|
| 392 |
+
body {
|
| 393 |
+
background-color: #1e1e1e;
|
| 394 |
+
color: #ffffff;
|
| 395 |
+
}
|
| 396 |
+
.gradio-container {
|
| 397 |
+
font-family: 'Segoe UI', sans-serif;
|
| 398 |
+
}
|
| 399 |
+
.gr-chatbot {
|
| 400 |
+
background-color: transparent !important;
|
| 401 |
+
}
|
| 402 |
+
.message.user {
|
| 403 |
+
background-color: #cce6ff !important;
|
| 404 |
+
color: #000000 !important;
|
| 405 |
border-radius: 10px !important;
|
| 406 |
padding: 10px;
|
| 407 |
margin: 6px;
|
| 408 |
+
}
|
| 409 |
+
.message.bot {
|
| 410 |
+
background-color: #99ccff !important;
|
| 411 |
+
color: #000000 !important;
|
|
|
|
| 412 |
border-radius: 10px !important;
|
| 413 |
padding: 10px;
|
| 414 |
margin: 6px;
|
| 415 |
+
}
|
| 416 |
+
.gr-button {
|
| 417 |
+
border-radius: 8px;
|
| 418 |
+
}
|
| 419 |
+
#custom-send {
|
| 420 |
+
background-color: #ec4899 !important;
|
| 421 |
+
color: white !important;
|
| 422 |
+
border-radius: 999px !important;
|
| 423 |
+
padding: 10px 24px !important;
|
| 424 |
+
font-weight: bold;
|
| 425 |
+
box-shadow: 0 0 10px #ec4899;
|
| 426 |
+
transition: all 0.3s ease-in-out;
|
| 427 |
+
}
|
| 428 |
+
#custom-send:hover {
|
| 429 |
+
background-color: #d63384 !important;
|
| 430 |
+
box-shadow: 0 0 12px #ec4899;
|
| 431 |
+
}
|
| 432 |
+
textarea, input {
|
| 433 |
+
background-color: #ffe4f1 !important;
|
| 434 |
+
color: #5e2c49 !important;
|
| 435 |
+
border: 1px solid #ec4899 !important;
|
| 436 |
+
}
|
| 437 |
+
footer {
|
| 438 |
+
display: none !important;
|
| 439 |
+
}
|
| 440 |
+
""") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
| 442 |
gr.HTML("""
|
| 443 |
<div style='display: flex; align-items: center; justify-content: center; gap: 20px; margin-bottom: 10px;'>
|
| 444 |
<!-- 左侧动图 -->
|
|
|
|
| 451 |
</div>
|
| 452 |
</div>
|
| 453 |
""")
|
| 454 |
+
gr.Markdown("## 🎓 职业规划助手\n如果你有明确职业目标 => 回答“是”;否则 => 回答“否”")
|
| 455 |
+
|
| 456 |
chatbot = gr.Chatbot(height=500, show_label=False, show_copy_button=True, type="messages")
|
| 457 |
msg = gr.Textbox(placeholder="请输入你的回答...")
|
| 458 |
send = gr.Button("发送 🚀", elem_id="custom-send")
|
|
|
|
| 464 |
return "", hist
|
| 465 |
|
| 466 |
def add_bot_response(hist):
|
| 467 |
+
user_msg = hist[-1]["content"]
|
| 468 |
+
bot_msg = predict(user_msg, hist[:-1])
|
| 469 |
+
hist.append({"role":"assistant","content":bot_msg})
|
| 470 |
return hist
|
| 471 |
|
| 472 |
send.click(add_user_message, [msg, chatbot], [msg, chatbot]) \
|
|
|
|
| 486 |
global roadmap_offered, roadmap_done
|
| 487 |
|
| 488 |
current_q_index = 0
|
| 489 |
+
questions[:] = base_questions
|
| 490 |
+
for k in user_profile:
|
| 491 |
+
user_profile[k] = None
|
| 492 |
|
| 493 |
in_forward_flow = False
|
| 494 |
in_backward_flow = False
|
| 495 |
forward_index = 0
|
| 496 |
backward_index = 0
|
| 497 |
+
forward_done = False
|
| 498 |
+
backward_done = False
|
| 499 |
forward_recommendation_given = False
|
| 500 |
recommendation_round = 0
|
| 501 |
forward_deep_dive_done = False
|
| 502 |
roadmap_offered = False
|
| 503 |
roadmap_done = False
|
|
|
|
| 504 |
return []
|
| 505 |
|
| 506 |
reset.click(reset_state, outputs=chatbot)
|
| 507 |
|
| 508 |
+
def auto_first():
|
| 509 |
return [{"role":"assistant","content": questions[0][1]}]
|
| 510 |
|
| 511 |
+
demo.load(auto_first, outputs=chatbot)
|
| 512 |
demo.launch()
|