Spaces:
Runtime error
Runtime error
| import torch | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| import gradio as gr | |
| import json | |
| # Load model từ model repo HuggingFace đã fine-tune | |
| model_path = "memengoc/chatgpt3.5nn" # ✅ Sửa lại đúng repo của bạn | |
| # Load model | |
| model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto", torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32) | |
| model.eval() | |
| # Tokenizer | |
| tokenizer = AutoTokenizer.from_pretrained(model_path) | |
| tokenizer.pad_token = tokenizer.eos_token | |
| # Tạo dữ liệu ngành + câu hỏi (ví dụ đơn giản) | |
| data_nganh = { | |
| "CNTT": ["Bạn có kinh nghiệm với Python không?", "Bạn từng làm việc nhóm như thế nào?"], | |
| "Marketing": ["Bạn biết gì về thương hiệu?", "Kể về một chiến dịch bạn từng tham gia?"] | |
| } | |
| def gpt_interview(nganh, user_input, chat_history): | |
| if nganh not in data_nganh: | |
| return "Ngành không hợp lệ.", chat_history | |
| if not chat_history: | |
| prompt = f"<|system|>\nBạn đang phỏng vấn ứng viên cho ngành: {nganh}\n<|user|>\n{user_input}" | |
| else: | |
| prompt = f"<|system|>\nBạn đang phỏng vấn ứng viên cho ngành: {nganh}\n" | |
| for u, a in chat_history: | |
| prompt += f"<|user|>\n{u}\n<|assistant|>\n{a}\n" | |
| prompt += f"<|user|>\n{user_input}" | |
| inputs = tokenizer(prompt, return_tensors="pt").to(model.device) | |
| outputs = model.generate(**inputs, max_new_tokens=256, temperature=0.7) | |
| reply = tokenizer.decode(outputs[0], skip_special_tokens=True).split("<|assistant|>")[-1].strip() | |
| chat_history.append((user_input, reply)) | |
| return "", chat_history | |
| def suggest_first(nganh): | |
| first_q = data_nganh[nganh][0] if nganh in data_nganh else "Bạn có thể giới thiệu bản thân?" | |
| return "", [(first_q, "")], [(first_q, "")] | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 🤖 Chatbot GPT-3.5 phỏng vấn theo ngành nghề") | |
| major = gr.Dropdown(choices=list(data_nganh.keys()), label="Chọn ngành") | |
| chatbot = gr.Chatbot(label="Lịch sử hội thoại") | |
| txt = gr.Textbox(label="Trả lời câu hỏi") | |
| state = gr.State([]) | |
| major.change(suggest_first, inputs=major, outputs=[txt, chatbot, state]) | |
| txt.submit(gpt_interview, inputs=[major, txt, state], outputs=[txt, chatbot]) | |
| gr.Button("🧹 Xóa").click(lambda: ("", []), None, [txt, chatbot, state]) | |
| demo.launch() | |