File size: 729 Bytes
3a9fc0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"""Ask TinyGPT a question:  python generate.py "How does a while loop work?" """
import sys
import torch
from model import load_model

weights = "tinygpt.pt"          # change if your file has another name
device = "cuda" if torch.cuda.is_available() else "cpu"
model, chars = load_model(weights, device)
stoi = {c: i for i, c in enumerate(chars)}
itos = {i: c for i, c in enumerate(chars)}

question = " ".join(sys.argv[1:]) or "How does a while loop work?"
prompt = f"### Question:\n{question}\n### Answer:\n"
idx = torch.tensor([[stoi[c] for c in prompt if c in stoi]], device=device)
out = model.generate(idx, 400, temperature=0.5)[0].tolist()
text = "".join(itos[i] for i in out)[len(prompt):]
print(text.split("<END>")[0])