Spaces:
Sleeping
Sleeping
| """Environment variable loader with HF Secrets support.""" | |
| import os | |
| import logging | |
| from typing import Optional | |
| logger = logging.getLogger(__name__) | |
| class EnvironmentLoader: | |
| """Loads environment variables with support for HF Secrets.""" | |
| def get_secret(key: str, default: Optional[str] = None) -> str: | |
| """Get environment variable or secret. | |
| In Hugging Face Spaces, secrets are injected as environment variables. | |
| This function provides a consistent interface for accessing them. | |
| Args: | |
| key: Environment variable name | |
| default: Default value if not found | |
| Returns: | |
| Environment variable value or default | |
| Raises: | |
| ValueError: If required variable is missing and no default provided | |
| """ | |
| value = os.getenv(key, default) | |
| if value is None: | |
| raise ValueError( | |
| f"Required environment variable '{key}' is not set. " | |
| f"Please add it to HF Secrets in your Space settings." | |
| ) | |
| if not value: | |
| logger.warning(f"Environment variable '{key}' is empty") | |
| return value | |
| def validate_secrets() -> bool: | |
| """Validate that all required secrets are configured. | |
| Returns: | |
| True if all required secrets are present, False otherwise | |
| """ | |
| required_secrets = [ | |
| "HUGGINGFACE_TOKEN", | |
| ] | |
| dashscope_secrets = [ | |
| "DASHSCOPE_API_KEY", | |
| "DASHSCOPE_BASE_URL", | |
| ] | |
| optional_secrets = [ | |
| "HUGGINGFACE_DATASET", | |
| "MODEL_NAME", | |
| ] | |
| all_valid = True | |
| # Check required secrets | |
| for secret in required_secrets: | |
| value = os.getenv(secret) | |
| if not value: | |
| logger.error(f"Missing required secret: {secret}") | |
| all_valid = False | |
| else: | |
| logger.info(f"✓ {secret} configured") | |
| # Check DashScope secrets (without exposing values) | |
| logger.info("\nDashScope Configuration:") | |
| dashscope_valid = True | |
| for secret in dashscope_secrets: | |
| value = os.getenv(secret) | |
| if value: | |
| logger.info(f"✓ {secret} configured (***hidden***)") | |
| else: | |
| logger.warning(f"⚠ {secret} not configured") | |
| dashscope_valid = False | |
| if not dashscope_valid: | |
| logger.warning( | |
| "\n⚠ DashScope credentials not fully configured. " | |
| "They will be set via HF API during Space deployment." | |
| ) | |
| # Check optional secrets | |
| for secret in optional_secrets: | |
| value = os.getenv(secret) | |
| if value: | |
| logger.info(f"✓ {secret} configured") | |
| else: | |
| logger.info(f"ℹ {secret} not configured (using default)") | |
| return all_valid | |
| def get_api_config() -> dict: | |
| """Get API configuration from environment. | |
| Returns: | |
| Dictionary with API configuration | |
| """ | |
| return { | |
| "openrouter_api_key": os.getenv("DASHSCOPE_API_KEY", ""), | |
| "openrouter_base_url": os.getenv( | |
| "DASHSCOPE_BASE_URL", "https://dashscope.aliyuncs.com/compatible-mode/v1" | |
| ), | |
| "model_name": os.getenv("MODEL_NAME", "qwen3.7-plus"), | |
| "huggingface_token": os.getenv("HUGGINGFACE_TOKEN", ""), | |
| "huggingface_dataset": os.getenv( | |
| "HUGGINGFACE_DATASET", "factorstudios/Pipeline" | |
| ), | |
| } | |
| # Validate secrets on import | |
| def validate_on_startup(): | |
| """Validate secrets when module is imported.""" | |
| if not EnvironmentLoader.validate_secrets(): | |
| logger.warning( | |
| "Some secrets are not configured. " | |
| "The application may not work correctly. " | |
| "Please add missing secrets to HF Secrets in your Space settings." | |
| ) | |