File size: 2,895 Bytes
58de40c
 
 
bc5091e
58de40c
 
f477f87
 
58de40c
f477f87
58de40c
 
 
 
 
1ed6720
048c3fc
1ed6720
5a007ca
58de40c
 
 
1ed6720
 
abaeb0b
d1ed6b1
abaeb0b
0e508c8
1ed6720
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e508c8
1ed6720
 
 
048c3fc
1ed6720
048c3fc
1ed6720
 
048c3fc
1ed6720
048c3fc
1ed6720
 
 
 
 
 
 
 
048c3fc
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
"""
config.py

Global configuration and logger setup for the project.

Key Features:
- Uses environment variables defined in the system (Docker in production).
- Loads a `.env` file only in development to simulate production variables locally.
- Configures the logger for consistent logging across all modules.
- Dynamically enables DEBUG logging in development and INFO logging in production (unless overridden).
"""

# Standard Library Imports
import logging
import os
from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING, ClassVar, Optional

# Third-Party Library Imports
from dotenv import load_dotenv

if TYPE_CHECKING:
    from src.integrations import AnthropicConfig, ElevenLabsConfig, HumeConfig

logger: logging.Logger = logging.getLogger("tts_arena")


@dataclass(frozen=True)
class Config:
    _config: ClassVar[Optional["Config"]] = None
    app_env: str
    debug: bool
    database_url: Optional[str]
    audio_dir: Path
    anthropic_config: "AnthropicConfig"
    hume_config: "HumeConfig"
    elevenlabs_config: "ElevenLabsConfig"

    @classmethod
    def get(cls) -> "Config":
        if cls._config is None:
            _config = Config._init()
            cls._config = _config
            return _config
        return cls._config

    @staticmethod
    def _init():
        app_env = os.getenv("APP_ENV", "dev").lower()
        if app_env not in {"dev", "prod"}:
            app_env = "dev"

        # In development, load environment variables from .env file (not used in production)
        if app_env == "dev" and Path(".env").exists():
            load_dotenv(".env", override=True)

        # Enable debug mode if in development (or if explicitly set in env variables)
        debug = app_env == "dev" or os.getenv("DEBUG", "false").lower() == "true"

        database_url = os.getenv("DATABASE_URL")

        # Configure the logger
        logging.basicConfig(
            level=logging.DEBUG if debug else logging.INFO,
            format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
        )
        logger.info(f'App running in "{app_env}" mode.')
        logger.info(f"Debug mode is {'enabled' if debug else 'disabled'}.")

        # Define the directory for audio files relative to the project root
        audio_dir = Path.cwd() / "static" / "audio"
        audio_dir.mkdir(parents=True, exist_ok=True)

        logger.info(f"Audio directory set to {audio_dir}")

        if debug:
            logger.debug("DEBUG mode enabled.")

        from src.integrations import AnthropicConfig, ElevenLabsConfig, HumeConfig

        return Config(
            app_env=app_env,
            debug=debug,
            database_url=database_url,
            audio_dir=audio_dir,
            anthropic_config=AnthropicConfig(),
            hume_config=HumeConfig(),
            elevenlabs_config=ElevenLabsConfig(),
        )