File size: 2,398 Bytes
59e06b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
"""
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)