Views
No views yet
1import json
2import torch
3
4from huggingface_hub import hf_hub_download
5
6# Download model files
7
8model_path = hf_hub_download(
9 repo_id="tenperformer/dungeon-master-gpt",
10 filename="best_dungeonmastergpt.pt"
11)
12
13config_path = hf_hub_download(
14 repo_id="tenperformer/dungeon-master-gpt",
15 filename="gpt_config.json"
16)
17
18# Load configuration
19
20with open(config_path, "r") as f:
21 cfg = json.load(f)
22
23config = GPTConfig(
24 vocab_size=cfg["vocab_size"],
25 block_size=cfg["block_size"],
26 n_layer=cfg["n_layer"],
27 n_head=cfg["n_head"],
28 n_embd=cfg["n_embd"],
29 dropout=cfg["dropout"]
30)
31
32# Create model
33
34device = "cuda" if torch.cuda.is_available() else "cpu"
35
36model = GPT(config).to(device)
37
38# Load weights
39
40model.load_state_dict(
41 torch.load(
42 model_path,
43 map_location=device
44 )
45)
46
47model.eval()
48
49# Generate text
50
51prompt = "The wizard entered the ancient dungeon"
52
53context = torch.tensor(
54 enc.encode(prompt),
55 dtype=torch.long
56).unsqueeze(0).to(device)
57
58output = model.generate(
59 context,
60 max_new_tokens=200
61)
62
63print(
64 enc.decode(output[0].tolist())
65)