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,35 @@ user_profile = {
|
|
| 12 |
"dream_day": None,
|
| 13 |
"forward_direction_choice": None,
|
| 14 |
|
| 15 |
-
|
| 16 |
-
"
|
| 17 |
-
"
|
| 18 |
-
"
|
| 19 |
-
"final_choice": None # 学生最终选定的具体职业
|
| 20 |
}
|
| 21 |
|
| 22 |
-
|
| 23 |
base_questions = [
|
| 24 |
-
("mode", "
|
| 25 |
]
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 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,44 +55,45 @@ 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 |
-
# ========== 用于 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
|
| 68 |
top_p_default = 0.95
|
| 69 |
|
| 70 |
|
| 71 |
-
|
| 72 |
-
# 动态函数:根据用户选择的 A/B/C,让 OpenAI 生成相应分析
|
| 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 |
try:
|
| 88 |
api_key = os.environ.get("API_TOKEN")
|
| 89 |
if not api_key:
|
| 90 |
-
return "
|
| 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,7 +106,7 @@ def backward_strategy_plan(specific_career, bg_info):
|
|
| 103 |
)
|
| 104 |
return resp.choices[0].message.content
|
| 105 |
except Exception as e:
|
| 106 |
-
return f"
|
| 107 |
|
| 108 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 109 |
def answer_abc_questions(selected, bg_info, wv, ps, dd):
|
|
@@ -719,8 +722,8 @@ def predict(message, history):
|
|
| 719 |
except Exception as e:
|
| 720 |
return f"发生错误: {str(e)}"
|
| 721 |
|
| 722 |
-
|
| 723 |
-
return "
|
| 724 |
|
| 725 |
# ============================ Gradio UI ============================
|
| 726 |
import gradio as gr
|
|
@@ -791,9 +794,9 @@ footer {
|
|
| 791 |
""")
|
| 792 |
|
| 793 |
chatbot = gr.Chatbot(height=500, show_label=False, show_copy_button=True, type="messages")
|
| 794 |
-
msg = gr.Textbox(placeholder="
|
| 795 |
-
send = gr.Button("
|
| 796 |
-
reset = gr.Button("🔄
|
| 797 |
|
| 798 |
def add_user_message(m, hist):
|
| 799 |
hist = hist or []
|
|
|
|
| 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 |
+
"recommended_directions": [],
|
| 16 |
+
"selected_direction": None,
|
| 17 |
+
"recommended_jobs": [],
|
| 18 |
+
"final_choice": None
|
|
|
|
| 19 |
}
|
| 20 |
|
| 21 |
+
|
| 22 |
base_questions = [
|
| 23 |
+
("mode", "Do you have any preferred future plans?\n- if yes, reply'Y'(Backward Mode)\n- if NO,reply'NO'(Forward Mode)")
|
| 24 |
]
|
| 25 |
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# ============================ Forward Mode Questions ============================
|
| 30 |
forward_additional_questions = [
|
| 31 |
+
("bg_info", """To get started, please tell me a bit about your academic background and interests. What's your school, year, major, and core courses? If you have a transcript or resume, that would be even better!"""),
|
| 32 |
+
("work_value", """Great! What do you think is the meaning or value of 'work'? What should an ideal job provide for you? (e.g., helping others, high income, free time, personal growth, creative space, etc.)"""),
|
| 33 |
+
("personality_summary", "Briefly describe your personality: Are you introverted or extroverted? Do you enjoy challenges? Detail-oriented? Do you dislike repetitive work?"),
|
| 34 |
+
("dream_day", "Now describe what your ideal workday looks like: Where are you working? What are you doing? Who are you collaborating with? Is it busy or relaxed? More freedom or more structure?")
|
| 35 |
]
|
| 36 |
|
| 37 |
+
# ============================ Backward Mode Questions ============================
|
| 38 |
backward_additional_questions = [
|
| 39 |
+
("specific_career", "Please describe the specific career path you want to pursue."),
|
| 40 |
+
("bg_info", "Please provide your school, year, major, and any internship or project experience you already have, so I can help you build a clearer path to your goal.")
|
| 41 |
]
|
| 42 |
|
| 43 |
+
|
| 44 |
current_q_index = 0
|
| 45 |
questions = base_questions[:]
|
| 46 |
in_forward_flow = False
|
|
|
|
| 55 |
roadmap_offered = False
|
| 56 |
roadmap_done = False
|
| 57 |
|
| 58 |
+
direction_chosen = False
|
| 59 |
+
jobs_recommended = False
|
| 60 |
+
job_chosen = False
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
post_career_detail_asked = False
|
| 64 |
+
post_career_detail_done = False
|
| 65 |
|
|
|
|
|
|
|
|
|
|
| 66 |
|
|
|
|
| 67 |
model_default = "gpt-4o"
|
| 68 |
token_default = 2000
|
| 69 |
temp_default = 0.7
|
| 70 |
top_p_default = 0.95
|
| 71 |
|
| 72 |
|
| 73 |
+
|
|
|
|
| 74 |
def backward_strategy_plan(specific_career, bg_info):
|
| 75 |
prompt = f"""
|
| 76 |
+
You are a professional career planning advisor.
|
| 77 |
+
A student has the target career: {specific_career}
|
| 78 |
+
Their current background is: {bg_info}
|
| 79 |
+
|
| 80 |
+
Please help the student build a clear plan to reach their goal, including:
|
| 81 |
+
1. Should they consider changing majors or taking a minor?
|
| 82 |
+
2. Are there any courses they should take? What are the key course topics?
|
| 83 |
+
3. Recommended certificates/exams (e.g., CFA) and study tips
|
| 84 |
+
4. Suggested internship directions (based on their background)
|
| 85 |
+
5. How to make use of university resources (career center, clubs, networking, etc.)
|
| 86 |
+
6. If their background is mismatched (e.g., mechanical engineering to finance), how to bridge the gap?
|
| 87 |
+
|
| 88 |
+
Use Markdown formatting in sections, with a minimum of 300 words. The content must align closely with the student's current background and goals.
|
| 89 |
"""
|
| 90 |
try:
|
| 91 |
api_key = os.environ.get("API_TOKEN")
|
| 92 |
if not api_key:
|
| 93 |
+
return "Wrong: API_TOKEN not set"
|
| 94 |
client = OpenAI(api_key=api_key)
|
| 95 |
msgs = [
|
| 96 |
+
{"role": "system", "content": "You are an experienced career advisor, good at creating roadmaps based on user background. "},
|
| 97 |
{"role": "user", "content": prompt}
|
| 98 |
]
|
| 99 |
resp = client.chat.completions.create(
|
|
|
|
| 106 |
)
|
| 107 |
return resp.choices[0].message.content
|
| 108 |
except Exception as e:
|
| 109 |
+
return f"Wrong path: {str(e)}"
|
| 110 |
|
| 111 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 112 |
def answer_abc_questions(selected, bg_info, wv, ps, dd):
|
|
|
|
| 722 |
except Exception as e:
|
| 723 |
return f"发生错误: {str(e)}"
|
| 724 |
|
| 725 |
+
|
| 726 |
+
return "Information down"
|
| 727 |
|
| 728 |
# ============================ Gradio UI ============================
|
| 729 |
import gradio as gr
|
|
|
|
| 794 |
""")
|
| 795 |
|
| 796 |
chatbot = gr.Chatbot(height=500, show_label=False, show_copy_button=True, type="messages")
|
| 797 |
+
msg = gr.Textbox(placeholder="enter your answer...")
|
| 798 |
+
send = gr.Button("Send 🚀", elem_id="custom-send")
|
| 799 |
+
reset = gr.Button("🔄 Restart")
|
| 800 |
|
| 801 |
def add_user_message(m, hist):
|
| 802 |
hist = hist or []
|