Upload test_model.py
Browse files- test_model.py +85 -0
test_model.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Quick test: load palindrome model, generate samples for multiple themes."""
|
| 2 |
+
import re
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
+
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
print(f"Device: {device}")
|
| 8 |
+
|
| 9 |
+
def _normalize(text):
|
| 10 |
+
return re.sub(r'[^a-zA-Z0-9]', '', text).lower()
|
| 11 |
+
|
| 12 |
+
def is_palindrome(text):
|
| 13 |
+
cleaned = _normalize(text)
|
| 14 |
+
return cleaned == cleaned[::-1]
|
| 15 |
+
|
| 16 |
+
# Test V1 (completed)
|
| 17 |
+
model_id = "SantiagoC/palindrome-grpo"
|
| 18 |
+
print(f"\nLoading {model_id}...")
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 20 |
+
if tokenizer.pad_token is None:
|
| 21 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 22 |
+
|
| 23 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
+
model_id,
|
| 25 |
+
torch_dtype=torch.float16,
|
| 26 |
+
device_map="auto",
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
themes = [
|
| 30 |
+
"cars", "nature", "food", "love",
|
| 31 |
+
"space", "animals", "music", "destiny",
|
| 32 |
+
"technology", "water",
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
print(f"\nModel: {model_id}")
|
| 36 |
+
print("=" * 70)
|
| 37 |
+
for theme in themes:
|
| 38 |
+
messages = [
|
| 39 |
+
{"role": "system", "content": "You generate palindromes. A palindrome reads the same forwards and backwards after removing non-alphanumeric characters and lowercasing."},
|
| 40 |
+
{"role": "user", "content": f"Here are some examples of palindromes:\n- 'A Toyota' (about cars)\n- 'No lemon, no melon' (about food)\n- 'Was it a car or a cat I saw?' (about animals)\n\nNow: Create a phrase that reads the same forwards and backwards, about {theme}."},
|
| 41 |
+
]
|
| 42 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 43 |
+
inputs = tokenizer(text, return_tensors="pt").to(device)
|
| 44 |
+
|
| 45 |
+
with torch.no_grad():
|
| 46 |
+
outputs = model.generate(
|
| 47 |
+
**inputs,
|
| 48 |
+
max_new_tokens=80,
|
| 49 |
+
temperature=0.9,
|
| 50 |
+
do_sample=True,
|
| 51 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
completion = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
|
| 55 |
+
pal = "YES" if is_palindrome(completion) else "NO "
|
| 56 |
+
cleaned = _normalize(completion)
|
| 57 |
+
print(f"\nTheme: {theme:15s} | palindrome: {pal} | chars: {len(cleaned):3d}")
|
| 58 |
+
print(f" → {completion[:150]}")
|
| 59 |
+
|
| 60 |
+
# Also test V5 if available
|
| 61 |
+
try:
|
| 62 |
+
model_id_v5 = "SantiagoC/palindrome-grpo-v5"
|
| 63 |
+
tokenizer_v5 = AutoTokenizer.from_pretrained(model_id_v5)
|
| 64 |
+
model_v5 = AutoModelForCausalLM.from_pretrained(model_id_v5, torch_dtype=torch.float16, device_map="auto")
|
| 65 |
+
if tokenizer_v5.pad_token is None:
|
| 66 |
+
tokenizer_v5.pad_token = tokenizer_v5.eos_token
|
| 67 |
+
|
| 68 |
+
print(f"\n\nModel: {model_id_v5}")
|
| 69 |
+
print("=" * 70)
|
| 70 |
+
for theme in themes[:5]:
|
| 71 |
+
messages = [
|
| 72 |
+
{"role": "system", "content": "You generate palindromes. A palindrome reads the same forwards and backwards after removing non-alphanumeric characters and lowercasing."},
|
| 73 |
+
{"role": "user", "content": f"Here are some examples of palindromes:\n- 'A Toyota' (about cars)\n- 'No lemon, no melon' (about food)\n- 'Was it a car or a cat I saw?' (about animals)\n\nNow: Create a phrase that reads the same forwards and backwards, about {theme}."},
|
| 74 |
+
]
|
| 75 |
+
text = tokenizer_v5.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 76 |
+
inputs = tokenizer_v5(text, return_tensors="pt").to(device)
|
| 77 |
+
with torch.no_grad():
|
| 78 |
+
outputs = model_v5.generate(**inputs, max_new_tokens=80, temperature=0.9, do_sample=True, pad_token_id=tokenizer_v5.pad_token_id)
|
| 79 |
+
completion = tokenizer_v5.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
|
| 80 |
+
pal = "YES" if is_palindrome(completion) else "NO "
|
| 81 |
+
cleaned = _normalize(completion)
|
| 82 |
+
print(f"\nTheme: {theme:15s} | palindrome: {pal} | chars: {len(cleaned):3d}")
|
| 83 |
+
print(f" → {completion[:150]}")
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"\nV5 model not available yet: {e}")
|