""" model_config.py — Read / write the active model configuration. Config file lives at: Final Project/model_config.json Keys ---- stage1 : folder name of the Stage 1 model inside artifacts/ e.g. "distilbert_stage1" or "distilbert_stage1_backup" pipeline : "two_stage" — Stage 1 type → per-type Stage 2 cluster model (default) "flat_cluster" — Stage 1 type → shared distilbert_cluster model similarity_threshold : float 0.5–1.0 — TF-IDF cosine similarity cutoff for corrections matching Higher = only near-exact paraphrase matches; lower = more lenient stage1_confidence_threshold: float 0.0–1.0 — Stage 1 confidence below this shows a warning banner 0.0 disables the warning (default) """ from __future__ import annotations import json import os from pathlib import Path # On Railway: CONFIG_PATH should also live on the persistent volume # e.g. CONFIG_PATH=/app_data/model_config.json _default_cfg = Path(__file__).resolve().parents[2] / "model_config.json" CONFIG_PATH = Path(os.environ.get("CONFIG_PATH", str(_default_cfg))) AVAILABLE_STAGE1 = { "distilbert_stage1": "DistilBERT Stage 1 (current — retrained, cleaner)", "distilbert_stage1_backup": "DistilBERT Stage 1 Backup (original, pre-cleaning)", } AVAILABLE_PIPELINES = { "two_stage": "Two-Stage (Stage 1 type → per-type Stage 2 cluster models)", "flat_cluster": "Flat Cluster (Stage 1 type → shared distilbert_cluster model)", } DEFAULTS: dict = { "stage1": "distilbert_stage1", "pipeline": "two_stage", "similarity_threshold": 0.80, "stage1_confidence_threshold": 0.0, } def read_config() -> dict: """Return the active model config, falling back to DEFAULTS.""" if CONFIG_PATH.exists(): try: with open(CONFIG_PATH, "r", encoding="utf-8") as f: cfg = json.load(f) for k, v in DEFAULTS.items(): cfg.setdefault(k, v) return cfg except Exception: pass return dict(DEFAULTS) def write_config(cfg: dict) -> None: """Persist model config to disk.""" with open(CONFIG_PATH, "w", encoding="utf-8") as f: json.dump(cfg, f, indent=2)