Spaces:
Sleeping
Sleeping
| """ | |
| Configuration file for the Keyword Spotting App. | |
| Contains authentication and app settings. | |
| """ | |
| import os | |
| from typing import Tuple, Optional | |
| class AppConfig: | |
| """Configuration class for the app.""" | |
| # Default authentication settings | |
| DEFAULT_USERNAME = "admin" | |
| DEFAULT_PASSWORD = "kws2024" | |
| # App settings | |
| DEFAULT_PORT = 7860 | |
| DEFAULT_HOST = "0.0.0.0" | |
| def get_auth_credentials() -> Optional[Tuple[str, str]]: | |
| """ | |
| Get authentication credentials from environment variables or defaults. | |
| Returns: | |
| Tuple of (username, password) or None to disable auth | |
| """ | |
| # Check environment variables first | |
| username = os.getenv("KWS_USERNAME") | |
| password = os.getenv("KWS_PASSWORD") | |
| # Check if authentication should be disabled | |
| if os.getenv("KWS_NO_AUTH", "").lower() in ["true", "1", "yes"]: | |
| return None | |
| # Use environment variables if available, otherwise use defaults | |
| if username and password: | |
| return (username, password) | |
| else: | |
| return (AppConfig.DEFAULT_USERNAME, AppConfig.DEFAULT_PASSWORD) | |
| def get_server_config() -> dict: | |
| """ | |
| Get server configuration. | |
| Returns: | |
| Dictionary with server configuration | |
| """ | |
| return { | |
| "server_name": os.getenv("KWS_HOST", AppConfig.DEFAULT_HOST), | |
| "server_port": int(os.getenv("KWS_PORT", AppConfig.DEFAULT_PORT)), | |
| "share": os.getenv("KWS_SHARE", "false").lower() in ["true", "1", "yes"], | |
| "debug": os.getenv("KWS_DEBUG", "false").lower() in ["true", "1", "yes"], | |
| } | |
| def print_config_info(): | |
| """Print configuration information.""" | |
| auth = AppConfig.get_auth_credentials() | |
| config = AppConfig.get_server_config() | |
| print("🔧 Configuración de la aplicación:") | |
| print(f" Host: {config['server_name']}") | |
| print(f" Puerto: {config['server_port']}") | |
| print(f" Compartir públicamente: {config['share']}") | |
| print(f" Modo debug: {config['debug']}") | |
| if auth: | |
| print(f"🔐 Autenticación habilitada:") | |
| print(f" Usuario: {auth[0]}") | |
| print(f" Contraseña: {auth[1]}") | |
| else: | |
| print("🔓 Autenticación deshabilitada") | |
| print("\n💡 Para cambiar la configuración, use variables de entorno:") | |
| print(" KWS_USERNAME=tu_usuario") | |
| print(" KWS_PASSWORD=tu_contraseña") | |
| print(" KWS_NO_AUTH=true (para deshabilitar autenticación)") | |
| print(" KWS_HOST=127.0.0.1 (para acceso local únicamente)") | |
| print(" KWS_PORT=8080 (para cambiar puerto)") | |
| print(" KWS_SHARE=true (para crear enlace público)") | |
| print(" KWS_DEBUG=true (para modo debug)") | |
| # Quick access functions | |
| def get_auth() -> Optional[Tuple[str, str]]: | |
| """Quick function to get auth credentials.""" | |
| return AppConfig.get_auth_credentials() | |
| def get_server_config() -> dict: | |
| """Quick function to get server config.""" | |
| return AppConfig.get_server_config() | |
| def print_config() -> None: | |
| """Quick function to print config.""" | |
| AppConfig.print_config_info() | |