-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgenerate.py
More file actions
192 lines (149 loc) · 6.01 KB
/
Copy pathgenerate.py
File metadata and controls
192 lines (149 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""
Text Generation Script for Shakespeare GPT
This script loads a trained model and generates Shakespeare-like text.
Key Concepts:
1. Loading a trained model from checkpoint
2. Autoregressive generation - predicting one character at a time
3. Temperature - controls randomness (higher = more creative, lower = more predictable)
"""
import torch
from model import GPT
from dataset import download_shakespeare, CharacterTokenizer, DATA_PATH
# ==============================================================================
# Load Model
# ==============================================================================
def load_model(checkpoint_path: str = "checkpoints/model.pt"):
"""
Load a trained GPT model from checkpoint.
Args:
checkpoint_path: Path to the saved checkpoint file
Returns:
model: The trained GPT model (ready for generation)
tokenizer: The character tokenizer (to convert text <-> numbers)
device: The device the model is on ('mps', 'cuda', or 'cpu')
"""
# 1. Set up device (use GPU if available)
device = "mps" if torch.backends.mps.is_available() else "cpu"
print(f"Using device: {device}")
# 2. Load the checkpoint file
print(f"Loading checkpoint from {checkpoint_path}...")
checkpoint = torch.load(checkpoint_path, map_location=device, weights_only=False)
# 3. Get the model configuration that was saved during training
config = checkpoint['config']
# 4. Create the tokenizer (we need the same vocabulary as training)
download_shakespeare()
with open(DATA_PATH, 'r', encoding='utf-8') as file:
text = file.read()
tokenizer = CharacterTokenizer(text)
# 5. Create the model with the saved configuration
model = GPT(
vocab_size=config['vocab_size'],
embedding_dim=config['embedding_dim'],
num_heads=config['num_heads'],
num_layers=config['num_layers'],
block_size=config['block_size'],
dropout=0.0 # No dropout during generation
)
# 6. Load the trained weights into the model
model.load_state_dict(checkpoint['model_state_dict'])
# 7. Move model to device and set to evaluation mode
model = model.to(device)
model.eval()
# 8. Print info
print(f"Model loaded!")
print(f" Validation loss: {checkpoint['val_loss']:.4f}")
print(f" Training iterations: {checkpoint['iteration']}")
return model, tokenizer, device
# ==============================================================================
# Generate Text
# ==============================================================================
@torch.no_grad()
def generate(model, tokenizer, device, prompt: str, max_tokens: int = 500, temperature: float = 0.8):
"""
Generate text given a starting prompt.
-------------------------------------------------------------------------
HOW TEXT GENERATION WORKS (Autoregressive)
-------------------------------------------------------------------------
The model generates one character at a time:
1. Start with prompt: "ROMEO:"
2. Model predicts next char: "ROMEO:" → 'O'
3. Append and repeat: "ROMEO:O" → ' '
4. Continue: "ROMEO:O " → 'w'
5. And so on... "ROMEO:O w" → 'h'
This continues until we reach max_tokens.
TEMPERATURE controls randomness:
- Low (0.5): Very predictable, "safe" choices
- Medium (0.8): Balanced creativity and coherence (default)
- High (1.5): Very creative but may be nonsensical
-------------------------------------------------------------------------
"""
# 1. Convert prompt text to token IDs
prompt_ids = tokenizer.encode(prompt)
input_ids = torch.tensor(prompt_ids, dtype=torch.long, device=device)
input_ids = input_ids.unsqueeze(0) # Add batch dimension: shape becomes (1, seq_len)
# 2. Generate new tokens using the model's generate method
output_ids = model.generate(
input_ids=input_ids,
max_new_tokens=max_tokens,
temperature=temperature
)
# 3. Convert token IDs back to text
generated_text = tokenizer.decode(output_ids[0])
return generated_text
# ==============================================================================
# Main - Run Generation
# ==============================================================================
if __name__ == "__main__":
# 1. Load the trained model
print("=" * 60)
print("Shakespeare GPT - Text Generation")
print("=" * 60)
model, tokenizer, device = load_model("checkpoints/model.pt")
# 2. Define some example prompts to try
prompts = [
"ROMEO:",
"To be, or not to be",
"JULIET:\nO Romeo, ",
]
# 3. Generate text for each prompt
print("\n" + "=" * 60)
print("Generating Text...")
print("=" * 60)
for prompt in prompts:
print(f"\n--- Prompt: {repr(prompt)} ---\n")
generated_text = generate(
model=model,
tokenizer=tokenizer,
device=device,
prompt=prompt,
max_tokens=300,
temperature=0.8
)
print(generated_text)
print("\n" + "-" * 60)
# 4. Interactive: Let user try their own prompts
print("\n" + "=" * 60)
print("Try Your Own Prompts!")
print("=" * 60)
print("Enter a prompt to generate text, or 'quit' to exit.")
print("Tip: Try character names like 'HAMLET:', 'KING:', 'First Citizen:'")
while True:
try:
prompt = input("\nYour prompt: ")
if prompt.lower() in ['quit', 'exit', 'q']:
print("Farewell!")
break
if not prompt:
continue
generated_text = generate(
model=model,
tokenizer=tokenizer,
device=device,
prompt=prompt,
max_tokens=500,
temperature=0.8
)
print("\n" + generated_text)
except KeyboardInterrupt:
print("\n\nFarewell!")
break