Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,13 @@ import os
|
|
| 4 |
|
| 5 |
# ============================ 用户信息结构 ============================
|
| 6 |
user_profile = {
|
| 7 |
-
"mode": None,
|
| 8 |
-
"specific_career": None,
|
| 9 |
-
"bg_info": None,
|
| 10 |
-
"work_value": None,
|
| 11 |
-
"personality_summary": None,
|
| 12 |
-
"dream_day": None,
|
|
|
|
| 13 |
"forward_direction_choice": None
|
| 14 |
}
|
| 15 |
|
|
@@ -20,29 +21,20 @@ base_questions = [
|
|
| 20 |
|
| 21 |
# ============================ Forward 模式问题 ============================
|
| 22 |
forward_additional_questions = [
|
| 23 |
-
(
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
接下来
|
| 27 |
-
),
|
| 28 |
-
(
|
| 29 |
-
"work_value",
|
| 30 |
-
"""很好!接下来我想更深入地了解你。
|
| 31 |
你认为“工作”的意义是什么?你觉得一份理想的工作,应该带来哪些价值或满足感?
|
| 32 |
你可以从以下几个角度思考,也可以自由表达:
|
| 33 |
- 是可以帮助他人?实现社会影响力?
|
| 34 |
- 还是可以赚大钱?获得物质回报?
|
| 35 |
- 或者是自由时间?个人成长?创意空间?
|
| 36 |
-
举个例子更好哦~"""
|
| 37 |
-
|
| 38 |
-
(
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
),
|
| 42 |
-
(
|
| 43 |
-
"dream_day",
|
| 44 |
-
"你能描述一下你理想工作的一天是什么样的吗?你在哪工作?做什么?跟谁合作?忙还是闲?更自由还是更有秩序?"
|
| 45 |
-
)
|
| 46 |
]
|
| 47 |
|
| 48 |
# ============================ Backward 模式问题 ============================
|
|
@@ -60,10 +52,12 @@ forward_index = 0
|
|
| 60 |
backward_index = 0
|
| 61 |
forward_done = False
|
| 62 |
backward_done = False
|
|
|
|
|
|
|
| 63 |
forward_recommendation_given = False
|
| 64 |
|
| 65 |
# ============================ 模型设置 ============================
|
| 66 |
-
model_default = "gpt-4o"
|
| 67 |
token_default = 2000
|
| 68 |
temp_default = 0.7
|
| 69 |
top_p_default = 0.95
|
|
@@ -76,39 +70,37 @@ def generate_system_prompt():
|
|
| 76 |
work_value = user_profile["work_value"]
|
| 77 |
personality_summary = user_profile["personality_summary"]
|
| 78 |
dream_day = user_profile["dream_day"]
|
| 79 |
-
|
| 80 |
|
| 81 |
if "是" in (mode or ""):
|
| 82 |
return f"""
|
| 83 |
你是一位专业的职业规划顾问,使用Backward Design方法帮助学生达成他们的职业目标。
|
| 84 |
目标职业: {specific_career}
|
| 85 |
背景信息: {bg_info}
|
| 86 |
-
|
| 87 |
请提供:
|
| 88 |
1. 该职业的简要分析
|
| 89 |
2. 排名前列的组织或公司
|
| 90 |
3. 所需技能和能力
|
| 91 |
4. 职业发展路径
|
| 92 |
5. 学术/课程建议、技能培养、资源使用、实习规划、简历优化等
|
| 93 |
-
|
| 94 |
最后请用Markdown格式输出职业路径图:
|
| 95 |
📍 现在 → 🎓 学习建议 → 💼 实践建议 → 📄 认证建议 → 🚀 求职建议
|
| 96 |
"""
|
| 97 |
else:
|
| 98 |
return f"""
|
| 99 |
你是一位专业的职业规划顾问,使用Forward Design方法帮助学生探索合适的职业路径。
|
| 100 |
-
|
| 101 |
学生背景:
|
| 102 |
- 学术背景: {bg_info}
|
| 103 |
- 工作意义: {work_value}
|
| 104 |
- 性格总结: {personality_summary}
|
| 105 |
- 理想一天: {dream_day}
|
| 106 |
-
- 选择方向: {
|
| 107 |
|
| 108 |
请输出:
|
| 109 |
1. 学生的优势、性格、价值观分析
|
| 110 |
-
2. 推荐具体职业
|
| 111 |
-
3. 输出职业路径图:
|
|
|
|
| 112 |
"""
|
| 113 |
|
| 114 |
# ============================ 主逻辑函数 ============================
|
|
@@ -116,14 +108,16 @@ def predict(message, history):
|
|
| 116 |
global current_q_index, questions
|
| 117 |
global in_forward_flow, in_backward_flow
|
| 118 |
global forward_index, backward_index
|
| 119 |
-
global forward_done, backward_done
|
|
|
|
| 120 |
|
| 121 |
-
# 如果是对话第一
|
| 122 |
if not history:
|
| 123 |
current_q_index = 0
|
| 124 |
questions = base_questions[:]
|
| 125 |
for key in user_profile:
|
| 126 |
user_profile[key] = None
|
|
|
|
| 127 |
in_forward_flow = False
|
| 128 |
in_backward_flow = False
|
| 129 |
forward_index = 0
|
|
@@ -132,178 +126,287 @@ def predict(message, history):
|
|
| 132 |
backward_done = False
|
| 133 |
forward_recommendation_given = False
|
| 134 |
|
| 135 |
-
# 如果
|
| 136 |
-
if
|
| 137 |
-
#
|
| 138 |
key = questions[current_q_index - 1][0]
|
| 139 |
user_profile[key] = message.strip()
|
| 140 |
|
| 141 |
-
# 根据回答决定
|
| 142 |
if key == "mode" and "是" in user_profile["mode"]:
|
| 143 |
-
# 如果是
|
| 144 |
questions.insert(1, ("specific_career", "请具体描述你想要从事的职业方向。"))
|
| 145 |
in_backward_flow = True
|
| 146 |
elif key == "mode" and "否" in user_profile["mode"]:
|
|
|
|
| 147 |
in_forward_flow = True
|
| 148 |
|
| 149 |
-
# 如果还
|
| 150 |
if current_q_index < len(questions):
|
| 151 |
-
|
| 152 |
current_q_index += 1
|
| 153 |
-
return
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
|
| 156 |
-
# ============ Backward 流程 ============
|
| 157 |
if "是" in mode:
|
| 158 |
if in_backward_flow and not backward_done:
|
| 159 |
-
# 如果
|
| 160 |
if backward_index < len(backward_additional_questions):
|
| 161 |
-
|
| 162 |
backward_index += 1
|
| 163 |
-
return
|
| 164 |
else:
|
| 165 |
-
# Backward 问完
|
| 166 |
backward_done = True
|
| 167 |
|
| 168 |
-
# ==========
|
| 169 |
else:
|
| 170 |
if in_forward_flow and not forward_done:
|
| 171 |
-
# 如果还
|
| 172 |
if forward_index < len(forward_additional_questions):
|
| 173 |
-
|
| 174 |
forward_index += 1
|
| 175 |
-
return
|
| 176 |
|
| 177 |
-
# 已问完
|
| 178 |
elif not forward_recommendation_given:
|
| 179 |
forward_recommendation_given = True
|
| 180 |
try:
|
| 181 |
api_key = os.environ.get("API_TOKEN")
|
| 182 |
if not api_key:
|
| 183 |
-
return "错误:API_TOKEN 未设置"
|
|
|
|
| 184 |
client = OpenAI(api_key=api_key)
|
| 185 |
|
| 186 |
-
#
|
| 187 |
recommendation_prompt = f"""
|
| 188 |
-
请你
|
| 189 |
-
|
| 190 |
-
- 工作意义: {user_profile['work_value']}
|
| 191 |
-
- 性格总结: {user_profile['personality_summary']}
|
| 192 |
-
- 理想一天: {user_profile['dream_day']}
|
| 193 |
-
|
| 194 |
-
然后再推荐3个主要职业方向(大的领域方向,例如:咨询、设计、工程、教育等),并用简短理由说明为什么适合。
|
| 195 |
最后用如下格式结尾:
|
| 196 |
1. xxx
|
| 197 |
2. xxx
|
| 198 |
3. xxx
|
| 199 |
|
| 200 |
请学生从中选择一个方向进行下一步。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
"""
|
| 202 |
|
| 203 |
-
|
| 204 |
-
{"role": "system", "content": "你是一位职业
|
| 205 |
{"role": "user", "content": recommendation_prompt}
|
| 206 |
]
|
| 207 |
-
|
| 208 |
model=model_default,
|
| 209 |
-
messages=
|
| 210 |
max_tokens=token_default,
|
| 211 |
temperature=temp_default,
|
| 212 |
top_p=top_p_default,
|
| 213 |
stream=False
|
| 214 |
)
|
| 215 |
-
return
|
| 216 |
|
| 217 |
except Exception as e:
|
| 218 |
return f"生成推荐方向时出错: {str(e)}"
|
| 219 |
|
| 220 |
-
# 如果已经推荐过方向,
|
| 221 |
else:
|
| 222 |
user_profile["forward_direction_choice"] = message.strip()
|
| 223 |
forward_done = True
|
| 224 |
|
| 225 |
-
# ==========
|
| 226 |
if ("是" in mode and backward_done) or ("否" in mode and forward_done):
|
| 227 |
-
#
|
| 228 |
try:
|
| 229 |
api_key = os.environ.get("API_TOKEN")
|
| 230 |
if not api_key:
|
| 231 |
-
return "错误:API密钥未设置,请在环境变量中添加
|
| 232 |
-
client = OpenAI(api_key=api_key)
|
| 233 |
|
|
|
|
| 234 |
system_prompt = generate_system_prompt()
|
| 235 |
-
|
|
|
|
| 236 |
{"role": "system", "content": system_prompt},
|
| 237 |
-
{"role": "user", "content": f"根据我提供的信息,
|
| 238 |
]
|
| 239 |
-
|
| 240 |
model=model_default,
|
| 241 |
-
messages=
|
| 242 |
max_tokens=token_default,
|
| 243 |
temperature=temp_default,
|
| 244 |
top_p=top_p_default,
|
| 245 |
stream=False
|
| 246 |
)
|
| 247 |
-
return
|
| 248 |
|
| 249 |
except Exception as e:
|
| 250 |
return f"发生错误: {str(e)}"
|
| 251 |
|
| 252 |
-
# 如果还没结束,就让用户输入下一条
|
| 253 |
return "信息收集完毕,若尚未得到最终回复,请输入任意文字以继续。"
|
| 254 |
|
| 255 |
# ============================ Gradio UI ============================
|
| 256 |
-
with gr.Blocks(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
gr.Markdown("""
|
| 258 |
-
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
| 260 |
""")
|
| 261 |
|
| 262 |
-
chatbot = gr.Chatbot(
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
|
| 267 |
-
def
|
| 268 |
history = history or []
|
| 269 |
history.append({"role": "user", "content": message})
|
| 270 |
return "", history
|
| 271 |
|
| 272 |
-
def
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
|
|
|
| 276 |
return history
|
| 277 |
|
| 278 |
-
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
)
|
| 281 |
-
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
)
|
| 284 |
|
| 285 |
-
def
|
| 286 |
global current_q_index, questions
|
| 287 |
global in_forward_flow, in_backward_flow
|
| 288 |
global forward_index, backward_index
|
| 289 |
-
global forward_done, backward_done
|
|
|
|
| 290 |
|
| 291 |
current_q_index = 0
|
| 292 |
questions = base_questions[:]
|
| 293 |
for key in user_profile:
|
| 294 |
user_profile[key] = None
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
forward_recommendation_given = False
|
|
|
|
| 299 |
return []
|
| 300 |
|
| 301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
-
def
|
| 304 |
-
|
| 305 |
-
return [{"role": "assistant", "content": base_questions[0][1]}]
|
| 306 |
|
| 307 |
-
demo.load(
|
|
|
|
| 308 |
|
| 309 |
-
demo.launch()
|
|
|
|
| 4 |
|
| 5 |
# ============================ 用户信息结构 ============================
|
| 6 |
user_profile = {
|
| 7 |
+
"mode": None, # 是 / 否 (Backward / Forward)
|
| 8 |
+
"specific_career": None, # Backward 目标职业
|
| 9 |
+
"bg_info": None, # 学术背景
|
| 10 |
+
"work_value": None, # 工作意义
|
| 11 |
+
"personality_summary": None, # 性格总结
|
| 12 |
+
"dream_day": None, # 理想工作一天
|
| 13 |
+
# 新增:记录用户在Forward时选的方向
|
| 14 |
"forward_direction_choice": None
|
| 15 |
}
|
| 16 |
|
|
|
|
| 21 |
|
| 22 |
# ============================ Forward 模式问题 ============================
|
| 23 |
forward_additional_questions = [
|
| 24 |
+
("bg_info", """那么接下来我会需要你提供一些你的资料,并分享一些你的性格和喜好,我会分析然后给你推荐定制的可行方向。
|
| 25 |
+
接下来,请你首先提供你的学术背景:你的学校、年级、专业,和主修课程是什么?如果能提供你的选课表或 resume 就最好啦。"""),
|
| 26 |
+
|
| 27 |
+
("work_value", """很好!接下来我想更深入地了解你。
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
你认为“工作”的意义是什么?你觉得一份理想的工作,应该带来哪些价值或满足感?
|
| 29 |
你可以从以下几个角度思考,也可以自由表达:
|
| 30 |
- 是可以帮助他人?实现社会影响力?
|
| 31 |
- 还是可以赚大钱?获得物质回报?
|
| 32 |
- 或者是自由时间?个人成长?创意空间?
|
| 33 |
+
举个例子更好哦~"""),
|
| 34 |
+
|
| 35 |
+
("personality_summary", "你能不能用几句话总结一下你自己的性格?比如:外向、喜欢挑战、注重细节、讨厌重复..."),
|
| 36 |
+
|
| 37 |
+
("dream_day", "你能描述一下你理想工作的一天是什么样的吗?你在哪工作?做什么?跟谁合作?忙还是闲?更自由还是更有秩序?")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
]
|
| 39 |
|
| 40 |
# ============================ Backward 模式问题 ============================
|
|
|
|
| 52 |
backward_index = 0
|
| 53 |
forward_done = False
|
| 54 |
backward_done = False
|
| 55 |
+
|
| 56 |
+
# 新增:用于判断是否在 Forward 模式下,已经给过“自动推荐方向”
|
| 57 |
forward_recommendation_given = False
|
| 58 |
|
| 59 |
# ============================ 模型设置 ============================
|
| 60 |
+
model_default = "gpt-4o"
|
| 61 |
token_default = 2000
|
| 62 |
temp_default = 0.7
|
| 63 |
top_p_default = 0.95
|
|
|
|
| 70 |
work_value = user_profile["work_value"]
|
| 71 |
personality_summary = user_profile["personality_summary"]
|
| 72 |
dream_day = user_profile["dream_day"]
|
| 73 |
+
forward_choice = user_profile.get("forward_direction_choice")
|
| 74 |
|
| 75 |
if "是" in (mode or ""):
|
| 76 |
return f"""
|
| 77 |
你是一位专业的职业规划顾问,使用Backward Design方法帮助学生达成他们的职业目标。
|
| 78 |
目标职业: {specific_career}
|
| 79 |
背景信息: {bg_info}
|
|
|
|
| 80 |
请提供:
|
| 81 |
1. 该职业的简要分析
|
| 82 |
2. 排名前列的组织或公司
|
| 83 |
3. 所需技能和能力
|
| 84 |
4. 职业发展路径
|
| 85 |
5. 学术/课程建议、技能培养、资源使用、实习规划、简历优化等
|
|
|
|
| 86 |
最后请用Markdown格式输出职业路径图:
|
| 87 |
📍 现在 → 🎓 学习建议 → 💼 实践建议 → 📄 认证建议 → 🚀 求职建议
|
| 88 |
"""
|
| 89 |
else:
|
| 90 |
return f"""
|
| 91 |
你是一位专业的职业规划顾问,使用Forward Design方法帮助学生探索合适的职业路径。
|
|
|
|
| 92 |
学生背景:
|
| 93 |
- 学术背景: {bg_info}
|
| 94 |
- 工作意义: {work_value}
|
| 95 |
- 性格总结: {personality_summary}
|
| 96 |
- 理想一天: {dream_day}
|
| 97 |
+
- 学生选择方向: {forward_choice}
|
| 98 |
|
| 99 |
请输出:
|
| 100 |
1. 学生的优势、性格、价值观分析
|
| 101 |
+
2. 推荐3个具体职业,并说明日常工作内容、适配性、要求、准备路径
|
| 102 |
+
3. 最后输出职业路径图:
|
| 103 |
+
📍 现在 → ��� 学习建议 → 💼 实践建议 → 📄 认证建议 → 🚀 求职建议
|
| 104 |
"""
|
| 105 |
|
| 106 |
# ============================ 主逻辑函数 ============================
|
|
|
|
| 108 |
global current_q_index, questions
|
| 109 |
global in_forward_flow, in_backward_flow
|
| 110 |
global forward_index, backward_index
|
| 111 |
+
global forward_done, backward_done
|
| 112 |
+
global forward_recommendation_given
|
| 113 |
|
| 114 |
+
# ========== 如果是对话第一轮,重置所有状态 ==========
|
| 115 |
if not history:
|
| 116 |
current_q_index = 0
|
| 117 |
questions = base_questions[:]
|
| 118 |
for key in user_profile:
|
| 119 |
user_profile[key] = None
|
| 120 |
+
|
| 121 |
in_forward_flow = False
|
| 122 |
in_backward_flow = False
|
| 123 |
forward_index = 0
|
|
|
|
| 126 |
backward_done = False
|
| 127 |
forward_recommendation_given = False
|
| 128 |
|
| 129 |
+
# ========== 如果还在问 base_questions (目前就1个问题) ==========
|
| 130 |
+
if 0 < current_q_index <= len(questions):
|
| 131 |
+
# 把刚才的回答存入 user_profile
|
| 132 |
key = questions[current_q_index - 1][0]
|
| 133 |
user_profile[key] = message.strip()
|
| 134 |
|
| 135 |
+
# 根据回答决定 Forward / Backward
|
| 136 |
if key == "mode" and "是" in user_profile["mode"]:
|
| 137 |
+
# 如果用户回答“是”,进入Backward
|
| 138 |
questions.insert(1, ("specific_career", "请具体描述你想要从事的职业方向。"))
|
| 139 |
in_backward_flow = True
|
| 140 |
elif key == "mode" and "否" in user_profile["mode"]:
|
| 141 |
+
# 如果用户回答“否”,进入Forward
|
| 142 |
in_forward_flow = True
|
| 143 |
|
| 144 |
+
# 如果还没问完 base_questions,就继续问
|
| 145 |
if current_q_index < len(questions):
|
| 146 |
+
nxt = questions[current_q_index][1]
|
| 147 |
current_q_index += 1
|
| 148 |
+
return nxt
|
| 149 |
+
|
| 150 |
+
# ========== 判断模式 ==========
|
| 151 |
+
mode = user_profile.get("mode") or ""
|
| 152 |
|
| 153 |
+
# ========== Backward 模式逻辑 ==========
|
|
|
|
| 154 |
if "是" in mode:
|
| 155 |
if in_backward_flow and not backward_done:
|
| 156 |
+
# 如果 backward_index 还没问完
|
| 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 |
+
# Backward 问题问完
|
| 163 |
backward_done = True
|
| 164 |
|
| 165 |
+
# ========== Forward 模式逻辑 ==========
|
| 166 |
else:
|
| 167 |
if in_forward_flow and not forward_done:
|
| 168 |
+
# 1) 如果还有 Forward 问题没有问完
|
| 169 |
if forward_index < len(forward_additional_questions):
|
| 170 |
+
k, prompt_text = forward_additional_questions[forward_index]
|
| 171 |
forward_index += 1
|
| 172 |
+
return prompt_text
|
| 173 |
|
| 174 |
+
# 2) 如果已经问完 4 个 Forward问题,但还没自动推荐过方向
|
| 175 |
elif not forward_recommendation_given:
|
| 176 |
forward_recommendation_given = True
|
| 177 |
try:
|
| 178 |
api_key = os.environ.get("API_TOKEN")
|
| 179 |
if not api_key:
|
| 180 |
+
return "错误:API_TOKEN 未设置,请在环境变量中添加。"
|
| 181 |
+
|
| 182 |
client = OpenAI(api_key=api_key)
|
| 183 |
|
| 184 |
+
# 根据用户信息,让AI自动生成简短画像+3个大方向
|
| 185 |
recommendation_prompt = f"""
|
| 186 |
+
请你根据以下信息,先用不超过4句话概括学生的特点(学术背景、工作意义、性格、理想一天);
|
| 187 |
+
然后推荐3个大的职业方向(如:工程、教育、咨询、设计等)并给出简短理由;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
最后用如下格式结尾:
|
| 189 |
1. xxx
|
| 190 |
2. xxx
|
| 191 |
3. xxx
|
| 192 |
|
| 193 |
请学生从中选择一个方向进行下一步。
|
| 194 |
+
|
| 195 |
+
学术背景: {user_profile['bg_info']}
|
| 196 |
+
工作意义: {user_profile['work_value']}
|
| 197 |
+
性格总结: {user_profile['personality_summary']}
|
| 198 |
+
理想一天: {user_profile['dream_day']}
|
| 199 |
"""
|
| 200 |
|
| 201 |
+
msgs = [
|
| 202 |
+
{"role": "system", "content": "你是一位专业的职业生涯顾问,善于总结用户信息并给出大方向建议。"},
|
| 203 |
{"role": "user", "content": recommendation_prompt}
|
| 204 |
]
|
| 205 |
+
resp = client.chat.completions.create(
|
| 206 |
model=model_default,
|
| 207 |
+
messages=msgs,
|
| 208 |
max_tokens=token_default,
|
| 209 |
temperature=temp_default,
|
| 210 |
top_p=top_p_default,
|
| 211 |
stream=False
|
| 212 |
)
|
| 213 |
+
return resp.choices[0].message.content
|
| 214 |
|
| 215 |
except Exception as e:
|
| 216 |
return f"生成推荐方向时出错: {str(e)}"
|
| 217 |
|
| 218 |
+
# 3) 如果已经自动推荐过方向,那么等待用户输入方向选择
|
| 219 |
else:
|
| 220 |
user_profile["forward_direction_choice"] = message.strip()
|
| 221 |
forward_done = True
|
| 222 |
|
| 223 |
+
# ========== 当 forward_done 或 backward_done 后,进入最终生成阶段 ==========
|
| 224 |
if ("是" in mode and backward_done) or ("否" in mode and forward_done):
|
| 225 |
+
# 此时调用OpenAI做最终规划建议
|
| 226 |
try:
|
| 227 |
api_key = os.environ.get("API_TOKEN")
|
| 228 |
if not api_key:
|
| 229 |
+
return "错误:API密钥未设置,请在环境变量中添加。"
|
|
|
|
| 230 |
|
| 231 |
+
client = OpenAI(api_key=api_key)
|
| 232 |
system_prompt = generate_system_prompt()
|
| 233 |
+
|
| 234 |
+
msgs = [
|
| 235 |
{"role": "system", "content": system_prompt},
|
| 236 |
+
{"role": "user", "content": f"请根据我提供的信息,给出职业规划建议: {user_profile}. {message}"}
|
| 237 |
]
|
| 238 |
+
resp = client.chat.completions.create(
|
| 239 |
model=model_default,
|
| 240 |
+
messages=msgs,
|
| 241 |
max_tokens=token_default,
|
| 242 |
temperature=temp_default,
|
| 243 |
top_p=top_p_default,
|
| 244 |
stream=False
|
| 245 |
)
|
| 246 |
+
return resp.choices[0].message.content
|
| 247 |
|
| 248 |
except Exception as e:
|
| 249 |
return f"发生错误: {str(e)}"
|
| 250 |
|
|
|
|
| 251 |
return "信息收集完毕,若尚未得到最终回复,请输入任意文字以继续。"
|
| 252 |
|
| 253 |
# ============================ Gradio UI ============================
|
| 254 |
+
with gr.Blocks(css="""
|
| 255 |
+
body {
|
| 256 |
+
background-color: #1e1e1e;
|
| 257 |
+
color: #ffffff;
|
| 258 |
+
}
|
| 259 |
+
.gradio-container {
|
| 260 |
+
font-family: 'Segoe UI', sans-serif;
|
| 261 |
+
}
|
| 262 |
+
.message.user {
|
| 263 |
+
background-color: #cce6ff !important;
|
| 264 |
+
color: #000000 !important;
|
| 265 |
+
border-radius: 10px !important;
|
| 266 |
+
padding: 10px;
|
| 267 |
+
margin: 6px;
|
| 268 |
+
}
|
| 269 |
+
.message.bot {
|
| 270 |
+
background-color: #5599ff !important;
|
| 271 |
+
color: #000000 !important;
|
| 272 |
+
border-radius: 10px !important;
|
| 273 |
+
padding: 10px;
|
| 274 |
+
margin: 6px;
|
| 275 |
+
}
|
| 276 |
+
.gradio-container .chat-msg.bot-msg .message.bot p {
|
| 277 |
+
color: #000000 !important;
|
| 278 |
+
}
|
| 279 |
+
.gr-button {
|
| 280 |
+
border-radius: 8px;
|
| 281 |
+
}
|
| 282 |
+
#custom-send {
|
| 283 |
+
background-color: #ec4899 !important;
|
| 284 |
+
color: white !important;
|
| 285 |
+
border-radius: 999px !important;
|
| 286 |
+
padding: 10px 24px !important;
|
| 287 |
+
font-weight: bold;
|
| 288 |
+
box-shadow: 0 0 10px #ec4899;
|
| 289 |
+
transition: all 0.3s ease-in-out;
|
| 290 |
+
}
|
| 291 |
+
#custom-send:hover {
|
| 292 |
+
background-color: #d63384 !important;
|
| 293 |
+
box-shadow: 0 0 12px #ec4899;
|
| 294 |
+
}
|
| 295 |
+
textarea, input {
|
| 296 |
+
background-color: #ffe4f1 !important;
|
| 297 |
+
color: #5e2c49 !important;
|
| 298 |
+
border: 1px solid #ec4899 !important;
|
| 299 |
+
}
|
| 300 |
+
footer {
|
| 301 |
+
display: none !important;
|
| 302 |
+
}
|
| 303 |
+
""") as demo:
|
| 304 |
+
|
| 305 |
+
with gr.Row():
|
| 306 |
+
gr.HTML("""
|
| 307 |
+
<div style='display: flex; align-items: center; justify-content: center; gap: 20px; margin-bottom: 10px;'>
|
| 308 |
+
<img src='https://media3.giphy.com/media/v1.Y2lkPTc5MGI3NjExMmFucGwxbmNsd3J5NXV0Y282NXNtMzNsZW5jMm4wNWh6c2dqbXIwdiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/l41m18LjqpzxUr2WA/giphy.gif' width='200' style='border-radius: 12px; box-shadow: 0 0 10px #ec4899;'>
|
| 309 |
+
<div style='text-align: left;'>
|
| 310 |
+
<h1 style='color:white; font-size: 36px; margin-bottom: 6px;'>🎓 AI职业规划助手(升级版)</h1>
|
| 311 |
+
<p style='font-size: 18px; font-weight:bold; color:#ec4899; margin-top: 0;margin-left: 150px'>让梦想照进现实 💖</p>
|
| 312 |
+
</div>
|
| 313 |
+
</div>
|
| 314 |
+
""")
|
| 315 |
+
|
| 316 |
gr.Markdown("""
|
| 317 |
+
**📝 Forward / Backward 多轮交互:新增“自动职业方向推荐”功能**
|
| 318 |
+
|
| 319 |
+
- 若已确定方向:回答“是”(Backward)
|
| 320 |
+
- 若还在探索:回答“否”(Forward)
|
| 321 |
+
- Forward模式会在你回答完四大问题后,自动给出一个简短“学生画像”+3个大方向供你选择
|
| 322 |
""")
|
| 323 |
|
| 324 |
+
chatbot = gr.Chatbot(
|
| 325 |
+
height=500,
|
| 326 |
+
show_label=False,
|
| 327 |
+
show_copy_button=True,
|
| 328 |
+
type="messages"
|
| 329 |
+
)
|
| 330 |
+
|
| 331 |
+
with gr.Row():
|
| 332 |
+
with gr.Column(scale=8):
|
| 333 |
+
msg = gr.Textbox(
|
| 334 |
+
placeholder="请在这里输入你的回答...",
|
| 335 |
+
show_label=False,
|
| 336 |
+
container=False
|
| 337 |
+
)
|
| 338 |
+
with gr.Column(scale=1):
|
| 339 |
+
submit_btn = gr.Button("🚀 发送", elem_id="custom-send")
|
| 340 |
+
|
| 341 |
+
with gr.Row():
|
| 342 |
+
reset_btn = gr.Button("🔄 重新开始")
|
| 343 |
|
| 344 |
+
def add_message(message, history):
|
| 345 |
history = history or []
|
| 346 |
history.append({"role": "user", "content": message})
|
| 347 |
return "", history
|
| 348 |
|
| 349 |
+
def bot_response(history):
|
| 350 |
+
history = history or []
|
| 351 |
+
user_message = history[-1]["content"]
|
| 352 |
+
bot_message = predict(user_message, history[:-1] if len(history) > 1 else [])
|
| 353 |
+
history.append({"role": "assistant", "content": bot_message})
|
| 354 |
return history
|
| 355 |
|
| 356 |
+
# 点击发送按钮
|
| 357 |
+
submit_btn.click(
|
| 358 |
+
fn=add_message,
|
| 359 |
+
inputs=[msg, chatbot],
|
| 360 |
+
outputs=[msg, chatbot]
|
| 361 |
+
).then(
|
| 362 |
+
fn=bot_response,
|
| 363 |
+
inputs=[chatbot],
|
| 364 |
+
outputs=[chatbot]
|
| 365 |
)
|
| 366 |
+
|
| 367 |
+
# 回车提交
|
| 368 |
+
msg.submit(
|
| 369 |
+
fn=add_message,
|
| 370 |
+
inputs=[msg, chatbot],
|
| 371 |
+
outputs=[msg, chatbot]
|
| 372 |
+
).then(
|
| 373 |
+
fn=bot_response,
|
| 374 |
+
inputs=[chatbot],
|
| 375 |
+
outputs=[chatbot]
|
| 376 |
)
|
| 377 |
|
| 378 |
+
def reset_conversation():
|
| 379 |
global current_q_index, questions
|
| 380 |
global in_forward_flow, in_backward_flow
|
| 381 |
global forward_index, backward_index
|
| 382 |
+
global forward_done, backward_done
|
| 383 |
+
global forward_recommendation_given
|
| 384 |
|
| 385 |
current_q_index = 0
|
| 386 |
questions = base_questions[:]
|
| 387 |
for key in user_profile:
|
| 388 |
user_profile[key] = None
|
| 389 |
+
|
| 390 |
+
in_forward_flow = False
|
| 391 |
+
in_backward_flow = False
|
| 392 |
+
forward_index = 0
|
| 393 |
+
backward_index = 0
|
| 394 |
+
forward_done = False
|
| 395 |
+
backward_done = False
|
| 396 |
forward_recommendation_given = False
|
| 397 |
+
|
| 398 |
return []
|
| 399 |
|
| 400 |
+
reset_btn.click(
|
| 401 |
+
fn=reset_conversation,
|
| 402 |
+
inputs=None,
|
| 403 |
+
outputs=chatbot,
|
| 404 |
+
queue=False
|
| 405 |
+
)
|
| 406 |
|
| 407 |
+
def auto_first_question():
|
| 408 |
+
return [{"role": "assistant", "content": questions[0][1]}]
|
|
|
|
| 409 |
|
| 410 |
+
demo.load(auto_first_question, inputs=None, outputs=chatbot)
|
| 411 |
+
demo.launch()
|
| 412 |
|
|
|