okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221progress words: step 6 with nothing to prepare claims no preparation3af562d54

main
7.6 KiB133 linesraw
1"""What the person is told while a model downloads and is prepared: ONE place for the words, read by the app (through
2tools/strata_mcp.py `install_progress`) and by the Terminal (`strata launch`, strata_cli/launch/runner.py).
3
4Setup prints its work in seven steps and the tools it runs print their command lines (`> python tools/strata_pack.py build ...`)
5and progress lines (`name: 1.20 / 5.04 GB (24%)`). Those titles are for people who read the setup log ("writing the start script",
6"preparing the model for Strata"), and the name in a progress line is a file name or an internal label ("MTP draft layer",
7"llama.cpp source"). This turns what setup last printed into one plain sentence that says what is really happening now.
8The sentences are English; the app translates them like its other texts.
9
10Nothing here reads a file or starts a process: it works on the last lines of setup's output.
11"""
12
13from __future__ import annotations
14
15import re
16from dataclasses import dataclass
17
18STEP_RE = re.compile(r"=== Step (\d+): (.+?) ===")
19CHECKING_RE = re.compile(r"^\s*checking\b.*\bSHA-256\b", re.I)
20PROGRESS_RE = re.compile(r"\s*(\S.*?): +([\d.]+) / ([\d.]+) GB \((\d+)%\)")
21try:                                                     # the network rule's own line and words (one place for them)
22    from strata_control.netpolicy import WAITING_RE, WORDS as _NET_WORDS
23    WAITING_WORDS = _NET_WORDS["waiting"]
24except ImportError:                                      # an older strata_control beside a newer reader: never waiting
25    WAITING_RE = re.compile(r"(?!)")
26    WAITING_WORDS = ""
27
28
29@dataclass(frozen=True)
30class Words:
31    phase: str        # a short key for tests and for the app to pick an icon: check, helpers, engine, download, verify, pictures,
32    #                   pack, tokenizer, draft_download, draft, settings, ...
33    label: str        # the sentence
34
35    def as_dict(self) -> dict:
36        return {"phase": self.phase, "label": self.label}
37
38
39def _after_last_step(lines: list[str]) -> list[str]:
40    for i in range(len(lines) - 1, -1, -1):
41        if STEP_RE.search(lines[i]):
42            return lines[i + 1:]
43    return lines
44
45
46def _last_matching(lines: list[str], *needles: str) -> str | None:
47    """The newest line that holds one of the needles (lower case compare)."""
48    for line in reversed(lines):
49        low = line.lower()
50        if any(n in low for n in needles):
51            return line
52    return None
53
54
55def describe(step: int | None, lines: list[str], download: dict | None = None) -> Words:
56    """The sentence for setup's step `step` (1 to 7; None when unknown) given the lines it printed since the step began and the
57    download figure in them (`{"file": ..., "percent": ...}` or None)."""
58    recent = _after_last_step(lines)
59    dl_file = str((download or {}).get("file", "")).lower()
60    last_line = next((ln for ln in reversed(recent) if ln.strip()), "")
61    waiting = WAITING_RE.match(last_line)
62    if waiting:                                          # a server has not answered yet (strata_control/netpolicy.py prints this line)
63        return Words("waiting", WAITING_WORDS.format(host=waiting.group("host"), seconds=waiting.group("seconds")))
64    if step is None:
65        return Words("working", "Getting your model ready")
66    if step <= 2:
67        return Words("check", "Checking your computer")
68    if step == 3:
69        return Words("helpers", "Getting the helper packages ready")
70    if step == 4:
71        if "engine" in dl_file:
72            return Words("engine", "Downloading the engine for this PC")
73        if "source" in dl_file or "llama.cpp" in dl_file:
74            return Words("helpers", "Downloading the small tools used to prepare models")
75        return Words("engine", "Checking the engine for this PC")
76    if step == 5:
77        last = recent[-1] if recent else ""
78        for line in reversed(recent):                    # a file with a wrong SHA-256 is read again and only the bad parts rewritten
79            if "were damaged; downloading only those parts" in line:
80                return Words("repair", "Some parts of a model file were damaged; downloading only those parts again")
81            if CHECKING_RE.search(line) or "were different and have been written again" in line:
82                break
83        if dl_file.startswith("checking") or CHECKING_RE.search(last):
84            return Words("verify", "Checking that the model files are complete (a few minutes for a big model; nothing is downloaded)")
85        if "vision" in dl_file or "picture" in dl_file or "mmproj" in dl_file:
86            return Words("pictures", "Downloading the picture reader")
87        if download and not dl_file.startswith("checking"):
88            return Words("download", "Downloading the model files")
89        if _last_matching(recent, "already downloaded", "model files present"):
90            return Words("verify", "The model files are on this computer; checking them")
91        return Words("download", "Getting the model files")
92    if step == 6:
93        marker = _last_matching(recent, "mtp_rt.py", "mtp_pack.py", "mtp_fetch.py", "mtp draft layer:", "strata_pack.py", "pack_index.py",
94                                "strata_tokenizer.py", "iq_pack.py", "nvfp4_local_pack.py", "converting the q2_0 experts",
95                                "writing the experts into one file", "of mtp tensors", "[ok] model prepared")
96        low = (marker or "").lower()
97        if not marker and dl_file.startswith("mtp draft"):
98            low = "mtp draft layer:"
99        if "[ok] mtp draft layer" in low:
100            return Words("draft", "Preparing the draft layer")
101        if "mtp_fetch.py" in low or "mtp draft layer:" in low or "of mtp tensors" in low:
102            return Words("draft_download", "Downloading the draft layer (about 5 GB; it makes answers about twice as fast)")
103        if "mtp_rt.py" in low or "mtp_pack.py" in low:
104            return Words("draft", "Preparing the draft layer")
105        if "[ok] model prepared" in low or not marker:      # nothing is being prepared (the model is prepared, or step 6 has only begun) and no draft-layer step runs:
106            return Words("verify", "The model files are on this computer; checking them")    # words that claim no preparation (0.2.14.1 check: no "(one time)" on every launch)
107        if "strata_pack.py" in low or "converting the q2_0 experts" in low:
108            return Words("pack", "Packing the model's experts for your processor (one time, a few minutes)")
109        if "pack_index.py" in low:
110            return Words("pack", "Indexing the packed experts")
111        if "strata_tokenizer.py" in low:
112            return Words("tokenizer", "Preparing the tokenizer")
113        if "writing the experts into one file" in low or ("iq_pack.py" in low and "--experts-bin" in low):
114            return Words("pack", "Packing the experts into one file for low-memory mode (one time)")
115        if "iq_pack.py" in low:
116            return Words("pack", "Preparing the model data for the engine (one time)")
117        if "nvfp4_local_pack.py" in low:
118            return Words("pack", "Converting NVIDIA's FP4 checkpoint on this PC (20 to 40 minutes, one time)")
119        return Words("pack", "Preparing your model for Strata (one time)")
120    return Words("settings", "Saving the model's settings")
121
122
123def with_numbers(words: Words, download: dict | None) -> str:
124    """The sentence with the figure after it when there is one: `Downloading the model files: 12.3 of 66.4 GB`."""
125    if not download:
126        return words.label
127    try:
128        done, total = float(download["done_gb"]), float(download["total_gb"])
129    except (KeyError, TypeError, ValueError):
130        return words.label
131    if total <= 0.05:
132        return words.label
133    return f"{words.label}: {done:.1f} of {total:.1f} GB"