""" Central configuration for EvidenceGuard AI. Every value can be overridden with an environment variable (or a Hugging Face Space secret/variable) of the same name. No credentials are stored in code. Backends -------- EG_BACKEND = "auto" | "full" | "lite" full : Hugging Face models (Sentence Transformer, cross-encoder reranker, NLI cross-encoder) lite : dependency-light, CPU-only fallbacks (TF-IDF+SVD embeddings, lexical reranker, rule-based numeric/negation contradiction detector). Used when models cannot be downloaded (offline machines) and for fast tests. auto : try "full", fall back to "lite" component-by-component if a model cannot load. LLM_BACKEND = "auto" | "hf_api" | "local" | "extractive" hf_api : Hugging Face Inference Providers via huggingface_hub (needs HF_TOKEN secret) local : small instruction-tuned model run locally with transformers (CPU OK) extractive : no LLM; answer is composed from retrieved sentences (fully offline) auto : hf_api if HF_TOKEN is set, else local, else extractive """ from __future__ import annotations import os from pathlib import Path ROOT = Path(__file__).resolve().parent SAMPLE_DATA_DIR = ROOT / "sample_data" EVAL_DATA_PATH = ROOT / "evaluation_data" / "questions.json" RESULTS_DIR = ROOT / "results" CHARTS_DIR = RESULTS_DIR / "charts" CACHE_DIR = Path(os.getenv("EG_CACHE_DIR", str(ROOT / ".cache"))) UPLOAD_DIR = CACHE_DIR / "uploads" def _env(name: str, default): v = os.getenv(name) if v is None or v == "": return default return type(default)(v) if not isinstance(default, bool) else v.lower() in {"1", "true", "yes"} # ---------------------------------------------------------------- backends EG_BACKEND = _env("EG_BACKEND", "auto") LLM_BACKEND = _env("LLM_BACKEND", "auto") # ---------------------------------------------------------------- models (all free / open licences) EMBEDDING_MODEL = _env("EMBEDDING_MODEL", "sentence-transformers/all-MiniLM-L6-v2") RERANKER_MODEL = _env("RERANKER_MODEL", "cross-encoder/ms-marco-MiniLM-L-6-v2") NLI_MODEL = _env("NLI_MODEL", "cross-encoder/nli-deberta-v3-xsmall") LOCAL_LLM_MODEL = _env("LOCAL_LLM_MODEL", "Qwen/Qwen2.5-0.5B-Instruct") HF_API_MODEL = _env("HF_API_MODEL", "Qwen/Qwen2.5-7B-Instruct") HF_TOKEN = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACEHUB_API_TOKEN") # Space secret, never hard-coded # ---------------------------------------------------------------- chunking & retrieval CHUNK_TARGET_WORDS = _env("CHUNK_TARGET_WORDS", 70) CHUNK_OVERLAP_SENTENCES = _env("CHUNK_OVERLAP_SENTENCES", 1) TOP_K = _env("TOP_K", 5) # passages given to the generator CANDIDATES = _env("CANDIDATES", 20) # candidates pulled by each retriever before fusion/reranking RRF_K = _env("RRF_K", 60) LSA_DIM = _env("LSA_DIM", 256) # ---------------------------------------------------------------- generation MAX_NEW_TOKENS = _env("MAX_NEW_TOKENS", 220) TEMPERATURE = _env("TEMPERATURE", 0.0) # ---------------------------------------------------------------- verification thresholds # Default values; scripts/run_evaluation.py re-calibrates them on a held-out DEV split # and writes results/calibrated_thresholds.json, which is loaded automatically if present. THRESHOLDS = { "min_relevance": _env("MIN_RELEVANCE", 0.30), # reranker score of best passage "min_coverage": _env("MIN_COVERAGE", 0.60), # IDF-weighted query-term coverage "partial_coverage": _env("PARTIAL_COVERAGE", 0.45), "min_confidence": _env("MIN_CONFIDENCE", 0.40), "conflict_topic_sim": _env("CONFLICT_TOPIC_SIM", 0.30), # overlap coefficient (polarity/NLI) "conflict_sentence_coverage": _env("CONFLICT_SENT_COV", 0.35), # sentence must cover the question "conflict_min_shared": _env("CONFLICT_MIN_SHARED", 2), # shared context words around a value "conflict_subject_mismatch": _env("CONFLICT_SUBJECT_MISMATCH", 0.30), # max weight of unshared question words "conflict_min_relevance": _env("CONFLICT_MIN_RELEVANCE", 0.25), "nli_contradiction": _env("NLI_CONTRADICTION", 0.60), "nli_entailment": _env("NLI_ENTAILMENT", 0.50), "citation_overlap": _env("CITATION_OVERLAP", 0.50), } CALIBRATED_THRESHOLDS_PATH = RESULTS_DIR / "calibrated_thresholds.json" def load_thresholds(effective_backend: str = "lite") -> dict: """Defaults, overridden by calibrated values only if they were calibrated for the same backend (reranker scores of the lexical fallback and of the cross-encoder live on different scales).""" import json th = dict(THRESHOLDS) if CALIBRATED_THRESHOLDS_PATH.exists() and not os.getenv("EG_IGNORE_CALIBRATION"): try: cal = json.loads(CALIBRATED_THRESHOLDS_PATH.read_text()) if cal.get("backend") == effective_backend: th.update(cal.get("thresholds", {})) except Exception: pass return th for _d in (CACHE_DIR, UPLOAD_DIR, CHARTS_DIR): _d.mkdir(parents=True, exist_ok=True)