File size: 2,689 Bytes
5d1db93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d1d9d2
5d1db93
 
 
 
 
 
 
 
 
6d1d9d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d1db93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Gradio wrapper for the "When Attention Closes" reproduction executor
(private Space).

Runs the stage named in ./stage.txt in a background thread on Space startup
(same stage functions as the org Docker executor, imported from
executor_entrypoint.py), pushes results to the bundle dataset repos, and
shows the latest payload in the UI.

Requires GPU hardware (Settings -> Hardware, e.g. L40S) for the smoke/run
stages, and the HF_TOKEN secret (Settings -> Variables and secrets).
"""
from __future__ import annotations

import json
import threading
import time
import traceback

import gradio as gr

import executor_entrypoint as ex

LATEST: dict = {"payload": {"state": "starting"}}


def _status_pusher() -> None:
    """Push the app's internal state to the mirror repo every 120 s so the
    run is observable without Space UI access."""
    while True:
        try:
            api = ex.hf_api()
            state = {
                "stage": ex.read_stage(),
                "ts": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
                "latest": LATEST["payload"],
            }
            api.upload_file(
                path_or_fileobj=json.dumps(state, indent=2, default=str).encode("utf-8"),
                path_in_repo="logs/status.json",
                repo_id=ex.DEST_MIRROR, repo_type="dataset")
        except Exception as exc:  # surface even push failures in the UI payload
            LATEST["pusher_error"] = repr(exc)[:300]
        time.sleep(120)


threading.Thread(target=_status_pusher, daemon=True).start()


def _worker() -> None:
    stage = ex.read_stage()
    try:
        payload = ex.run_stage(stage)
    except Exception:  # run_stage already captures; belt and braces
        payload = {"stage": stage, "error": traceback.format_exc()[-2000:]}
    LATEST["payload"] = payload


def fetch_status() -> dict:
    return LATEST["payload"]


threading.Thread(target=_worker, daemon=True).start()

with gr.Blocks(title="attention-closes executor (private)") as demo:
    gr.Markdown(
        "Private executor for the When-Attention-Closes reproduction "
        "(arXiv:2605.12922v2, Mistral-7B Table 1 cells). The stage in "
        "`stage.txt` runs on every restart; results are pushed to "
        "[Frame17/attention-closes-repro](https://huggingface.co/datasets/Frame17/attention-closes-repro) "
        "(logs/) and shown below. **Pause the Space when a stage has finished "
        "to stop GPU billing.**"
    )
    status = gr.JSON(value=fetch_status, label="Last run payload")
    refresh = gr.Button("Refresh")
    refresh.click(fn=fetch_status, outputs=status)

if __name__ == "__main__":
    demo.launch()