Spaces:
Sleeping
Sleeping
File size: 3,741 Bytes
da4d1f9 cf9addc 76de232 cf9addc f1fc130 cf9addc 76de232 0f06294 76de232 f1fc130 cf9addc 76de232 cf9addc f1fc130 da4d1f9 b0d2e27 676f57c da4d1f9 76de232 da4d1f9 676f57c 62a0bb6 676f57c b0d2e27 26e874e b0d2e27 26e874e b0d2e27 da4d1f9 b0d2e27 cf9addc da4d1f9 cf9addc 26e874e 8ea8e25 26e874e db09be5 76de232 db09be5 76de232 f1fc130 76de232 62a0bb6 cf9addc 76de232 62a0bb6 cf9addc b0d2e27 62a0bb6 da4d1f9 cf9addc da4d1f9 cf9addc b0d2e27 da4d1f9 cf9addc da4d1f9 | 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 | import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
# Load tokenizer và model
print("Đang tải model...")
model_name = "cochi1706/codingassistant"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Xác định device cho pipeline (0 cho cuda, -1 cho cpu)
device = 0 if torch.cuda.is_available() else -1
# Set padding token nếu chưa có
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
# Tạo pipeline để sinh text
text_generator = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
device=device,
do_sample=True,
)
model.eval()
print(f"Model đã sẵn sàng! Device: {device}")
def respond(
message,
history: list[dict[str, str]],
system_message=None,
max_tokens=None,
temperature=None,
top_p=None,
):
"""
Tạo phản hồi từ model coding assistant sử dụng pipeline
"""
# Đặt giá trị mặc định nếu không có
if max_tokens is None:
max_tokens = 250
if temperature is None:
temperature = 0.7
if top_p is None:
top_p = 0.95
# Xử lý message - có thể là dict hoặc string
if isinstance(message, dict):
user_message = message.get("content", "")
else:
user_message = str(message) if message else ""
# Chuẩn bị messages từ history và message hiện tại
messages = []
# Thêm system message - mặc định bằng tiếng Việt nếu không có
if system_message:
messages.append({"role": "system", "content": system_message})
else:
# System message mặc định bằng tiếng Việt để model trả lời bằng tiếng Việt
messages.append({
"role": "system",
"content": "Bạn là một trợ lý lập trình hữu ích. Hãy trả lời bằng tiếng Việt. Cung cấp các giải pháp code rõ ràng, ngắn gọn và chính xác cùng với giải thích."
})
# Thêm history (đã có format đúng)
messages.extend(history)
# Thêm message hiện tại từ user
if user_message:
messages.append({"role": "user", "content": user_message})
# Format messages với chat template
prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
# Thêm instruction rõ ràng để model trả lời bằng tiếng Việt
# (nếu model vẫn có xu hướng trả lời tiếng Anh)
prompt += "\n\nLưu ý: Hãy trả lời bằng tiếng Việt."
# Sử dụng pipeline để generate text - chỉ dùng max_new_tokens
generated = text_generator(
prompt,
max_new_tokens=max_tokens,
num_return_sequences=1,
temperature=temperature,
top_p=top_p,
do_sample=True,
truncation=True,
)
# Lấy câu trả lời từ kết quả
answer = generated[0]['generated_text']
# Loại bỏ prompt ban đầu để chỉ lấy phần response
if prompt in answer:
answer = answer.replace(prompt, "").strip()
# Đảm bảo trả về string không rỗng
if not answer or len(answer.strip()) == 0:
answer = "Xin lỗi, tôi không thể tạo phản hồi."
return answer
"""
Chatbot hỗ trợ lập trình sử dụng Qwen3 fine-tuned model
"""
chatbot = gr.ChatInterface(
respond,
title="🤖 Coding Assistant",
description="Chatbot hỗ trợ lập trình",
type="messages"
)
demo = chatbot
if __name__ == "__main__":
demo.launch()
|