File size: 6,024 Bytes
5d7656c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test script to verify model repository name automation
"""

import os
import sys
import subprocess
from pathlib import Path
from datetime import datetime

def test_model_repo_automation():
    """Test that model repository names are automatically generated"""
    print("πŸ” Testing Model Repository Automation")
    print("=" * 50)
    
    # Test token from user
    test_token = "xxxx"
    
    print(f"Testing model repository automation with token: {'*' * 10}...{test_token[-4:]}")
    
    # Set environment variables
    os.environ['HF_TOKEN'] = test_token
    os.environ['HUGGING_FACE_HUB_TOKEN'] = test_token
    os.environ['HF_USERNAME'] = 'Tonic'
    
    # Import the validation function to get username
    try:
        sys.path.append(str(Path(__file__).parent.parent / "scripts"))
        from validate_hf_token import validate_hf_token
        print("βœ… Token validation module imported successfully")
    except ImportError as e:
        print(f"❌ Failed to import token validation module: {e}")
        return False
    
    # Get username from token
    try:
        success, username, error = validate_hf_token(test_token)
        
        if not success:
            print(f"❌ Token validation failed: {error}")
            return False
        
        print(f"βœ… Username extracted: {username}")
        
    except Exception as e:
        print(f"❌ Username extraction error: {e}")
        return False
    
    # Test automatic repository name generation
    try:
        # Generate repository name using the same logic as launch.sh
        current_date = datetime.now().strftime("%Y%m%d")
        auto_repo_name = f"{username}/smollm3-finetuned-{current_date}"
        
        print(f"βœ… Auto-generated repository name: {auto_repo_name}")
        
        # Verify the format is correct
        if "/" in auto_repo_name and username in auto_repo_name:
            print("βœ… Repository name format is correct")
            return True
        else:
            print("❌ Repository name format is incorrect")
            return False
            
    except Exception as e:
        print(f"❌ Repository name generation error: {e}")
        return False

def test_launch_script_automation():
    """Test that launch script handles model repository automation"""
    print("\nπŸ” Testing Launch Script Model Repository Automation")
    print("=" * 50)
    
    # Check if launch.sh exists
    launch_script = Path("launch.sh")
    if not launch_script.exists():
        print("❌ launch.sh not found")
        return False
    
    # Read launch script and check for automation
    script_content = launch_script.read_text(encoding='utf-8')
    
    # Check for automatic model repository generation
    automation_patterns = [
        'REPO_NAME="$HF_USERNAME/smollm3-finetuned-$(date +%Y%m%d)"',
        'Setting up model repository automatically',
        'Model repository: $REPO_NAME'
    ]
    
    all_found = True
    for pattern in automation_patterns:
        if pattern in script_content:
            print(f"βœ… Found: {pattern}")
        else:
            print(f"❌ Missing: {pattern}")
            all_found = False
    
    # Check that get_input for model repository name is removed
    if 'get_input "Model repository name"' in script_content:
        print("❌ Found manual model repository input (should be automated)")
        all_found = False
    else:
        print("βœ… Manual model repository input removed")
    
    return all_found

def test_push_script_integration():
    """Test that push script works with auto-generated repository names"""
    print("\nπŸ” Testing Push Script Integration")
    print("=" * 50)
    
    # Test token from user
    test_token = "xxxx"
    
    # Import the push script
    try:
        sys.path.append(str(Path(__file__).parent.parent / "scripts" / "model_tonic"))
        from push_to_huggingface import HuggingFacePusher
        print("βœ… Push script module imported successfully")
    except ImportError as e:
        print(f"❌ Failed to import push script module: {e}")
        return False
    
    # Test with auto-generated repository name
    try:
        username = "Tonic"  # From token validation
        current_date = datetime.now().strftime("%Y%m%d")
        auto_repo_name = f"{username}/smollm3-finetuned-{current_date}"
        
        # Create a mock pusher (we won't actually push)
        pusher = HuggingFacePusher(
            model_path="/mock/path",
            repo_name=auto_repo_name,
            token=test_token
        )
        
        print(f"βœ… Push script initialized with auto-generated repo: {auto_repo_name}")
        print(f"βœ… Repository name format: {pusher.repo_name}")
        
        return True
        
    except Exception as e:
        print(f"❌ Push script integration error: {e}")
        return False

def main():
    """Run all model repository automation tests"""
    print("πŸš€ Model Repository Automation Verification")
    print("=" * 50)
    
    tests = [
        test_model_repo_automation,
        test_launch_script_automation,
        test_push_script_integration
    ]
    
    all_passed = True
    for test in tests:
        try:
            if not test():
                all_passed = False
        except Exception as e:
            print(f"❌ Test failed with error: {e}")
            all_passed = False
    
    print("\n" + "=" * 50)
    if all_passed:
        print("πŸŽ‰ ALL MODEL REPOSITORY AUTOMATION TESTS PASSED!")
        print("βœ… Model repository name generation: Working")
        print("βœ… Launch script automation: Working")
        print("βœ… Push script integration: Working")
        print("\nThe model repository automation is working correctly!")
    else:
        print("❌ SOME MODEL REPOSITORY AUTOMATION TESTS FAILED!")
        print("Please check the failed tests above.")
    
    return all_passed

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)