Spaces:
Sleeping
Sleeping
| 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() |