from __future__ import annotations import os from dataclasses import dataclass from pathlib import Path def _bool_env(name: str, default: bool) -> bool: value = os.getenv(name) if value is None: return default return value.strip().lower() in {"1", "true", "yes", "on"} def _float_env(name: str, default: float) -> float: value = os.getenv(name) return default if value is None else float(value) def _int_env(name: str, default: int) -> int: value = os.getenv(name) return default if value is None else int(value) @dataclass(frozen=True) class Settings: data_dir: Path database_path: Path asr_model: str asr_timestamps: str embedding_model: str visual_model: str device: str torch_dtype: str chunk_length_seconds: float chunk_overlap_seconds: float frame_sample_interval_seconds: float max_audio_seconds: float | None visual_search_enabled: bool keyword_boost: float semantic_weight: float keyword_weight: float min_speech_score: float min_visual_score: float @classmethod def from_env(cls) -> "Settings": data_dir = Path(os.getenv("IRIS_DATA_DIR", ".iris-data")).resolve() return cls( data_dir=data_dir, database_path=Path(os.getenv("IRIS_DATABASE_PATH", data_dir / "iris.sqlite3")).resolve(), asr_model=os.getenv("IRIS_ASR_MODEL", "openai/whisper-large-v3-turbo"), asr_timestamps=os.getenv("IRIS_ASR_TIMESTAMPS", "word"), embedding_model=os.getenv("IRIS_EMBEDDING_MODEL", "sentence-transformers/all-MiniLM-L6-v2"), visual_model=os.getenv("IRIS_VISUAL_MODEL", "openai/clip-vit-large-patch14"), device=os.getenv("IRIS_DEVICE", "auto"), torch_dtype=os.getenv("IRIS_TORCH_DTYPE", "auto"), chunk_length_seconds=_float_env("IRIS_CHUNK_LENGTH_SECONDS", 24.0), chunk_overlap_seconds=_float_env("IRIS_CHUNK_OVERLAP_SECONDS", 4.0), frame_sample_interval_seconds=_float_env("IRIS_FRAME_SAMPLE_INTERVAL_SECONDS", 2.0), max_audio_seconds=_optional_float_env("IRIS_MAX_AUDIO_SECONDS"), visual_search_enabled=_bool_env("IRIS_VISUAL_SEARCH_ENABLED", True), keyword_boost=_float_env("IRIS_KEYWORD_BOOST", 0.15), semantic_weight=_float_env("IRIS_SEMANTIC_WEIGHT", 0.78), keyword_weight=_float_env("IRIS_KEYWORD_WEIGHT", 0.22), min_speech_score=_float_env("IRIS_MIN_SPEECH_SCORE", 0.55), min_visual_score=_float_env("IRIS_MIN_VISUAL_SCORE", 0.45), ) def ensure_directories(self) -> None: self.data_dir.mkdir(parents=True, exist_ok=True) self.database_path.parent.mkdir(parents=True, exist_ok=True) def resolve_device(device: str) -> str: if device != "auto": return device try: import torch return "cuda" if torch.cuda.is_available() else "cpu" except Exception: return "cpu" def transformers_pipeline_device(device: str) -> int: resolved = resolve_device(device) return 0 if resolved == "cuda" else -1 def resolve_torch_dtype(device: str, dtype: str): normalized = dtype.strip().lower() resolved_device = resolve_device(device) if normalized == "auto": normalized = "float16" if resolved_device == "cuda" else "float32" try: import torch if normalized in {"float16", "fp16", "half"}: return torch.float16 if normalized in {"bfloat16", "bf16"}: return torch.bfloat16 if normalized in {"float32", "fp32", "full"}: return torch.float32 except Exception: pass return "auto" def device_info(device: str, dtype: str) -> dict: resolved = resolve_device(device) info = { "requested_device": device, "resolved_device": resolved, "requested_dtype": dtype, "cuda_available": False, "cuda_version": None, "gpu_count": 0, "gpu_name": None, } try: import torch info.update( { "torch_version": torch.__version__, "cuda_available": torch.cuda.is_available(), "cuda_version": torch.version.cuda, "gpu_count": torch.cuda.device_count(), "gpu_name": torch.cuda.get_device_name(0) if torch.cuda.is_available() else None, } ) except Exception as exc: info["torch_error"] = str(exc) return info def _optional_float_env(name: str) -> float | None: value = os.getenv(name) if value is None or value.strip() == "": return None return float(value)