#!/usr/bin/env python3 """ Test script for PRIVATE Hugging Face Spaces with authentication. Usage: uv run python test_private_space.py [space-url] [hf-token] """ import sys import requests import json import os # ANSI color codes GREEN = '\033[0;32m' RED = '\033[0;31m' YELLOW = '\033[1;33m' BLUE = '\033[0;34m' NC = '\033[0m' # No Color def get_headers(token: str = None) -> dict: """Get headers with authentication if token provided""" headers = {"Content-Type": "application/json"} if token: headers["Authorization"] = f"Bearer {token}" return headers def test_with_auth(base_url: str, token: str = None): """Test space with optional authentication""" print("=" * 50) print(f"Testing Space: {base_url}") if token: print(f"Using token: {token[:10]}...{token[-5:]}") else: print("No token provided (testing public access)") print("=" * 50) print() headers = get_headers(token) # Test health endpoint print(f"{YELLOW}Test: Health Check{NC}") try: response = requests.get(f"{base_url}/health", headers=headers, timeout=10) if response.status_code == 200: print(f"{GREEN}✓ SUCCESS{NC} - Space is accessible!") print(f"Response: {response.json()}") return True elif response.status_code == 404: print(f"{RED}✗ 404 ERROR{NC}") print(f"\n{YELLOW}Possible reasons:{NC}") print("1. Space is PRIVATE and needs authentication token") print("2. Space is still building") print("3. Space build failed") print("\nTo fix:") print("• Make space public in settings, OR") print("• Use: uv run python test_private_space.py [url] [your-hf-token]") return False elif response.status_code == 401: print(f"{RED}✗ 401 UNAUTHORIZED{NC}") print("Space is PRIVATE. You need a valid Hugging Face token.") print("\nGet your token:") print("1. Go to https://huggingface.co/settings/tokens") print("2. Create a new token with 'read' permissions") print("3. Run: uv run python test_private_space.py [url] [token]") return False else: print(f"{RED}✗ ERROR{NC} - Status: {response.status_code}") print(f"Response: {response.text[:200]}") return False except Exception as e: print(f"{RED}✗ ERROR{NC} - {str(e)}") return False def main(): # Get space URL and token base_url = sys.argv[1] if len(sys.argv) > 1 else "https://ocx2025-basicsearch.hf.space" token = sys.argv[2] if len(sys.argv) > 2 else os.getenv("HF_TOKEN") # Remove trailing slash base_url = base_url.rstrip('/') # Test success = test_with_auth(base_url, token) if success: print(f"\n{GREEN}✅ Space is working!{NC}") print(f"You can now run the full test suite:") if token: print(f"HF_TOKEN={token} uv run python test_deployment.py") else: print("uv run python test_deployment.py") else: print(f"\n{YELLOW}📝 Next Steps:{NC}") print("\n1. Check if space is private:") print(f" Visit: {base_url.replace('.hf.space', '').replace('https://', 'https://huggingface.co/spaces/').replace('-', '/', 1)}") print("\n2. Make it PUBLIC:") print(" Settings → Make public") print("\n3. OR get your HF token:") print(" https://huggingface.co/settings/tokens") print(f" Then run: uv run python test_private_space.py {base_url} YOUR_TOKEN") if __name__ == "__main__": main()