File size: 5,363 Bytes
8ef4c38 1db37ad 8ef4c38 1db37ad 8ef4c38 1db37ad 8ef4c38 | 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 | """Test HF_TOKEN configuration for repository access.
Run this script to verify that HF_TOKEN is properly configured
and has the necessary permissions for the private repo.
"""
import os
import sys
from huggingface_hub import HfApi
from pathlib import Path
REPO_ID = "UII-AI/MedVidBench-GroundTruth"
REPO_TYPE = "dataset"
def test_hf_token():
"""Test HF_TOKEN configuration."""
print("=" * 80)
print("TESTING HF_TOKEN CONFIGURATION")
print("=" * 80)
# Check if token exists
print("\n[1/4] Checking HF_TOKEN environment variable...")
token = os.environ.get('HF_TOKEN')
if not token:
print("β FAILED: HF_TOKEN not found in environment")
print("\nHow to fix:")
print("1. Generate token at: https://huggingface.co/settings/tokens")
print("2. Grant 'write' permission to repositories")
print("3. Set as environment variable:")
print(" export HF_TOKEN='your_token_here'")
print("\n4. For HuggingFace Spaces:")
print(" Settings β Repository secrets β Add HF_TOKEN")
sys.exit(1)
print(f"β HF_TOKEN found (length: {len(token)} chars)")
masked_token = token[:7] + "..." + token[-4:] if len(token) > 11 else "***"
print(f" Token: {masked_token}")
# Initialize API
print("\n[2/4] Initializing HuggingFace API...")
try:
api = HfApi()
print("β HfApi initialized")
except Exception as e:
print(f"β FAILED: {e}")
sys.exit(1)
# Test repository access (read)
print(f"\n[3/4] Testing READ access to {REPO_ID}...")
try:
repo_info = api.repo_info(
repo_id=REPO_ID,
repo_type=REPO_TYPE,
token=token
)
print(f"β Successfully accessed repository")
print(f" Repo: {repo_info.id}")
print(f" Private: {repo_info.private}")
print(f" Last modified: {repo_info.lastModified}")
# List files
files = api.list_repo_files(
repo_id=REPO_ID,
repo_type=REPO_TYPE,
token=token
)
print(f" Files in repo: {len(files)}")
for file in files:
print(f" - {file}")
except Exception as e:
error_msg = str(e)
print(f"β FAILED: {error_msg}")
if "401" in error_msg or "Unauthorized" in error_msg:
print("\nβ Issue: Invalid or expired token")
print("β Fix: Regenerate token at https://huggingface.co/settings/tokens")
elif "404" in error_msg or "Not Found" in error_msg:
print(f"\nβ Issue: Repository '{REPO_ID}' not found")
print("β Fix: Create the repository:")
print(f" 1. Go to: https://huggingface.co/new-dataset")
print(f" 2. Owner: UII-AI")
print(f" 3. Name: MedVidBench-GroundTruth")
print(f" 4. Visibility: Private")
elif "403" in error_msg or "Forbidden" in error_msg:
print("\nβ Issue: No access to private repository")
print("β Fix: Ensure you're a member of UII-AI organization")
sys.exit(1)
# Test write access
print(f"\n[4/4] Testing WRITE access to {REPO_ID}...")
try:
# Create a test file
test_file = Path("test_upload.txt")
with open(test_file, 'w') as f:
f.write("Test upload to verify write permissions\n")
print(" Creating test file...")
result = api.upload_file(
path_or_fileobj=str(test_file),
path_in_repo="test_upload.txt",
repo_id=REPO_ID,
repo_type=REPO_TYPE,
token=token,
commit_message="Test write access"
)
print(f"β Successfully uploaded test file")
print(f" Commit: {result}")
# Clean up test file
print(" Cleaning up test file...")
api.delete_file(
path_in_repo="test_upload.txt",
repo_id=REPO_ID,
repo_type=REPO_TYPE,
token=token,
commit_message="Remove test file"
)
test_file.unlink()
print(f"β Successfully deleted test file")
except Exception as e:
error_msg = str(e)
print(f"β FAILED: {error_msg}")
if "403" in error_msg or "Forbidden" in error_msg:
print("\nβ Issue: Token does not have write permission")
print("β Fix:")
print(" 1. Go to: https://huggingface.co/settings/tokens")
print(" 2. Create new token with WRITE permission")
print(" 3. Update HF_TOKEN environment variable")
elif "401" in error_msg:
print("\nβ Issue: Token invalid for write operations")
print("β Fix: Ensure token has 'write' scope")
sys.exit(1)
# Success
print("\n" + "=" * 80)
print("β
ALL TESTS PASSED")
print("=" * 80)
print("\nYour HF_TOKEN is correctly configured with:")
print(" β Valid authentication")
print(" β Read access to private repository")
print(" β Write access to private repository")
print("\nYou can now:")
print(" 1. Deploy app.py to HuggingFace Spaces")
print(" 2. Add HF_TOKEN as a Space secret")
print(" 3. Leaderboard will automatically sync to private repo")
if __name__ == "__main__":
test_hf_token()
|