File size: 7,675 Bytes
2274b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""Add the nearest-normal-photo index to the Space (backend + data, one commit; the thumbnails ship with the screens).

    python -m deployment.space.references_release --dry-run     # read-only: shows what would change
    python -m deployment.space.references_release --stage DIR   # rebuilds the patched Space tree in DIR, boots it, queries the index
    python -m deployment.space.references_release --publish    # one commit: patched app.py + backend/references.py + 12 index files
    python -m deployment.space.references_release --rollback    # restores the saved app.py and removes the new files

The change is additive: results gain a "references" list (ids and similarities); detection is untouched. Publish the new
screens afterwards with deployment.space.publish --frontend-only (the thumbnails are in frontend/public/assets/references).
The live deployment/space/app.py is patched with exact-text replacements that must each match once.
"""

import argparse
import json
import subprocess
import sys
from pathlib import Path

from deployment.space.tuned_release import CATEGORIES, OUT, REPO, ROOT, boot, lf, once

TARGET = "deployment/space/app.py"
NEW_FILES = ["backend/references.py"] + [f"models/highres_tuned/references/{c}.npz" for c in CATEGORIES]
PROBE = (
    "import sys\n"
    "import numpy as np\n"
    "import torch\n"
    "from pathlib import Path\n"
    "from backend.references import ReferenceIndex\n"
    "index=ReferenceIndex(Path('models/highres_tuned/references'))\n"
    "tokens=torch.randn(64,384)\n"
    "found=index.nearest('pcb1',tokens,np.ones(64,dtype=bool))\n"
    "assert len(found)==3 and all(r['id'].startswith('pcb1/') for r in found), found\n"
    "assert index.nearest('not_a_product',tokens,np.ones(64,dtype=bool))==[]\n"
    "print('STAGED INDEX OK:', found[0]['id'], round(found[0]['similarity'],3))\n"
)


def patch(live):
    text = once(
        live,
        "from backend.storage import now\n",
        "from backend.storage import now\nfrom backend.references import ReferenceIndex\n",
        TARGET,
    )
    text = once(
        text,
        ".to(DEVICE)\n\n\n@spaces.GPU(duration=20)\n",
        ".to(DEVICE)\n"
        "# Nearest normal reference photos (an explanation aid only; the score and verdict never use it).\n"
        "REFERENCES = ReferenceIndex(BANK_DIRECTORY/'references') if PROFILE == 'dinov2_tuned_v1' else None\n\n\n"
        "@spaces.GPU(duration=20)\n",
        TARGET,
    )
    text = once(
        text,
        "    return {'raw': raw, 'geometry': geometry, 'score': score,\n",
        "    references = REFERENCES.nearest(category, tokens, mask) if REFERENCES is not None else []\n"
        "    return {'raw': raw, 'geometry': geometry, 'score': score, 'references': references,\n",
        TARGET,
    )
    text = once(
        text,
        "    record = {key:output[key] for key in ('inference_ms','device','gpu_name','peak_gpu_mb','foreground_fraction')}\n",
        "    record = {key:output[key] for key in ('inference_ms','device','gpu_name','peak_gpu_mb','foreground_fraction')}\n"
        "    if output.get('references'):record['references']=output['references']\n",
        TARGET,
    )
    compile(text, TARGET, "exec")
    return text


def plan(api):
    from huggingface_hub import hf_hub_download

    before = api.space_info(REPO)
    if api.get_space_runtime(REPO).requested_hardware != "zero-a10g":
        raise SystemExit("The Space is not on the expected zero-a10g hardware")
    live = lf(Path(hf_hub_download(REPO, TARGET, repo_type="space", revision=before.sha)).read_bytes()).decode("utf-8")
    if "ReferenceIndex" in live:
        raise SystemExit("The Space already has the reference index")
    for name in NEW_FILES:
        if not (ROOT / name).is_file():
            raise SystemExit(f"Missing local file: {name}")
    return before, live, patch(live)


def stage(api, directory):
    from huggingface_hub import snapshot_download

    before, _, patched = plan(api)
    target = Path(directory)
    if target.exists() and any(target.iterdir()):
        raise SystemExit("Choose an empty directory for staging")
    snapshot_download(
        REPO,
        repo_type="space",
        revision=before.sha,
        local_dir=target,
        allow_patterns=[
            "backend/*",
            "deployment/space/*.py",
            "results/*.json",
            "models/highres/*.json",
            "models/highres_tuned/*",
            "models/encoder_dinov2_tuned/*",
            "knowledge/*",
            "app.py",
        ],
    )
    (target / TARGET).write_bytes(patched.encode("utf-8"))
    for name in NEW_FILES:
        (target / name).parent.mkdir(parents=True, exist_ok=True)
        (target / name).write_bytes((ROOT / name).read_bytes())
    boot(target)
    result = subprocess.run(
        [sys.executable, "-c", PROBE],
        cwd=target,
        capture_output=True,
        text=True,
        timeout=300,
        env={**__import__("os").environ, "PYTHONPATH": str(target)},
    )
    print(result.stdout.strip() or result.stderr.strip()[-1500:])
    if result.returncode:
        raise SystemExit("The staged Space tree failed the index check; nothing was uploaded")


def main():
    parser = argparse.ArgumentParser()
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument("--dry-run", action="store_true")
    group.add_argument("--stage", metavar="DIR")
    group.add_argument("--publish", action="store_true")
    group.add_argument("--rollback", action="store_true")
    args = parser.parse_args()
    from huggingface_hub import CommitOperationAdd, CommitOperationDelete, HfApi

    api = HfApi()
    if args.rollback:
        receipt = json.loads((OUT / "publish-references.json").read_text())
        saved = (Path(receipt["rollback_directory"]) / TARGET).read_bytes()
        before = api.space_info(REPO)
        operations = [CommitOperationAdd(path_in_repo=TARGET, path_or_fileobj=saved)]
        operations += [CommitOperationDelete(path_in_repo=name) for name in NEW_FILES]
        commit = api.create_commit(
            repo_id=REPO,
            repo_type="space",
            operations=operations,
            parent_commit=before.sha,
            commit_message="Remove the nearest normal photo index again",
        )
        print(json.dumps({"rollback_commit": commit.oid}))
        return
    if args.stage:
        stage(api, args.stage)
        return
    before, live, patched = plan(api)
    summary = {"parent": before.sha, "patched_live_files": [TARGET], "new_files": len(NEW_FILES)}
    if args.dry_run:
        print(json.dumps(summary, indent=2))
        return
    rollback = OUT / f"references-rollback-{before.sha[:12]}"
    (rollback / TARGET).parent.mkdir(parents=True, exist_ok=True)
    (rollback / TARGET).write_bytes(live.encode("utf-8"))
    operations = [CommitOperationAdd(path_in_repo=TARGET, path_or_fileobj=patched.encode("utf-8"))]
    for name in NEW_FILES:
        path = ROOT / name
        data = lf(path.read_bytes()) if path.suffix == ".py" else str(path)
        operations.append(CommitOperationAdd(path_in_repo=name, path_or_fileobj=data))
    commit = api.create_commit(
        repo_id=REPO,
        repo_type="space",
        operations=operations,
        parent_commit=before.sha,
        commit_message="Inspection results name the closest normal reference photos (explanation only)",
    )
    receipt = {**summary, "commit": commit.oid, "rollback_directory": str(rollback)}
    (OUT / "publish-references.json").write_text(json.dumps(receipt, indent=2))
    print(json.dumps(receipt))


if __name__ == "__main__":
    sys.exit(main())