Spaces:
Sleeping
Sleeping
Sync from simready-oem-library-pm@d1ecea89
Browse files- tools/hf_space/app.py +431 -431
- tools/hf_space/runner.py +0 -0
tools/hf_space/app.py
CHANGED
|
@@ -1,431 +1,431 @@
|
|
| 1 |
-
"""SimReady Validator — Gradio UI for the HuggingFace Space.
|
| 2 |
-
|
| 3 |
-
Two surfaces, same engine:
|
| 4 |
-
|
| 5 |
-
- **/run** (the on-screen button) — streams log lines to the UI for
|
| 6 |
-
interactive use by an operator in the browser.
|
| 7 |
-
- **/run_api** (hidden, programmatic) — returns the full RunResult as
|
| 8 |
-
a JSON-serializable dict. This is what `tools/hf_watch/call_hf_space.py`
|
| 9 |
-
hits from the GitHub Actions runner so the workflow can patch
|
| 10 |
-
status.json and asset-status.json without scraping the UI's text.
|
| 11 |
-
|
| 12 |
-
Both go through `runner.run()`. The split is purely about output
|
| 13 |
-
shape (streaming text vs. one-shot dict).
|
| 14 |
-
|
| 15 |
-
The Space is internal-pilot scope: HF_TOKEN comes from the Space's
|
| 16 |
-
secrets, NOT from the requester. When a customer's dataset PR triggers
|
| 17 |
-
this (next milestone), the webhook payload identifies the dataset and
|
| 18 |
-
the Space's own token opens the verdict PR.
|
| 19 |
-
"""
|
| 20 |
-
from __future__ import annotations
|
| 21 |
-
|
| 22 |
-
import json
|
| 23 |
-
import os
|
| 24 |
-
from pathlib import Path
|
| 25 |
-
|
| 26 |
-
import gradio as gr
|
| 27 |
-
|
| 28 |
-
from runner import (run as run_validator, progress_path_for, cancel_path_for,
|
| 29 |
-
run_token_path_for, CANCEL_DIR)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
PROFILE_CHOICES = [
|
| 33 |
-
"Prop-Robotics-Neutral",
|
| 34 |
-
"Prop-Robotics-Physx",
|
| 35 |
-
"Prop-Robotics-Isaac",
|
| 36 |
-
"Robot-Body-Neutral",
|
| 37 |
-
"Robot-Body-Runnable",
|
| 38 |
-
"Robot-Body-Isaac",
|
| 39 |
-
"Package",
|
| 40 |
-
"Package-Candidate",
|
| 41 |
-
]
|
| 42 |
-
DEFAULT_PROFILE = "Prop-Robotics-Neutral"
|
| 43 |
-
DEFAULT_VERSION = "1.0.0"
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
def _run_api(dataset: str, profile: str, version: str, open_pr: bool,
|
| 47 |
-
submission_id: str = "", force: bool = False,
|
| 48 |
-
preliminary: bool = False, use_kit: bool = False) -> dict:
|
| 49 |
-
"""Programmatic endpoint. Returns the RunResult as a JSON dict.
|
| 50 |
-
|
| 51 |
-
Caller is typically `tools/hf_watch/call_hf_space.py` running from
|
| 52 |
-
a GitHub Actions ubuntu-latest runner. Output shape must stay
|
| 53 |
-
stable — bump `schema_version` if you change it. The receiver
|
| 54 |
-
pattern-matches on the same field names `tools/hf_watch/validate.py`
|
| 55 |
-
produces, so status.json patching is identical regardless of which
|
| 56 |
-
backend ran the validation.
|
| 57 |
-
|
| 58 |
-
`submission_id` is optional — when set, the validator writes
|
| 59 |
-
per-asset progress to /tmp/sr-progress/<id>.json, which the
|
| 60 |
-
get_progress endpoint serves to the dashboard.
|
| 61 |
-
|
| 62 |
-
`preliminary` switches the runner to a structure-only sweep:
|
| 63 |
-
zip-bundled datasets are scanned (instead of failing
|
| 64 |
-
PKG.NO-ARCHIVES at the listing stage) and per-asset validation is
|
| 65 |
-
sliced to the first asset only. Used by the dashboard's
|
| 66 |
-
Preliminary scan tab.
|
| 67 |
-
"""
|
| 68 |
-
print(f"[run_api] preliminary={preliminary!r} force={force!r} "
|
| 69 |
-
f"use_kit={use_kit!r} submission_id={submission_id!r}", flush=True)
|
| 70 |
-
# Untrusted callers can hit /run_api directly — profile/version flow
|
| 71 |
-
# into the validator's argv, so validate them before use. Empty
|
| 72 |
-
# falls back to the defaults (existing behavior).
|
| 73 |
-
import re
|
| 74 |
-
profile = profile or DEFAULT_PROFILE
|
| 75 |
-
if profile not in PROFILE_CHOICES and profile.lower() != "auto":
|
| 76 |
-
raise ValueError(f"invalid profile: {profile!r}")
|
| 77 |
-
version = (version or DEFAULT_VERSION).strip()
|
| 78 |
-
if not re.fullmatch(r"[\w.\-]+", version):
|
| 79 |
-
raise ValueError(f"invalid version: {version!r}")
|
| 80 |
-
result = run_validator(
|
| 81 |
-
dataset=(dataset or "").strip(),
|
| 82 |
-
profile=profile,
|
| 83 |
-
version=version,
|
| 84 |
-
open_pr=bool(open_pr),
|
| 85 |
-
submission_id=(submission_id or "").strip(),
|
| 86 |
-
force=bool(force),
|
| 87 |
-
preliminary=bool(preliminary),
|
| 88 |
-
use_kit=bool(use_kit),
|
| 89 |
-
)
|
| 90 |
-
return {
|
| 91 |
-
"schema_version": 1,
|
| 92 |
-
"dataset": result.dataset,
|
| 93 |
-
"profile": result.profile,
|
| 94 |
-
"version": result.version,
|
| 95 |
-
"status": result.status,
|
| 96 |
-
"summary": result.summary,
|
| 97 |
-
"results_json": _sanitize_results_json(result.results_json),
|
| 98 |
-
"pr_url": result.pr_url,
|
| 99 |
-
}
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
def _list_profiles() -> dict:
|
| 103 |
-
"""Return the set of profiles that actually load on this Space's
|
| 104 |
-
foundation+validator combination. The dashboard polls this to
|
| 105 |
-
populate its dropdown so operators can't pick a profile that
|
| 106 |
-
would fatally fail at registration time.
|
| 107 |
-
|
| 108 |
-
Uses --use-plugin since the default CLI loader has known
|
| 109 |
-
registration mismatches against the current foundation pin; the
|
| 110 |
-
plugin path is what runner.py's streaming-zip flow falls back
|
| 111 |
-
to and is the source of truth for "actually usable" here.
|
| 112 |
-
|
| 113 |
-
Output format from validate.py is `PROFILE: <id> v<version>`
|
| 114 |
-
per profile, one per line.
|
| 115 |
-
"""
|
| 116 |
-
import subprocess, sys
|
| 117 |
-
from runner import VALIDATOR
|
| 118 |
-
try:
|
| 119 |
-
proc = subprocess.run(
|
| 120 |
-
# --list-profiles only ENUMERATES registered profiles from the
|
| 121 |
-
# spec/plugin registry (--use-plugin) — it runs no validation
|
| 122 |
-
# rules, so it never needs Kit. Force --no-use-kit: on a
|
| 123 |
-
# Kit-enabled image the validator auto-enables --use-kit for the
|
| 124 |
-
# PhysX-bearing default profile and boots the full Isaac Sim
|
| 125 |
-
# runtime (~5 min) just to print the list, blowing the 300s
|
| 126 |
-
# timeout below. Actual validation (runner.py) still uses Kit.
|
| 127 |
-
[sys.executable, str(VALIDATOR), "--list-profiles", "--use-plugin", "--no-use-kit"],
|
| 128 |
-
capture_output=True, text=True, timeout=300,
|
| 129 |
-
)
|
| 130 |
-
names: list[str] = []
|
| 131 |
-
for line in (proc.stdout or "").splitlines():
|
| 132 |
-
s = line.strip()
|
| 133 |
-
# Validator emits "PROFILE: <id> v<version>" — that's our
|
| 134 |
-
# only authoritative shape. Anything else is noise.
|
| 135 |
-
if s.startswith("PROFILE:"):
|
| 136 |
-
rest = s[len("PROFILE:"):].strip()
|
| 137 |
-
pid = rest.split()[0] if rest else ""
|
| 138 |
-
if pid:
|
| 139 |
-
names.append(pid)
|
| 140 |
-
# Dedupe while preserving order.
|
| 141 |
-
seen = set()
|
| 142 |
-
unique = []
|
| 143 |
-
for n in names:
|
| 144 |
-
if n not in seen:
|
| 145 |
-
seen.add(n)
|
| 146 |
-
unique.append(n)
|
| 147 |
-
result: dict = {"profiles": unique, "schema_version": 1, "rc": proc.returncode}
|
| 148 |
-
if not unique:
|
| 149 |
-
# No profiles registered AND no parse hits — surface why so
|
| 150 |
-
# the dashboard can show something useful. Truncate so the
|
| 151 |
-
# JSON response stays small.
|
| 152 |
-
stderr_tail = "\n".join((proc.stderr or "").splitlines()[-20:])[:2000]
|
| 153 |
-
stdout_tail = "\n".join((proc.stdout or "").splitlines()[-20:])[:2000]
|
| 154 |
-
result["stderr_tail"] = stderr_tail
|
| 155 |
-
result["stdout_tail"] = stdout_tail
|
| 156 |
-
return result
|
| 157 |
-
except subprocess.TimeoutExpired:
|
| 158 |
-
return {"profiles": [], "error": "timeout after 300s (spec load >5 min)"}
|
| 159 |
-
except Exception as e:
|
| 160 |
-
return {"profiles": [], "error": f"{type(e).__name__}: {e}"}
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
def _cancel_run(submission_id: str, run_token: str = "") -> dict:
|
| 164 |
-
"""Write the cancel-signal file for a given submission. The
|
| 165 |
-
streaming-zip loop in runner.py checks for this file between zips
|
| 166 |
-
and aborts when present. Idempotent — calling multiple times has no
|
| 167 |
-
extra effect; consuming runner.py deletes it.
|
| 168 |
-
|
| 169 |
-
`run_token` is the per-run token the dashboard read from get_progress.
|
| 170 |
-
It becomes the flag's content so runner._is_cancelled only honors it
|
| 171 |
-
for the exact run it was issued against — a flag left over from a
|
| 172 |
-
prior run of this submission can never abort a fresh one."""
|
| 173 |
-
sid = (submission_id or "").strip()
|
| 174 |
-
if not sid:
|
| 175 |
-
return {"state": "no_id"}
|
| 176 |
-
path = cancel_path_for(sid)
|
| 177 |
-
if path is None:
|
| 178 |
-
return {"state": "no_id"}
|
| 179 |
-
try:
|
| 180 |
-
CANCEL_DIR.mkdir(parents=True, exist_ok=True)
|
| 181 |
-
path.write_text((run_token or "").strip(), encoding="utf-8")
|
| 182 |
-
return {"state": "signaled", "path": str(path)}
|
| 183 |
-
except OSError as e:
|
| 184 |
-
return {"state": "error", "error": f"{type(e).__name__}: {e}"}
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
def _get_progress(submission_id: str) -> dict:
|
| 188 |
-
"""Read the validator's per-asset progress file for this submission.
|
| 189 |
-
|
| 190 |
-
Polled by the dashboard ~every 3 s while a Validate-now click is
|
| 191 |
-
in-flight, so the "Validate now" button can fill up as the
|
| 192 |
-
validator works through the asset list.
|
| 193 |
-
|
| 194 |
-
Returns one of three shapes:
|
| 195 |
-
- {"state": "not_found"} — no progress file (Space restarted, or
|
| 196 |
-
the dashboard is polling a Space-run that never happened).
|
| 197 |
-
- {"state": "starting"} — file seeded by runner.py before the
|
| 198 |
-
validator started its loop. processed/total are 0.
|
| 199 |
-
- {processed, total, current, started_at, updated_at} — live
|
| 200 |
-
per-asset progress written by validate.py._emit_progress.
|
| 201 |
-
|
| 202 |
-
Every shape also carries `run_token` (the current run's cancel
|
| 203 |
-
token, from the sidecar file) when one exists, so the dashboard can
|
| 204 |
-
echo it back to cancel_run and target the exact run.
|
| 205 |
-
|
| 206 |
-
Caller treats anything with total > 0 as "show the fill bar".
|
| 207 |
-
"""
|
| 208 |
-
sid = (submission_id or "").strip()
|
| 209 |
-
if not sid:
|
| 210 |
-
return {"state": "no_id"}
|
| 211 |
-
# Per-run cancel token (sidecar; see runner.run_token_path_for).
|
| 212 |
-
# Surfaced on every shape so the dashboard can echo it back to
|
| 213 |
-
# cancel_run — a cancel then only aborts the run it was issued
|
| 214 |
-
# against, never a later one that reused the submission_id.
|
| 215 |
-
run_token = ""
|
| 216 |
-
tok_path = run_token_path_for(sid)
|
| 217 |
-
if tok_path and tok_path.is_file():
|
| 218 |
-
try:
|
| 219 |
-
run_token = tok_path.read_text(encoding="utf-8").strip()
|
| 220 |
-
except OSError:
|
| 221 |
-
pass
|
| 222 |
-
path = progress_path_for(sid)
|
| 223 |
-
if path is None or not path.is_file():
|
| 224 |
-
return {"state": "not_found", "run_token": run_token}
|
| 225 |
-
try:
|
| 226 |
-
data = json.loads(path.read_text(encoding="utf-8"))
|
| 227 |
-
if isinstance(data, dict) and run_token:
|
| 228 |
-
data["run_token"] = run_token
|
| 229 |
-
return data
|
| 230 |
-
except (OSError, json.JSONDecodeError):
|
| 231 |
-
# Mid-write — caller will poll again in a few seconds.
|
| 232 |
-
return {"state": "transient", "run_token": run_token}
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
def _sanitize_results_json(raw: dict) -> dict:
|
| 236 |
-
"""Strip absolute filesystem paths from results_json before returning.
|
| 237 |
-
|
| 238 |
-
Gradio's JSON serializer treats string fields that resolve to files
|
| 239 |
-
on the Space's filesystem as downloadable references and tries to
|
| 240 |
-
serve them through `/gradio_api/file=...`. The validator's
|
| 241 |
-
results.json contains absolute paths (target dir + per-asset
|
| 242 |
-
`path`) which point into the Space's ephemeral tempdir and are
|
| 243 |
-
NOT exposed through gradio's allowed_paths — gradio_client then
|
| 244 |
-
fails with 403 trying to auto-fetch them after a successful run.
|
| 245 |
-
|
| 246 |
-
Callers don't need filesystem paths anyway — only `rel_path`
|
| 247 |
-
(dataset-relative), `passed`, and `issues` are used downstream.
|
| 248 |
-
Keep the rest of the report intact (profile_coverage, summary,
|
| 249 |
-
layout_findings, etc.).
|
| 250 |
-
"""
|
| 251 |
-
if not isinstance(raw, dict):
|
| 252 |
-
return raw
|
| 253 |
-
sanitized = {k: v for k, v in raw.items() if k != "target"}
|
| 254 |
-
if "results" in sanitized and isinstance(sanitized["results"], list):
|
| 255 |
-
sanitized["results"] = [
|
| 256 |
-
{k: v for k, v in asset.items() if k != "path"}
|
| 257 |
-
for asset in sanitized["results"]
|
| 258 |
-
if isinstance(asset, dict)
|
| 259 |
-
]
|
| 260 |
-
# Specs/dashboard dir paths are local to the Space, useless to caller.
|
| 261 |
-
for k in ("specs_docs_dir", "dashboard_docs_dir"):
|
| 262 |
-
sanitized.pop(k, None)
|
| 263 |
-
return sanitized
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
def _run_streaming(dataset: str, profile: str, version: str, open_pr: bool):
|
| 267 |
-
"""Generator that yields incremental log output to the UI as the
|
| 268 |
-
validator runs. Gradio streams each yielded tuple to the connected
|
| 269 |
-
outputs."""
|
| 270 |
-
lines: list[str] = []
|
| 271 |
-
|
| 272 |
-
def log(line: str) -> None:
|
| 273 |
-
lines.append(line)
|
| 274 |
-
|
| 275 |
-
yield "\n".join(lines), "", "(running…)", None
|
| 276 |
-
|
| 277 |
-
try:
|
| 278 |
-
result = run_validator(
|
| 279 |
-
dataset=dataset.strip(),
|
| 280 |
-
profile=profile,
|
| 281 |
-
version=version.strip() or DEFAULT_VERSION,
|
| 282 |
-
open_pr=open_pr,
|
| 283 |
-
log=log,
|
| 284 |
-
)
|
| 285 |
-
except Exception as e:
|
| 286 |
-
lines.append(f"\nERROR: {type(e).__name__}: {e}")
|
| 287 |
-
yield "\n".join(lines), "", f"error: {e}", None
|
| 288 |
-
return
|
| 289 |
-
|
| 290 |
-
status_badge = f"**{result.status.upper()}** — {result.summary}"
|
| 291 |
-
if result.pr_url:
|
| 292 |
-
status_badge += f"\n\nPR: {result.pr_url}"
|
| 293 |
-
|
| 294 |
-
report_index = result.report_path / "index.html"
|
| 295 |
-
report_url = str(report_index) if report_index.is_file() else None
|
| 296 |
-
|
| 297 |
-
yield (
|
| 298 |
-
"\n".join(lines),
|
| 299 |
-
status_badge,
|
| 300 |
-
result.summary,
|
| 301 |
-
report_url,
|
| 302 |
-
)
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
def _read_md(name: str) -> str:
|
| 306 |
-
"""Return the contents of name (relative to this file's dir),
|
| 307 |
-
stripping a leading YAML frontmatter block if present. Falls back
|
| 308 |
-
to a friendly stub when the file is missing — keeps the Space
|
| 309 |
-
bootable even before the space-deploy workflow has synced the
|
| 310 |
-
assembled docs into the container."""
|
| 311 |
-
from pathlib import Path
|
| 312 |
-
p = Path(__file__).resolve().parent / name
|
| 313 |
-
try:
|
| 314 |
-
src = p.read_text(encoding="utf-8")
|
| 315 |
-
except FileNotFoundError:
|
| 316 |
-
return f"_{name} not yet synced into this Space — check back after the next deploy._"
|
| 317 |
-
if src.startswith("---"):
|
| 318 |
-
end = src.find("\n---\n", 4)
|
| 319 |
-
if end > 0:
|
| 320 |
-
src = src[end + len("\n---\n"):].lstrip()
|
| 321 |
-
return src
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
with gr.Blocks(title="SimReady Validator") as demo:
|
| 325 |
-
with gr.Tabs():
|
| 326 |
-
with gr.Tab("Overview"):
|
| 327 |
-
gr.Markdown(_read_md("README.md"))
|
| 328 |
-
with gr.Tab("Validator"):
|
| 329 |
-
gr.Markdown(
|
| 330 |
-
"Submit a HuggingFace dataset to validate against a SimReady "
|
| 331 |
-
"profile. With **Open PR** enabled, the verdict is uploaded "
|
| 332 |
-
"back to the dataset as a `validation/` pull request."
|
| 333 |
-
)
|
| 334 |
-
with gr.Row():
|
| 335 |
-
dataset = gr.Textbox(
|
| 336 |
-
label="Dataset",
|
| 337 |
-
placeholder="org/dataset (e.g. imagineio/PhysicalAI-SimReady-Kitchens-v1)",
|
| 338 |
-
)
|
| 339 |
-
with gr.Row():
|
| 340 |
-
profile = gr.Dropdown(
|
| 341 |
-
choices=PROFILE_CHOICES, value=DEFAULT_PROFILE, label="Profile",
|
| 342 |
-
)
|
| 343 |
-
version = gr.Textbox(label="Version", value=DEFAULT_VERSION)
|
| 344 |
-
open_pr = gr.Checkbox(label="Open PR on dataset with verdict", value=False)
|
| 345 |
-
run_btn = gr.Button("Validate", variant="primary")
|
| 346 |
-
status_md = gr.Markdown(label="Verdict")
|
| 347 |
-
summary_box = gr.Textbox(label="Summary", interactive=False)
|
| 348 |
-
log_box = gr.Textbox(label="Log", lines=20, interactive=False)
|
| 349 |
-
report_link = gr.File(label="HTML report (download)", interactive=False)
|
| 350 |
-
with gr.Tab("Partner walkthrough"):
|
| 351 |
-
gr.Markdown(_read_md("VALIDATE.md"))
|
| 352 |
-
|
| 353 |
-
run_btn.click(
|
| 354 |
-
fn=_run_streaming,
|
| 355 |
-
inputs=[dataset, profile, version, open_pr],
|
| 356 |
-
outputs=[log_box, status_md, summary_box, report_link],
|
| 357 |
-
api_name="run",
|
| 358 |
-
)
|
| 359 |
-
|
| 360 |
-
# Programmatic endpoint — bound to invisible components so the UI
|
| 361 |
-
# doesn't render anything extra, but the Gradio queue still exposes
|
| 362 |
-
# an `/api/predict/run_api` route the gradio_client can hit. The
|
| 363 |
-
# outputs[0] is the JSON return; api_name turns it into a stable
|
| 364 |
-
# path the GitHub Actions caller depends on.
|
| 365 |
-
api_dataset = gr.Textbox(visible=False)
|
| 366 |
-
api_profile = gr.Textbox(visible=False)
|
| 367 |
-
api_version = gr.Textbox(visible=False)
|
| 368 |
-
api_open_pr = gr.Checkbox(visible=False)
|
| 369 |
-
api_submission_id = gr.Textbox(visible=False)
|
| 370 |
-
api_force = gr.Checkbox(visible=False)
|
| 371 |
-
api_preliminary = gr.Checkbox(visible=False)
|
| 372 |
-
api_use_kit = gr.Checkbox(visible=False)
|
| 373 |
-
api_output = gr.JSON(visible=False)
|
| 374 |
-
api_button = gr.Button(visible=False)
|
| 375 |
-
api_button.click(
|
| 376 |
-
fn=_run_api,
|
| 377 |
-
inputs=[api_dataset, api_profile, api_version, api_open_pr,
|
| 378 |
-
api_submission_id, api_force, api_preliminary, api_use_kit],
|
| 379 |
-
outputs=api_output,
|
| 380 |
-
api_name="run_api",
|
| 381 |
-
)
|
| 382 |
-
|
| 383 |
-
# Progress endpoint — polled by the dashboard while a row is
|
| 384 |
-
# validating. CORS is open on /gradio_api/* by default, so the
|
| 385 |
-
# browser can fetch this from github.io directly without any
|
| 386 |
-
# GitHub-Actions side polling/commit churn.
|
| 387 |
-
prog_in = gr.Textbox(visible=False)
|
| 388 |
-
prog_out = gr.JSON(visible=False)
|
| 389 |
-
prog_button = gr.Button(visible=False)
|
| 390 |
-
prog_button.click(
|
| 391 |
-
fn=_get_progress,
|
| 392 |
-
inputs=[prog_in],
|
| 393 |
-
outputs=prog_out,
|
| 394 |
-
api_name="get_progress",
|
| 395 |
-
)
|
| 396 |
-
|
| 397 |
-
# Profile-listing endpoint — polled by the dashboard at startup
|
| 398 |
-
# so its dropdown reflects what's actually loadable on this Space
|
| 399 |
-
# right now (foundation+validator pin determines which profiles
|
| 400 |
-
# register). Stops the operator from picking something that
|
| 401 |
-
# would fatal at runtime.
|
| 402 |
-
profiles_out = gr.JSON(visible=False)
|
| 403 |
-
profiles_button = gr.Button(visible=False)
|
| 404 |
-
profiles_button.click(
|
| 405 |
-
fn=_list_profiles,
|
| 406 |
-
inputs=None,
|
| 407 |
-
outputs=profiles_out,
|
| 408 |
-
api_name="list_profiles",
|
| 409 |
-
)
|
| 410 |
-
|
| 411 |
-
# Cancel endpoint — the dashboard's Cancel button calls this AFTER
|
| 412 |
-
# cancelling the GH Action so the in-flight server-side gradio call
|
| 413 |
-
# actually stops (cancelling the Action alone only kills the
|
| 414 |
-
# gradio_client wrapper, the Space's loop keeps going).
|
| 415 |
-
cancel_in = gr.Textbox(visible=False)
|
| 416 |
-
cancel_token = gr.Textbox(visible=False)
|
| 417 |
-
cancel_out = gr.JSON(visible=False)
|
| 418 |
-
cancel_button = gr.Button(visible=False)
|
| 419 |
-
cancel_button.click(
|
| 420 |
-
fn=_cancel_run,
|
| 421 |
-
inputs=[cancel_in, cancel_token],
|
| 422 |
-
outputs=cancel_out,
|
| 423 |
-
api_name="cancel_run",
|
| 424 |
-
)
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
if __name__ == "__main__":
|
| 428 |
-
demo.queue().launch(
|
| 429 |
-
server_name=os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0"),
|
| 430 |
-
server_port=int(os.environ.get("GRADIO_SERVER_PORT", "7860")),
|
| 431 |
-
)
|
|
|
|
| 1 |
+
"""SimReady Validator — Gradio UI for the HuggingFace Space.
|
| 2 |
+
|
| 3 |
+
Two surfaces, same engine:
|
| 4 |
+
|
| 5 |
+
- **/run** (the on-screen button) — streams log lines to the UI for
|
| 6 |
+
interactive use by an operator in the browser.
|
| 7 |
+
- **/run_api** (hidden, programmatic) — returns the full RunResult as
|
| 8 |
+
a JSON-serializable dict. This is what `tools/hf_watch/call_hf_space.py`
|
| 9 |
+
hits from the GitHub Actions runner so the workflow can patch
|
| 10 |
+
status.json and asset-status.json without scraping the UI's text.
|
| 11 |
+
|
| 12 |
+
Both go through `runner.run()`. The split is purely about output
|
| 13 |
+
shape (streaming text vs. one-shot dict).
|
| 14 |
+
|
| 15 |
+
The Space is internal-pilot scope: HF_TOKEN comes from the Space's
|
| 16 |
+
secrets, NOT from the requester. When a customer's dataset PR triggers
|
| 17 |
+
this (next milestone), the webhook payload identifies the dataset and
|
| 18 |
+
the Space's own token opens the verdict PR.
|
| 19 |
+
"""
|
| 20 |
+
from __future__ import annotations
|
| 21 |
+
|
| 22 |
+
import json
|
| 23 |
+
import os
|
| 24 |
+
from pathlib import Path
|
| 25 |
+
|
| 26 |
+
import gradio as gr
|
| 27 |
+
|
| 28 |
+
from runner import (run as run_validator, progress_path_for, cancel_path_for,
|
| 29 |
+
run_token_path_for, CANCEL_DIR)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
PROFILE_CHOICES = [
|
| 33 |
+
"Prop-Robotics-Neutral",
|
| 34 |
+
"Prop-Robotics-Physx",
|
| 35 |
+
"Prop-Robotics-Isaac",
|
| 36 |
+
"Robot-Body-Neutral",
|
| 37 |
+
"Robot-Body-Runnable",
|
| 38 |
+
"Robot-Body-Isaac",
|
| 39 |
+
"Package",
|
| 40 |
+
"Package-Candidate",
|
| 41 |
+
]
|
| 42 |
+
DEFAULT_PROFILE = "Prop-Robotics-Neutral"
|
| 43 |
+
DEFAULT_VERSION = "1.0.0"
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def _run_api(dataset: str, profile: str, version: str, open_pr: bool,
|
| 47 |
+
submission_id: str = "", force: bool = False,
|
| 48 |
+
preliminary: bool = False, use_kit: bool = False) -> dict:
|
| 49 |
+
"""Programmatic endpoint. Returns the RunResult as a JSON dict.
|
| 50 |
+
|
| 51 |
+
Caller is typically `tools/hf_watch/call_hf_space.py` running from
|
| 52 |
+
a GitHub Actions ubuntu-latest runner. Output shape must stay
|
| 53 |
+
stable — bump `schema_version` if you change it. The receiver
|
| 54 |
+
pattern-matches on the same field names `tools/hf_watch/validate.py`
|
| 55 |
+
produces, so status.json patching is identical regardless of which
|
| 56 |
+
backend ran the validation.
|
| 57 |
+
|
| 58 |
+
`submission_id` is optional — when set, the validator writes
|
| 59 |
+
per-asset progress to /tmp/sr-progress/<id>.json, which the
|
| 60 |
+
get_progress endpoint serves to the dashboard.
|
| 61 |
+
|
| 62 |
+
`preliminary` switches the runner to a structure-only sweep:
|
| 63 |
+
zip-bundled datasets are scanned (instead of failing
|
| 64 |
+
PKG.NO-ARCHIVES at the listing stage) and per-asset validation is
|
| 65 |
+
sliced to the first asset only. Used by the dashboard's
|
| 66 |
+
Preliminary scan tab.
|
| 67 |
+
"""
|
| 68 |
+
print(f"[run_api] preliminary={preliminary!r} force={force!r} "
|
| 69 |
+
f"use_kit={use_kit!r} submission_id={submission_id!r}", flush=True)
|
| 70 |
+
# Untrusted callers can hit /run_api directly — profile/version flow
|
| 71 |
+
# into the validator's argv, so validate them before use. Empty
|
| 72 |
+
# falls back to the defaults (existing behavior).
|
| 73 |
+
import re
|
| 74 |
+
profile = profile or DEFAULT_PROFILE
|
| 75 |
+
if profile not in PROFILE_CHOICES and profile.lower() != "auto":
|
| 76 |
+
raise ValueError(f"invalid profile: {profile!r}")
|
| 77 |
+
version = (version or DEFAULT_VERSION).strip()
|
| 78 |
+
if not re.fullmatch(r"[\w.\-]+", version):
|
| 79 |
+
raise ValueError(f"invalid version: {version!r}")
|
| 80 |
+
result = run_validator(
|
| 81 |
+
dataset=(dataset or "").strip(),
|
| 82 |
+
profile=profile,
|
| 83 |
+
version=version,
|
| 84 |
+
open_pr=bool(open_pr),
|
| 85 |
+
submission_id=(submission_id or "").strip(),
|
| 86 |
+
force=bool(force),
|
| 87 |
+
preliminary=bool(preliminary),
|
| 88 |
+
use_kit=bool(use_kit),
|
| 89 |
+
)
|
| 90 |
+
return {
|
| 91 |
+
"schema_version": 1,
|
| 92 |
+
"dataset": result.dataset,
|
| 93 |
+
"profile": result.profile,
|
| 94 |
+
"version": result.version,
|
| 95 |
+
"status": result.status,
|
| 96 |
+
"summary": result.summary,
|
| 97 |
+
"results_json": _sanitize_results_json(result.results_json),
|
| 98 |
+
"pr_url": result.pr_url,
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def _list_profiles() -> dict:
|
| 103 |
+
"""Return the set of profiles that actually load on this Space's
|
| 104 |
+
foundation+validator combination. The dashboard polls this to
|
| 105 |
+
populate its dropdown so operators can't pick a profile that
|
| 106 |
+
would fatally fail at registration time.
|
| 107 |
+
|
| 108 |
+
Uses --use-plugin since the default CLI loader has known
|
| 109 |
+
registration mismatches against the current foundation pin; the
|
| 110 |
+
plugin path is what runner.py's streaming-zip flow falls back
|
| 111 |
+
to and is the source of truth for "actually usable" here.
|
| 112 |
+
|
| 113 |
+
Output format from validate.py is `PROFILE: <id> v<version>`
|
| 114 |
+
per profile, one per line.
|
| 115 |
+
"""
|
| 116 |
+
import subprocess, sys
|
| 117 |
+
from runner import VALIDATOR
|
| 118 |
+
try:
|
| 119 |
+
proc = subprocess.run(
|
| 120 |
+
# --list-profiles only ENUMERATES registered profiles from the
|
| 121 |
+
# spec/plugin registry (--use-plugin) — it runs no validation
|
| 122 |
+
# rules, so it never needs Kit. Force --no-use-kit: on a
|
| 123 |
+
# Kit-enabled image the validator auto-enables --use-kit for the
|
| 124 |
+
# PhysX-bearing default profile and boots the full Isaac Sim
|
| 125 |
+
# runtime (~5 min) just to print the list, blowing the 300s
|
| 126 |
+
# timeout below. Actual validation (runner.py) still uses Kit.
|
| 127 |
+
[sys.executable, str(VALIDATOR), "--list-profiles", "--use-plugin", "--no-use-kit"],
|
| 128 |
+
capture_output=True, text=True, timeout=300,
|
| 129 |
+
)
|
| 130 |
+
names: list[str] = []
|
| 131 |
+
for line in (proc.stdout or "").splitlines():
|
| 132 |
+
s = line.strip()
|
| 133 |
+
# Validator emits "PROFILE: <id> v<version>" — that's our
|
| 134 |
+
# only authoritative shape. Anything else is noise.
|
| 135 |
+
if s.startswith("PROFILE:"):
|
| 136 |
+
rest = s[len("PROFILE:"):].strip()
|
| 137 |
+
pid = rest.split()[0] if rest else ""
|
| 138 |
+
if pid:
|
| 139 |
+
names.append(pid)
|
| 140 |
+
# Dedupe while preserving order.
|
| 141 |
+
seen = set()
|
| 142 |
+
unique = []
|
| 143 |
+
for n in names:
|
| 144 |
+
if n not in seen:
|
| 145 |
+
seen.add(n)
|
| 146 |
+
unique.append(n)
|
| 147 |
+
result: dict = {"profiles": unique, "schema_version": 1, "rc": proc.returncode}
|
| 148 |
+
if not unique:
|
| 149 |
+
# No profiles registered AND no parse hits — surface why so
|
| 150 |
+
# the dashboard can show something useful. Truncate so the
|
| 151 |
+
# JSON response stays small.
|
| 152 |
+
stderr_tail = "\n".join((proc.stderr or "").splitlines()[-20:])[:2000]
|
| 153 |
+
stdout_tail = "\n".join((proc.stdout or "").splitlines()[-20:])[:2000]
|
| 154 |
+
result["stderr_tail"] = stderr_tail
|
| 155 |
+
result["stdout_tail"] = stdout_tail
|
| 156 |
+
return result
|
| 157 |
+
except subprocess.TimeoutExpired:
|
| 158 |
+
return {"profiles": [], "error": "timeout after 300s (spec load >5 min)"}
|
| 159 |
+
except Exception as e:
|
| 160 |
+
return {"profiles": [], "error": f"{type(e).__name__}: {e}"}
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
def _cancel_run(submission_id: str, run_token: str = "") -> dict:
|
| 164 |
+
"""Write the cancel-signal file for a given submission. The
|
| 165 |
+
streaming-zip loop in runner.py checks for this file between zips
|
| 166 |
+
and aborts when present. Idempotent — calling multiple times has no
|
| 167 |
+
extra effect; consuming runner.py deletes it.
|
| 168 |
+
|
| 169 |
+
`run_token` is the per-run token the dashboard read from get_progress.
|
| 170 |
+
It becomes the flag's content so runner._is_cancelled only honors it
|
| 171 |
+
for the exact run it was issued against — a flag left over from a
|
| 172 |
+
prior run of this submission can never abort a fresh one."""
|
| 173 |
+
sid = (submission_id or "").strip()
|
| 174 |
+
if not sid:
|
| 175 |
+
return {"state": "no_id"}
|
| 176 |
+
path = cancel_path_for(sid)
|
| 177 |
+
if path is None:
|
| 178 |
+
return {"state": "no_id"}
|
| 179 |
+
try:
|
| 180 |
+
CANCEL_DIR.mkdir(parents=True, exist_ok=True)
|
| 181 |
+
path.write_text((run_token or "").strip(), encoding="utf-8")
|
| 182 |
+
return {"state": "signaled", "path": str(path)}
|
| 183 |
+
except OSError as e:
|
| 184 |
+
return {"state": "error", "error": f"{type(e).__name__}: {e}"}
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def _get_progress(submission_id: str) -> dict:
|
| 188 |
+
"""Read the validator's per-asset progress file for this submission.
|
| 189 |
+
|
| 190 |
+
Polled by the dashboard ~every 3 s while a Validate-now click is
|
| 191 |
+
in-flight, so the "Validate now" button can fill up as the
|
| 192 |
+
validator works through the asset list.
|
| 193 |
+
|
| 194 |
+
Returns one of three shapes:
|
| 195 |
+
- {"state": "not_found"} — no progress file (Space restarted, or
|
| 196 |
+
the dashboard is polling a Space-run that never happened).
|
| 197 |
+
- {"state": "starting"} — file seeded by runner.py before the
|
| 198 |
+
validator started its loop. processed/total are 0.
|
| 199 |
+
- {processed, total, current, started_at, updated_at} — live
|
| 200 |
+
per-asset progress written by validate.py._emit_progress.
|
| 201 |
+
|
| 202 |
+
Every shape also carries `run_token` (the current run's cancel
|
| 203 |
+
token, from the sidecar file) when one exists, so the dashboard can
|
| 204 |
+
echo it back to cancel_run and target the exact run.
|
| 205 |
+
|
| 206 |
+
Caller treats anything with total > 0 as "show the fill bar".
|
| 207 |
+
"""
|
| 208 |
+
sid = (submission_id or "").strip()
|
| 209 |
+
if not sid:
|
| 210 |
+
return {"state": "no_id"}
|
| 211 |
+
# Per-run cancel token (sidecar; see runner.run_token_path_for).
|
| 212 |
+
# Surfaced on every shape so the dashboard can echo it back to
|
| 213 |
+
# cancel_run — a cancel then only aborts the run it was issued
|
| 214 |
+
# against, never a later one that reused the submission_id.
|
| 215 |
+
run_token = ""
|
| 216 |
+
tok_path = run_token_path_for(sid)
|
| 217 |
+
if tok_path and tok_path.is_file():
|
| 218 |
+
try:
|
| 219 |
+
run_token = tok_path.read_text(encoding="utf-8").strip()
|
| 220 |
+
except OSError:
|
| 221 |
+
pass
|
| 222 |
+
path = progress_path_for(sid)
|
| 223 |
+
if path is None or not path.is_file():
|
| 224 |
+
return {"state": "not_found", "run_token": run_token}
|
| 225 |
+
try:
|
| 226 |
+
data = json.loads(path.read_text(encoding="utf-8"))
|
| 227 |
+
if isinstance(data, dict) and run_token:
|
| 228 |
+
data["run_token"] = run_token
|
| 229 |
+
return data
|
| 230 |
+
except (OSError, json.JSONDecodeError):
|
| 231 |
+
# Mid-write — caller will poll again in a few seconds.
|
| 232 |
+
return {"state": "transient", "run_token": run_token}
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
def _sanitize_results_json(raw: dict) -> dict:
|
| 236 |
+
"""Strip absolute filesystem paths from results_json before returning.
|
| 237 |
+
|
| 238 |
+
Gradio's JSON serializer treats string fields that resolve to files
|
| 239 |
+
on the Space's filesystem as downloadable references and tries to
|
| 240 |
+
serve them through `/gradio_api/file=...`. The validator's
|
| 241 |
+
results.json contains absolute paths (target dir + per-asset
|
| 242 |
+
`path`) which point into the Space's ephemeral tempdir and are
|
| 243 |
+
NOT exposed through gradio's allowed_paths — gradio_client then
|
| 244 |
+
fails with 403 trying to auto-fetch them after a successful run.
|
| 245 |
+
|
| 246 |
+
Callers don't need filesystem paths anyway — only `rel_path`
|
| 247 |
+
(dataset-relative), `passed`, and `issues` are used downstream.
|
| 248 |
+
Keep the rest of the report intact (profile_coverage, summary,
|
| 249 |
+
layout_findings, etc.).
|
| 250 |
+
"""
|
| 251 |
+
if not isinstance(raw, dict):
|
| 252 |
+
return raw
|
| 253 |
+
sanitized = {k: v for k, v in raw.items() if k != "target"}
|
| 254 |
+
if "results" in sanitized and isinstance(sanitized["results"], list):
|
| 255 |
+
sanitized["results"] = [
|
| 256 |
+
{k: v for k, v in asset.items() if k != "path"}
|
| 257 |
+
for asset in sanitized["results"]
|
| 258 |
+
if isinstance(asset, dict)
|
| 259 |
+
]
|
| 260 |
+
# Specs/dashboard dir paths are local to the Space, useless to caller.
|
| 261 |
+
for k in ("specs_docs_dir", "dashboard_docs_dir"):
|
| 262 |
+
sanitized.pop(k, None)
|
| 263 |
+
return sanitized
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
def _run_streaming(dataset: str, profile: str, version: str, open_pr: bool):
|
| 267 |
+
"""Generator that yields incremental log output to the UI as the
|
| 268 |
+
validator runs. Gradio streams each yielded tuple to the connected
|
| 269 |
+
outputs."""
|
| 270 |
+
lines: list[str] = []
|
| 271 |
+
|
| 272 |
+
def log(line: str) -> None:
|
| 273 |
+
lines.append(line)
|
| 274 |
+
|
| 275 |
+
yield "\n".join(lines), "", "(running…)", None
|
| 276 |
+
|
| 277 |
+
try:
|
| 278 |
+
result = run_validator(
|
| 279 |
+
dataset=dataset.strip(),
|
| 280 |
+
profile=profile,
|
| 281 |
+
version=version.strip() or DEFAULT_VERSION,
|
| 282 |
+
open_pr=open_pr,
|
| 283 |
+
log=log,
|
| 284 |
+
)
|
| 285 |
+
except Exception as e:
|
| 286 |
+
lines.append(f"\nERROR: {type(e).__name__}: {e}")
|
| 287 |
+
yield "\n".join(lines), "", f"error: {e}", None
|
| 288 |
+
return
|
| 289 |
+
|
| 290 |
+
status_badge = f"**{result.status.upper()}** — {result.summary}"
|
| 291 |
+
if result.pr_url:
|
| 292 |
+
status_badge += f"\n\nPR: {result.pr_url}"
|
| 293 |
+
|
| 294 |
+
report_index = result.report_path / "index.html"
|
| 295 |
+
report_url = str(report_index) if report_index.is_file() else None
|
| 296 |
+
|
| 297 |
+
yield (
|
| 298 |
+
"\n".join(lines),
|
| 299 |
+
status_badge,
|
| 300 |
+
result.summary,
|
| 301 |
+
report_url,
|
| 302 |
+
)
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
def _read_md(name: str) -> str:
|
| 306 |
+
"""Return the contents of name (relative to this file's dir),
|
| 307 |
+
stripping a leading YAML frontmatter block if present. Falls back
|
| 308 |
+
to a friendly stub when the file is missing — keeps the Space
|
| 309 |
+
bootable even before the space-deploy workflow has synced the
|
| 310 |
+
assembled docs into the container."""
|
| 311 |
+
from pathlib import Path
|
| 312 |
+
p = Path(__file__).resolve().parent / name
|
| 313 |
+
try:
|
| 314 |
+
src = p.read_text(encoding="utf-8")
|
| 315 |
+
except FileNotFoundError:
|
| 316 |
+
return f"_{name} not yet synced into this Space — check back after the next deploy._"
|
| 317 |
+
if src.startswith("---"):
|
| 318 |
+
end = src.find("\n---\n", 4)
|
| 319 |
+
if end > 0:
|
| 320 |
+
src = src[end + len("\n---\n"):].lstrip()
|
| 321 |
+
return src
|
| 322 |
+
|
| 323 |
+
|
| 324 |
+
with gr.Blocks(title="SimReady Validator") as demo:
|
| 325 |
+
with gr.Tabs():
|
| 326 |
+
with gr.Tab("Overview"):
|
| 327 |
+
gr.Markdown(_read_md("README.md"))
|
| 328 |
+
with gr.Tab("Validator"):
|
| 329 |
+
gr.Markdown(
|
| 330 |
+
"Submit a HuggingFace dataset to validate against a SimReady "
|
| 331 |
+
"profile. With **Open PR** enabled, the verdict is uploaded "
|
| 332 |
+
"back to the dataset as a `validation/` pull request."
|
| 333 |
+
)
|
| 334 |
+
with gr.Row():
|
| 335 |
+
dataset = gr.Textbox(
|
| 336 |
+
label="Dataset",
|
| 337 |
+
placeholder="org/dataset (e.g. imagineio/PhysicalAI-SimReady-Kitchens-v1)",
|
| 338 |
+
)
|
| 339 |
+
with gr.Row():
|
| 340 |
+
profile = gr.Dropdown(
|
| 341 |
+
choices=PROFILE_CHOICES, value=DEFAULT_PROFILE, label="Profile",
|
| 342 |
+
)
|
| 343 |
+
version = gr.Textbox(label="Version", value=DEFAULT_VERSION)
|
| 344 |
+
open_pr = gr.Checkbox(label="Open PR on dataset with verdict", value=False)
|
| 345 |
+
run_btn = gr.Button("Validate", variant="primary")
|
| 346 |
+
status_md = gr.Markdown(label="Verdict")
|
| 347 |
+
summary_box = gr.Textbox(label="Summary", interactive=False)
|
| 348 |
+
log_box = gr.Textbox(label="Log", lines=20, interactive=False)
|
| 349 |
+
report_link = gr.File(label="HTML report (download)", interactive=False)
|
| 350 |
+
with gr.Tab("Partner walkthrough"):
|
| 351 |
+
gr.Markdown(_read_md("VALIDATE.md"))
|
| 352 |
+
|
| 353 |
+
run_btn.click(
|
| 354 |
+
fn=_run_streaming,
|
| 355 |
+
inputs=[dataset, profile, version, open_pr],
|
| 356 |
+
outputs=[log_box, status_md, summary_box, report_link],
|
| 357 |
+
api_name="run",
|
| 358 |
+
)
|
| 359 |
+
|
| 360 |
+
# Programmatic endpoint — bound to invisible components so the UI
|
| 361 |
+
# doesn't render anything extra, but the Gradio queue still exposes
|
| 362 |
+
# an `/api/predict/run_api` route the gradio_client can hit. The
|
| 363 |
+
# outputs[0] is the JSON return; api_name turns it into a stable
|
| 364 |
+
# path the GitHub Actions caller depends on.
|
| 365 |
+
api_dataset = gr.Textbox(visible=False)
|
| 366 |
+
api_profile = gr.Textbox(visible=False)
|
| 367 |
+
api_version = gr.Textbox(visible=False)
|
| 368 |
+
api_open_pr = gr.Checkbox(visible=False)
|
| 369 |
+
api_submission_id = gr.Textbox(visible=False)
|
| 370 |
+
api_force = gr.Checkbox(visible=False)
|
| 371 |
+
api_preliminary = gr.Checkbox(visible=False)
|
| 372 |
+
api_use_kit = gr.Checkbox(visible=False)
|
| 373 |
+
api_output = gr.JSON(visible=False)
|
| 374 |
+
api_button = gr.Button(visible=False)
|
| 375 |
+
api_button.click(
|
| 376 |
+
fn=_run_api,
|
| 377 |
+
inputs=[api_dataset, api_profile, api_version, api_open_pr,
|
| 378 |
+
api_submission_id, api_force, api_preliminary, api_use_kit],
|
| 379 |
+
outputs=api_output,
|
| 380 |
+
api_name="run_api",
|
| 381 |
+
)
|
| 382 |
+
|
| 383 |
+
# Progress endpoint — polled by the dashboard while a row is
|
| 384 |
+
# validating. CORS is open on /gradio_api/* by default, so the
|
| 385 |
+
# browser can fetch this from github.io directly without any
|
| 386 |
+
# GitHub-Actions side polling/commit churn.
|
| 387 |
+
prog_in = gr.Textbox(visible=False)
|
| 388 |
+
prog_out = gr.JSON(visible=False)
|
| 389 |
+
prog_button = gr.Button(visible=False)
|
| 390 |
+
prog_button.click(
|
| 391 |
+
fn=_get_progress,
|
| 392 |
+
inputs=[prog_in],
|
| 393 |
+
outputs=prog_out,
|
| 394 |
+
api_name="get_progress",
|
| 395 |
+
)
|
| 396 |
+
|
| 397 |
+
# Profile-listing endpoint — polled by the dashboard at startup
|
| 398 |
+
# so its dropdown reflects what's actually loadable on this Space
|
| 399 |
+
# right now (foundation+validator pin determines which profiles
|
| 400 |
+
# register). Stops the operator from picking something that
|
| 401 |
+
# would fatal at runtime.
|
| 402 |
+
profiles_out = gr.JSON(visible=False)
|
| 403 |
+
profiles_button = gr.Button(visible=False)
|
| 404 |
+
profiles_button.click(
|
| 405 |
+
fn=_list_profiles,
|
| 406 |
+
inputs=None,
|
| 407 |
+
outputs=profiles_out,
|
| 408 |
+
api_name="list_profiles",
|
| 409 |
+
)
|
| 410 |
+
|
| 411 |
+
# Cancel endpoint — the dashboard's Cancel button calls this AFTER
|
| 412 |
+
# cancelling the GH Action so the in-flight server-side gradio call
|
| 413 |
+
# actually stops (cancelling the Action alone only kills the
|
| 414 |
+
# gradio_client wrapper, the Space's loop keeps going).
|
| 415 |
+
cancel_in = gr.Textbox(visible=False)
|
| 416 |
+
cancel_token = gr.Textbox(visible=False)
|
| 417 |
+
cancel_out = gr.JSON(visible=False)
|
| 418 |
+
cancel_button = gr.Button(visible=False)
|
| 419 |
+
cancel_button.click(
|
| 420 |
+
fn=_cancel_run,
|
| 421 |
+
inputs=[cancel_in, cancel_token],
|
| 422 |
+
outputs=cancel_out,
|
| 423 |
+
api_name="cancel_run",
|
| 424 |
+
)
|
| 425 |
+
|
| 426 |
+
|
| 427 |
+
if __name__ == "__main__":
|
| 428 |
+
demo.queue().launch(
|
| 429 |
+
server_name=os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0"),
|
| 430 |
+
server_port=int(os.environ.get("GRADIO_SERVER_PORT", "7860")),
|
| 431 |
+
)
|
tools/hf_space/runner.py
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|