diff --git a/BEDIENUNGSANLEITUNG.md b/BEDIENUNGSANLEITUNG.md index e13a294..ccfef77 100644 --- a/BEDIENUNGSANLEITUNG.md +++ b/BEDIENUNGSANLEITUNG.md @@ -1755,7 +1755,10 @@ Kategorie, Textausschnitt). #### Status Zeigt aktives Profil, Provider-Konfiguration, Laufzeit-Metriken und verfügbare Provider. -Am Ende: **⬇ voice-assistant.db herunterladen** — lädt die SQLite-Datenbank als Backup. +Zusätzlich eine **LLM-Backend-Karte** (read-only): aktives Backend (Ollama/llama.cpp), +Modell, ob die Backends laufen, geladene Ollama-Modelle und die **GPU-Auslastung** je +Karte als Balken. Am Ende: **⬇ voice-assistant.db herunterladen** — lädt die +SQLite-Datenbank als Backup. #### Metriken diff --git a/app/admin_llm.py b/app/admin_llm.py new file mode 100644 index 0000000..8a5acf1 --- /dev/null +++ b/app/admin_llm.py @@ -0,0 +1,115 @@ +"""Read-only Statusabfragen rund um das lokale LLM-Backend (fuer das Admin-Panel). + +Alles nur lesend, ohne sudo: `docker ps`, `ollama ps`, `nvidia-smi`, `systemctl --user`. +Fehlende Tools oder Fehler fuehren zu sicheren Defaults (None/[]), nie zu Exceptions. +""" + +from __future__ import annotations + +import asyncio +import os +import shutil + +from app.config import settings + + +async def _run(cmd: list[str], timeout: float = 6.0) -> str | None: + """Fuehrt ein Kommando aus (shell=False) und liefert stdout, oder None bei Fehler.""" + if not shutil.which(cmd[0]): + return None + try: + proc = await asyncio.create_subprocess_exec( + *cmd, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.DEVNULL, + env={**os.environ, "XDG_RUNTIME_DIR": os.environ.get( + "XDG_RUNTIME_DIR", f"/run/user/{os.getuid()}")}, + ) + out, _ = await asyncio.wait_for(proc.communicate(), timeout=timeout) + if proc.returncode != 0: + return None + return out.decode("utf-8", "replace") + except (asyncio.TimeoutError, OSError): + return None + + +def _detect_backend(base_url: str) -> str: + if ":11434" in base_url: + return "ollama" + if ":8001" in base_url: + return "llamacpp" + return "unknown" + + +async def _llamacpp_running(container: str = "va_llm") -> bool: + out = await _run(["docker", "ps", "--filter", f"name=^{container}$", "--format", "{{.Names}}"]) + return bool(out and container in out) + + +async def _ollama_loaded() -> tuple[bool, list[dict]]: + """(dienst_erreichbar, [geladene Modelle]).""" + out = await _run(["ollama", "ps"]) + if out is None: + return False, [] + models: list[dict] = [] + lines = [ln for ln in out.splitlines() if ln.strip()] + for ln in lines[1:]: # Kopfzeile ueberspringen + # Spalten sind durch 2+ Leerzeichen getrennt: NAME ID SIZE PROCESSOR CONTEXT UNTIL + import re + cols = re.split(r"\s{2,}", ln.strip()) + if cols: + models.append({ + "name": cols[0], + "size": cols[2] if len(cols) > 2 else "", + "processor": cols[3] if len(cols) > 3 else "", + }) + return True, models + + +async def _gpus() -> list[dict]: + out = await _run([ + "nvidia-smi", + "--query-gpu=index,memory.used,memory.total", + "--format=csv,noheader,nounits", + ]) + if out is None: + return [] + gpus: list[dict] = [] + for ln in out.splitlines(): + parts = [p.strip() for p in ln.split(",")] + if len(parts) == 3 and parts[0].isdigit(): + used, total = int(parts[1]), int(parts[2]) + gpus.append({ + "index": int(parts[0]), + "used_mib": used, + "total_mib": total, + "percent": round(used / total * 100) if total else 0, + }) + return gpus + + +async def _gateway_service_active() -> bool: + out = await _run(["systemctl", "--user", "is-active", "voice-assistant.service"], timeout=4.0) + return bool(out and out.strip() == "active") + + +async def llm_status() -> dict: + """Aggregierter, read-only LLM-/System-Status fuer das Admin-Panel.""" + base_url = settings.local_llm_base_url + backend = _detect_backend(base_url) + llamacpp, (ollama_reachable, ollama_models), gpus, gw = await asyncio.gather( + _llamacpp_running(), + _ollama_loaded(), + _gpus(), + _gateway_service_active(), + ) + return { + "backend": backend, + "model": settings.local_llm_model, + "base_url": base_url, + "llamacpp_running": llamacpp, + "ollama_reachable": ollama_reachable, + "ollama_loaded": ollama_models, + "gpus": gpus, + "gateway_service_active": gw, + } diff --git a/app/api/admin.py b/app/api/admin.py index ece441d..a862d5a 100644 --- a/app/api/admin.py +++ b/app/api/admin.py @@ -6,6 +6,7 @@ from fastapi import APIRouter, Depends, HTTPException, Request, WebSocket, WebSo from fastapi.responses import FileResponse from pydantic import BaseModel +from app.admin_llm import llm_status from app.auth import is_admin_user, require_admin, require_admin_or_user from app.config import settings from app.dependencies import get_store @@ -187,6 +188,14 @@ async def export_db(): ) +# ── LLM-/System-Status (read-only) ────────────────────────────────────────── + +@router.get("/admin/llm/status", dependencies=[Depends(require_admin)]) +async def get_llm_status(): + """Read-only Status: aktives Backend, Modell, GPU-Auslastung, Dienste.""" + return await llm_status() + + # ── Aussprache-Lexikon CRUD ───────────────────────────────────────────────── def _read_pronunciation(lang: str) -> dict: diff --git a/app/web/app.js b/app/web/app.js index f04bd9f..f330bf6 100644 --- a/app/web/app.js +++ b/app/web/app.js @@ -956,13 +956,56 @@ async function loadEmergencyEvents() { // ════════════════════════════════════════ // TAB: STATUS // ════════════════════════════════════════ +// LLM-/GPU-Status-Karte (read-only) für den Status-Tab. +function renderLlmCard(llm) { + if (!llm) return ""; + const backendLabel = { ollama: "Ollama", llamacpp: "llama.cpp", unknown: "—" }[llm.backend] || llm.backend; + const dot = (on) => ``; + const row = (label, val) => + `
keine GPU-Daten (nvidia-smi)
'; + + return ` +lade …
'; try { - const [cfg, met] = await Promise.all([ + const [cfg, met, llm] = await Promise.all([ fetch("/api/config").then((r) => r.json()), fetch("/api/metrics").then((r) => r.json()), + adminFetch("/api/admin/llm/status"), ]); const route = cfg.default_route || {}; const counters = met.counters || {}; @@ -1000,6 +1043,8 @@ async function loadStatus() { + ${renderLlmCard(llm)} +