""" Central configuration loaded from environment variables. All tunables live here — no magic numbers scattered across modules. Override any of these via a .env file (see .env.example) or real env vars. """ import os from pathlib import Path try: from dotenv import load_dotenv # Load .env from project root if present (safe to call even if missing) load_dotenv(Path(__file__).resolve().parent.parent / ".env") except ImportError: pass # python-dotenv is optional; env vars still work def _bool(v: str, default: bool = False) -> bool: if v is None: return default return v.strip().lower() in ("1", "true", "yes", "on") # ── Model ────────────────────────────────────────────────────────── MODEL_NAME = os.getenv("HF_MODEL_NAME", "deepset/bert-base-cased-squad2") MAX_CONTEXT_CHARS = int(os.getenv("MAX_CONTEXT_CHARS", "2500")) MAX_SEQ_LENGTH = int(os.getenv("MAX_SEQ_LENGTH", "512")) WARMUP_ON_START = _bool(os.getenv("WARMUP_ON_START"), default=True) # ── Scraper ──────────────────────────────────────────────────────── SCRAPE_TIMEOUT = int(os.getenv("SCRAPE_TIMEOUT", "20")) SCRAPE_MAX_RETRIES = int(os.getenv("SCRAPE_MAX_RETRIES", "3")) # ── Rate limiting ────────────────────────────────────────────────── RATE_LIMIT_ENABLED = _bool(os.getenv("RATE_LIMIT_ENABLED"), default=True) RATE_LIMIT_SCRAPE = os.getenv("RATE_LIMIT_SCRAPE", "10 per minute") RATE_LIMIT_PREDICT = os.getenv("RATE_LIMIT_PREDICT", "30 per minute") RATE_LIMIT_DEFAULT = os.getenv("RATE_LIMIT_DEFAULT", "200 per hour") # ── Persistence (SQLite) ─────────────────────────────────────────── # HF Spaces persistent storage lives under /data when enabled; fallback to local file otherwise _default_db = "/data/history.db" if Path("/data").exists() and os.access("/data", os.W_OK) else "history.db" DB_PATH = os.getenv("DB_PATH", _default_db) HISTORY_LIMIT = int(os.getenv("HISTORY_LIMIT", "100")) # ── Server ───────────────────────────────────────────────────────── # HF Spaces Docker SDK defaults to 7860; honor $PORT if set (Render/Railway/etc.) PORT = int(os.getenv("PORT", "7860")) DEBUG = _bool(os.getenv("FLASK_DEBUG"), default=False)