OzTianlu commited on
Commit
5d40ed0
·
verified ·
1 Parent(s): b2a8d36

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -13
app.py CHANGED
@@ -16,21 +16,17 @@ def respond(message, history):
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
 
@@ -43,7 +39,7 @@ def respond(message, history):
43
  generate_kwargs = dict(
44
  input_ids=input_ids,
45
  streamer=streamer,
46
- max_new_tokens=1024,
47
  temperature=0.5,
48
  top_p=0.9,
49
  do_sample=True,
@@ -57,10 +53,9 @@ def respond(message, history):
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
  )
 
16
  if model is None:
17
  model = AutoModelForCausalLM.from_pretrained(
18
  MODEL_ID,
19
+ dtype=torch.float16,
20
  trust_remote_code=True,
21
  ).to("cuda")
22
 
23
+ # 针对旧版 Gradio 结构处理:history 是 [[q1, a1], [q2, a2]]
24
  messages = [{"role": "system", "content": "You are Spartacus, a helpful assistant."}]
25
 
26
+ if history:
27
+ for user_msg, assistant_msg in history:
28
+ messages.append({"role": "user", "content": user_msg})
29
+ messages.append({"role": "assistant", "content": assistant_msg})
 
 
 
 
30
 
31
  messages.append({"role": "user", "content": message})
32
 
 
39
  generate_kwargs = dict(
40
  input_ids=input_ids,
41
  streamer=streamer,
42
+ max_new_tokens=2048,
43
  temperature=0.5,
44
  top_p=0.9,
45
  do_sample=True,
 
53
  response += token
54
  yield response
55
 
56
+ # 彻底删掉 type 参数只保留最基础的配置
57
  demo = gr.ChatInterface(
58
  fn=respond,
 
59
  title="Spartacus Chat",
60
  description="Chat with NoesisLab/Spartacus-1B-Instruct",
61
  )