File size: 547 Bytes
d38b7b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import torch
from transformers import GPT2Tokenizer
from gpt2 import GPT,GPTConfig
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")


model=GPT.from_pretrained('nanoGPT2-124m.pth')
model=model.to(device)
prompt = "Hello, I'm a mathematics teacher"
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
generated_ids=model.generate(input_ids.to(device))
print(tokenizer.decode(generated_ids))