File size: 3,620 Bytes
a0debed |
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 |
"""
Configuration management using Pydantic Settings.
Handles environment variables with validation and defaults.
"""
import os
from typing import Optional
from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
"""Application settings with environment variable support."""
model_config = SettingsConfigDict(
env_file=".env",
env_file_encoding="utf-8",
case_sensitive=False,
extra="ignore"
)
# Server configuration
server_name: str = Field(
default="0.0.0.0",
description="Server bind address"
)
server_port: int = Field(
default=7860,
ge=1024,
le=65535,
description="Server port (1024-65535)"
)
# Security configuration
mcp_token: Optional[str] = Field(
default=None,
description="MCP authentication token (required for production)"
)
# Request limits
max_request_size: int = Field(
default=65536, # 64KB
ge=1024,
le=1048576, # 1MB max
description="Maximum request body size in bytes"
)
# Development settings
debug: bool = Field(
default=False,
description="Enable debug mode"
)
reload: bool = Field(
default=False,
description="Enable hot reload in development"
)
# Logging configuration
log_level: str = Field(
default="INFO",
description="Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)"
)
def is_production(self) -> bool:
"""Check if running in production mode."""
return not self.debug and self.mcp_token is not None
def validate_production_settings(self) -> None:
"""Validate settings for production deployment."""
if self.is_production():
assert self.mcp_token, "MCP_TOKEN environment variable is required for production deployment"
# Global settings instance
settings = Settings()
def get_settings() -> Settings:
"""Get the current settings instance."""
return settings
def setup_logging() -> None:
"""Configure logging based on settings."""
import logging
logging.basicConfig(
level=getattr(logging, settings.log_level.upper()),
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S"
)
# Set Gradio logging level
if not settings.debug:
logging.getLogger("gradio").setLevel(logging.WARNING)
logging.getLogger("uvicorn").setLevel(logging.WARNING)
# Environment detection helpers
def is_huggingface_space() -> bool:
"""Check if running on Hugging Face Spaces."""
return "SPACE_ID" in os.environ
def is_docker() -> bool:
"""Check if running in Docker container."""
return os.path.exists("/.dockerenv") or "DOCKER_CONTAINER" in os.environ
def get_deployment_info() -> dict[str, str]:
"""Get deployment environment information."""
info = {
"environment": "unknown",
"platform": "local"
}
if is_huggingface_space():
info["platform"] = "huggingface_spaces"
info["environment"] = "production"
elif is_docker():
info["platform"] = "docker"
info["environment"] = "production" if settings.is_production() else "development"
else:
info["platform"] = "local"
info["environment"] = "development"
return info |