explorer-consultant-bot / FastAPI /core /model_config.py
Sakda101's picture
Deploy FastAPI app to Hugging Face Space
59e06b1
Raw History Blame Contribute Delete
2.4 kB
"""
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)