Views
No views yet
tokenizers)model.py for the architecture, config.json for hyperparameters,
and tokenizer.json for the BPE tokenizer.1import json, torch
2from tokenizers import Tokenizer
3from huggingface_hub import hf_hub_download
4from model import GPTLanguageModel
5
6repo = "achavan1211/shakespeare-gpt-bpe"
7
8config = json.load(open(hf_hub_download(repo, "config.json")))
9tokenizer = Tokenizer.from_file(hf_hub_download(repo, "tokenizer.json"))
10
11encode = lambda s: tokenizer.encode(s).ids
12decode = lambda l: tokenizer.decode(l)
13
14model_cfg = {k: v for k, v in config.items() if k != "tokenizer"}
15model = GPTLanguageModel(**model_cfg)
16model.load_state_dict(torch.load(hf_hub_download(repo, "pytorch_model.bin"), map_location="cpu"))
17model.eval()
18
19context = torch.zeros((1, 1), dtype=torch.long)
20print(decode(model.generate(context, max_new_tokens=500)[0].tolist()))