File size: 4,832 Bytes
676b3f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test Hugging Face Setup for Trackio Integration

This script helps verify your Hugging Face token setup and test space name generation.
Run this before using the training scripts to ensure everything is configured correctly.

Authentication:
This script only checks for HF_TOKEN or HUGGINGFACE_HUB_TOKEN environment variables.
It does NOT use huggingface-cli login state.

Setup:
  Linux/Mac: export HF_TOKEN=your_token_here
  Windows: set HF_TOKEN=your_token_here
  Or: export HUGGINGFACE_HUB_TOKEN=your_token_here

Get your token from: https://huggingface.co/settings/tokens
"""

import os
from datetime import datetime
from typing import Tuple, Optional
from huggingface_hub import HfApi


def validate_hf_token(token: str) -> Tuple[bool, Optional[str], Optional[str]]:
    """
    Validate a Hugging Face token and return the username.

    Args:
        token (str): The Hugging Face token to validate

    Returns:
        Tuple[bool, Optional[str], Optional[str]]:
            - success: True if token is valid, False otherwise
            - username: The username associated with the token (if valid)
            - error_message: Error message if validation failed
    """
    try:
        # Create API client with token directly
        api = HfApi(token=token)

        # Try to get user info - this will fail if token is invalid
        user_info = api.whoami()

        # Extract username from user info
        username = user_info.get("name", user_info.get("username"))

        if not username:
            return False, None, "Could not retrieve username from token"

        return True, username, None

    except Exception as e:
        error_msg = str(e)
        if "401" in error_msg or "unauthorized" in error_msg.lower():
            return False, None, "Invalid token - unauthorized access"
        elif "403" in error_msg:
            return False, None, "Token lacks required permissions"
        elif "network" in error_msg.lower() or "connection" in error_msg.lower():
            return False, None, f"Network error: {error_msg}"
        else:
            return False, None, f"Validation error: {error_msg}"


def get_default_space_name(project_type: str = "voxtral-asr-finetuning") -> str:
    """
    Generate a default space name with username and timestamp.

    Args:
        project_type: Type of project (e.g., "voxtral-asr-finetuning", "voxtral-lora-finetuning")

    Returns:
        str: Default space name in format "username/project-type-timestamp"
    """
    try:
        # Get token from environment variables only
        token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN")

        if not token:
            return None

        # Validate token and get username
        success, username, error = validate_hf_token(token)
        if success and username:
            timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
            return f"{username}/{project_type}-{timestamp}"
        else:
            return None

    except Exception as e:
        print(f"Failed to generate default space name: {e}")
        return None


def main():
    print("πŸ” Testing Hugging Face Setup for Trackio Integration")
    print("=" * 60)

    # Check for tokens
    print("\n1. Checking for Hugging Face tokens...")

    token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN")
    if token:
        print(f"βœ… Found token in environment: {token[:10]}...")
    else:
        print("❌ No token found in environment variables")
        print("\n❌ No Hugging Face token found!")
        print("Please set the HF_TOKEN environment variable:")
        print("  Linux/Mac: export HF_TOKEN=your_token_here")
        print("  Windows: set HF_TOKEN=your_token_here")
        print("  Or: set HUGGINGFACE_HUB_TOKEN=your_token_here")
        print("\nGet your token from: https://huggingface.co/settings/tokens")
        return

    # Validate token
    print("\n2. Validating token...")
    success, username, error = validate_hf_token(token)

    if success:
        print(f"βœ… Token is valid! Username: {username}")
    else:
        print(f"❌ Token validation failed: {error}")
        return

    # Generate space names
    print("\n3. Generating default space names...")

    full_finetune_space = get_default_space_name("voxtral-asr-finetuning")
    lora_finetune_space = get_default_space_name("voxtral-lora-finetuning")

    print(f"πŸ“ Full fine-tuning space: {full_finetune_space}")
    print(f"πŸ“ LoRA fine-tuning space: {lora_finetune_space}")

    print("\nβœ… Setup complete! You can now run training scripts.")
    print("   They will automatically use the generated space names.")
    print("\nπŸ’‘ To override the auto-generated names, use --trackio-space yourname/custom-space")


if __name__ == "__main__":
    main()