braindeck commited on
Commit
f5b2793
·
1 Parent(s): bf33a33

Decode only generated tokens

Browse files
Files changed (1) hide show
  1. app.py +4 -2
app.py CHANGED
@@ -14,10 +14,12 @@ def generate_response(prompt):
14
  """
15
  chat = [{"role": "user", "content": prompt}]
16
  inputs = tokenizer.apply_chat_template(chat, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(model.device)
 
17
  outputs = model.generate(inputs, max_new_tokens=512, do_sample=False)
18
 
19
  # Decode the generated text
20
- generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
21
 
22
  # Remove the think block
23
  ix = generated_text.find("</think>")
@@ -47,4 +49,4 @@ with gr.Blocks() as demo:
47
  )
48
 
49
  if __name__ == "__main__":
50
- demo.launch()
 
14
  """
15
  chat = [{"role": "user", "content": prompt}]
16
  inputs = tokenizer.apply_chat_template(chat, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(model.device)
17
+ input_length = inputs.shape[1]
18
  outputs = model.generate(inputs, max_new_tokens=512, do_sample=False)
19
 
20
  # Decode the generated text
21
+ generated_tokens = outputs[0, input_length:]
22
+ generated_text = tokenizer.decode(generated_tokens, skip_special_tokens=True)
23
 
24
  # Remove the think block
25
  ix = generated_text.find("</think>")
 
49
  )
50
 
51
  if __name__ == "__main__":
52
+ demo.launch()