Views
No views yet
model.py for the architecture, config.json for hyperparameters, and vocab.json for the character-level tokenizer mappings.1import json, torch
2from huggingface_hub import hf_hub_download
3from model import GPTLanguageModel
4
5
6config = json.load(open(hf_hub_download('achavan1211/shakespeare-gpt', 'config.json')))
7vocab = json.load(open(hf_hub_download('achavan1211/shakespeare-gpt', 'vocab.json')))
8stoi, itos = vocab['stoi'], {int(k): v for k, v in vocab['itos'].items()}
9encode = lambda s: [stoi[c] for c in s]
10decode = lambda l: ''.join(itos[i] for i in l)
11
12model = GPTLanguageModel(**config)
13model.load_state_dict(torch.load(hf_hub_download('achavan1211/shakespeare-gpt', 'pytorch_model.bin'), map_location='cpu'))
14model.eval()
15
16# Generate text
17context = torch.zeros((1, 1), dtype=torch.long)
18print(decode(model.generate(context, max_new_tokens=500)[0].tolist()))