File size: 1,818 Bytes
af5b4af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Poll the submission and its run once a minute; print one line per state change. Exits on a terminal run state or rejection."""

import json
import sys
import time

sys.path.insert(0, "tools")
import httpx  # noqa: E402
from orkey import env_key  # noqa: E402

BASE = "https://openenvarena-arena.hf.space/api/openenv"
H = {"Authorization": "Bearer " + env_key("HF_TOKEN")}
sid = sys.argv[1]
last = None
while True:
    try:
        s = httpx.get(f"{BASE}/submissions/{sid}", headers=H, timeout=60).json()
        run = s.get("run") or {}
        line = f"submission={s.get('state')} slot={(s.get('slot') or {}).get('state')} errors={s.get('report', {}).get('errors')} origin={s.get('error_origin')} run={run.get('run_id')}"
        if run.get("run_id"):
            r = httpx.get(f"{BASE}/runs/{run['run_id']}", headers=H, timeout=60).json()
            state = "training" if r.get("state") in ("rolling", "updating") else r.get("state")
            line += f" run_state={state}"
            step = r.get("policy_version") or 0
            if r.get("state") in ("completed", "failed", "cancelled"):
                print(line, flush=True)
                print("DASHBOARD", run.get("dashboard"), flush=True)
                break
        if line != last:
            print(line, flush=True)
            last = line
        if run.get("run_id") and step // 50 > globals().get("_bucket", 0):
            globals()["_bucket"] = step // 50
            print(f"step {step}", flush=True)
        if s.get("state") == "rejected":
            print("REJECTED", json.dumps(s.get("report", {}).get("error_details"))[:800], flush=True)
            break
    except Exception as exc:  # transient proxy 502/503/504: the guide says retry unchanged
        print(f"poll error {type(exc).__name__}", flush=True)
    time.sleep(60)