f07a8440b2
- write_config schreibt atomar (tmp + os.replace) -> llama-swap (-watch-config) sieht nie eine halb geschriebene config.yaml; PermissionError wird in eine klare Klartext-Meldung uebersetzt (Hinweis auf chown) - generischer Exception-Handler: unerwartete Fehler kommen als lesbare JSON- Meldung im UI an statt als nackter 500 (Anfaenger-Diagnose) - Download: nutzt die `hf`-CLI aus dem venv (Dienst-PATH kennt venv/bin nicht) + HF_HUB_DISABLE_XET=1 statt HF_XET_HIGH_PERFORMANCE (XET-Haenger bei ~6 MB) - huggingface_hub als Dependency ergaenzt (liefert `hf`) - favicon.ico: themed SVG (Link-Tag + Route) -> kein 404 mehr Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
"""
|
|
Helfer rund um llama-swap und dessen config.yaml.
|
|
|
|
- _swap_get: liest llama-swap-Endpoints (/running, /v1/models, ...)
|
|
- read_config / write_config: lesen/schreiben der config.yaml ueber ruamel.yaml,
|
|
sodass Kommentare und Quotes erhalten bleiben.
|
|
"""
|
|
|
|
import os
|
|
|
|
import httpx
|
|
|
|
from config import CONFIG_PATH, LLAMA_SWAP_URL, yaml
|
|
|
|
|
|
def _swap_get(path: str):
|
|
with httpx.Client(timeout=5.0) as c:
|
|
r = c.get(f"{LLAMA_SWAP_URL}{path}")
|
|
r.raise_for_status()
|
|
return r.json()
|
|
|
|
|
|
def read_config() -> dict:
|
|
if not CONFIG_PATH.exists():
|
|
return {"models": {}}
|
|
with CONFIG_PATH.open("r", encoding="utf-8") as f:
|
|
data = yaml.load(f) or {}
|
|
if "models" not in data or data["models"] is None:
|
|
data["models"] = {}
|
|
return data
|
|
|
|
|
|
def write_config(cfg: dict) -> None:
|
|
"""Schreibt die config.yaml atomar (tmp-Datei + os.replace), damit llama-swap
|
|
mit -watch-config nie eine halb geschriebene Datei sieht. Fehlende Schreibrechte
|
|
werden in eine klare Meldung uebersetzt statt als roher Traceback zu landen."""
|
|
try:
|
|
CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True)
|
|
tmp = CONFIG_PATH.with_name(CONFIG_PATH.name + ".tmp")
|
|
with tmp.open("w", encoding="utf-8") as f:
|
|
yaml.dump(cfg, f)
|
|
os.replace(tmp, CONFIG_PATH)
|
|
except PermissionError as exc:
|
|
raise PermissionError(
|
|
f"Mission Control darf '{CONFIG_PATH}' nicht schreiben. "
|
|
f"Einmalig Besitz uebergeben: sudo chown -R hitonabi:hitonabi {CONFIG_PATH.parent}"
|
|
) from exc
|