Skip to content

Commit 5110055

Browse files
committed
Add Ctrl-C handling to gracefully exit training early
- Wrap trainer.train() in try-catch to handle KeyboardInterrupt - Allow users to stop training and proceed to text generation demo - Add clear instruction about Ctrl-C functionality for better UX - Ensure text generation always runs regardless of training completion
1 parent bdbfaf8 commit 5110055

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

examples/simple.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,13 @@ def main():
141141
)
142142

143143
print("\nStarting training...")
144-
trainer.train(data=data_source, tokenizer=tokenizer)
144+
print("(Press Ctrl-C to stop training early and see text generation)")
145+
146+
try:
147+
trainer.train(data=data_source, tokenizer=tokenizer)
148+
print("\nTraining completed successfully!")
149+
except KeyboardInterrupt:
150+
print("\n\nTraining interrupted by user. Moving to text generation with current model state...")
145151

146152
# Step 6: Simple text generation demo
147153
print("\nTesting text generation:")
@@ -162,7 +168,7 @@ def main():
162168
result = tokenizer.decode(generated[0].tolist())
163169
print(f"Generated: {result}")
164170

165-
print("\nTraining complete! All temporary files automatically cleaned up.")
171+
print("\nAll temporary files automatically cleaned up.")
166172
print("Run the script again to start fresh.")
167173

168174

0 commit comments

Comments
 (0)