File size: 4,634 Bytes
457e012
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d360571
 
457e012
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d360571
 
 
 
 
3c8a257
 
 
 
d360571
 
 
 
 
 
 
 
457e012
 
 
 
 
 
 
 
 
 
d360571
457e012
 
 
 
 
 
 
d360571
457e012
 
 
 
 
 
 
 
 
 
c600f78
457e012
3c8a257
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c600f78
3c8a257
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""Drop-in helpers for studio/ui.py model status (live probe).

Replace _model_status_html usage with model_status_html_live after wiring
AdapterRouter / HealthCache.
"""
from __future__ import annotations

from html import escape

from studio.adapters.health import HealthCache, probe_space
from studio.adapters.router import route_id_for_model
from studio.registry import ModelRegistry

_CACHE = HealthCache(ttl_s=20.0)

_LABEL_DE = {
    "live": "Live",
    "sleeping": "Schlafend (wird beim Generate geweckt)",
    "paused": "Pausiert",
    "mock": "Demo-Modus (Mock)",
    "broken": "Defekt",
    "unauthorized": "Privat — HF_TOKEN fehlt/ungültig",
    "private-needs-token": "Privat — HF_TOKEN setzen",
    "unreachable": "Nicht erreichbar",
}


def model_status_html_live(model_id: str, registry: ModelRegistry) -> str:
    model = registry.get(model_id)
    route_id = route_id_for_model(model)
    space_ref = model.space_ref or "—"
    probe = _CACHE.get(space_ref) if model.space_ref else {
        "ok": False,
        "label": "unreachable",
        "stage": None,
        "hardware": None,
    }
    state = str(probe.get("label"))
    if state == "live" and route_id.endswith("-mock"):
        state = "mock"
    status = _LABEL_DE.get(state, state)
    hint = {
        "paused": "Dieses Modell ist derzeit pausiert und kann nicht generieren – bitte ein Modell "
                  "mit Status „Live“ wählen. (Betreiber: Worker im HF-Dashboard neu starten.)",
        "sleeping": "Der Worker schläft und wird beim Generieren geweckt – der erste Take dauert "
                    "dann 1–3 Minuten länger.",
        "mock": "Kein HF_TOKEN als Space-Secret gesetzt – dieses Modell liefert nur Test-Audio. "
                "Secret HF_TOKEN in den Space-Settings eintragen.",
        "unauthorized": "Privater Worker – HF_TOKEN mit Lesezugriff als Space-Secret setzen.",
        "private-needs-token": "Privater Worker – HF_TOKEN mit Lesezugriff als Space-Secret setzen.",
    }.get(state, "")
    hint_html = (
        f'<p class="model-status-hint" data-state="{escape(state)}">{escape(hint)}</p>' if hint else ""
    )
    vocals = "Gesang" if model.supports_vocals else "Instrumental"
    lyrics = "Lyrics" if model.supports_lyrics else "keine Lyrics"
    blockers = (
        f" · Worker-Blocker: {', '.join(model.blockers)}" if model.blockers else ""
    )
    hw = probe.get("hardware") or "—"
    return f"""
    <section class="model-status-card">
      <div><strong>{escape(model.label)}</strong><span>{escape(model.route_label)}</span></div>
      <div class="model-status-pills">
        <b data-state="{escape(state)}">{escape(status)}</b>
        <span>{vocals}</span><span>{lyrics}</span>
        <span>max. {model.max_duration_s} s</span>
        <span>HW {escape(str(hw))}</span>
      </div>
      <small>Worker <code>{escape(space_ref)}</code> · Stage <code>{escape(str(probe.get('stage')))}</code>
      · Route <code>{escape(route_id)}</code> · Adapter <code>{escape(model.adapter)}</code>
      {escape(blockers)}.</small>
      {hint_html}
    </section>
    """


def refresh_all_model_labels(registry: ModelRegistry) -> list[tuple[str, str]]:
    """Dropdown labels with live suffix: 'MiniMax Music 3 · Live'."""
    out: list[tuple[str, str]] = []
    for model in registry.models:
        probe = probe_space(model.space_ref) if model.space_ref else {"label": "?"}
        suffix = _LABEL_DE.get(str(probe.get("label")), str(probe.get("label")))
        out.append((f"{model.label} · {suffix} · {model.license_badge_label}", model.id))
    return out


def model_state(model_id: str, registry: ModelRegistry) -> str:
    """Kurzzustand eines Modells: live · sleeping · paused · mock · broken · unreachable …"""
    model = registry.get(model_id)
    if not model.space_ref:
        return "unreachable"
    state = str(_CACHE.get(model.space_ref).get("label"))
    if state == "live" and route_id_for_model(model).endswith("-mock"):
        state = "mock"
    return state


def is_unusable(state: str) -> bool:
    return state in {"paused", "broken", "unauthorized", "private-needs-token", "unreachable"}


def refresh_model_labels_cached(registry: ModelRegistry) -> list[tuple[str, str]]:
    """Wie refresh_all_model_labels, aber über den 20-s-Cache (für ↻ Status / Seitenaufruf)."""
    out: list[tuple[str, str]] = []
    for model in registry.models:
        state = model_state(model.id, registry)
        out.append((f"{model.label} · {_LABEL_DE.get(state, state).split(' (')[0]} · {model.license_badge_label}", model.id))
    return out