File size: 24,355 Bytes
486eff6 |
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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 |
#!/usr/bin/env python3
"""
Test Script: Thematic Word Generation + LLM Clue Generation
Integrates the existing thematic_word_generator.py with the new llm_clue_generator.py
to create a complete word-to-clue pipeline for crossword puzzles.
Tests various scenarios:
- Single topics
- Multiple topics
- Custom sentences
- Different difficulties
- Performance analysis
"""
import os
import sys
import time
import logging
from typing import List, Dict, Tuple, Any
from pathlib import Path
# Add hack directory to path for imports
sys.path.insert(0, str(Path(__file__).parent))
try:
from thematic_word_generator import UnifiedThematicWordGenerator
from llm_clue_generator import LLMClueGenerator
GENERATORS_AVAILABLE = True
except ImportError as e:
print(f"β Import error: {e}")
print("Make sure thematic_word_generator.py and llm_clue_generator.py are in the same directory")
GENERATORS_AVAILABLE = False
# Set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s:%(lineno)d - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)
class CrosswordClueTestSuite:
"""
Test suite for integrated thematic word generation + LLM clue generation.
"""
def __init__(self, vocab_size_limit: int = 50000):
"""Initialize the test suite.
Args:
vocab_size_limit: Vocabulary size for thematic generator (smaller for faster testing)
"""
if not GENERATORS_AVAILABLE:
raise ImportError("Required generators not available")
self.vocab_size_limit = vocab_size_limit
self.word_generator = None
self.clue_generator = None
# Test results storage
self.test_results = {}
self.performance_stats = {}
def initialize(self):
"""Initialize both generators."""
print("π Initializing Crossword Clue Test Suite")
print("=" * 60)
# Initialize thematic word generator
print("\nπ Initializing thematic word generator...")
start_time = time.time()
self.word_generator = UnifiedThematicWordGenerator(
vocab_size_limit=self.vocab_size_limit
)
self.word_generator.initialize()
word_gen_time = time.time() - start_time
print(f"β
Word generator ready in {word_gen_time:.2f}s")
# Initialize LLM clue generator
print("\nπ Initializing LLM clue generator...")
start_time = time.time()
self.clue_generator = LLMClueGenerator()
self.clue_generator.initialize()
clue_gen_time = time.time() - start_time
print(f"β
Clue generator ready in {clue_gen_time:.2f}s")
# Store initialization stats
self.performance_stats['word_gen_init_time'] = word_gen_time
self.performance_stats['clue_gen_init_time'] = clue_gen_time
self.performance_stats['total_init_time'] = word_gen_time + clue_gen_time
print(f"\nβ
Test suite initialized in {word_gen_time + clue_gen_time:.2f}s")
def run_single_topic_test(self, topic: str, num_words: int = 10) -> Dict[str, Any]:
"""Test single topic word+clue generation.
Args:
topic: Single topic string
num_words: Number of words to generate
Returns:
Test results dictionary
"""
print(f"\nπ― Single Topic Test: '{topic}'")
print("-" * 50)
start_time = time.time()
# Step 1: Generate thematic words
print(f"π Generating {num_words} thematic words...")
word_start_time = time.time()
thematic_results = self.word_generator.generate_thematic_words(
inputs=topic,
num_words=num_words,
min_similarity=0.3
)
word_gen_time = time.time() - word_start_time
print(f"β
Generated {len(thematic_results)} words in {word_gen_time:.2f}s")
if not thematic_results:
return {"error": "No thematic words generated"}
# Step 2: Generate clues for each word
print(f"π Generating clues for {len(thematic_results)} words...")
clue_start_time = time.time()
word_clue_pairs = []
for word, similarity, tier in thematic_results:
try:
clue = self.clue_generator.generate_clue(
word=word,
topic=topic,
clue_style="category",
difficulty="medium"
)
word_clue_pairs.append({
"word": word.upper(),
"clue": clue,
"similarity": similarity,
"tier": tier,
"length": len(word)
})
except Exception as e:
logger.error(f"Failed to generate clue for '{word}': {e}")
word_clue_pairs.append({
"word": word.upper(),
"clue": f"Related to {topic}: {word}", # Fallback
"similarity": similarity,
"tier": tier,
"length": len(word),
"error": str(e)
})
clue_gen_time = time.time() - clue_start_time
total_time = time.time() - start_time
# Display results
print(f"β
Generated {len(word_clue_pairs)} clues in {clue_gen_time:.2f}s")
print(f"\nπ Results for topic '{topic}':")
print("=" * 60)
for i, item in enumerate(word_clue_pairs, 1):
tier_desc = self.word_generator.tier_descriptions.get(item['tier'], item['tier'])
error_marker = " β οΈ" if 'error' in item else ""
print(f"{i:2d}. {item['word']:<12} ({item['length']} letters) - {item['clue']}{error_marker}")
print(f" Similarity: {item['similarity']:.3f} | {tier_desc}")
# Performance summary
print(f"\nβ±οΈ Performance:")
print(f" Word generation: {word_gen_time:.2f}s")
print(f" Clue generation: {clue_gen_time:.2f}s ({clue_gen_time/len(word_clue_pairs):.2f}s per clue)")
print(f" Total time: {total_time:.2f}s")
return {
"topic": topic,
"num_words_requested": num_words,
"num_words_generated": len(word_clue_pairs),
"word_clue_pairs": word_clue_pairs,
"performance": {
"word_gen_time": word_gen_time,
"clue_gen_time": clue_gen_time,
"total_time": total_time,
"avg_clue_time": clue_gen_time / len(word_clue_pairs) if word_clue_pairs else 0
}
}
def run_multi_topic_test(self, topics: List[str], num_words: int = 12) -> Dict[str, Any]:
"""Test multi-topic word+clue generation.
Args:
topics: List of topic strings
num_words: Number of words to generate
Returns:
Test results dictionary
"""
print(f"\nπ― Multi-Topic Test: {topics}")
print("-" * 50)
start_time = time.time()
# Step 1: Generate thematic words (multi-theme enabled)
print(f"π Generating {num_words} multi-thematic words...")
word_start_time = time.time()
thematic_results = self.word_generator.generate_thematic_words(
inputs=topics,
num_words=num_words,
min_similarity=0.25, # Lower threshold for multi-topic
multi_theme=True # Enable multi-theme processing
)
word_gen_time = time.time() - word_start_time
print(f"β
Generated {len(thematic_results)} words in {word_gen_time:.2f}s")
if not thematic_results:
return {"error": "No thematic words generated"}
# Step 2: Generate contextual clues
print(f"π Generating contextual clues...")
clue_start_time = time.time()
# Create topic context string for clue generation
topic_context = " and ".join(topics)
word_clue_pairs = []
for word, similarity, tier in thematic_results:
try:
clue = self.clue_generator.generate_clue(
word=word,
topic=topic_context,
clue_style="description", # Use descriptive style for multi-topic
difficulty="medium"
)
word_clue_pairs.append({
"word": word.upper(),
"clue": clue,
"similarity": similarity,
"tier": tier,
"length": len(word)
})
except Exception as e:
logger.error(f"Failed to generate clue for '{word}': {e}")
word_clue_pairs.append({
"word": word.upper(),
"clue": f"Related to {topic_context}: {word}",
"similarity": similarity,
"tier": tier,
"length": len(word),
"error": str(e)
})
clue_gen_time = time.time() - clue_start_time
total_time = time.time() - start_time
# Display results
print(f"β
Generated {len(word_clue_pairs)} clues in {clue_gen_time:.2f}s")
print(f"\nπ Results for topics {topics}:")
print("=" * 70)
for i, item in enumerate(word_clue_pairs, 1):
tier_desc = self.word_generator.tier_descriptions.get(item['tier'], item['tier'])
error_marker = " β οΈ" if 'error' in item else ""
print(f"{i:2d}. {item['word']:<12} ({item['length']} letters) - {item['clue']}{error_marker}")
print(f" Similarity: {item['similarity']:.3f} | {tier_desc}")
# Performance summary
print(f"\nβ±οΈ Performance:")
print(f" Word generation: {word_gen_time:.2f}s")
print(f" Clue generation: {clue_gen_time:.2f}s ({clue_gen_time/len(word_clue_pairs):.2f}s per clue)")
print(f" Total time: {total_time:.2f}s")
return {
"topics": topics,
"num_words_requested": num_words,
"num_words_generated": len(word_clue_pairs),
"word_clue_pairs": word_clue_pairs,
"performance": {
"word_gen_time": word_gen_time,
"clue_gen_time": clue_gen_time,
"total_time": total_time,
"avg_clue_time": clue_gen_time / len(word_clue_pairs) if word_clue_pairs else 0
}
}
def run_custom_sentence_test(self, sentence: str, num_words: int = 10) -> Dict[str, Any]:
"""Test custom sentence word+clue generation.
Args:
sentence: Custom sentence input
num_words: Number of words to generate
Returns:
Test results dictionary
"""
print(f"\nπ― Custom Sentence Test: '{sentence}'")
print("-" * 60)
start_time = time.time()
# Step 1: Generate thematic words from sentence
print(f"π Generating {num_words} words from sentence...")
word_start_time = time.time()
thematic_results = self.word_generator.generate_thematic_words(
inputs=sentence,
num_words=num_words,
min_similarity=0.2 # Lower threshold for sentences
)
word_gen_time = time.time() - word_start_time
print(f"β
Generated {len(thematic_results)} words in {word_gen_time:.2f}s")
if not thematic_results:
return {"error": "No thematic words generated"}
# Step 2: Generate personalized clues
print(f"π Generating personalized clues...")
clue_start_time = time.time()
word_clue_pairs = []
for word, similarity, tier in thematic_results:
try:
# Use the original sentence as context for more personalized clues
clue = self.clue_generator.generate_clue(
word=word,
topic=f"theme: {sentence}",
clue_style="description",
difficulty="medium"
)
word_clue_pairs.append({
"word": word.upper(),
"clue": clue,
"similarity": similarity,
"tier": tier,
"length": len(word)
})
except Exception as e:
logger.error(f"Failed to generate clue for '{word}': {e}")
word_clue_pairs.append({
"word": word.upper(),
"clue": f"From '{sentence[:30]}...': {word}",
"similarity": similarity,
"tier": tier,
"length": len(word),
"error": str(e)
})
clue_gen_time = time.time() - clue_start_time
total_time = time.time() - start_time
# Display results
print(f"β
Generated {len(word_clue_pairs)} clues in {clue_gen_time:.2f}s")
print(f"\nπ Results for sentence: '{sentence}'")
print("=" * 70)
for i, item in enumerate(word_clue_pairs, 1):
tier_desc = self.word_generator.tier_descriptions.get(item['tier'], item['tier'])
error_marker = " β οΈ" if 'error' in item else ""
print(f"{i:2d}. {item['word']:<12} ({item['length']} letters) - {item['clue']}{error_marker}")
print(f" Similarity: {item['similarity']:.3f} | {tier_desc}")
# Performance summary
print(f"\nβ±οΈ Performance:")
print(f" Word generation: {word_gen_time:.2f}s")
print(f" Clue generation: {clue_gen_time:.2f}s ({clue_gen_time/len(word_clue_pairs):.2f}s per clue)")
print(f" Total time: {total_time:.2f}s")
return {
"sentence": sentence,
"num_words_requested": num_words,
"num_words_generated": len(word_clue_pairs),
"word_clue_pairs": word_clue_pairs,
"performance": {
"word_gen_time": word_gen_time,
"clue_gen_time": clue_gen_time,
"total_time": total_time,
"avg_clue_time": clue_gen_time / len(word_clue_pairs) if word_clue_pairs else 0
}
}
def run_difficulty_comparison_test(self, topic: str, num_words: int = 6) -> Dict[str, Any]:
"""Test different difficulty levels for the same topic.
Args:
topic: Topic to test
num_words: Number of words to generate
Returns:
Comparison results
"""
print(f"\nπ― Difficulty Comparison Test: '{topic}'")
print("-" * 50)
difficulties = ["easy", "medium", "hard"]
results = {}
# Generate words once (reuse for all difficulties)
thematic_results = self.word_generator.generate_thematic_words(
inputs=topic,
num_words=num_words,
min_similarity=0.3
)[:num_words] # Take only requested number
if not thematic_results:
return {"error": "No thematic words generated"}
print(f"π Testing {len(thematic_results)} words at different difficulty levels...")
for difficulty in difficulties:
print(f"\n--- {difficulty.upper()} Difficulty ---")
clue_pairs = []
start_time = time.time()
for word, similarity, tier in thematic_results:
try:
clue = self.clue_generator.generate_clue(
word=word,
topic=topic,
clue_style="category",
difficulty=difficulty
)
clue_pairs.append({
"word": word.upper(),
"clue": clue,
"similarity": similarity,
"tier": tier
})
except Exception as e:
logger.error(f"Failed to generate {difficulty} clue for '{word}': {e}")
clue_pairs.append({
"word": word.upper(),
"clue": f"{difficulty.title()} clue for {word}",
"similarity": similarity,
"tier": tier,
"error": str(e)
})
generation_time = time.time() - start_time
results[difficulty] = {
"clue_pairs": clue_pairs,
"generation_time": generation_time
}
# Display this difficulty's results
for i, item in enumerate(clue_pairs, 1):
error_marker = " β οΈ" if 'error' in item else ""
print(f" {i}. {item['word']:<10} - {item['clue']}{error_marker}")
return {
"topic": topic,
"difficulties_tested": difficulties,
"results": results,
"base_words": [{"word": w, "similarity": s, "tier": t} for w, s, t in thematic_results]
}
def run_performance_analysis(self) -> Dict[str, Any]:
"""Analyze overall performance characteristics."""
print(f"\nπ Performance Analysis")
print("-" * 40)
# Collect performance stats from previous tests
if not self.test_results:
print("β οΈ No test results available for performance analysis")
return {}
all_word_times = []
all_clue_times = []
all_total_times = []
for test_name, result in self.test_results.items():
if 'performance' in result:
perf = result['performance']
all_word_times.append(perf.get('word_gen_time', 0))
all_clue_times.append(perf.get('clue_gen_time', 0))
all_total_times.append(perf.get('total_time', 0))
if all_word_times:
print(f"π Word Generation Performance:")
print(f" Average: {sum(all_word_times)/len(all_word_times):.2f}s")
print(f" Min: {min(all_word_times):.2f}s")
print(f" Max: {max(all_word_times):.2f}s")
print(f"\nπ Clue Generation Performance:")
print(f" Average: {sum(all_clue_times)/len(all_clue_times):.2f}s")
print(f" Min: {min(all_clue_times):.2f}s")
print(f" Max: {max(all_clue_times):.2f}s")
print(f"\nβ±οΈ Total Pipeline Performance:")
print(f" Average: {sum(all_total_times)/len(all_total_times):.2f}s")
print(f" Min: {min(all_total_times):.2f}s")
print(f" Max: {max(all_total_times):.2f}s")
return {
"word_gen_stats": {
"avg": sum(all_word_times)/len(all_word_times) if all_word_times else 0,
"min": min(all_word_times) if all_word_times else 0,
"max": max(all_word_times) if all_word_times else 0
},
"clue_gen_stats": {
"avg": sum(all_clue_times)/len(all_clue_times) if all_clue_times else 0,
"min": min(all_clue_times) if all_clue_times else 0,
"max": max(all_clue_times) if all_clue_times else 0
},
"total_stats": {
"avg": sum(all_total_times)/len(all_total_times) if all_total_times else 0,
"min": min(all_total_times) if all_total_times else 0,
"max": max(all_total_times) if all_total_times else 0
}
}
def run_full_test_suite(self):
"""Run the complete test suite."""
print("π§ͺ CROSSWORD CLUE GENERATION TEST SUITE")
print("=" * 70)
if not GENERATORS_AVAILABLE:
print("β Cannot run tests - generators not available")
return
# Initialize
self.initialize()
# Test 1: Single topics
print("\n" + "="*70)
print("TEST 1: SINGLE TOPIC TESTS")
print("="*70)
single_topics = ["animals", "technology", "music", "food"]
for topic in single_topics:
result = self.run_single_topic_test(topic, num_words=8)
self.test_results[f"single_{topic}"] = result
# Test 2: Multi-topic
print("\n" + "="*70)
print("TEST 2: MULTI-TOPIC TEST")
print("="*70)
multi_result = self.run_multi_topic_test(["science", "technology"], num_words=10)
self.test_results["multi_science_tech"] = multi_result
# Test 3: Custom sentence
print("\n" + "="*70)
print("TEST 3: CUSTOM SENTENCE TEST")
print("="*70)
sentence_result = self.run_custom_sentence_test("I love cats and playing guitar", num_words=8)
self.test_results["sentence_cats_guitar"] = sentence_result
# Test 4: Difficulty comparison
print("\n" + "="*70)
print("TEST 4: DIFFICULTY COMPARISON")
print("="*70)
difficulty_result = self.run_difficulty_comparison_test("sports", num_words=5)
self.test_results["difficulty_sports"] = difficulty_result
# Test 5: Performance analysis
print("\n" + "="*70)
print("TEST 5: PERFORMANCE ANALYSIS")
print("="*70)
perf_result = self.run_performance_analysis()
self.test_results["performance"] = perf_result
# Final summary
print("\n" + "="*70)
print("π FINAL SUMMARY")
print("="*70)
print(f"β
Test suite completed!")
print(f"π Tests run: {len(self.test_results)}")
# Model info
word_info = {
"vocab_size": self.word_generator.get_vocabulary_size(),
"tier_distribution": len(self.word_generator.get_tier_distribution())
}
clue_info = self.clue_generator.get_model_info()
print(f"\nπ§ System Information:")
print(f" Word vocabulary: {word_info['vocab_size']:,} words")
print(f" Clue model: {clue_info['model_name']}")
print(f" Model size: {clue_info.get('model_size_mb', 0):.1f} MB")
if perf_result:
avg_total = perf_result['total_stats']['avg']
print(f" Average pipeline time: {avg_total:.2f}s")
print(f"\nπ‘ Recommendations for HF Spaces:")
if perf_result and perf_result['total_stats']['avg'] < 15:
print(" β
Performance suitable for interactive use")
else:
print(" β οΈ Consider optimizations for better user experience")
print("\nπ Test suite complete!")
def main():
"""Run the test suite."""
if not GENERATORS_AVAILABLE:
print("β Cannot run tests - required generators not available")
print("Make sure thematic_word_generator.py and llm_clue_generator.py are working")
return
# Create and run test suite
test_suite = CrosswordClueTestSuite(vocab_size_limit=50000) # Use existing cached embeddings
try:
test_suite.run_full_test_suite()
except KeyboardInterrupt:
print("\n\nβΉοΈ Test suite interrupted by user")
except Exception as e:
print(f"\nβ Test suite failed: {e}")
logger.error(f"Test suite error: {e}", exc_info=True)
if __name__ == "__main__":
main() |