File size: 1,783 Bytes
784ecc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Application configuration loaded from environment variables."""

from __future__ import annotations

from dataclasses import dataclass
import os


def _env(name: str, default: str = "") -> str:
    return os.environ.get(name, default).strip()


def _numbered_keys(prefix: str, count: int) -> tuple[str, ...]:
    return tuple(_env(f"{prefix}_{index}") for index in range(1, count + 1))


@dataclass(frozen=True)
class Settings:
    groq_api_keys: tuple[str, ...]
    gemini_api_keys: tuple[str, ...]
    openrouter_api_key: str
    provider_timeout_seconds: float
    max_upload_bytes: int
    max_pdf_pages: int

    @property
    def any_text_provider_enabled(self) -> bool:
        return any(self.groq_api_keys) or any(self.gemini_api_keys) or bool(self.openrouter_api_key)



def load_settings() -> Settings:
    """Load and normalize settings once at application startup."""
    timeout_raw = _env("PROVIDER_TIMEOUT_SECONDS", "60")
    upload_raw = _env("MAX_UPLOAD_BYTES", str(5 * 1024 * 1024))
    pages_raw = _env("MAX_PDF_PAGES", "6")

    try:
        timeout = max(5.0, min(float(timeout_raw), 120.0))
    except ValueError:
        timeout = 60.0

    try:
        max_upload_bytes = max(1024, min(int(upload_raw), 25 * 1024 * 1024))
    except ValueError:
        max_upload_bytes = 5 * 1024 * 1024

    try:
        max_pdf_pages = max(1, min(int(pages_raw), 20))
    except ValueError:
        max_pdf_pages = 6

    return Settings(
        groq_api_keys=_numbered_keys("GROQ_API_KEY", 3),
        gemini_api_keys=_numbered_keys("GEMINI_API_KEY", 4),
        openrouter_api_key=_env("OPENROUTER_API_KEY"),
        provider_timeout_seconds=timeout,
        max_upload_bytes=max_upload_bytes,
        max_pdf_pages=max_pdf_pages,
    )


settings = load_settings()