-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_hf_token.py
More file actions
104 lines (86 loc) · 3.72 KB
/
test_hf_token.py
File metadata and controls
104 lines (86 loc) · 3.72 KB
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
#!/usr/bin/env python3
"""
Test script to verify Hugging Face token configuration.
This script tests the token loading functionality without actually loading models.
"""
import os
import sys
import json
from pathlib import Path
# Add the project root to Python path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
def test_token_loading():
"""Test the HF token loading functionality"""
print("Testing Hugging Face token configuration...")
# Test 1: Import the utility function
try:
from inference.visual_equation_solving.icon_only.open_source.inference_icon import get_huggingface_token
print("✅ Successfully imported get_huggingface_token function")
except ImportError as e:
print(f"❌ Failed to import token function: {e}")
return False
# Test 2: Check if config file exists
config_path = project_root / "inference_config.json"
if config_path.exists():
print("✅ Configuration file exists")
# Test 3: Check config structure
try:
with open(config_path, 'r') as f:
config = json.load(f)
if 'huggingface' in config:
print("✅ Hugging Face section found in config")
hf_config = config['huggingface']
required_fields = ['token', 'use_env', 'env_var']
missing_fields = [field for field in required_fields if field not in hf_config]
if not missing_fields:
print("✅ All required HF config fields present")
else:
print(f"⚠️ Missing HF config fields: {missing_fields}")
else:
print("❌ Hugging Face section not found in config")
return False
except json.JSONDecodeError as e:
print(f"❌ Invalid JSON in config file: {e}")
return False
else:
print("❌ Configuration file not found")
return False
# Test 4: Test token loading
try:
token = get_huggingface_token()
if token:
# Don't print the actual token for security
print(f"✅ Token loaded successfully (length: {len(token)})")
# Check if it looks like a valid HF token
if token.startswith('hf_') and len(token) > 20:
print("✅ Token format appears valid")
elif token == "your-huggingface-token-here":
print("⚠️ Using placeholder token - replace with your actual token")
else:
print("⚠️ Token format may be invalid")
else:
print("⚠️ No token found - set HUGGING_FACE_HUB_TOKEN environment variable or update config")
except Exception as e:
print(f"❌ Error loading token: {e}")
return False
# Test 5: Check environment variables
env_vars = ['HUGGING_FACE_HUB_TOKEN', 'HF_TOKEN']
env_token_found = False
for var in env_vars:
if os.getenv(var):
print(f"✅ Environment variable {var} is set")
env_token_found = True
break
if not env_token_found:
print("ℹ️ No HF token environment variables found (this is okay if using config file)")
print("\n" + "="*50)
print("Token configuration test completed!")
print("\nNext steps:")
print("1. Set HUGGING_FACE_HUB_TOKEN environment variable, OR")
print("2. Edit inference_config.json and replace 'your-huggingface-token-here'")
print("3. Get your token from: https://huggingface.co/settings/tokens")
print("="*50)
return True
if __name__ == "__main__":
test_token_loading()