Update app.py
Browse files
app.py
CHANGED
|
@@ -2,9 +2,9 @@ import gradio as gr
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# ============================
|
| 6 |
user_profile = {
|
| 7 |
-
"mode": None, #
|
| 8 |
"specific_career": None,
|
| 9 |
"bg_info": None,
|
| 10 |
"work_value": None,
|
|
@@ -12,33 +12,33 @@ user_profile = {
|
|
| 12 |
"dream_day": None,
|
| 13 |
"forward_direction_choice": None,
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
"recommended_directions": [], # 3
|
| 17 |
-
"selected_direction": None, #
|
| 18 |
-
"recommended_jobs": [], # 3
|
| 19 |
-
"final_choice": None #
|
| 20 |
}
|
| 21 |
|
| 22 |
-
# ============================
|
| 23 |
base_questions = [
|
| 24 |
-
("mode", "
|
| 25 |
]
|
| 26 |
|
| 27 |
-
# ============================ Forward
|
| 28 |
forward_additional_questions = [
|
| 29 |
-
("bg_info", """
|
| 30 |
-
("work_value", """
|
| 31 |
-
("personality_summary", "
|
| 32 |
-
("dream_day", "
|
| 33 |
]
|
| 34 |
|
| 35 |
-
# ============================ Backward
|
| 36 |
backward_additional_questions = [
|
| 37 |
-
("specific_career", "
|
| 38 |
-
("bg_info", "
|
| 39 |
]
|
| 40 |
|
| 41 |
-
# ============================
|
| 42 |
current_q_index = 0
|
| 43 |
questions = base_questions[:]
|
| 44 |
in_forward_flow = False
|
|
@@ -53,15 +53,15 @@ forward_deep_dive_done = False
|
|
| 53 |
roadmap_offered = False
|
| 54 |
roadmap_done = False
|
| 55 |
|
| 56 |
-
direction_chosen = False #
|
| 57 |
-
jobs_recommended = False #
|
| 58 |
-
job_chosen = False #
|
| 59 |
|
| 60 |
-
# ==========
|
| 61 |
-
post_career_detail_asked = False #
|
| 62 |
-
post_career_detail_done = False #
|
| 63 |
|
| 64 |
-
# ============================
|
| 65 |
model_default = "gpt-4o"
|
| 66 |
token_default = 2000
|
| 67 |
temp_default = 0.7
|
|
@@ -69,30 +69,28 @@ top_p_default = 0.95
|
|
| 69 |
|
| 70 |
|
| 71 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 72 |
-
#
|
| 73 |
def backward_strategy_plan(specific_career, bg_info):
|
| 74 |
prompt = f"""
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
Use Markdown formatting in sections, with a minimum of 300 words. The content must align closely with the student's current background and goals.
|
| 88 |
"""
|
| 89 |
try:
|
| 90 |
api_key = os.environ.get("API_TOKEN")
|
| 91 |
if not api_key:
|
| 92 |
-
return "
|
| 93 |
client = OpenAI(api_key=api_key)
|
| 94 |
msgs = [
|
| 95 |
-
{"role": "system", "content": "
|
| 96 |
{"role": "user", "content": prompt}
|
| 97 |
]
|
| 98 |
resp = client.chat.completions.create(
|
|
@@ -105,11 +103,9 @@ Use Markdown formatting in sections, with a minimum of 300 words. The content mu
|
|
| 105 |
)
|
| 106 |
return resp.choices[0].message.content
|
| 107 |
except Exception as e:
|
| 108 |
-
return f"
|
| 109 |
-
|
| 110 |
|
| 111 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 112 |
-
# Dynamic function: Generate detailed analysis based on selected A/B/C modules
|
| 113 |
def answer_abc_questions(selected, bg_info, wv, ps, dd):
|
| 114 |
desired_parts = []
|
| 115 |
if "a" in selected.lower():
|
|
@@ -120,46 +116,29 @@ def answer_abc_questions(selected, bg_info, wv, ps, dd):
|
|
| 120 |
desired_parts.append("C")
|
| 121 |
|
| 122 |
if not desired_parts:
|
| 123 |
-
return "
|
| 124 |
|
| 125 |
prompt_text = f"""
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
-
|
| 130 |
-
-
|
| 131 |
-
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
- Analyze the student’s current academic background and what capabilities they already have.
|
| 139 |
-
- Based on their intended career, recommend what types of courses they should focus on.
|
| 140 |
-
- Suggest course names and keywords to guide their course planning.
|
| 141 |
-
|
| 142 |
-
(B: School Resource Utilization)
|
| 143 |
-
- Provide 3 types of suggestions:
|
| 144 |
-
1. How to utilize the university's career services.
|
| 145 |
-
2. How to get involved with career-related clubs, organizations, and platforms.
|
| 146 |
-
3. How to network with alumni in their field and improve networking skills.
|
| 147 |
-
|
| 148 |
-
(C: Internships & Experience Building)
|
| 149 |
-
- Suggest platforms to find internships.
|
| 150 |
-
- Recommend internship directions aligned with the student’s career goal.
|
| 151 |
-
- Provide tips for preparing materials and applying.
|
| 152 |
-
- Strategies for gaining experience through networking, internships, or campus jobs.
|
| 153 |
-
|
| 154 |
-
Only include the modules selected by the student. Do not generate content for unselected modules.
|
| 155 |
"""
|
| 156 |
try:
|
| 157 |
api_key = os.environ.get("API_TOKEN")
|
| 158 |
if not api_key:
|
| 159 |
-
return "
|
| 160 |
client = OpenAI(api_key=api_key)
|
| 161 |
msgs = [
|
| 162 |
-
{"role": "system", "content": "
|
| 163 |
{"role": "user", "content": prompt_text}
|
| 164 |
]
|
| 165 |
resp = client.chat.completions.create(
|
|
@@ -172,39 +151,35 @@ Only include the modules selected by the student. Do not generate content for un
|
|
| 172 |
)
|
| 173 |
return resp.choices[0].message.content
|
| 174 |
except Exception as e:
|
| 175 |
-
return f"
|
| 176 |
|
| 177 |
|
| 178 |
-
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 179 |
def do_selected_career_detail(selected_career, bg_info, wv, ps, dd):
|
| 180 |
prompt = f"""
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
5. Typical work rhythm (fast-paced or routine?)
|
| 196 |
-
6. Industry outlook
|
| 197 |
-
|
| 198 |
-
Please use Markdown formatting in separate sections, with a minimum of 300 words.
|
| 199 |
"""
|
| 200 |
try:
|
| 201 |
api_key = os.environ.get("API_TOKEN")
|
| 202 |
if not api_key:
|
| 203 |
-
return "
|
| 204 |
client = OpenAI(api_key=api_key)
|
| 205 |
|
| 206 |
msgs = [
|
| 207 |
-
{"role": "system", "content": "
|
| 208 |
{"role": "user", "content": prompt}
|
| 209 |
]
|
| 210 |
resp = client.chat.completions.create(
|
|
@@ -217,46 +192,42 @@ Please use Markdown formatting in separate sections, with a minimum of 300 words
|
|
| 217 |
)
|
| 218 |
return resp.choices[0].message.content
|
| 219 |
except Exception as e:
|
| 220 |
-
return f"
|
| 221 |
|
| 222 |
|
| 223 |
-
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 224 |
def do_time_roadmap():
|
| 225 |
direction = user_profile.get("final_choice") or user_profile.get("selected_direction") or \
|
| 226 |
user_profile.get("forward_direction_choice") or user_profile.get("specific_career")
|
| 227 |
-
bg_info = user_profile["bg_info"] or "
|
| 228 |
-
wv = user_profile["work_value"] or "
|
| 229 |
-
ps = user_profile["personality_summary"] or "
|
| 230 |
-
dd = user_profile["dream_day"] or "
|
| 231 |
|
| 232 |
prompt_roadmap = f"""
|
| 233 |
-
|
| 234 |
-
-
|
| 235 |
-
-
|
| 236 |
-
-
|
| 237 |
-
-
|
| 238 |
-
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
📄 6–12 Months Before Graduation: [Certifications to complete, job/grad school application materials to prepare]
|
| 247 |
-
🚀 Post-Graduation: [Target roles, how to apply, next steps]
|
| 248 |
-
|
| 249 |
-
Please write the roadmap using Markdown formatting and tailor your suggestions specifically to the student's profile.
|
| 250 |
"""
|
| 251 |
try:
|
| 252 |
api_key = os.environ.get("API_TOKEN")
|
| 253 |
if not api_key:
|
| 254 |
-
return "
|
| 255 |
client = OpenAI(api_key=api_key)
|
| 256 |
|
| 257 |
msgs = [
|
| 258 |
-
{"role":
|
| 259 |
-
{"role":
|
| 260 |
]
|
| 261 |
resp = client.chat.completions.create(
|
| 262 |
model=model_default,
|
|
@@ -268,41 +239,38 @@ Please write the roadmap using Markdown formatting and tailor your suggestions s
|
|
| 268 |
)
|
| 269 |
return resp.choices[0].message.content
|
| 270 |
except Exception as e:
|
| 271 |
-
return f"
|
| 272 |
|
| 273 |
|
| 274 |
-
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 275 |
def recommend_3jobs_for_direction(direction, bg_info, wv, ps, dd):
|
| 276 |
prompt = f"""
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
2. [Job Title 2]
|
| 295 |
-
3. [Job Title 3]
|
| 296 |
-
If none of these suit you, enter 'change'.
|
| 297 |
"""
|
| 298 |
try:
|
| 299 |
api_key = os.environ.get("API_TOKEN")
|
| 300 |
if not api_key:
|
| 301 |
-
return "
|
| 302 |
client = OpenAI(api_key=api_key)
|
| 303 |
|
| 304 |
msgs = [
|
| 305 |
-
{"role": "system", "content": "
|
| 306 |
{"role": "user", "content": prompt}
|
| 307 |
]
|
| 308 |
resp = client.chat.completions.create(
|
|
@@ -315,69 +283,62 @@ If none of these suit you, enter 'change'.
|
|
| 315 |
)
|
| 316 |
output_text = resp.choices[0].message.content
|
| 317 |
|
| 318 |
-
#
|
| 319 |
import re
|
| 320 |
job_pattern = r'(\d+)\.\s+\[?(.*?)\]?(?:\n|$)'
|
| 321 |
job_matches = re.findall(job_pattern, output_text)
|
| 322 |
job_list = [match[1].strip() for match in job_matches]
|
| 323 |
-
|
| 324 |
-
#
|
| 325 |
if len(job_list) < 3:
|
| 326 |
job_list = [
|
| 327 |
-
f"{direction} -
|
| 328 |
-
f"{direction} -
|
| 329 |
-
f"{direction} -
|
| 330 |
]
|
| 331 |
-
|
| 332 |
-
user_profile["recommended_jobs"] = job_list[:3] #
|
| 333 |
-
|
| 334 |
-
#
|
| 335 |
global jobs_recommended
|
| 336 |
jobs_recommended = True
|
| 337 |
-
|
| 338 |
return output_text
|
| 339 |
except Exception as e:
|
| 340 |
-
return f"
|
| 341 |
|
| 342 |
|
| 343 |
-
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 344 |
def recommend_3directions():
|
| 345 |
-
bg_info = user_profile["bg_info"] or "
|
| 346 |
-
wv = user_profile["work_value"] or "
|
| 347 |
-
ps = user_profile["personality_summary"] or "
|
| 348 |
-
dd = user_profile["dream_day"] or "
|
| 349 |
|
| 350 |
rec_prompt = f"""
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
-
|
| 355 |
-
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
End with only the following format (do not repeat it earlier):
|
| 366 |
-
|
| 367 |
-
Please choose your career direction:
|
| 368 |
-
1. [Direction Name 1]
|
| 369 |
-
2. [Direction Name 2]
|
| 370 |
-
3. [Direction Name 3]
|
| 371 |
-
If none are suitable, type 'change'.
|
| 372 |
"""
|
| 373 |
try:
|
| 374 |
api_key = os.environ.get("API_TOKEN")
|
| 375 |
if not api_key:
|
| 376 |
-
return "
|
| 377 |
client = OpenAI(api_key=api_key)
|
| 378 |
msgs = [
|
| 379 |
-
{"role":
|
| 380 |
-
{"role":
|
| 381 |
]
|
| 382 |
resp = client.chat.completions.create(
|
| 383 |
model=model_default,
|
|
@@ -389,24 +350,27 @@ If none are suitable, type 'change'.
|
|
| 389 |
)
|
| 390 |
output_text = resp.choices[0].message.content
|
| 391 |
|
| 392 |
-
#
|
| 393 |
import re
|
| 394 |
dir_pattern = r'(\d+)\.\s+\[?(.*?)\]?(?:\n|$)'
|
| 395 |
dir_matches = re.findall(dir_pattern, output_text)
|
| 396 |
dir_list = [match[1].strip() for match in dir_matches]
|
| 397 |
-
|
| 398 |
-
#
|
| 399 |
if len(dir_list) < 3:
|
| 400 |
-
dir_list = [
|
| 401 |
-
|
| 402 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
return output_text
|
| 404 |
except Exception as e:
|
| 405 |
-
return f"
|
| 406 |
|
| 407 |
|
| 408 |
-
|
| 409 |
-
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Generate System Prompt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 410 |
def generate_system_prompt():
|
| 411 |
mode = user_profile["mode"]
|
| 412 |
sc = user_profile["specific_career"]
|
|
@@ -416,40 +380,39 @@ def generate_system_prompt():
|
|
| 416 |
dd = user_profile["dream_day"]
|
| 417 |
fwd = user_profile["forward_direction_choice"]
|
| 418 |
|
| 419 |
-
if mode == "是":
|
| 420 |
return f"""
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
1.
|
| 426 |
-
2.
|
| 427 |
-
3.
|
| 428 |
-
4.
|
| 429 |
-
5.
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
📍 Now → 🎓 Learning Suggestions → 💼 Practice Suggestions → 📄 Certification Advice → 🚀 Job Search Suggestions
|
| 433 |
"""
|
| 434 |
-
else:
|
| 435 |
return f"""
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
1.
|
| 444 |
-
2. 3
|
| 445 |
-
3.
|
| 446 |
-
📍
|
| 447 |
"""
|
| 448 |
|
| 449 |
|
| 450 |
-
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 451 |
def predict(message, history):
|
| 452 |
-
# =========
|
| 453 |
global current_q_index, questions
|
| 454 |
global in_forward_flow, in_backward_flow
|
| 455 |
global forward_index, backward_index
|
|
@@ -461,7 +424,7 @@ def predict(message, history):
|
|
| 461 |
global direction_chosen, jobs_recommended, job_chosen
|
| 462 |
global post_career_detail_asked, post_career_detail_done
|
| 463 |
|
| 464 |
-
# ========================
|
| 465 |
if not history:
|
| 466 |
current_q_index = 0
|
| 467 |
questions[:] = base_questions
|
|
@@ -486,32 +449,35 @@ def predict(message, history):
|
|
| 486 |
post_career_detail_asked = False
|
| 487 |
post_career_detail_done = False
|
| 488 |
|
| 489 |
-
# ========================
|
| 490 |
if 0 < current_q_index <= len(questions):
|
| 491 |
key = questions[current_q_index - 1][0]
|
|
|
|
| 492 |
|
| 493 |
if key == "mode" and current_q_index == 1:
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
|
|
|
| 501 |
|
| 502 |
-
if current_q_index < len(questions)
|
| 503 |
nxt = questions[current_q_index][1]
|
| 504 |
current_q_index += 1
|
| 505 |
return nxt
|
| 506 |
|
|
|
|
| 507 |
|
| 508 |
|
| 509 |
-
# ========================
|
| 510 |
-
mode = user_profile.get("mode") or ""
|
| 511 |
|
| 512 |
-
# ~~~~~~~~~ Backward
|
| 513 |
-
if mode == "是":
|
| 514 |
-
|
| 515 |
if backward_index > 0 and backward_index <= len(backward_additional_questions):
|
| 516 |
prev_key = backward_additional_questions[backward_index - 1][0]
|
| 517 |
user_profile[prev_key] = message.strip()
|
|
@@ -519,9 +485,9 @@ if mode == "是":
|
|
| 519 |
if backward_index < len(backward_additional_questions):
|
| 520 |
k, prompt_text = backward_additional_questions[backward_index]
|
| 521 |
backward_index += 1
|
| 522 |
-
return prompt_text # ✅
|
| 523 |
else:
|
| 524 |
-
#
|
| 525 |
strategy = backward_strategy_plan(
|
| 526 |
specific_career=user_profile.get("specific_career"),
|
| 527 |
bg_info=user_profile.get("bg_info")
|
|
@@ -531,36 +497,37 @@ if mode == "是":
|
|
| 531 |
forward_deep_dive_done = True
|
| 532 |
post_career_detail_asked = True
|
| 533 |
|
| 534 |
-
return strategy + "\n\
|
| 535 |
-
"\n- A
|
| 536 |
-
"\n- B
|
| 537 |
-
"\n- C
|
| 538 |
-
"\
|
| 539 |
-
"\
|
| 540 |
|
| 541 |
-
|
|
|
|
| 542 |
elif post_career_detail_asked and not post_career_detail_done:
|
| 543 |
user_choice = message.strip().lower()
|
| 544 |
if user_choice in ["不需要", "no", "n"]:
|
| 545 |
post_career_detail_done = True
|
| 546 |
roadmap_offered = True
|
| 547 |
-
return "
|
| 548 |
else:
|
| 549 |
abc_text = answer_abc_questions(
|
| 550 |
selected=user_choice,
|
| 551 |
bg_info=user_profile.get("bg_info", ""),
|
| 552 |
-
wv=user_profile.get("work_value", "
|
| 553 |
-
ps=user_profile.get("personality_summary", "
|
| 554 |
-
dd=user_profile.get("dream_day", "
|
| 555 |
)
|
| 556 |
post_career_detail_done = True
|
| 557 |
roadmap_offered = True
|
| 558 |
-
return abc_text + "\n\
|
| 559 |
|
| 560 |
-
#
|
| 561 |
elif job_chosen and not roadmap_offered and not roadmap_done:
|
| 562 |
roadmap_offered = True
|
| 563 |
-
return "
|
| 564 |
|
| 565 |
elif roadmap_offered and not roadmap_done:
|
| 566 |
ans = message.strip().lower()
|
|
@@ -571,19 +538,19 @@ if mode == "是":
|
|
| 571 |
else:
|
| 572 |
roadmap_done = True
|
| 573 |
backward_done = True
|
| 574 |
-
return "
|
| 575 |
|
| 576 |
-
#
|
| 577 |
if backward_done:
|
| 578 |
try:
|
| 579 |
api_key = os.environ.get("API_TOKEN")
|
| 580 |
if not api_key:
|
| 581 |
-
return "
|
| 582 |
client = OpenAI(api_key=api_key)
|
| 583 |
sprompt = generate_system_prompt()
|
| 584 |
msgs = [
|
| 585 |
{"role": "system", "content": sprompt},
|
| 586 |
-
{"role": "user", "content": f"
|
| 587 |
]
|
| 588 |
resp = client.chat.completions.create(
|
| 589 |
model=model_default,
|
|
@@ -595,165 +562,165 @@ if mode == "是":
|
|
| 595 |
)
|
| 596 |
return resp.choices[0].message.content
|
| 597 |
except Exception as e:
|
| 598 |
-
return f"
|
| 599 |
-
|
| 600 |
-
# ~~~~~~~~~ Forward Mode ~~~~~~~~~
|
| 601 |
-
else:
|
| 602 |
-
# Step 1: Collect user information and recommend 3 career directions
|
| 603 |
-
if in_forward_flow and not direction_chosen:
|
| 604 |
-
# Collect the 4 basic background questions
|
| 605 |
-
if forward_index > 0 and forward_index <= len(forward_additional_questions):
|
| 606 |
-
prev_key = forward_additional_questions[forward_index - 1][0]
|
| 607 |
-
user_profile[prev_key] = message.strip()
|
| 608 |
|
| 609 |
-
if forward_index < len(forward_additional_questions):
|
| 610 |
-
k, prompt_text = forward_additional_questions[forward_index]
|
| 611 |
-
forward_index += 1
|
| 612 |
-
return prompt_text
|
| 613 |
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 618 |
|
| 619 |
-
|
| 620 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 621 |
choice = message.strip().lower()
|
| 622 |
-
if choice in ["1",
|
| 623 |
idx = int(choice) - 1
|
| 624 |
-
if idx < len(user_profile["
|
| 625 |
-
|
| 626 |
-
user_profile["
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
bg_info=user_profile["bg_info"] or "",
|
| 632 |
wv=user_profile["work_value"] or "",
|
| 633 |
ps=user_profile["personality_summary"] or "",
|
| 634 |
dd=user_profile["dream_day"] or ""
|
| 635 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 636 |
else:
|
| 637 |
-
return "
|
| 638 |
elif choice == "换":
|
| 639 |
recommendation_round += 1
|
| 640 |
if recommendation_round > 2:
|
| 641 |
forward_done = True
|
| 642 |
-
return "
|
| 643 |
else:
|
| 644 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 645 |
else:
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
idx = int(choice) - 1
|
| 653 |
-
if idx < len(user_profile["recommended_jobs"]):
|
| 654 |
-
final_job = user_profile["recommended_jobs"][idx]
|
| 655 |
-
user_profile["final_choice"] = final_job
|
| 656 |
-
job_chosen = True
|
| 657 |
-
|
| 658 |
-
detail_msg = do_selected_career_detail(
|
| 659 |
-
selected_career=final_job,
|
| 660 |
-
bg_info=user_profile["bg_info"] or "",
|
| 661 |
-
wv=user_profile["work_value"] or "",
|
| 662 |
-
ps=user_profile["personality_summary"] or "",
|
| 663 |
-
dd=user_profile["dream_day"] or ""
|
| 664 |
)
|
| 665 |
-
|
|
|
|
|
|
|
| 666 |
|
| 667 |
-
|
| 668 |
-
|
|
|
|
|
|
|
| 669 |
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
"\nIf you'd like to view one or more, please type A / B / C / AB / BC / AC / ABC" \
|
| 675 |
-
"\nIf not needed, just reply with 'no'."
|
| 676 |
-
else:
|
| 677 |
-
return "Invalid option. Please reply with 1/2/3 or 'switch'."
|
| 678 |
-
elif choice == "换":
|
| 679 |
-
recommendation_round += 1
|
| 680 |
-
if recommendation_round > 2:
|
| 681 |
forward_done = True
|
| 682 |
-
return
|
| 683 |
else:
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 691 |
)
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
# Step 3: Answer user-selected A/B/C questions
|
| 696 |
-
elif post_career_detail_asked and not post_career_detail_done:
|
| 697 |
-
user_choice = message.strip().lower()
|
| 698 |
-
if user_choice in ["不需要", "no", "n"]:
|
| 699 |
-
post_career_detail_done = True
|
| 700 |
-
roadmap_offered = True
|
| 701 |
-
return "Okay, skipping the three detailed sections. Would you like a timeline-style career roadmap? Reply 'yes' or 'no'."
|
| 702 |
-
else:
|
| 703 |
-
abc_text = answer_abc_questions(
|
| 704 |
-
selected=user_choice,
|
| 705 |
-
bg_info=user_profile["bg_info"],
|
| 706 |
-
wv=user_profile["work_value"],
|
| 707 |
-
ps=user_profile["personality_summary"],
|
| 708 |
-
dd=user_profile["dream_day"]
|
| 709 |
-
)
|
| 710 |
-
post_career_detail_done = True
|
| 711 |
-
roadmap_offered = True
|
| 712 |
-
return abc_text + "\n\nAbove is the A/B/C section you selected. Would you like a timeline-style career roadmap? Reply 'yes' or 'no'."
|
| 713 |
-
|
| 714 |
-
# Step 4: Ask if user wants a timeline roadmap
|
| 715 |
-
elif job_chosen and not roadmap_offered and not roadmap_done:
|
| 716 |
-
roadmap_offered = True
|
| 717 |
-
return "Would you like a timeline-style career roadmap? Reply 'yes' or 'no'."
|
| 718 |
-
|
| 719 |
-
elif roadmap_offered and not roadmap_done:
|
| 720 |
-
ans = message.strip().lower()
|
| 721 |
-
if ans in ["要", "yes", "y"]:
|
| 722 |
-
roadmap_done = True
|
| 723 |
-
forward_done = True
|
| 724 |
-
return do_time_roadmap()
|
| 725 |
-
else:
|
| 726 |
-
roadmap_done = True
|
| 727 |
-
forward_done = True
|
| 728 |
-
return "Okay, no roadmap generated. This concludes the session."
|
| 729 |
-
|
| 730 |
-
# Step 5: Final wrap-up after Forward mode finishes
|
| 731 |
-
if forward_done:
|
| 732 |
-
try:
|
| 733 |
-
api_key = os.environ.get("API_TOKEN")
|
| 734 |
-
if not api_key:
|
| 735 |
-
return "Error: API_TOKEN is not set"
|
| 736 |
-
client = OpenAI(api_key=api_key)
|
| 737 |
-
sprompt = generate_system_prompt()
|
| 738 |
-
msgs = [
|
| 739 |
-
{"role": "system", "content": sprompt},
|
| 740 |
-
{"role": "user", "content": f"Here is the user profile: {user_profile}. Feel free to ask more questions if needed."}
|
| 741 |
-
]
|
| 742 |
-
resp = client.chat.completions.create(
|
| 743 |
-
model=model_default,
|
| 744 |
-
messages=msgs,
|
| 745 |
-
max_tokens=token_default,
|
| 746 |
-
temperature=temp_default,
|
| 747 |
-
top_p=top_p_default,
|
| 748 |
-
stream=False
|
| 749 |
-
)
|
| 750 |
-
return resp.choices[0].message.content
|
| 751 |
-
except Exception as e:
|
| 752 |
-
return f"An error occurred: {str(e)}"
|
| 753 |
-
|
| 754 |
-
# Fallback response
|
| 755 |
-
return "Information collection is complete. If you haven’t received a final reply, send any message to continue."
|
| 756 |
|
|
|
|
|
|
|
| 757 |
|
| 758 |
# ============================ Gradio UI ============================
|
| 759 |
import gradio as gr
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# ============================ 用户信息结构 ============================
|
| 6 |
user_profile = {
|
| 7 |
+
"mode": None, # 只在回答第一个问题时设置(是/否)
|
| 8 |
"specific_career": None,
|
| 9 |
"bg_info": None,
|
| 10 |
"work_value": None,
|
|
|
|
| 12 |
"dream_day": None,
|
| 13 |
"forward_direction_choice": None,
|
| 14 |
|
| 15 |
+
# Forward模式下更多状态
|
| 16 |
+
"recommended_directions": [], # 3个大方向
|
| 17 |
+
"selected_direction": None, # 学生最终选定的大方向
|
| 18 |
+
"recommended_jobs": [], # 3个具体职业
|
| 19 |
+
"final_choice": None # 学生最终选定的具体职业
|
| 20 |
}
|
| 21 |
|
| 22 |
+
# ============================ 基础问题 ============================
|
| 23 |
base_questions = [
|
| 24 |
+
("mode", "你是否有心仪的职业方向?\n- 如果有,请回复'是'(Backward模式)\n- 如果没有,请回复'否'(Forward模式)")
|
| 25 |
]
|
| 26 |
|
| 27 |
+
# ============================ Forward 模式问题 ============================
|
| 28 |
forward_additional_questions = [
|
| 29 |
+
("bg_info", """那么接下来我会需要你提供一些你的资料,并分享一些你的性格和喜好。请先告诉我,你的学校、年级、专业,和主修课程是什么?如果能提供你的选课表或resume就更好啦。"""),
|
| 30 |
+
("work_value", """非常好!那你认为"工作"的意义是什么?你觉得一份理想的工作,应该带来哪些价值或满足感?(例如:帮助他人、赚大钱、自由时间、个人成长、创意空间等)"""),
|
| 31 |
+
("personality_summary", "用几句话总结你的性格:比如外向/内向?喜欢挑战?注重细节?讨厌重复吗?"),
|
| 32 |
+
("dream_day", "再描述一下你理想工作的一天是什么样:在哪工作?做什么?和谁合作?忙还是闲?更自由还是更有秩序?")
|
| 33 |
]
|
| 34 |
|
| 35 |
+
# ============================ Backward 模式问题 ============================
|
| 36 |
backward_additional_questions = [
|
| 37 |
+
("specific_career", "请具体描述你想要从事的职业方向。"),
|
| 38 |
+
("bg_info", "请提供你的学校、年级、专业以及已有的实习或项目经历,以便更好地帮你规划如何达成目标。")
|
| 39 |
]
|
| 40 |
|
| 41 |
+
# ============================ 状态控制变量 ============================
|
| 42 |
current_q_index = 0
|
| 43 |
questions = base_questions[:]
|
| 44 |
in_forward_flow = False
|
|
|
|
| 53 |
roadmap_offered = False
|
| 54 |
roadmap_done = False
|
| 55 |
|
| 56 |
+
direction_chosen = False # 是否已经选定大方向
|
| 57 |
+
jobs_recommended = False # 是否已给出3个具体职业
|
| 58 |
+
job_chosen = False # 是否选定了具体职业
|
| 59 |
|
| 60 |
+
# ========== 用于 A/B/C 专项问题的标记 ==========
|
| 61 |
+
post_career_detail_asked = False # 是否已询问用户要看A/B/C
|
| 62 |
+
post_career_detail_done = False # 是否已回答完A/B/C
|
| 63 |
|
| 64 |
+
# ============================ 模型设置 ============================
|
| 65 |
model_default = "gpt-4o"
|
| 66 |
token_default = 2000
|
| 67 |
temp_default = 0.7
|
|
|
|
| 69 |
|
| 70 |
|
| 71 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 72 |
+
# 动态函数:根据用户选择的 A/B/C,让 OpenAI 生成相应分析
|
| 73 |
def backward_strategy_plan(specific_career, bg_info):
|
| 74 |
prompt = f"""
|
| 75 |
+
你是一位专业的职业规划顾问。
|
| 76 |
+
现在有一位学生,他的目标职业是:{specific_career}
|
| 77 |
+
他的当前背景是:{bg_info}
|
| 78 |
+
请根据这个学生的背景和目标,帮他做一个清晰的达成路径规划,内容要包括:
|
| 79 |
+
1. 是否需要考虑转专业或辅修?
|
| 80 |
+
2. 是否需要补哪些课?课程关键词有哪些?
|
| 81 |
+
3. 推荐的证书/考试(如CFA等)和学习建议
|
| 82 |
+
4. 推荐的实习方向(基于他的背景)
|
| 83 |
+
5. 如何利用本校资源(career center、club、networking等)
|
| 84 |
+
6. 如果他背景不匹配(比如机械去金融),如何填补Gap?
|
| 85 |
+
请使用 Markdown 分段格式,不少于300字,内容务必紧扣他目前的背景和目标。
|
|
|
|
|
|
|
| 86 |
"""
|
| 87 |
try:
|
| 88 |
api_key = os.environ.get("API_TOKEN")
|
| 89 |
if not api_key:
|
| 90 |
+
return "错误:API_TOKEN 未设置"
|
| 91 |
client = OpenAI(api_key=api_key)
|
| 92 |
msgs = [
|
| 93 |
+
{"role": "system", "content": "你是一位经验丰富的职业顾问,擅长根据用户背景制定路径规划"},
|
| 94 |
{"role": "user", "content": prompt}
|
| 95 |
]
|
| 96 |
resp = client.chat.completions.create(
|
|
|
|
| 103 |
)
|
| 104 |
return resp.choices[0].message.content
|
| 105 |
except Exception as e:
|
| 106 |
+
return f"生成职业路径建议时出错: {str(e)}"
|
|
|
|
| 107 |
|
| 108 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
| 109 |
def answer_abc_questions(selected, bg_info, wv, ps, dd):
|
| 110 |
desired_parts = []
|
| 111 |
if "a" in selected.lower():
|
|
|
|
| 116 |
desired_parts.append("C")
|
| 117 |
|
| 118 |
if not desired_parts:
|
| 119 |
+
return "好的,你暂时不需要查看A/B/C的专项信息。"
|
| 120 |
|
| 121 |
prompt_text = f"""
|
| 122 |
+
你是一位专业的职业规划顾问。以下是学生背景信息,请根据他选择的模块(A/B/C)给出详细分析和建议。
|
| 123 |
+
学生背景:
|
| 124 |
+
- 学术背景: {bg_info}
|
| 125 |
+
- 工作意义: {wv}
|
| 126 |
+
- 性格: {ps}
|
| 127 |
+
- 理想工作: {dd}
|
| 128 |
+
学生目前想要的额外信息模块:{', '.join(desired_parts)}
|
| 129 |
+
请分别按顺序为每个模块写一段分析和建议,可以使用Markdown分段形式。
|
| 130 |
+
(A: 专业/选课方向 & 求职入门建议部分的回答请根据学生的学术背景信息分析学生已经有的能力,然后根据其选择的职业方向分析该学生未来的选课应该注重什么课程方向,提示学生课程名称和关键词)
|
| 131 |
+
('B: 本校资源利用建议'部分的回答请你从3个方面回答学生,包括1.如何利用学校的career service;2.如何关注学校与职业准备和证书考试相关的club、组织和平台;3.学生如何与学校的及其所在专业的校友取得联系和networking技巧)
|
| 132 |
+
('C: 实习 & Experience积累'部分的回答请你从申请internship的平台、达成目前职业目标需要的internship方向,申请需要的资料和准备技巧、通过networking获得实习或者学生工作经历的策略)
|
| 133 |
+
请只回答用户所选模块,不要回答未选的模块。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
"""
|
| 135 |
try:
|
| 136 |
api_key = os.environ.get("API_TOKEN")
|
| 137 |
if not api_key:
|
| 138 |
+
return "错误:API_TOKEN 未设置"
|
| 139 |
client = OpenAI(api_key=api_key)
|
| 140 |
msgs = [
|
| 141 |
+
{"role": "system", "content": "你是一位专业职业顾问,会针对A/B/C进行分析。"},
|
| 142 |
{"role": "user", "content": prompt_text}
|
| 143 |
]
|
| 144 |
resp = client.chat.completions.create(
|
|
|
|
| 151 |
)
|
| 152 |
return resp.choices[0].message.content
|
| 153 |
except Exception as e:
|
| 154 |
+
return f"生成A/B/C专项信息时出错: {str(e)}"
|
| 155 |
|
| 156 |
|
| 157 |
+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 职业详细介绍 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 158 |
def do_selected_career_detail(selected_career, bg_info, wv, ps, dd):
|
| 159 |
prompt = f"""
|
| 160 |
+
你是一位专业的职业规划顾问。
|
| 161 |
+
学生最终选择的具体职业: {selected_career}
|
| 162 |
+
学生背景: {bg_info}
|
| 163 |
+
工作意义: {wv}
|
| 164 |
+
性格: {ps}
|
| 165 |
+
理想工作: {dd}
|
| 166 |
+
请写一份更详细的职业说明,至少包含:
|
| 167 |
+
1. 行业平均薪资(初级/中级/高级)
|
| 168 |
+
2. 工作环境(远程/混合/办公室,团队规模等)
|
| 169 |
+
3. 晋升难度(需要什么资历或条件)
|
| 170 |
+
4. 关键技能和考证
|
| 171 |
+
5. 日常工作节奏
|
| 172 |
+
6. 行业前景
|
| 173 |
+
用分段Markdown格式,字数不少于300字。
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
"""
|
| 175 |
try:
|
| 176 |
api_key = os.environ.get("API_TOKEN")
|
| 177 |
if not api_key:
|
| 178 |
+
return "错误:API_TOKEN 未设置"
|
| 179 |
client = OpenAI(api_key=api_key)
|
| 180 |
|
| 181 |
msgs = [
|
| 182 |
+
{"role": "system", "content": "你是一位专业职业顾问,会为用户选定的具体职业提供详细的分析。"},
|
| 183 |
{"role": "user", "content": prompt}
|
| 184 |
]
|
| 185 |
resp = client.chat.completions.create(
|
|
|
|
| 192 |
)
|
| 193 |
return resp.choices[0].message.content
|
| 194 |
except Exception as e:
|
| 195 |
+
return f"生成职业详细信息时出错: {str(e)}"
|
| 196 |
|
| 197 |
|
| 198 |
+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 时间轴式规划 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 199 |
def do_time_roadmap():
|
| 200 |
direction = user_profile.get("final_choice") or user_profile.get("selected_direction") or \
|
| 201 |
user_profile.get("forward_direction_choice") or user_profile.get("specific_career")
|
| 202 |
+
bg_info = user_profile["bg_info"] or "未知专业"
|
| 203 |
+
wv = user_profile["work_value"] or "暂无"
|
| 204 |
+
ps = user_profile["personality_summary"] or "暂无"
|
| 205 |
+
dd = user_profile["dream_day"] or "暂无"
|
| 206 |
|
| 207 |
prompt_roadmap = f"""
|
| 208 |
+
学生信息:
|
| 209 |
+
- 学术背景:{bg_info}
|
| 210 |
+
- 职业方向/具体职业:{direction}
|
| 211 |
+
- 工作价值:{wv}
|
| 212 |
+
- 性格:{ps}
|
| 213 |
+
- 理想工作:{dd}
|
| 214 |
+
请你使用时间轴方式,给出一个职业路线图,如:
|
| 215 |
+
📍 大二暑假: [申请什么实习,参加什么活动,考什么证书]
|
| 216 |
+
🎓 Year 3: [需要选的课/要做的项目]
|
| 217 |
+
💼 大三暑假: [申请什么实习、参加什么活动,考什么证书]
|
| 218 |
+
📄 毕业前1年到6个月: [考什么证书、准备什么申请工作或者申请研究生的材料]
|
| 219 |
+
🚀 毕业后: [目标岗位、如何申请]
|
| 220 |
+
请用Markdown分段写作,结合学生当前背景合理推断要点,写得具体些。
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
"""
|
| 222 |
try:
|
| 223 |
api_key = os.environ.get("API_TOKEN")
|
| 224 |
if not api_key:
|
| 225 |
+
return "错误:API_TOKEN 未设置"
|
| 226 |
client = OpenAI(api_key=api_key)
|
| 227 |
|
| 228 |
msgs = [
|
| 229 |
+
{"role":"system","content":"你是一位专业的职业规划顾问,会生成时间轴式路线图"},
|
| 230 |
+
{"role":"user","content": prompt_roadmap}
|
| 231 |
]
|
| 232 |
resp = client.chat.completions.create(
|
| 233 |
model=model_default,
|
|
|
|
| 239 |
)
|
| 240 |
return resp.choices[0].message.content
|
| 241 |
except Exception as e:
|
| 242 |
+
return f"生成路线图时出错: {str(e)}"
|
| 243 |
|
| 244 |
|
| 245 |
+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 推荐具体职业 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 246 |
def recommend_3jobs_for_direction(direction, bg_info, wv, ps, dd):
|
| 247 |
prompt = f"""
|
| 248 |
+
你是一位专业的职业规划顾问。
|
| 249 |
+
学生选定的大方向:{direction}
|
| 250 |
+
学生背景:{bg_info}
|
| 251 |
+
工作意义:{wv}
|
| 252 |
+
性格:{ps}
|
| 253 |
+
理想工作:{dd}
|
| 254 |
+
请为该方向推荐3个更具体的职业岗位,并分点说明:
|
| 255 |
+
1) 为什么适合这个大方向
|
| 256 |
+
2) 典型工作职责
|
| 257 |
+
3) 技能/证书要求
|
| 258 |
+
4) 未来发展前景
|
| 259 |
+
最后以这样的格式结束(不要在前面重复这些选项):
|
| 260 |
+
请选择你的理想职业:
|
| 261 |
+
1. [职业名称1]
|
| 262 |
+
2. [职业名称2]
|
| 263 |
+
3. [职业名称3]
|
| 264 |
+
如都不满意,可输入'换'。
|
|
|
|
|
|
|
|
|
|
| 265 |
"""
|
| 266 |
try:
|
| 267 |
api_key = os.environ.get("API_TOKEN")
|
| 268 |
if not api_key:
|
| 269 |
+
return "错误:API_TOKEN 未设置"
|
| 270 |
client = OpenAI(api_key=api_key)
|
| 271 |
|
| 272 |
msgs = [
|
| 273 |
+
{"role": "system", "content": "你是一位专业职业顾问,会根据用户选定的大方向再推荐3个具体职业。"},
|
| 274 |
{"role": "user", "content": prompt}
|
| 275 |
]
|
| 276 |
resp = client.chat.completions.create(
|
|
|
|
| 283 |
)
|
| 284 |
output_text = resp.choices[0].message.content
|
| 285 |
|
| 286 |
+
# 从回复中尝试解析职业名称 - 简单示例实现
|
| 287 |
import re
|
| 288 |
job_pattern = r'(\d+)\.\s+\[?(.*?)\]?(?:\n|$)'
|
| 289 |
job_matches = re.findall(job_pattern, output_text)
|
| 290 |
job_list = [match[1].strip() for match in job_matches]
|
| 291 |
+
|
| 292 |
+
# 如果没有找到合适的匹配,创建默认值
|
| 293 |
if len(job_list) < 3:
|
| 294 |
job_list = [
|
| 295 |
+
f"{direction} - 职业A",
|
| 296 |
+
f"{direction} - 职业B",
|
| 297 |
+
f"{direction} - 职业C"
|
| 298 |
]
|
| 299 |
+
|
| 300 |
+
user_profile["recommended_jobs"] = job_list[:3] # 确保只取前3个
|
| 301 |
+
|
| 302 |
+
# 设置为已经推荐过职业
|
| 303 |
global jobs_recommended
|
| 304 |
jobs_recommended = True
|
| 305 |
+
|
| 306 |
return output_text
|
| 307 |
except Exception as e:
|
| 308 |
+
return f"推荐具体职业时出错: {str(e)}"
|
| 309 |
|
| 310 |
|
| 311 |
+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 推荐大方向 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 312 |
def recommend_3directions():
|
| 313 |
+
bg_info = user_profile["bg_info"] or "未知"
|
| 314 |
+
wv = user_profile["work_value"] or "无"
|
| 315 |
+
ps = user_profile["personality_summary"] or "不详"
|
| 316 |
+
dd = user_profile["dream_day"] or "无"
|
| 317 |
|
| 318 |
rec_prompt = f"""
|
| 319 |
+
请根据以下信息,写一份条理清晰的人物画像+3个大方向,其中称呼对面用"你"不要用"他/她":
|
| 320 |
+
- 学术背景: {bg_info}
|
| 321 |
+
- 工作意义: {wv}
|
| 322 |
+
- 性格: {ps}
|
| 323 |
+
- 理想工作: {dd}
|
| 324 |
+
分两部分:
|
| 325 |
+
[一、人物画像]:1) 学术背景 2) 价值观 3) 性格 4) 理想工作
|
| 326 |
+
[二、推荐3个大方向]:每个方向写1~2段文字分析,并说明为什么适合学生。
|
| 327 |
+
最后结束时只有这样的选项(不要在前面重复这些选项):
|
| 328 |
+
请选择你的职业方向:
|
| 329 |
+
1. [方向名称1]
|
| 330 |
+
2. [方向名称2]
|
| 331 |
+
3. [方向名称3]
|
| 332 |
+
如都不满意,可输入'换'。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
"""
|
| 334 |
try:
|
| 335 |
api_key = os.environ.get("API_TOKEN")
|
| 336 |
if not api_key:
|
| 337 |
+
return "错误:API_TOKEN 未设置"
|
| 338 |
client = OpenAI(api_key=api_key)
|
| 339 |
msgs = [
|
| 340 |
+
{"role":"system","content":"你是一位职业顾问,擅长根据用户资料推荐3大方向"},
|
| 341 |
+
{"role":"user","content": rec_prompt}
|
| 342 |
]
|
| 343 |
resp = client.chat.completions.create(
|
| 344 |
model=model_default,
|
|
|
|
| 350 |
)
|
| 351 |
output_text = resp.choices[0].message.content
|
| 352 |
|
| 353 |
+
# 从回复中尝试解析方向名称 - 简单示例实现
|
| 354 |
import re
|
| 355 |
dir_pattern = r'(\d+)\.\s+\[?(.*?)\]?(?:\n|$)'
|
| 356 |
dir_matches = re.findall(dir_pattern, output_text)
|
| 357 |
dir_list = [match[1].strip() for match in dir_matches]
|
| 358 |
+
|
| 359 |
+
# 如果没有找到合适的匹配,创建默认值
|
| 360 |
if len(dir_list) < 3:
|
| 361 |
+
dir_list = [
|
| 362 |
+
"方向1",
|
| 363 |
+
"方向2",
|
| 364 |
+
"方向3"
|
| 365 |
+
]
|
| 366 |
+
|
| 367 |
+
user_profile["recommended_directions"] = dir_list[:3] # 确保只取前3个
|
| 368 |
return output_text
|
| 369 |
except Exception as e:
|
| 370 |
+
return f"多方向推荐出错: {str(e)}"
|
| 371 |
|
| 372 |
|
| 373 |
+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 生成系统提示 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
| 374 |
def generate_system_prompt():
|
| 375 |
mode = user_profile["mode"]
|
| 376 |
sc = user_profile["specific_career"]
|
|
|
|
| 380 |
dd = user_profile["dream_day"]
|
| 381 |
fwd = user_profile["forward_direction_choice"]
|
| 382 |
|
| 383 |
+
if mode == "是":
|
| 384 |
return f"""
|
| 385 |
+
你是一位专业的职业规划顾问,使用Backward Design方法帮助学生达成他们的职业目标。
|
| 386 |
+
目标职业: {sc}
|
| 387 |
+
背景信息: {bg}
|
| 388 |
+
请提供:
|
| 389 |
+
1. 该职业的简要分析
|
| 390 |
+
2. 排名前列的组织或公司
|
| 391 |
+
3. 所需技能和能力
|
| 392 |
+
4. 职业发展路径
|
| 393 |
+
5. 学术/课程建议、技能培养、资源使用、实习规划、简历优化等
|
| 394 |
+
最后请用Markdown格式输出职业路线图:
|
| 395 |
+
📍 现在 → 🎓 学习建议 → 💼 实践建议 → 📄 认证建议 → 🚀 求职建议
|
|
|
|
| 396 |
"""
|
| 397 |
+
else:
|
| 398 |
return f"""
|
| 399 |
+
你是一位专业的职业规划顾问,使用Forward Design方法帮助学生探索合适的职业路径。
|
| 400 |
+
学生背景: {bg}
|
| 401 |
+
工作意义: {wv}
|
| 402 |
+
性格总结: {ps}
|
| 403 |
+
理想工作: {dd}
|
| 404 |
+
学生选择方向: {fwd}
|
| 405 |
+
请输出:
|
| 406 |
+
1. 学生的优势、性格、价值观分析
|
| 407 |
+
2. 推荐3个具体职业,并说明日常工作内容、适配性、要求、准备路径
|
| 408 |
+
3. 最后输出职业路线图:
|
| 409 |
+
📍 现在 → 🎓 学习建议 → 💼 实践建议 → 📄 认证建议 → 🚀 求职建议
|
| 410 |
"""
|
| 411 |
|
| 412 |
|
| 413 |
+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 主对话逻辑函数 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 414 |
def predict(message, history):
|
| 415 |
+
# ========= 在函数开头,提前声明所有 global =========
|
| 416 |
global current_q_index, questions
|
| 417 |
global in_forward_flow, in_backward_flow
|
| 418 |
global forward_index, backward_index
|
|
|
|
| 424 |
global direction_chosen, jobs_recommended, job_chosen
|
| 425 |
global post_career_detail_asked, post_career_detail_done
|
| 426 |
|
| 427 |
+
# ======================== 初始化 =========================
|
| 428 |
if not history:
|
| 429 |
current_q_index = 0
|
| 430 |
questions[:] = base_questions
|
|
|
|
| 449 |
post_career_detail_asked = False
|
| 450 |
post_career_detail_done = False
|
| 451 |
|
| 452 |
+
# ======================== 先处理基础问题 =========================
|
| 453 |
if 0 < current_q_index <= len(questions):
|
| 454 |
key = questions[current_q_index - 1][0]
|
| 455 |
+
|
| 456 |
|
| 457 |
if key == "mode" and current_q_index == 1:
|
| 458 |
+
ans = message.strip()
|
| 459 |
+
user_profile["mode"] = ans
|
| 460 |
+
questions[:] = [] # 清空基础问答流程
|
| 461 |
+
if "是" in ans:
|
| 462 |
+
in_backward_flow = True
|
| 463 |
+
else:
|
| 464 |
+
in_forward_flow = True
|
| 465 |
+
|
| 466 |
|
| 467 |
+
if current_q_index < len(questions)and not in_forward_flow and not in_backward_flow:
|
| 468 |
nxt = questions[current_q_index][1]
|
| 469 |
current_q_index += 1
|
| 470 |
return nxt
|
| 471 |
|
| 472 |
+
|
| 473 |
|
| 474 |
|
| 475 |
+
# ======================== 分模式处理 =========================
|
| 476 |
+
mode = user_profile.get("mode") or ""
|
| 477 |
|
| 478 |
+
# ~~~~~~~~~ Backward模式 ~~~~~~~~~
|
| 479 |
+
if mode == "是":
|
| 480 |
+
if in_backward_flow and not backward_done:
|
| 481 |
if backward_index > 0 and backward_index <= len(backward_additional_questions):
|
| 482 |
prev_key = backward_additional_questions[backward_index - 1][0]
|
| 483 |
user_profile[prev_key] = message.strip()
|
|
|
|
| 485 |
if backward_index < len(backward_additional_questions):
|
| 486 |
k, prompt_text = backward_additional_questions[backward_index]
|
| 487 |
backward_index += 1
|
| 488 |
+
return prompt_text # ✅ 正确:缩进在 if 块内
|
| 489 |
else:
|
| 490 |
+
# 已完成背景收集,生成路径分析
|
| 491 |
strategy = backward_strategy_plan(
|
| 492 |
specific_career=user_profile.get("specific_career"),
|
| 493 |
bg_info=user_profile.get("bg_info")
|
|
|
|
| 497 |
forward_deep_dive_done = True
|
| 498 |
post_career_detail_asked = True
|
| 499 |
|
| 500 |
+
return strategy + "\n\n我还可以针对以下三方面提供更深入的分析建议:" \
|
| 501 |
+
"\n- A:专业/选课 & 求职方向" \
|
| 502 |
+
"\n- B:本校资源利用建议" \
|
| 503 |
+
"\n- C:实习 & networking 积累" \
|
| 504 |
+
"\n如果你想查看其中一个或多个,请输入 A / B / C / AB / BC / AC / ABC" \
|
| 505 |
+
"\n如果都不需要,请回复'不需要'。"
|
| 506 |
|
| 507 |
+
|
| 508 |
+
# 处理 A/B/C 答疑
|
| 509 |
elif post_career_detail_asked and not post_career_detail_done:
|
| 510 |
user_choice = message.strip().lower()
|
| 511 |
if user_choice in ["不需要", "no", "n"]:
|
| 512 |
post_career_detail_done = True
|
| 513 |
roadmap_offered = True
|
| 514 |
+
return "好的,不查看这三个专项。那需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
|
| 515 |
else:
|
| 516 |
abc_text = answer_abc_questions(
|
| 517 |
selected=user_choice,
|
| 518 |
bg_info=user_profile.get("bg_info", ""),
|
| 519 |
+
wv=user_profile.get("work_value", "未提供"),
|
| 520 |
+
ps=user_profile.get("personality_summary", "未提供"),
|
| 521 |
+
dd=user_profile.get("dream_day", "未提供")
|
| 522 |
)
|
| 523 |
post_career_detail_done = True
|
| 524 |
roadmap_offered = True
|
| 525 |
+
return abc_text + "\n\n以上是你所选的A/B/C专项信息。需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
|
| 526 |
|
| 527 |
+
# 路线图询问
|
| 528 |
elif job_chosen and not roadmap_offered and not roadmap_done:
|
| 529 |
roadmap_offered = True
|
| 530 |
+
return "需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
|
| 531 |
|
| 532 |
elif roadmap_offered and not roadmap_done:
|
| 533 |
ans = message.strip().lower()
|
|
|
|
| 538 |
else:
|
| 539 |
roadmap_done = True
|
| 540 |
backward_done = True
|
| 541 |
+
return "好的,不生成时间轴,本次规划结束。"
|
| 542 |
|
| 543 |
+
# Backward 最终总结
|
| 544 |
if backward_done:
|
| 545 |
try:
|
| 546 |
api_key = os.environ.get("API_TOKEN")
|
| 547 |
if not api_key:
|
| 548 |
+
return "错误:API_TOKEN 未设置"
|
| 549 |
client = OpenAI(api_key=api_key)
|
| 550 |
sprompt = generate_system_prompt()
|
| 551 |
msgs = [
|
| 552 |
{"role": "system", "content": sprompt},
|
| 553 |
+
{"role": "user", "content": f"以下是资料: {user_profile}. 若需要更多咨询可再次输入"}
|
| 554 |
]
|
| 555 |
resp = client.chat.completions.create(
|
| 556 |
model=model_default,
|
|
|
|
| 562 |
)
|
| 563 |
return resp.choices[0].message.content
|
| 564 |
except Exception as e:
|
| 565 |
+
return f"发生错误: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 566 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 567 |
|
| 568 |
+
# ~~~~~~~~~ Forward模式 ~~~~~~~~~
|
| 569 |
+
else:
|
| 570 |
+
# 前期处理:收集信息和推荐大方向
|
| 571 |
+
if in_forward_flow and not direction_chosen:
|
| 572 |
+
# 收集 4个基础问题
|
| 573 |
+
if forward_index > 0 and forward_index <= len(forward_additional_questions):
|
| 574 |
+
prev_key = forward_additional_questions[forward_index - 1][0]
|
| 575 |
+
user_profile[prev_key] = message.strip()
|
| 576 |
|
| 577 |
+
if forward_index < len(forward_additional_questions):
|
| 578 |
+
k, prompt_text = forward_additional_questions[forward_index]
|
| 579 |
+
forward_index += 1
|
| 580 |
+
return prompt_text
|
| 581 |
+
|
| 582 |
+
# 问完 4个问题 -> 推荐3方向
|
| 583 |
+
elif not forward_recommendation_given:
|
| 584 |
+
forward_recommendation_given = True
|
| 585 |
+
return recommend_3directions()
|
| 586 |
+
|
| 587 |
+
# 等待用户选 1/2/3 or 换
|
| 588 |
+
else:
|
| 589 |
+
choice = message.strip().lower()
|
| 590 |
+
if choice in ["1","2","3"]:
|
| 591 |
+
idx = int(choice) - 1
|
| 592 |
+
if idx < len(user_profile["recommended_directions"]):
|
| 593 |
+
sel_dir = user_profile["recommended_directions"][idx]
|
| 594 |
+
user_profile["selected_direction"] = sel_dir
|
| 595 |
+
direction_chosen = True
|
| 596 |
+
# 不在这里设置jobs_recommended=True,而是在函数内部设置
|
| 597 |
+
return recommend_3jobs_for_direction(
|
| 598 |
+
direction=sel_dir,
|
| 599 |
+
bg_info=user_profile["bg_info"] or "",
|
| 600 |
+
wv=user_profile["work_value"] or "",
|
| 601 |
+
ps=user_profile["personality_summary"] or "",
|
| 602 |
+
dd=user_profile["dream_day"] or ""
|
| 603 |
+
)
|
| 604 |
+
else:
|
| 605 |
+
return "无效选项,请重新输入1/2/3或'换'"
|
| 606 |
+
elif choice == "换":
|
| 607 |
+
recommendation_round += 1
|
| 608 |
+
if recommendation_round > 2:
|
| 609 |
+
forward_done = True
|
| 610 |
+
return "已多次换方向,结束推荐。"
|
| 611 |
+
else:
|
| 612 |
+
return recommend_3directions()
|
| 613 |
+
else:
|
| 614 |
+
return "请回复1/2/3选择方向,或输入'换'来重新推荐"
|
| 615 |
+
|
| 616 |
+
# 已推荐具体职业 -> 等待用户选择
|
| 617 |
+
elif direction_chosen and jobs_recommended and not job_chosen:
|
| 618 |
choice = message.strip().lower()
|
| 619 |
+
if choice in ["1","2","3"]:
|
| 620 |
idx = int(choice) - 1
|
| 621 |
+
if idx < len(user_profile["recommended_jobs"]):
|
| 622 |
+
final_job = user_profile["recommended_jobs"][idx]
|
| 623 |
+
user_profile["final_choice"] = final_job
|
| 624 |
+
job_chosen = True
|
| 625 |
+
|
| 626 |
+
detail_msg = do_selected_career_detail(
|
| 627 |
+
selected_career=final_job,
|
| 628 |
bg_info=user_profile["bg_info"] or "",
|
| 629 |
wv=user_profile["work_value"] or "",
|
| 630 |
ps=user_profile["personality_summary"] or "",
|
| 631 |
dd=user_profile["dream_day"] or ""
|
| 632 |
)
|
| 633 |
+
forward_deep_dive_done = True
|
| 634 |
+
|
| 635 |
+
# **询问 A/B/C 专项**
|
| 636 |
+
post_career_detail_asked = True
|
| 637 |
+
|
| 638 |
+
return detail_msg + "\n\n我还可以针对以下三方面提供更深入的分析建议:" \
|
| 639 |
+
"\n- A:专业/选课 & 求职方向" \
|
| 640 |
+
"\n- B:本校资源利用建议" \
|
| 641 |
+
"\n- C:实习 & networking 积累" \
|
| 642 |
+
"\n如果你想查看其中一个或多个,请输入 A / B / C / AB / BC / AC / ABC" \
|
| 643 |
+
"\n如果都不需要,请回复'不需要'。"
|
| 644 |
else:
|
| 645 |
+
return "无效的选项,请重新输入1/2/3或'换'"
|
| 646 |
elif choice == "换":
|
| 647 |
recommendation_round += 1
|
| 648 |
if recommendation_round > 2:
|
| 649 |
forward_done = True
|
| 650 |
+
return "已多次换职业,结束推荐。"
|
| 651 |
else:
|
| 652 |
+
sel_dir = user_profile.get("selected_direction") or "未知方向"
|
| 653 |
+
return recommend_3jobs_for_direction(
|
| 654 |
+
direction=sel_dir,
|
| 655 |
+
bg_info=user_profile["bg_info"] or "",
|
| 656 |
+
wv=user_profile["work_value"] or "",
|
| 657 |
+
ps=user_profile["personality_summary"] or "",
|
| 658 |
+
dd=user_profile["dream_day"] or ""
|
| 659 |
+
)
|
| 660 |
+
else:
|
| 661 |
+
return "请回复1/2/3选择具体职业,或输入'换'来重新推荐"
|
| 662 |
+
|
| 663 |
+
# 处理 A/B/C 答疑
|
| 664 |
+
elif post_career_detail_asked and not post_career_detail_done:
|
| 665 |
+
user_choice = message.strip().lower()
|
| 666 |
+
if user_choice in ["不需要", "no", "n"]:
|
| 667 |
+
post_career_detail_done = True
|
| 668 |
+
roadmap_offered = True
|
| 669 |
+
return "好的,不查看这三个专项。那需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
|
| 670 |
else:
|
| 671 |
+
abc_text = answer_abc_questions(
|
| 672 |
+
selected=user_choice,
|
| 673 |
+
bg_info=user_profile["bg_info"],
|
| 674 |
+
wv=user_profile["work_value"],
|
| 675 |
+
ps=user_profile["personality_summary"],
|
| 676 |
+
dd=user_profile["dream_day"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 677 |
)
|
| 678 |
+
post_career_detail_done = True
|
| 679 |
+
roadmap_offered = True
|
| 680 |
+
return abc_text + "\n\n以上是你所选的A/B/C专项信息。需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
|
| 681 |
|
| 682 |
+
# 已选定具体职业 -> 问是否需要时间轴
|
| 683 |
+
elif job_chosen and not roadmap_offered and not roadmap_done:
|
| 684 |
+
roadmap_offered = True
|
| 685 |
+
return "需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
|
| 686 |
|
| 687 |
+
elif roadmap_offered and not roadmap_done:
|
| 688 |
+
ans = message.strip().lower()
|
| 689 |
+
if ans in ["要", "yes", "y"]:
|
| 690 |
+
roadmap_done = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 691 |
forward_done = True
|
| 692 |
+
return do_time_roadmap()
|
| 693 |
else:
|
| 694 |
+
roadmap_done = True
|
| 695 |
+
forward_done = True
|
| 696 |
+
return "好的,不生成时间轴,本次规划结束。"
|
| 697 |
+
|
| 698 |
+
# 如果 forward_done,就给个收尾
|
| 699 |
+
if forward_done:
|
| 700 |
+
try:
|
| 701 |
+
api_key = os.environ.get("API_TOKEN")
|
| 702 |
+
if not api_key:
|
| 703 |
+
return "错误:API_TOKEN 未设置"
|
| 704 |
+
client = OpenAI(api_key=api_key)
|
| 705 |
+
sprompt = generate_system_prompt()
|
| 706 |
+
msgs = [
|
| 707 |
+
{"role":"system","content":sprompt},
|
| 708 |
+
{"role":"user","content": f"以下是资料: {user_profile}. 若需要更多咨询可再次输入"}
|
| 709 |
+
]
|
| 710 |
+
resp = client.chat.completions.create(
|
| 711 |
+
model=model_default,
|
| 712 |
+
messages=msgs,
|
| 713 |
+
max_tokens=token_default,
|
| 714 |
+
temperature=temp_default,
|
| 715 |
+
top_p=top_p_default,
|
| 716 |
+
stream=False
|
| 717 |
)
|
| 718 |
+
return resp.choices[0].message.content
|
| 719 |
+
except Exception as e:
|
| 720 |
+
return f"发生错误: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 721 |
|
| 722 |
+
# 兜底
|
| 723 |
+
return "信息收集完毕,如还未得到最终回复,请再输入任意文字继续。"
|
| 724 |
|
| 725 |
# ============================ Gradio UI ============================
|
| 726 |
import gradio as gr
|