Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,87 +1,89 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
# 模型加载
|
| 7 |
-
#
|
| 8 |
MODEL_ID = "caobin/llm-caobin"
|
| 9 |
|
| 10 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
MODEL_ID,
|
| 13 |
-
device_map="auto",
|
| 14 |
trust_remote_code=True
|
| 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 |
-
full_prompt += f"<|user|>{message}<|assistant|>"
|
| 49 |
-
|
| 50 |
-
# tokenizer -> tensor
|
| 51 |
-
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
| 52 |
-
|
| 53 |
-
# 生成回答
|
| 54 |
-
output_ids = model.generate(
|
| 55 |
-
**inputs,
|
| 56 |
-
max_new_tokens=128,
|
| 57 |
-
temperature=0.5,
|
| 58 |
-
top_p=0.7,
|
| 59 |
-
do_sample=True,
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if "<|assistant|>" in output_text:
|
| 64 |
output_text = output_text.split("<|assistant|>")[-1]
|
|
|
|
| 65 |
return output_text.strip()
|
| 66 |
|
| 67 |
-
|
|
|
|
| 68 |
# Gradio UI
|
| 69 |
-
#
|
| 70 |
with gr.Blocks(title="caobin LLM Chatbot") as demo:
|
| 71 |
-
gr.Markdown("# 🤖 caobin's AI
|
|
|
|
| 72 |
chatbot = gr.Chatbot(height=450)
|
| 73 |
-
msg = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
def respond(message, chat_history):
|
| 76 |
response = chat_fn(message, chat_history)
|
| 77 |
-
|
| 78 |
-
chat_history.append({"role": "user", "content": message})
|
| 79 |
-
chat_history.append({"role": "assistant", "content": response})
|
| 80 |
return "", chat_history
|
| 81 |
|
| 82 |
-
msg.submit(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
#
|
| 85 |
# 启动
|
| 86 |
-
#
|
| 87 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
+
# ===============================
|
| 6 |
# 模型加载
|
| 7 |
+
# ===============================
|
| 8 |
MODEL_ID = "caobin/llm-caobin"
|
| 9 |
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 11 |
+
MODEL_ID,
|
| 12 |
+
trust_remote_code=True
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
MODEL_ID,
|
| 17 |
+
device_map="auto",
|
| 18 |
trust_remote_code=True
|
| 19 |
)
|
| 20 |
|
| 21 |
+
model.eval()
|
| 22 |
+
|
| 23 |
+
# ===============================
|
| 24 |
+
# 核心聊天逻辑(使用 tuple history)
|
| 25 |
+
# ===============================
|
| 26 |
+
def chat_fn(message, history):
|
| 27 |
"""
|
| 28 |
+
history: List[Tuple[user, assistant]]
|
| 29 |
"""
|
| 30 |
+
# 只保留最近 3 轮
|
| 31 |
+
history = history[-3:]
|
| 32 |
+
|
| 33 |
+
prompt = ""
|
| 34 |
+
for user, assistant in history:
|
| 35 |
+
prompt += f"<|user|>{user}<|assistant|>{assistant}"
|
| 36 |
+
|
| 37 |
+
prompt += f"<|user|>{message}<|assistant|>"
|
| 38 |
+
|
| 39 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 40 |
+
|
| 41 |
+
with torch.no_grad():
|
| 42 |
+
output_ids = model.generate(
|
| 43 |
+
**inputs,
|
| 44 |
+
max_new_tokens=128,
|
| 45 |
+
temperature=0.5,
|
| 46 |
+
top_p=0.7,
|
| 47 |
+
do_sample=True,
|
| 48 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
output_text = tokenizer.decode(
|
| 52 |
+
output_ids[0],
|
| 53 |
+
skip_special_tokens=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# 只取 assistant 的新回答
|
| 57 |
if "<|assistant|>" in output_text:
|
| 58 |
output_text = output_text.split("<|assistant|>")[-1]
|
| 59 |
+
|
| 60 |
return output_text.strip()
|
| 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(height=450)
|
| 70 |
+
msg = gr.Textbox(
|
| 71 |
+
label="输入你的问题",
|
| 72 |
+
placeholder="请输入你的问题,支持多轮对话"
|
| 73 |
+
)
|
| 74 |
|
| 75 |
def respond(message, chat_history):
|
| 76 |
response = chat_fn(message, chat_history)
|
| 77 |
+
chat_history.append((message, response))
|
|
|
|
|
|
|
| 78 |
return "", chat_history
|
| 79 |
|
| 80 |
+
msg.submit(
|
| 81 |
+
respond,
|
| 82 |
+
inputs=[msg, chatbot],
|
| 83 |
+
outputs=[msg, chatbot]
|
| 84 |
+
)
|
| 85 |
|
| 86 |
+
# ===============================
|
| 87 |
# 启动
|
| 88 |
+
# ===============================
|
| 89 |
demo.launch()
|