Spaces:
Running
Running
File size: 4,661 Bytes
c8243d5 d1a53e8 b9161fb d1a53e8 434c40e c8243d5 434c40e c8243d5 |
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 |
"""
Environment Detection Utilities
This module provides utilities to detect the deployment environment
and configure authentication accordingly.
"""
import os
from typing import Dict, Any, Optional
def is_huggingface_space() -> bool:
"""
Detect if the application is running in Hugging Face Spaces.
Returns:
bool: True if running in HF Spaces, False otherwise
"""
# HF Spaces sets specific environment variables
hf_indicators = [
"SPACE_ID", # HF Space identifier
"SPACE_AUTHOR_NAME", # Space author
"SPACE_REPO_NAME", # Space repository name
"OAUTH_CLIENT_ID", # OAuth client ID (when oauth enabled)
]
return any(os.getenv(indicator) for indicator in hf_indicators)
def is_local_development() -> bool:
"""
Detect if the application is running in local development mode.
Returns:
bool: True if running locally, False otherwise
"""
return not is_huggingface_space()
def get_environment_info() -> Dict[str, Any]:
"""
Get comprehensive environment information.
Returns:
Dict containing environment details
"""
env_info = {
"is_hf_space": is_huggingface_space(),
"is_local_dev": is_local_development(),
"space_id": os.getenv("SPACE_ID"),
"space_author": os.getenv("SPACE_AUTHOR_NAME"),
"space_repo": os.getenv("SPACE_REPO_NAME"),
"oauth_enabled": bool(os.getenv("OAUTH_CLIENT_ID")),
"host": os.getenv("HOST", "localhost"),
"port": os.getenv("PORT", "7860"),
}
return env_info
def should_enable_auth() -> bool:
"""
Determine if authentication should be enabled based on environment.
Returns:
bool: True if auth should be enabled, False otherwise
"""
return is_huggingface_space()
def get_oauth_config() -> Optional[Dict[str, str]]:
"""
Get OAuth configuration if available.
Returns:
Dict with OAuth config or None if not available
"""
if not should_enable_auth():
return None
# Force HF-compatible scope for HF Spaces, ignore environment variable
if is_huggingface_space():
# HF automatically includes 'openid profile', we specify additional scopes
scopes = "openid profile read-repos"
else:
scopes = os.getenv("OAUTH_SCOPES", "read-repos")
# Warn about unsupported scopes for HF Spaces
if is_huggingface_space():
unsupported_scopes = []
for scope in scopes.split():
if scope not in ["email", "read-repos", "write-repos", "manage-repos",
"read-mcp", "write-discussions", "read-billing",
"inference-api", "jobs", "webhooks"]:
unsupported_scopes.append(scope)
if unsupported_scopes:
print(f"β οΈ Warning: Unsupported OAuth scopes detected: {unsupported_scopes}")
print("π Supported HF scopes: email, read-repos, write-repos, manage-repos, read-mcp, write-discussions, read-billing, inference-api, jobs, webhooks")
oauth_config = {
"client_id": os.getenv("OAUTH_CLIENT_ID"),
"client_secret": os.getenv("OAUTH_CLIENT_SECRET"),
"scopes": scopes,
"provider_url": os.getenv("OPENID_PROVIDER_URL", "https://huggingface.co"),
}
# Only return config if client_id is available
if oauth_config["client_id"]:
return oauth_config
return None
def debug_environment() -> None:
"""
Print debug information about the current environment.
"""
env_info = get_environment_info()
oauth_config = get_oauth_config()
print("π Environment Debug Information:")
print("=" * 50)
print(f"ποΈ Environment Type: {'HF Spaces' if env_info['is_hf_space'] else 'Local Development'}")
print(f"π Authentication: {'Enabled' if should_enable_auth() else 'Disabled'}")
if env_info['is_hf_space']:
print(f"π Space ID: {env_info['space_id']}")
print(f"π€ Author: {env_info['space_author']}")
print(f"π¦ Repo: {env_info['space_repo']}")
if oauth_config:
print(f"π OAuth Client ID: {oauth_config['client_id'][:8]}..." if oauth_config['client_id'] else "Not set")
print(f"π OAuth Scopes: {oauth_config['scopes']}")
else:
print("β OAuth not configured")
else:
print("π Running in local development mode")
print("π‘ Authentication will be automatically enabled when deployed to HF Spaces")
print("=" * 50)
|