Remember the last selected model across runs

Store it in .cache/last_model and restore it on startup, falling back
to DEFAULT_MODEL if missing or unknown.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
guofangandClaude Sonnet 5.5 committed 2026-11-09 23:42:12 +08:00
1 parent bafc517f57
commit 4f1ac0c69f
3 files changed
+24 -1

No files matched your search

+1
View File
@@ -1 +1,2 @@
.venv/
.cache/
Binary file not shown.
+23 -1
View File
@@ -2,6 +2,7 @@
print("Loading torch/transformers, please wait...", flush=True)
import os
import readline
import torch
@@ -20,6 +21,7 @@ MODELS = {
}
PARAM_KEYS = ["pooling", "max_length", "batch_size"]
DEFAULT_MODEL = "BAAI/bge-small-zh-v1.5"
LAST_MODEL_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".cache", "last_model")
COMMANDS = ["/select", "/sim", "/tokens", "/params", "/set", "/reset", "/lsmod", "/help", "/quit", "/exit"]
device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -38,6 +40,25 @@ def is_cached(name):
)
def load_last_model():
"""The model selected last time, or DEFAULT_MODEL if unknown."""
try:
with open(LAST_MODEL_FILE) as f:
name = f.read().strip()
except OSError:
return DEFAULT_MODEL
return name if name in MODELS else DEFAULT_MODEL
def save_last_model(name):
try:
os.makedirs(os.path.dirname(LAST_MODEL_FILE), exist_ok=True)
with open(LAST_MODEL_FILE, "w") as f:
f.write(name)
except OSError:
pass
def select_model(name):
global current
if name not in MODELS:
@@ -49,6 +70,7 @@ def select_model(name):
mod = AutoModel.from_pretrained(name).to(device).eval()
_loaded[name] = (tok, mod)
current = name
save_last_model(name)
print(f"Current model: {name}")
@@ -204,7 +226,7 @@ def main():
args = parser.parse_args()
setup_readline()
select_model(DEFAULT_MODEL)
select_model(load_last_model())
print(HELP)
while True:
try: