Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3tokenizer = AutoTokenizer.from_pretrained('nikitharao/catlm', use_fast = False)
4model = AutoModelForCausalLM.from_pretrained('nikitharao/catlm')
5
6prompt = """
7def add(x,y):
8 \"\"\"Add two numbers x and y\"\"\"
9 return x+y
10<|codetestpair|>
11"""
12
13print('Input prompt:')
14print(prompt)
15
16input_ids = tokenizer(prompt, return_tensors="pt").input_ids
17
18# The model was trained without the `</s>` token and should be removed.
19if tokenizer.decode(input_ids[0,-1]) == '</s>':
20 input_ids = input_ids[:,:-1]
21
22print(input_ids)
23len_input = input_ids.shape[1]
24
25sample_output = model.generate(
26 input_ids,
27 do_sample=True,
28 max_new_tokens = 512,
29 top_k=50,
30 top_p=0.95,
31 temperature=0.2
32)
33generated_output = sample_output[0][len_input:]
34output = tokenizer.decode(generated_output, skip_special_tokens=True)
35print('Output:')
36print(output)</s> token and should be removed.