caobin commited on
Commit
e620cc1
·
verified ·
1 Parent(s): 3a8e995

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -14
app.py CHANGED
@@ -2,44 +2,39 @@ import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
 
5
- # 模型 ID
6
  MODEL_ID = "caobin/llm-caobin"
7
 
8
- # 加载 tokenizer 和模型
9
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
10
  model = AutoModelForCausalLM.from_pretrained(
11
  MODEL_ID,
12
- device_map="auto", # CPU 上会自动映射到 CPU
13
  trust_remote_code=True
14
  )
15
 
16
- # 聊天函数
17
  def chat_fn(message, history):
18
  # 只保留最近 3 轮历史
19
- history = history[-3:]
20
  full_prompt = ""
21
- for user_msg, bot_msg in history:
22
- full_prompt += f"<|user|>{user_msg}<|assistant|>{bot_msg}"
 
 
 
23
  full_prompt += f"<|user|>{message}<|assistant|>"
24
 
25
- # tokenizer 转 tensor
26
  inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
27
-
28
- # 生成回答
29
  output_ids = model.generate(
30
  **inputs,
31
- max_new_tokens=256,
32
  temperature=0.7,
33
  top_p=0.9,
34
  do_sample=True,
35
  )
36
-
37
  output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
38
  if "<|assistant|>" in output_text:
39
  output_text = output_text.split("<|assistant|>")[-1]
40
  return output_text.strip()
41
 
42
- # Gradio UI
43
  with gr.Blocks(title="caobin LLM Chatbot") as demo:
44
  gr.Markdown("# 🤖 caobin's AI assistant")
45
  chatbot = gr.Chatbot(height=450)
@@ -47,7 +42,9 @@ with gr.Blocks(title="caobin LLM Chatbot") as demo:
47
 
48
  def respond(message, chat_history):
49
  response = chat_fn(message, chat_history)
50
- chat_history.append((message, response))
 
 
51
  return "", chat_history
52
 
53
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
@@ -57,3 +54,4 @@ demo.launch()
57
 
58
 
59
 
 
 
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
 
 
5
  MODEL_ID = "caobin/llm-caobin"
6
 
 
7
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
8
  model = AutoModelForCausalLM.from_pretrained(
9
  MODEL_ID,
10
+ device_map="auto", # CPU 上也可以
11
  trust_remote_code=True
12
  )
13
 
 
14
  def chat_fn(message, history):
15
  # 只保留最近 3 轮历史
16
+ recent_history = history[-6:] # 3轮对话,每轮2条消息
17
  full_prompt = ""
18
+ for msg in recent_history:
19
+ if msg["role"] == "user":
20
+ full_prompt += f"<|user|>{msg['content']}<|assistant|>"
21
+ elif msg["role"] == "assistant":
22
+ full_prompt += msg['content']
23
  full_prompt += f"<|user|>{message}<|assistant|>"
24
 
 
25
  inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
 
 
26
  output_ids = model.generate(
27
  **inputs,
28
+ max_new_tokens=256,
29
  temperature=0.7,
30
  top_p=0.9,
31
  do_sample=True,
32
  )
 
33
  output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
34
  if "<|assistant|>" in output_text:
35
  output_text = output_text.split("<|assistant|>")[-1]
36
  return output_text.strip()
37
 
 
38
  with gr.Blocks(title="caobin LLM Chatbot") as demo:
39
  gr.Markdown("# 🤖 caobin's AI assistant")
40
  chatbot = gr.Chatbot(height=450)
 
42
 
43
  def respond(message, chat_history):
44
  response = chat_fn(message, chat_history)
45
+ # 用字典格式添加消息
46
+ chat_history.append({"role": "user", "content": message})
47
+ chat_history.append({"role": "assistant", "content": response})
48
  return "", chat_history
49
 
50
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
 
54
 
55
 
56
 
57
+