Spaces:
Sleeping
Sleeping
File size: 10,903 Bytes
07cb905 1f05e99 e29e7eb 07cb905 6dcb856 07cb905 6dcb856 07cb905 6dcb856 07cb905 6dcb856 07cb905 6dcb856 07cb905 6dcb856 07cb905 6dcb856 07cb905 6dcb856 07cb905 6dcb856 07cb905 6dcb856 07cb905 6dcb856 07cb905 6dcb856 fda6b9a 07cb905 e29e7eb 07cb905 e29e7eb 07cb905 e29e7eb 07cb905 e29e7eb 07cb905 e29e7eb 07cb905 fda6b9a 07cb905 fda6b9a 07cb905 e29e7eb 07cb905 9c03c46 07cb905 fda6b9a 07cb905 6dcb856 07cb905 9c03c46 07cb905 ed3f256 07cb905 e29e7eb 07cb905 e29e7eb 07cb905 1f05e99 7d7edd5 9c03c46 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | import requests
import gradio as gr
MBTI_QUESTIONS = [
{
"question": "Bạn thích làm việc theo cách nào hơn?",
"options": ["Một mình (I)", "Trong nhóm (E)"],
"dimension": "IE"
},
{
"question": "Khi tiếp nhận thông tin, bạn thiên về:",
"options": ["Các sự kiện và chi tiết cụ thể (S)", "Ý tưởng và khái niệm trừu tượng (N)"],
"dimension": "SN"
},
{
"question": "Khi đưa ra quyết định, bạn dựa vào:",
"options": ["Logic và phân tích khách quan (T)", "Cảm xúc và giá trị cá nhân (F)"],
"dimension": "TF"
},
{
"question": "Trong cuộc sống, bạn thích:",
"options": ["Lập kế hoạch và tổ chức (J)", "Linh hoạt và thích nghi (P)"],
"dimension": "JP"
}
]
def validate_api_key(api_key):
if not api_key:
return "Vui lòng nhập API Key"
return None
def determine_mbti(*answers):
if any(answer is None for answer in answers):
return "Vui lòng trả lời tất cả các câu hỏi"
scores = {
"E": 0, "I": 0, "S": 0, "N": 0,
"T": 0, "F": 0, "J": 0, "P": 0
}
for i, answer in enumerate(answers):
if answer:
dimension = MBTI_QUESTIONS[i]["dimension"]
if dimension == "IE":
scores["E"] += 1 if answer == "B" else 0
scores["I"] += 1 if answer == "A" else 0
elif dimension == "SN":
scores["S"] += 1 if answer == "A" else 0
scores["N"] += 1 if answer == "B" else 0
elif dimension == "TF":
scores["T"] += 1 if answer == "A" else 0
scores["F"] += 1 if answer == "B" else 0
elif dimension == "JP":
scores["J"] += 1 if answer == "A" else 0
scores["P"] += 1 if answer == "B" else 0
mbti_type = ""
mbti_type += "E" if scores["E"] > scores["I"] else "I"
mbti_type += "S" if scores["S"] > scores["N"] else "N"
mbti_type += "T" if scores["T"] > scores["F"] else "F"
mbti_type += "J" if scores["J"] > scores["P"] else "P"
return mbti_type
def generate_career_advice(api_key, name, age, mbti_type, interests):
if not all([api_key, name, age, mbti_type, interests]):
return "Vui lòng điền đầy đủ thông tin (API Key, tên, tuổi, MBTI, lĩnh vực quan tâm)"
API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
prompt = f"""
Tôi tên là {name}, năm nay {age} tuổi, có kiểu tính cách MBTI là {mbti_type} và quan tâm đến lĩnh vực {interests}.
Hãy giúp tôi định hướng nghề nghiệp phù hợp với tôi và xây dựng lộ trình phát triển nghề nghiệp. Lưu ý, bạn trả lời theo cấu trúc sau:
1. Ưu điểm và nhược điểm của {mbti_type}
2. Nghề nghiệp liên quan đến lĩnh vực {interests}
3. Điểm mạnh và điểm yếu của {mbti_type} trong lĩnh vực {interests}
4. Xây dựng lộ trình phát triển nghề nghiệp trong lĩnh vực {interests}.
Lưu ý 1: Bạn phải xây dựng nội dung phù hợp với {age} tuổi, {mbti_type} và {interests}
Lưu ý 2: Bạn phải trả lời bằng Tiếng Việt
"""
payload = {
"contents": [{"parts": [{"text": prompt}]}]
}
headers = {"Content-Type": "application/json"}
try:
response = requests.post(API_URL + "?key=" + api_key, json=payload, headers=headers)
response.raise_for_status()
data = response.json()
advice = data['candidates'][0]['content']['parts'][0]['text']
return format_advice(advice, name, mbti_type, interests)
except Exception as e:
return f"Xin lỗi, có lỗi xảy ra: {str(e)}"
def format_advice(advice, name, mbti_type, interests):
formatted_advice = f"## Định hướng nghề nghiệp và Lộ trình phát triển cho {name} ({mbti_type} - {interests})\n\n"
sections = advice.split("\n\n")
for section in sections:
if "**" in section:
formatted_advice += f"### {section.strip('* ')}\n\n"
else:
formatted_advice += f"{section}\n\n"
return formatted_advice
def chat_with_ai(message, chat_history, api_key, name, mbti_type, interests):
if not message.strip():
return "", chat_history
if not all([api_key, name, mbti_type, interests]):
chat_history.append((message, "Vui lòng điền đầy đủ thông tin (API Key, thông tin cá nhân và xác định MBTI) trước khi chat"))
return "", chat_history
API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
if not chat_history:
chat_history = []
context = f"Tôi là {name}, có kiểu tính cách MBTI là {mbti_type} và quan tâm đến lĩnh vực {interests}. "
full_message = context + message
payload = {
"contents": [{"parts": [{"text": full_message}]}]
}
headers = {"Content-Type": "application/json"}
try:
response = requests.post(API_URL + "?key=" + api_key, json=payload, headers=headers)
response.raise_for_status()
data = response.json()
ai_response = data['candidates'][0]['content']['parts'][0]['text']
chat_history.append((message, ai_response))
except Exception as e:
chat_history.append((message, f"Xin lỗi, có lỗi xảy ra: {str(e)}"))
return "", chat_history
def main():
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown(
"""
# 🎯 CareerCompass-AI: Hệ Thống Tư Vấn Nghề Nghiệp Dựa Trên MBTI
### 📝 Hướng dẫn sử dụng:
1. **Lấy API Key:**
- Truy cập [Google AI Studio](https://aistudio.google.com/app/u/1/apikey?pli=1)
- Đăng nhập và tạo API Key mới
- Copy API Key và dán vào ô bên dưới
2. **Các bước sử dụng:**
- Nhập API Key từ Google AI Studio
- Điền thông tin cá nhân
- Trả lời bộ câu hỏi MBTI
- Nhấn "Xác định MBTI" để xem kết quả tính cách
- Nhấn "Nhận Tư Vấn Nghề Nghiệp" để nhận phân tích chi tiết
- Sử dụng chat để đặt câu hỏi thêm
"""
)
# API Key input
with gr.Row():
api_key_input = gr.Textbox(
label="API Key",
placeholder="Nhập API Key từ Google AI Studio",
type="password"
)
with gr.Row():
# Cột thông tin cá nhân
with gr.Column(scale=1):
with gr.Group():
gr.Markdown("### 📋 Thông Tin Cá Nhân")
name_input = gr.Textbox(
label="Họ và tên",
placeholder="Nhập họ tên của bạn"
)
age_input = gr.Number(
label="Tuổi",
minimum=1,
maximum=100,
value=18
)
interests_input = gr.Textbox(
label="Lĩnh vực quan tâm",
placeholder="VD: Công nghệ thông tin, Marketing,..."
)
# Cột MBTI
with gr.Column(scale=1):
with gr.Group():
gr.Markdown("### 🎭 Xác Định MBTI")
answers = []
for question_info in MBTI_QUESTIONS:
gr.Markdown(f"**{question_info['question']}**")
answer = gr.Radio(
choices=["A", "B"],
label=" / ".join(question_info['options']),
interactive=True
)
answers.append(answer)
mbti_button = gr.Button("🎯 Xác định MBTI", variant="primary")
mbti_result = gr.Textbox(
label="Kết quả MBTI của bạn",
placeholder="Nhấn nút 'Xác định MBTI' sau khi trả lời các câu hỏi"
)
with gr.Row():
loading_text = gr.Markdown(visible=False, value="🔄 Đang phân tích và tạo tư vấn nghề nghiệp... Vui lòng đợi trong giây lát...")
generate_button = gr.Button(
"🚀 Nhận Tư Vấn Nghề Nghiệp",
variant="primary",
scale=2
)
with gr.Row():
career_advice_output = gr.Markdown(label="Kết quả phân tích")
gr.Markdown("### 💬 Chat với AI Tư Vấn")
with gr.Row():
with gr.Column(scale=3):
chatbot = gr.Chatbot(
label="Lịch sử chat",
height=400
)
msg = gr.Textbox(
label="Nhập câu hỏi của bạn",
placeholder="VD: Tôi nên học thêm kỹ năng gì để phát triển trong ngành này?",
show_label=True
)
chat_button = gr.Button("🗨️ Gửi")
mbti_button.click(
fn=determine_mbti,
inputs=answers,
outputs=mbti_result
)
def show_loading():
return gr.update(visible=True)
def hide_loading():
return gr.update(visible=False)
generate_button.click(
fn=show_loading,
outputs=loading_text,
queue=False
).then(
fn=generate_career_advice,
inputs=[api_key_input, name_input, age_input, mbti_result, interests_input],
outputs=career_advice_output
).then(
fn=hide_loading,
outputs=loading_text
)
chat_button.click(
fn=chat_with_ai,
inputs=[msg, chatbot, api_key_input, name_input, mbti_result, interests_input],
outputs=[msg, chatbot]
)
msg.submit(
fn=chat_with_ai,
inputs=[msg, chatbot, api_key_input, name_input, mbti_result, interests_input],
outputs=[msg, chatbot]
)
demo.launch()
if __name__ == "__main__":
main() |