Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,22 +21,29 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 21 |
model.eval()
|
| 22 |
|
| 23 |
# ===============================
|
| 24 |
-
#
|
| 25 |
# ===============================
|
| 26 |
def chat_fn(message, history):
|
| 27 |
"""
|
| 28 |
-
history: List[
|
| 29 |
"""
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
prompt = ""
|
| 34 |
-
for
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
|
|
|
| 37 |
prompt += f"<|user|>{message}<|assistant|>"
|
| 38 |
|
| 39 |
-
inputs = tokenizer(
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
with torch.no_grad():
|
| 42 |
output_ids = model.generate(
|
|
@@ -53,7 +60,7 @@ def chat_fn(message, history):
|
|
| 53 |
skip_special_tokens=True
|
| 54 |
)
|
| 55 |
|
| 56 |
-
# 只取 assistant
|
| 57 |
if "<|assistant|>" in output_text:
|
| 58 |
output_text = output_text.split("<|assistant|>")[-1]
|
| 59 |
|
|
@@ -61,20 +68,36 @@ def chat_fn(message, history):
|
|
| 61 |
|
| 62 |
|
| 63 |
# ===============================
|
| 64 |
-
# Gradio UI
|
| 65 |
# ===============================
|
| 66 |
with gr.Blocks(title="caobin LLM Chatbot") as demo:
|
| 67 |
gr.Markdown("# 🤖 caobin's AI Assistant")
|
| 68 |
|
| 69 |
-
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
msg = gr.Textbox(
|
| 71 |
label="输入你的问题",
|
| 72 |
placeholder="请输入你的问题,支持多轮对话"
|
| 73 |
)
|
| 74 |
|
| 75 |
def respond(message, chat_history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
response = chat_fn(message, chat_history)
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
return "", chat_history
|
| 79 |
|
| 80 |
msg.submit(
|
|
|
|
| 21 |
model.eval()
|
| 22 |
|
| 23 |
# ===============================
|
| 24 |
+
# 聊天核心(messages schema)
|
| 25 |
# ===============================
|
| 26 |
def chat_fn(message, history):
|
| 27 |
"""
|
| 28 |
+
history: List[Dict] with keys: role, content
|
| 29 |
"""
|
| 30 |
+
|
| 31 |
+
# 只保留最近 3 轮(6 条 message)
|
| 32 |
+
history = history[-6:]
|
| 33 |
|
| 34 |
prompt = ""
|
| 35 |
+
for msg in history:
|
| 36 |
+
role = msg["role"]
|
| 37 |
+
content = msg["content"]
|
| 38 |
+
prompt += f"<|{role}|>{content}"
|
| 39 |
|
| 40 |
+
# 当前用户问题
|
| 41 |
prompt += f"<|user|>{message}<|assistant|>"
|
| 42 |
|
| 43 |
+
inputs = tokenizer(
|
| 44 |
+
prompt,
|
| 45 |
+
return_tensors="pt"
|
| 46 |
+
).to(model.device)
|
| 47 |
|
| 48 |
with torch.no_grad():
|
| 49 |
output_ids = model.generate(
|
|
|
|
| 60 |
skip_special_tokens=True
|
| 61 |
)
|
| 62 |
|
| 63 |
+
# 只取 assistant 新生成的部分
|
| 64 |
if "<|assistant|>" in output_text:
|
| 65 |
output_text = output_text.split("<|assistant|>")[-1]
|
| 66 |
|
|
|
|
| 68 |
|
| 69 |
|
| 70 |
# ===============================
|
| 71 |
+
# Gradio UI(messages 模式)
|
| 72 |
# ===============================
|
| 73 |
with gr.Blocks(title="caobin LLM Chatbot") as demo:
|
| 74 |
gr.Markdown("# 🤖 caobin's AI Assistant")
|
| 75 |
|
| 76 |
+
chatbot = gr.Chatbot(
|
| 77 |
+
type="messages",
|
| 78 |
+
height=450
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
msg = gr.Textbox(
|
| 82 |
label="输入你的问题",
|
| 83 |
placeholder="请输入你的问题,支持多轮对话"
|
| 84 |
)
|
| 85 |
|
| 86 |
def respond(message, chat_history):
|
| 87 |
+
# 用户消息
|
| 88 |
+
chat_history.append({
|
| 89 |
+
"role": "user",
|
| 90 |
+
"content": message
|
| 91 |
+
})
|
| 92 |
+
|
| 93 |
+
# 模型回复
|
| 94 |
response = chat_fn(message, chat_history)
|
| 95 |
+
|
| 96 |
+
chat_history.append({
|
| 97 |
+
"role": "assistant",
|
| 98 |
+
"content": response
|
| 99 |
+
})
|
| 100 |
+
|
| 101 |
return "", chat_history
|
| 102 |
|
| 103 |
msg.submit(
|