Spaces:
Sleeping
Sleeping
Enhance text generation parameters in chatbot application by setting max_length to 100 and temperature to 0.7. Simplify response function by directly passing the message to the text generator, improving clarity and efficiency in message processing.
Browse files
app.py
CHANGED
|
@@ -22,7 +22,9 @@ text_generator = pipeline(
|
|
| 22 |
model=model,
|
| 23 |
tokenizer=tokenizer,
|
| 24 |
device=device,
|
|
|
|
| 25 |
do_sample=True,
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
model.eval()
|
|
@@ -30,79 +32,16 @@ print(f"Model đã sẵn sàng! Device: {device}")
|
|
| 30 |
|
| 31 |
|
| 32 |
def respond(
|
| 33 |
-
message
|
| 34 |
-
history: list[dict[str, str]],
|
| 35 |
-
system_message=None,
|
| 36 |
-
max_tokens=None,
|
| 37 |
-
temperature=None,
|
| 38 |
-
top_p=None,
|
| 39 |
):
|
| 40 |
"""
|
| 41 |
Tạo phản hồi từ model coding assistant sử dụng pipeline
|
| 42 |
"""
|
| 43 |
-
|
| 44 |
-
if max_tokens is None:
|
| 45 |
-
max_tokens = 250
|
| 46 |
-
if temperature is None:
|
| 47 |
-
temperature = 0.7
|
| 48 |
-
if top_p is None:
|
| 49 |
-
top_p = 0.95
|
| 50 |
-
|
| 51 |
-
# Xử lý message - có thể là dict hoặc string
|
| 52 |
-
if isinstance(message, dict):
|
| 53 |
-
user_message = message.get("content", "")
|
| 54 |
-
else:
|
| 55 |
-
user_message = str(message) if message else ""
|
| 56 |
-
|
| 57 |
-
# Chuẩn bị messages từ history và message hiện tại
|
| 58 |
-
messages = []
|
| 59 |
-
|
| 60 |
-
# Thêm system message - mặc định bằng tiếng Việt nếu không có
|
| 61 |
-
# if system_message:
|
| 62 |
-
# messages.append({"role": "system", "content": system_message})
|
| 63 |
-
# else:
|
| 64 |
-
# # System message mặc định bằng tiếng Việt để model trả lời bằng tiếng Việt
|
| 65 |
-
# messages.append({
|
| 66 |
-
# "role": "system",
|
| 67 |
-
# "content": "Bạn là một trợ lý lập trình hữu ích. 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. Hãy trả lời bằng tiếng Việt. "
|
| 68 |
-
# })
|
| 69 |
-
|
| 70 |
-
# Thêm history (đã có format đúng)
|
| 71 |
-
# messages.extend(history)
|
| 72 |
-
|
| 73 |
-
# Thêm message hiện tại từ user
|
| 74 |
-
if user_message:
|
| 75 |
-
messages.append({"role": "user", "content": user_message})
|
| 76 |
-
|
| 77 |
-
# Format messages với chat template
|
| 78 |
-
prompt = tokenizer.apply_chat_template(
|
| 79 |
-
messages,
|
| 80 |
-
tokenize=False,
|
| 81 |
-
add_generation_prompt=True
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
# Thêm instruction rõ ràng để model trả lời bằng tiếng Việt
|
| 85 |
-
# (nếu model vẫn có xu hướng trả lời tiếng Anh)
|
| 86 |
-
prompt += "\n\nLưu ý: Hãy trả lời bằng tiếng Việt."
|
| 87 |
-
|
| 88 |
-
# Sử dụng pipeline để generate text - chỉ dùng max_new_tokens
|
| 89 |
-
generated = text_generator(
|
| 90 |
-
prompt,
|
| 91 |
-
max_new_tokens=max_tokens,
|
| 92 |
-
num_return_sequences=1,
|
| 93 |
-
temperature=temperature,
|
| 94 |
-
top_p=top_p,
|
| 95 |
-
do_sample=True,
|
| 96 |
-
truncation=True,
|
| 97 |
-
)
|
| 98 |
|
| 99 |
# Lấy câu trả lời từ kết quả
|
| 100 |
answer = generated[0]['generated_text']
|
| 101 |
|
| 102 |
-
# Loại bỏ prompt ban đầu để chỉ lấy phần response
|
| 103 |
-
if prompt in answer:
|
| 104 |
-
answer = answer.replace(prompt, "").strip()
|
| 105 |
-
|
| 106 |
# Đảm bảo trả về string không rỗng
|
| 107 |
if not answer or len(answer.strip()) == 0:
|
| 108 |
answer = "Xin lỗi, tôi không thể tạo phản hồi."
|
|
|
|
| 22 |
model=model,
|
| 23 |
tokenizer=tokenizer,
|
| 24 |
device=device,
|
| 25 |
+
max_length=100,
|
| 26 |
do_sample=True,
|
| 27 |
+
temperature=0.7
|
| 28 |
)
|
| 29 |
|
| 30 |
model.eval()
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
def respond(
|
| 35 |
+
message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
):
|
| 37 |
"""
|
| 38 |
Tạo phản hồi từ model coding assistant sử dụng pipeline
|
| 39 |
"""
|
| 40 |
+
generated = text_generator(message, num_return_sequences=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Lấy câu trả lời từ kết quả
|
| 43 |
answer = generated[0]['generated_text']
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
# Đảm bảo trả về string không rỗng
|
| 46 |
if not answer or len(answer.strip()) == 0:
|
| 47 |
answer = "Xin lỗi, tôi không thể tạo phản hồi."
|