File size: 3,464 Bytes
e685c03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

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"
    
    @staticmethod
    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)
    
    @staticmethod
    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"],
        }
    
    @staticmethod
    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()