IMJONEZZ commited on
Commit
aac926a
·
1 Parent(s): 0c2e095

space: transformers 5 apply_chat_template returns BatchEncoding — use return_dict + **enc into generate (fixes AttributeError on .shape)

Browse files
Files changed (1) hide show
  1. space/app.py +9 -4
space/app.py CHANGED
@@ -86,21 +86,26 @@ WARDEN_READY = not WARDEN_ERR
86
  def _generate_impl(messages, max_tokens, temperature, enable_thinking):
87
  import torch
88
 
89
- inputs = tok.apply_chat_template(
 
 
90
  messages,
91
  add_generation_prompt=True,
92
  return_tensors="pt",
 
93
  enable_thinking=enable_thinking,
94
- ).to("cuda")
 
95
  with torch.no_grad():
96
  out = model.generate(
97
- input_ids=inputs,
98
  max_new_tokens=max_tokens,
99
  do_sample=temperature > 0,
100
  temperature=max(temperature, 1e-3),
101
  top_p=0.95,
102
  )
103
- return tok.decode(out[0, inputs.shape[1]:], skip_special_tokens=True)
 
104
 
105
 
106
  # bf16 30B (~60GB) needs the 96GB xlarge slice; duration covers first-call
 
86
  def _generate_impl(messages, max_tokens, temperature, enable_thinking):
87
  import torch
88
 
89
+ # transformers 5: apply_chat_template returns a BatchEncoding (dict), not a
90
+ # bare tensor — splat it into generate() rather than passing as input_ids.
91
+ enc = tok.apply_chat_template(
92
  messages,
93
  add_generation_prompt=True,
94
  return_tensors="pt",
95
+ return_dict=True,
96
  enable_thinking=enable_thinking,
97
+ )
98
+ enc = {k: v.to("cuda") for k, v in enc.items()}
99
  with torch.no_grad():
100
  out = model.generate(
101
+ **enc,
102
  max_new_tokens=max_tokens,
103
  do_sample=temperature > 0,
104
  temperature=max(temperature, 1e-3),
105
  top_p=0.95,
106
  )
107
+ input_len = enc["input_ids"].shape[1]
108
+ return tok.decode(out[0, input_len:], skip_special_tokens=True)
109
 
110
 
111
  # bf16 30B (~60GB) needs the 96GB xlarge slice; duration covers first-call