Spaces:
Running
Running
#!/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) |