File size: 715 Bytes
f0cf42d
 
1403f74
 
f0cf42d
 
1403f74
f0cf42d
1403f74
 
 
 
 
f0cf42d
1403f74
 
 
 
f0cf42d
 
1403f74
 
 
 
 
 
f0cf42d
1403f74
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from pathlib import Path

from transformers import AutoModelForCausalLM, AutoTokenizer

MODEL_DIR = Path(__file__).resolve().parent

model = AutoModelForCausalLM.from_pretrained(
    MODEL_DIR,
    torch_dtype="auto",
    device_map="auto",
    trust_remote_code=True,
)
tokenizer = AutoTokenizer.from_pretrained(
    MODEL_DIR,
    trust_remote_code=True,
)

# Code completion example
prompt = '''"""Complete the fibonacci function in Python."""
def fibonacci(n: int) -> int:
'''

inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
    **inputs,
    max_new_tokens=256,
    temperature=0.5,
    do_sample=True,
)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))