-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_interactive.py
More file actions
194 lines (156 loc) · 5.39 KB
/
Copy pathgenerate_interactive.py
File metadata and controls
194 lines (156 loc) · 5.39 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
193
194
"""
Interactive Text Generation
Easy interface for generating text from your trained model
"""
import os
import torch
from gpt_from_scratch.model import GPT
def print_header(text):
"""Print a fancy header"""
print(f"\n{'='*70}")
print(f"{text.center(70)}")
print(f"{'='*70}\n")
def interactive_generate():
"""Interactive generation interface"""
print_header("GPT TEXT GENERATION")
# Check for checkpoint
if not os.path.exists('out/ckpt.pt'):
print("✗ No trained model found!")
print("Train a model first with: python gpt.py train")
return
# Load model
print("Loading model...")
try:
checkpoint = torch.load('out/ckpt.pt', map_location='cpu')
model_config = checkpoint['model_config']
model = GPT(model_config)
model.load_state_dict(checkpoint['model'])
model.eval()
model.to('cpu')
# Load vocabulary
vocab = checkpoint['vocab']
stoi = vocab['stoi']
itos = vocab['itos']
print(f"✓ Model loaded! Parameters: {sum(p.numel() for p in model.parameters())/1e6:.2f}M")
print(f"✓ Vocabulary size: {len(itos)}")
except (FileNotFoundError, KeyError, RuntimeError) as e:
print(f"✗ Failed to load model: {e}")
if isinstance(e, FileNotFoundError):
print(" - The model checkpoint file was not found")
elif isinstance(e, KeyError):
print(" - The checkpoint file is missing required keys")
elif 'UnicodeDecodeError' in str(e):
print(" - The checkpoint file is corrupted or in an unexpected format")
return
# Generation loop
while True:
print("\n" + "-"*70)
print("TEXT GENERATION OPTIONS")
print("-"*70)
# Get prompt
print("\nEnter your prompt (or press Enter for random start):")
prompt = input("> ").strip()
# Get parameters
print("\nGeneration parameters:")
try:
max_tokens = int(input("Max tokens to generate [200]: ").strip() or "200")
temperature = float(input("Temperature (0.1-2.0) [0.8]: ").strip() or "0.8")
top_k = int(input("Top-k filtering [200]: ").strip() or "200")
except ValueError:
print("Invalid input, using defaults")
max_tokens = 200
temperature = 0.8
top_k = 200
# Encode prompt
if prompt:
# The .get(c, 0) handles characters not in the vocabulary
context = torch.tensor(
[stoi.get(c, 0) for c in prompt],
dtype=torch.long,
device='cpu'
).unsqueeze(0)
else:
# Random start
context = torch.zeros((1, 1), dtype=torch.long, device='cpu')
# Generate
print("\nGenerating...\n")
print("="*70)
try:
with torch.no_grad():
generated = model.generate(
context,
max_new_tokens=max_tokens,
temperature=temperature,
top_k=top_k
)
# Decode
generated_text = ''.join([itos[int(i)] for i in generated[0]])
print(generated_text)
except Exception as e:
print(f"✗ Generation failed: {e}")
print("="*70)
# Continue?
again = input("\nGenerate more? (y/n) [y]: ").strip().lower()
if again == 'n':
break
print("\n✓ Generation complete!")
def batch_generate():
"""Generate multiple samples at once"""
print_header("BATCH TEXT GENERATION")
if not os.path.exists('out/ckpt.pt'):
print("✗ No trained model found!")
return
# Load model
print("Loading model...")
checkpoint = torch.load('out/ckpt.pt', map_location='cpu')
model_config = checkpoint['model_config']
model = GPT(model_config)
model.load_state_dict(checkpoint['model'])
model.eval()
model.to('cpu')
vocab = checkpoint['vocab']
stoi = vocab['stoi']
itos = vocab['itos']
print("✓ Model loaded!")
# Get prompts
print("\nEnter prompts (one per line, empty line to finish):")
prompts = []
while True:
prompt = input("> ").strip()
if not prompt:
break
prompts.append(prompt)
if not prompts:
print("No prompts entered")
return
# Generation parameters
max_tokens = int(input("\nMax tokens per sample [100]: ").strip() or "100")
temperature = float(input("Temperature [0.8]: ").strip() or "0.8")
# Generate for each prompt
print("\n" + "="*70)
for i, prompt in enumerate(prompts, 1):
print(f"\nSample {i}/{len(prompts)}")
print(f"Prompt: '{prompt}'")
print("-"*70)
# Encode
context = torch.tensor(
[stoi.get(c, 0) for c in prompt],
dtype=torch.long,
device='cpu'
).unsqueeze(0)
# Generate
with torch.no_grad():
generated = model.generate(
context,
max_new_tokens=max_tokens,
temperature=temperature,
top_k=200
)
# Decode and print
text = ''.join([itos[int(i)] for i in generated[0]])
print(text)
print()
print("="*70)
print(f"✓ Generated {len(prompts)} samples!")
if __name__ == '__main__':
interactive_generate()