File size: 6,264 Bytes
e9fc2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Test the game generation pipeline with llama.cpp and NVIDIA Nemotron 3 Nano 4B GGUF.

Run this script to:
1. Test model availability (llama-cpp-python)
2. Demonstrate GGUF model loading from HuggingFace
3. Generate games with the Nemotron model
4. Fall back to mock generation if model unavailable
5. Validate all outputs against schema
"""

import json
from pathlib import Path
from app.services.retrieval import load_games_dataset, normalize_game_record, retrieve_examples
from app.services.generator import generate_game, build_generation_prompt, NEMOTRON_MODEL_ID, NEMOTRON_GGUF_FILE
from app.services.schema_validator import validate_game_schema


def check_llama_cpp_availability():
    """Check if llama-cpp-python is installed."""
    print("\n" + "=" * 80)
    print("CHECKING ENVIRONMENT")
    print("=" * 80)
    
    try:
        import llama_cpp
        print(f"βœ“ llama-cpp-python is installed")
        print(f"  Version: {llama_cpp.__version__ if hasattr(llama_cpp, '__version__') else 'unknown'}")
        return True
    except ImportError:
        print("βœ— llama-cpp-python not found")
        print("  Install with: pip install llama-cpp-python")
        print("  Or for GPU support: pip install llama-cpp-python[cuda]")
        return False


def main():
    print("\n" + "=" * 80)
    print("PHASE 2, TASK 6: GAME GENERATION WITH NEMOTRON 3 NANO 4B GGUF")
    print("=" * 80)
    
    # Check environment
    llama_cpp_available = check_llama_cpp_availability()
    
    print(f"\nModel Configuration:")
    print(f"  Repository: {NEMOTRON_MODEL_ID}")
    print(f"  File: {NEMOTRON_GGUF_FILE}")
    print(f"  Runtime: llama.cpp (GGUF quantized)")
    print(f"  Benefits:")
    print(f"    β€’ GGUF quantization: 4-bit, memory efficient")
    print(f"    β€’ llama.cpp: Fast CPU/GPU inference")
    print(f"    β€’ Hackathon bonus: Extra credit for llama.cpp runtime")
    
    # Load dataset
    print("\n1. Loading dataset...")
    raw_records = load_games_dataset("app/data/games_dataset.json")
    normalized_records = [normalize_game_record(r) for r in raw_records]
    print(f"βœ“ Loaded {len(normalized_records)} records")
    
    # Test cases
    test_configs = [
        {
            "name": "Scavenger Hunt - Adults - Medium",
            "config": {
                "game_type": "scavenger_hunt",
                "city": "Paris",
                "area": "Le Marais",
                "location_type": "mixed",
                "duration_minutes": 60,
                "num_players": 4,
                "difficulty": "medium",
                "age_group": "adults"
            }
        },
        {
            "name": "Hide & Seek - Kids - Easy",
            "config": {
                "game_type": "hide_and_seek",
                "city": "Paris",
                "area": "Parc des Buttes-Chaumont",
                "location_type": "park",
                "duration_minutes": 45,
                "num_players": 5,
                "difficulty": "easy",
                "age_group": "kids"
            }
        }
    ]
    
    # Test generation
    results = []
    for test in test_configs:
        print("\n" + "=" * 80)
        print(f"TEST: {test['name']}")
        print("=" * 80)
        
        config = test['config']
        
        # Retrieve similar games
        print("\n2. Retrieving similar games...")
        retrieved = retrieve_examples(config, normalized_records, k=3)
        print(f"βœ“ Retrieved {len(retrieved)} examples")
        
        # Build prompt
        print("\n3. Building generation prompt...")
        prompt = build_generation_prompt(config, retrieved)
        print(f"βœ“ Prompt ready ({len(prompt)} chars)")
        
        # Generate game
        print("\n4. Generating game...")
        print(f"   (Using: NVIDIA Nemotron 3 Nano 4B GGUF via llama.cpp)")
        
        try:
            game = generate_game(config, retrieved)
            print(f"βœ“ Game generated: {game['game_id']}")
            
            # Validate
            print("\n5. Validating against schema...")
            is_valid, errors = validate_game_schema(game)
            
            if is_valid:
                print("βœ“ Game VALID against schema")
            else:
                print(f"βœ— Validation errors: {len(errors)}")
            
            # Display summary
            print("\n6. Game Summary:")
            print(f"  Title: {game['title']}")
            print(f"  Area: {game['setup']['area']}")
            print(f"  Duration: {game['setup']['duration_minutes']} min | Players: {game['setup']['num_players']}")
            print(f"  Tasks: {len(game['tasks'])} | Rules: {len(game['rules'])}")
            print(f"  Tone: {game['story_seed']['tone']}")
            
            results.append({
                'name': test['name'],
                'valid': is_valid,
                'game_id': game['game_id']
            })
            
        except Exception as e:
            print(f"βœ— Generation failed: {e}")
            results.append({
                'name': test['name'],
                'valid': False,
                'game_id': None
            })
    
    # Summary
    print("\n" + "=" * 80)
    print("TEST SUMMARY")
    print("=" * 80)
    
    passed = sum(1 for r in results if r['valid'])
    print(f"\nResults: {passed}/{len(results)} tests passed")
    
    for result in results:
        status = "βœ“ PASS" if result['valid'] else "βœ— FAIL"
        print(f"{status}: {result['name']}")
        if result['game_id']:
            print(f"       {result['game_id']}")
    
    print("\n" + "=" * 80)
    print("NOTES")
    print("=" * 80)
    if not llama_cpp_available:
        print("⚠ llama-cpp-python not installed - using mock generation")
        print("  For actual model-based generation, install:")
        print("    pip install llama-cpp-python[cuda]  # For GPU")
        print("    or")
        print("    pip install llama-cpp-python        # For CPU")
    else:
        print("βœ“ llama-cpp-python available")
        print("βœ“ NVIDIA Nemotron 3 Nano 4B GGUF ready for download from HuggingFace")
        print("βœ“ Hackathon extra credit: llama.cpp runtime βœ“")
    
    print("\n" + "=" * 80)


if __name__ == "__main__":
    main()