File size: 7,245 Bytes
f251d3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test script for the fixed Trackio API client
Verifies connection to the deployed Trackio Space with automatic URL resolution
"""

import sys
import os
import logging

# Add the project root to the path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from scripts.trackio_tonic.trackio_api_client import TrackioAPIClient

# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def test_trackio_connection():
    """Test connection to Trackio Space"""
    print("πŸ”§ Testing Trackio API Client with automatic URL resolution...")
    
    # Initialize the API client with Space ID
    space_id = "Tonic/trackio-monitoring-20250727"
    client = TrackioAPIClient(space_id)
    
    # Test 1: Space info
    print("\n1️⃣ Testing Space info resolution...")
    space_info = client.get_space_info()
    print(f"Space info result: {space_info}")
    
    if space_info.get('error'):
        print("❌ Space info failed")
        return False
    
    print("βœ… Space info successful!")
    
    # Test 2: Connection test
    print("\n2️⃣ Testing connection...")
    connection_result = client.test_connection()
    print(f"Connection result: {connection_result}")
    
    if connection_result.get('error'):
        print("❌ Connection failed")
        return False
    
    print("βœ… Connection successful!")
    
    # Test 3: List experiments
    print("\n3️⃣ Testing list experiments...")
    list_result = client.list_experiments()
    print(f"List experiments result: {list_result}")
    
    if list_result.get('error'):
        print("❌ List experiments failed")
        return False
    
    print("βœ… List experiments successful!")
    
    # Test 4: Create a test experiment
    print("\n4️⃣ Testing create experiment...")
    create_result = client.create_experiment(
        name="test_experiment_auto_resolve",
        description="Test experiment with automatic URL resolution"
    )
    print(f"Create experiment result: {create_result}")
    
    if create_result.get('error'):
        print("❌ Create experiment failed")
        return False
    
    print("βœ… Create experiment successful!")
    
    # Test 5: Log metrics
    print("\n5️⃣ Testing log metrics...")
    metrics = {
        "loss": 1.234,
        "accuracy": 0.85,
        "learning_rate": 2e-5,
        "gpu_memory": 22.5
    }
    
    log_metrics_result = client.log_metrics(
        experiment_id="test_experiment_auto_resolve",
        metrics=metrics,
        step=100
    )
    print(f"Log metrics result: {log_metrics_result}")
    
    if log_metrics_result.get('error'):
        print("❌ Log metrics failed")
        return False
    
    print("βœ… Log metrics successful!")
    
    # Test 6: Log parameters
    print("\n6️⃣ Testing log parameters...")
    parameters = {
        "learning_rate": 2e-5,
        "batch_size": 8,
        "model_name": "HuggingFaceTB/SmolLM3-3B",
        "max_iters": 18000,
        "mixed_precision": "bf16"
    }
    
    log_params_result = client.log_parameters(
        experiment_id="test_experiment_auto_resolve",
        parameters=parameters
    )
    print(f"Log parameters result: {log_params_result}")
    
    if log_params_result.get('error'):
        print("❌ Log parameters failed")
        return False
    
    print("βœ… Log parameters successful!")
    
    # Test 7: Get experiment details
    print("\n7️⃣ Testing get experiment details...")
    details_result = client.get_experiment_details("test_experiment_auto_resolve")
    print(f"Get experiment details result: {details_result}")
    
    if details_result.get('error'):
        print("❌ Get experiment details failed")
        return False
    
    print("βœ… Get experiment details successful!")
    
    print("\nπŸŽ‰ All tests passed! Trackio API client with automatic URL resolution is working correctly.")
    return True

def test_monitoring_integration():
    """Test the monitoring integration with the fixed API client"""
    print("\nπŸ”§ Testing monitoring integration...")
    
    try:
        from src.monitoring import SmolLM3Monitor
        
        # Create a monitor instance
        monitor = SmolLM3Monitor(
            experiment_name="test_monitoring_auto_resolve",
            enable_tracking=True,
            log_metrics=True,
            log_config=True
        )
        
        print("βœ… Monitor created successfully")
        
        # Test logging metrics
        metrics = {
            "loss": 1.123,
            "accuracy": 0.87,
            "learning_rate": 2e-5
        }
        
        monitor.log_metrics(metrics, step=50)
        print("βœ… Metrics logged successfully")
        
        # Test logging configuration
        config = {
            "model_name": "HuggingFaceTB/SmolLM3-3B",
            "batch_size": 8,
            "learning_rate": 2e-5
        }
        
        monitor.log_config(config)
        print("βœ… Configuration logged successfully")
        
        print("πŸŽ‰ Monitoring integration test passed!")
        return True
        
    except Exception as e:
        print(f"❌ Monitoring integration test failed: {e}")
        return False

def test_space_url_resolution():
    """Test automatic Space URL resolution"""
    print("\nπŸ”§ Testing Space URL resolution...")
    
    try:
        from huggingface_hub import HfApi
        
        # Test Space info retrieval
        api = HfApi()
        space_id = "Tonic/trackio-monitoring-20250727"
        
        space_info = api.space_info(space_id)
        print(f"βœ… Space info retrieved: {space_info}")
        
        if hasattr(space_info, 'host'):
            space_url = f"https://{space_info.host}"
            print(f"βœ… Resolved Space URL: {space_url}")
        else:
            print("⚠️ Space host not available, using fallback")
            space_url = f"https://{space_id.replace('/', '-')}.hf.space"
            print(f"βœ… Fallback Space URL: {space_url}")
        
        return True
        
    except Exception as e:
        print(f"❌ Space URL resolution failed: {e}")
        return False

if __name__ == "__main__":
    print("πŸš€ Starting Trackio API Client Tests with Automatic URL Resolution")
    print("=" * 70)
    
    # Test 1: Space URL Resolution
    url_resolution_success = test_space_url_resolution()
    
    # Test 2: API Client
    api_success = test_trackio_connection()
    
    # Test 3: Monitoring Integration
    monitoring_success = test_monitoring_integration()
    
    print("\n" + "=" * 70)
    print("πŸ“Š Test Results Summary:")
    print(f"Space URL Resolution: {'βœ… PASSED' if url_resolution_success else '❌ FAILED'}")
    print(f"API Client Test: {'βœ… PASSED' if api_success else '❌ FAILED'}")
    print(f"Monitoring Integration: {'βœ… PASSED' if monitoring_success else '❌ FAILED'}")
    
    if url_resolution_success and api_success and monitoring_success:
        print("\nπŸŽ‰ All tests passed! The Trackio integration with automatic URL resolution is working correctly.")
        sys.exit(0)
    else:
        print("\n❌ Some tests failed. Please check the errors above.")
        sys.exit(1)