OzTianlu commited on
Commit
b2a8d36
·
verified ·
1 Parent(s): 190ebf5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -8
app.py CHANGED
@@ -6,7 +6,7 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStream
6
 
7
  MODEL_ID = "NoesisLab/Spartacus-1B-Instruct"
8
 
9
- # Tokenizer 静态加载
10
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
11
  model = None
12
 
@@ -16,15 +16,21 @@ def respond(message, history):
16
  if model is None:
17
  model = AutoModelForCausalLM.from_pretrained(
18
  MODEL_ID,
19
- torch_dtype=torch.float16,
20
  trust_remote_code=True,
21
  ).to("cuda")
22
 
23
- # 适配旧版 Gradio 的 history 格式: [[user, assistant], [user, assistant]]
24
  messages = [{"role": "system", "content": "You are Spartacus, a helpful assistant."}]
25
- for user_msg, assistant_msg in history:
26
- messages.append({"role": "user", "content": user_msg})
27
- messages.append({"role": "assistant", "content": assistant_msg})
 
 
 
 
 
 
28
 
29
  messages.append({"role": "user", "content": message})
30
 
@@ -49,11 +55,12 @@ def respond(message, history):
49
  response = ""
50
  for token in streamer:
51
  response += token
52
- yield response # 旧版 ChatInterface 需要 yield 完整的累计字符串
53
 
54
- # 移除 type="messages" 参数
55
  demo = gr.ChatInterface(
56
  fn=respond,
 
57
  title="Spartacus Chat",
58
  description="Chat with NoesisLab/Spartacus-1B-Instruct",
59
  )
 
6
 
7
  MODEL_ID = "NoesisLab/Spartacus-1B-Instruct"
8
 
9
+ # 静态加载 Tokenizer
10
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
11
  model = None
12
 
 
16
  if model is None:
17
  model = AutoModelForCausalLM.from_pretrained(
18
  MODEL_ID,
19
+ dtype=torch.float16, # 修复弃用警告:torch_dtype -> dtype
20
  trust_remote_code=True,
21
  ).to("cuda")
22
 
23
+ # 更加鲁棒的 history 处理逻辑
24
  messages = [{"role": "system", "content": "You are Spartacus, a helpful assistant."}]
25
+
26
+ for entry in history:
27
+ # 自动兼容 字典格式 {'role': '...', 'content': '...'}
28
+ if isinstance(entry, dict):
29
+ messages.append(entry)
30
+ # 自动兼容 元组/列表格式 [user, assistant]
31
+ elif isinstance(entry, (list, tuple)) and len(entry) == 2:
32
+ messages.append({"role": "user", "content": entry[0]})
33
+ messages.append({"role": "assistant", "content": entry[1]})
34
 
35
  messages.append({"role": "user", "content": message})
36
 
 
55
  response = ""
56
  for token in streamer:
57
  response += token
58
+ yield response
59
 
60
+ # 强制指定 type="tuples" 来适配上面的 history 处理,避免 unpacked 错误
61
  demo = gr.ChatInterface(
62
  fn=respond,
63
+ type="tuples", # 显式声明,确保 entry 是 [user, bot] 结构
64
  title="Spartacus Chat",
65
  description="Chat with NoesisLab/Spartacus-1B-Instruct",
66
  )