caobin commited on
Commit
4e7b745
·
verified ·
1 Parent(s): 7a68b64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -53
app.py CHANGED
@@ -2,9 +2,6 @@ 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(
@@ -20,30 +17,19 @@ model = AutoModelForCausalLM.from_pretrained(
20
 
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,53 +46,23 @@ def chat_fn(message, history):
60
  skip_special_tokens=True
61
  )
62
 
63
- # 只取 assistant 新生成的部分
64
  if "<|assistant|>" in output_text:
65
  output_text = output_text.split("<|assistant|>")[-1]
66
 
67
  return output_text.strip()
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(
104
- respond,
105
- inputs=[msg, chatbot],
106
- outputs=[msg, chatbot]
107
- )
108
 
109
- # ===============================
110
- # 启动
111
- # ===============================
112
  demo.launch()
 
2
  import torch
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
 
 
 
 
5
  MODEL_ID = "caobin/llm-caobin"
6
 
7
  tokenizer = AutoTokenizer.from_pretrained(
 
17
 
18
  model.eval()
19
 
 
 
 
20
  def chat_fn(message, history):
21
  """
22
+ history: List[Tuple[user, assistant]]
23
  """
24
+ history = history[-3:]
 
 
25
 
26
  prompt = ""
27
+ for user, assistant in history:
28
+ prompt += f"<|user|>{user}<|assistant|>{assistant}"
 
 
29
 
 
30
  prompt += f"<|user|>{message}<|assistant|>"
31
 
32
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
 
 
 
33
 
34
  with torch.no_grad():
35
  output_ids = model.generate(
 
46
  skip_special_tokens=True
47
  )
48
 
 
49
  if "<|assistant|>" in output_text:
50
  output_text = output_text.split("<|assistant|>")[-1]
51
 
52
  return output_text.strip()
53
 
54
 
 
 
 
55
  with gr.Blocks(title="caobin LLM Chatbot") as demo:
56
  gr.Markdown("# 🤖 caobin's AI Assistant")
57
 
58
+ chatbot = gr.Chatbot(height=450)
59
+ msg = gr.Textbox(label="输入你的问题")
 
 
 
 
 
 
 
60
 
61
  def respond(message, chat_history):
 
 
 
 
 
 
 
62
  response = chat_fn(message, chat_history)
63
+ chat_history.append((message, response))
 
 
 
 
 
64
  return "", chat_history
65
 
66
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
 
 
 
 
67
 
 
 
 
68
  demo.launch()