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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
"""Daily validation job for the workflow tool's curated catalog."""

from __future__ import annotations

import argparse
import base64
import io
import json
import logging
import os
import sys
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime, timezone
from typing import Any, Optional

import httpx

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(message)s",
)
logger = logging.getLogger("validate_workflow_curated")

CURATED_DATASET = "gradio/workflow-curated"
CURATED_FILENAME = "curated.json"
SMOKE_TIMEOUT = 30  # seconds per smoke call
INFO_TIMEOUT = 10
INFO_PATHS = ("/gradio_api/info", "/info", "/api/info")

TASK_INPUT_TYPES: dict[str, list[str]] = {
    "text-to-image": ["text"],
    "text-to-video": ["text"],
    "text-to-speech": ["text"],
    "text-to-audio": ["text"],
    "text-to-3d": ["text"],
    "text-generation": ["text"],
    "summarization": ["text"],
    "translation": ["text"],
    "text-classification": ["text"],
    "question-answering": ["text"],
    "zero-shot-classification": ["text"],
    "token-classification": ["text"],
    "fill-mask": ["text"],
    "visual-question-answering": ["image"],
    "feature-extraction": ["text"],
    "image-text-to-text": ["image"],
    "image-to-image": ["image"],
    "image-to-text": ["image"],
    "image-to-video": ["image"],
    "image-to-3d": ["image"],
    "image-classification": ["image"],
    "image-segmentation": ["image"],
    "object-detection": ["image"],
    "depth-estimation": ["image"],
    "automatic-speech-recognition": ["audio"],
    "audio-classification": ["audio"],
    "audio-to-audio": ["audio"],
}

TASK_OUTPUT_TYPES: dict[str, list[str]] = {
    "text-to-image": ["image"],
    "text-to-video": ["video"],
    "text-to-speech": ["audio"],
    "text-to-audio": ["audio"],
    "text-to-3d": ["model3d"],
    "text-generation": ["text"],
    "summarization": ["text"],
    "translation": ["text"],
    "text-classification": ["json"],
    "question-answering": ["text"],
    "zero-shot-classification": ["json"],
    "token-classification": ["json"],
    "fill-mask": ["json"],
    "visual-question-answering": ["text"],
    "feature-extraction": ["json"],
    "image-text-to-text": ["text"],
    "image-to-image": ["image"],
    "image-to-text": ["text"],
    "image-to-video": ["video"],
    "image-to-3d": ["model3d"],
    "image-classification": ["json"],
    "image-segmentation": ["json"],
    "object-detection": ["json"],
    "depth-estimation": ["image"],
    "automatic-speech-recognition": ["text"],
    "audio-classification": ["json"],
    "audio-to-audio": ["audio"],
}


def now_iso() -> str:
    return datetime.now(timezone.utc).isoformat(timespec="seconds")


def space_subdomain(repo_id: str) -> str:
    return repo_id.replace("/", "-").replace(".", "-").lower()


def fetch_space_info(repo_id: str) -> tuple[Optional[dict], Optional[str]]:
    base = f"https://{space_subdomain(repo_id)}.hf.space"
    last_err = "unreachable"
    for path in INFO_PATHS:
        try:
            r = httpx.get(base + path, timeout=INFO_TIMEOUT, follow_redirects=True)
        except httpx.HTTPError as e:
            last_err = f"{type(e).__name__}: {e}"
            continue
        if r.status_code == 200:
            try:
                return r.json(), None
            except json.JSONDecodeError:
                last_err = "non-json info response"
                continue
        if r.status_code in (401, 403):
            return None, "gated"
        last_err = f"http {r.status_code}"
    return None, last_err


def fetch_space_runtime(repo_id: str) -> Optional[dict]:
    try:
        r = httpx.get(
            f"https://huggingface.co/api/spaces/{repo_id}?expand[]=runtime",
            timeout=INFO_TIMEOUT,
        )
        if r.status_code == 200:
            return r.json()
    except httpx.HTTPError:
        pass
    return None


def primary_endpoint(info: dict, override: Optional[str]) -> Optional[tuple[str, dict]]:
    UTILITY = (
        "/on_",
        "/handle_",
        "/update_",
        "/prepare_",
        "/load_",
        "/clear_",
        "/reset_",
    )
    named = info.get("named_endpoints") or {}
    unnamed = info.get("unnamed_endpoints") or {}
    all_eps = [
        (n, ep)
        for n, ep in list(named.items()) + list(unnamed.items())
        if not any(n.startswith(p) for p in UTILITY)
    ]
    if override:
        for n, ep in all_eps:
            if n == override:
                return n, ep
    for n, ep in all_eps:
        if n == "/predict":
            return n, ep
    return (all_eps[0][0], all_eps[0][1]) if all_eps else None


def schema_cross_check(ep: dict, task: str) -> Optional[str]:
    expected_in = TASK_INPUT_TYPES.get(task)
    expected_out = TASK_OUTPUT_TYPES.get(task)
    if not expected_in and not expected_out:
        return None

    required_inputs = [
        p for p in (ep.get("parameters") or []) if not p.get("parameter_has_default")
    ]
    if expected_in:
        if len(required_inputs) > len(expected_in) + 2:
            return f"too many required inputs ({len(required_inputs)} > {len(expected_in)})"
    if expected_out:
        returns = ep.get("returns") or []
        if not returns:
            return "endpoint has no return values"
    return None


def default_smoke_inputs(task: str) -> list[Any]:
    if task in ("text-to-image", "text-to-video", "text-to-3d", "text-to-speech", "text-to-audio"):
        return ["a small red square"]
    if task in ("text-generation", "summarization", "translation", "text-classification",
                "question-answering", "feature-extraction", "zero-shot-classification",
                "token-classification", "fill-mask"):
        return ["hello world"]
    if task in ("image-to-image", "image-to-text", "image-to-video", "image-to-3d",
                "image-classification", "image-segmentation", "object-detection",
                "depth-estimation"):
        return [_tiny_png_data_url()]
    if task in ("automatic-speech-recognition", "audio-classification", "audio-to-audio"):
        return [_tiny_wav_path()]
    return []


def _tiny_png_data_url() -> str:
    raw = base64.b64decode(
        "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
    )
    return "data:image/png;base64," + base64.b64encode(raw).decode("ascii")


def _tiny_wav_path() -> str:
    import struct
    import tempfile
    import wave

    fd, path = tempfile.mkstemp(suffix=".wav")
    os.close(fd)
    with wave.open(path, "wb") as w:
        w.setnchannels(1)
        w.setsampwidth(2)
        w.setframerate(8000)
        w.writeframes(struct.pack("<" + "h" * 1600, *([0] * 1600)))
    return path


def smoke_inference(
    repo_id: str, endpoint: str, inputs: list[Any], hf_token: Optional[str]
) -> tuple[bool, Optional[str], int]:
    import subprocess
    import sys

    script = f"""
import sys
try:
    from gradio_client import Client
    c = Client({repo_id!r}, token={hf_token!r})
    c.predict(*{inputs!r}, api_name={endpoint!r})
except Exception as e:
    print(f"{{type(e).__name__}}: {{e}}", file=sys.stderr)
    sys.exit(1)
"""
    started = time.monotonic()
    try:
        proc = subprocess.run(
            [sys.executable, "-c", script],
            timeout=SMOKE_TIMEOUT,
            capture_output=True,
            text=True,
        )
    except subprocess.TimeoutExpired:
        return False, f"timed out after {SMOKE_TIMEOUT}s", int((time.monotonic() - started) * 1000)
    except Exception as e:
        return False, f"{type(e).__name__}: {e}", int((time.monotonic() - started) * 1000)
    latency = int((time.monotonic() - started) * 1000)
    if proc.returncode != 0:
        return False, proc.stderr.strip() or f"exit {proc.returncode}", latency
    return True, None, latency


WAKE_POLL_INTERVAL = 10  # seconds between runtime polls
WAKE_TIMEOUT = 180  # seconds to wait for a space to wake


def wake_space(repo_id: str) -> bool:
    """Trigger a wakeup by hitting the space URL, then poll until RUNNING."""
    base = f"https://{space_subdomain(repo_id)}.hf.space"
    try:
        httpx.get(base, timeout=10, follow_redirects=True)
    except httpx.HTTPError:
        pass
    deadline = time.monotonic() + WAKE_TIMEOUT
    while time.monotonic() < deadline:
        rt = fetch_space_runtime(repo_id)
        stage = (rt or {}).get("runtime", {}).get("stage") if rt else None
        if stage == "RUNNING":
            return True
        if stage and "ERROR" in str(stage).upper():
            return False
        time.sleep(WAKE_POLL_INTERVAL)
    return False


def validate_space(entry: dict, hf_token: Optional[str], skip_smoke: bool, wake_sleeping: bool = False) -> dict:
    repo_id = entry["id"]
    task = entry.get("task", "")
    logger.info("validating space %s", repo_id)
    info, err = fetch_space_info(repo_id)
    if err == "gated":
        return {"last_checked": now_iso(), "status": "gated", "error": "auth required"}
    if info is None:
        rt = fetch_space_runtime(repo_id)
        stage = (rt or {}).get("runtime", {}).get("stage") if rt else None
        if stage == "SLEEPING" and wake_sleeping:
            logger.info("waking sleeping space %s", repo_id)
            if wake_space(repo_id):
                info, err = fetch_space_info(repo_id)
            if info is None:
                return {"last_checked": now_iso(), "status": "sleeping", "error": "did not wake in time"}
        elif stage in ("SLEEPING", "PAUSED", "STOPPED"):
            return {"last_checked": now_iso(), "status": "sleeping", "error": stage}
        if info is None:
            if stage and "ERROR" in str(stage).upper():
                return {"last_checked": now_iso(), "status": "broken", "error": stage}
            return {"last_checked": now_iso(), "status": "unreachable", "error": err}

    pick = primary_endpoint(info, entry.get("endpoint"))
    if not pick:
        return {"last_checked": now_iso(), "status": "broken", "error": "no usable endpoints"}
    ep_name, ep = pick

    if task:
        mismatch = schema_cross_check(ep, task)
        if mismatch:
            return {"last_checked": now_iso(), "status": "schema_mismatch", "error": mismatch}

    if skip_smoke:
        return {"last_checked": now_iso(), "status": "ok", "error": None, "latency_ms": 0, "endpoint": ep_name}

    inputs = entry.get("smoke_inputs")
    inputs_list = (
        list(inputs.values()) if isinstance(inputs, dict) else (inputs or default_smoke_inputs(task))
    )
    ok, err, latency = smoke_inference(repo_id, ep_name, inputs_list, hf_token)
    if not ok:
        msg = (err or "").lower()
        if "401" in msg or "403" in msg or "auth" in msg:
            status = "gated"
        else:
            status = "smoke_failed"
        return {"last_checked": now_iso(), "status": status, "error": err, "latency_ms": latency, "endpoint": ep_name}
    return {
        "last_checked": now_iso(),
        "status": "ok",
        "error": None,
        "latency_ms": latency,
        "endpoint": ep_name,
    }


def validate_model(entry: dict, hf_token: Optional[str]) -> dict:
    repo_id = entry["id"]
    task = entry.get("task", "")
    logger.info("validating model %s", repo_id)
    try:
        r = httpx.get(
            f"https://huggingface.co/api/models/{repo_id}",
            timeout=INFO_TIMEOUT,
            headers={"Authorization": f"Bearer {hf_token}"} if hf_token else {},
        )
    except httpx.HTTPError as e:
        return {"last_checked": now_iso(), "status": "unreachable", "error": str(e)}
    if r.status_code in (401, 403):
        return {"last_checked": now_iso(), "status": "gated", "error": "auth required"}
    if r.status_code != 200:
        return {"last_checked": now_iso(), "status": "unreachable", "error": f"http {r.status_code}"}

    body = r.json()
    actual_task = body.get("pipeline_tag")
    if task and actual_task and actual_task != task:
        return {
            "last_checked": now_iso(),
            "status": "schema_mismatch",
            "error": f"pipeline_tag is {actual_task!r}, manifest says {task!r}",
        }

    try:
        from huggingface_hub import HfApi

        api = HfApi(token=hf_token)
        files = api.list_repo_files(repo_id=repo_id, repo_type="model")
    except Exception as e:
        return {"last_checked": now_iso(), "status": "unreachable", "error": str(e)}

    has_weights = any(
        f.endswith((".safetensors", ".bin", ".gguf", ".onnx", ".pt"))
        for f in files
    )
    if not has_weights:
        return {
            "last_checked": now_iso(),
            "status": "missing_weights",
            "error": "no weight file found",
        }
    return {"last_checked": now_iso(), "status": "ok", "error": None}


def load_manifest(local_path: Optional[str], hf_token: Optional[str]) -> tuple[dict, str]:
    if local_path:
        with open(local_path, encoding="utf-8") as f:
            data = json.load(f)
    else:
        from huggingface_hub import hf_hub_download

        local = hf_hub_download(
            repo_id=CURATED_DATASET,
            filename=CURATED_FILENAME,
            repo_type="dataset",
            token=hf_token,
        )
        with open(local, encoding="utf-8") as f:
            data = json.load(f)

    # tolerate a bare-array manifest by wrapping it back up
    if isinstance(data, list):
        data = {"snapshot_version": 1, "items": data}
    src = local_path or f"{CURATED_DATASET}/{CURATED_FILENAME}"
    return data, src


def upload_manifest(payload: dict, hf_token: str) -> None:
    from huggingface_hub import upload_file

    blob = json.dumps(payload, indent=2, ensure_ascii=False).encode("utf-8")
    upload_file(
        path_or_fileobj=io.BytesIO(blob),
        path_in_repo=CURATED_FILENAME,
        repo_id=CURATED_DATASET,
        repo_type="dataset",
        token=hf_token,
        commit_message=f"daily validation {now_iso()}",
    )


def main() -> int:
    ap = argparse.ArgumentParser(description="Validate the workflow curated catalog.")
    ap.add_argument(
        "--dry-run",
        action="store_true",
        help="Don't upload the result; print the proposed manifest to stdout.",
    )
    ap.add_argument(
        "--source",
        default=None,
        help="Path to a local manifest JSON instead of the Hub dataset.",
    )
    ap.add_argument(
        "--limit",
        type=int,
        default=0,
        help="Only validate the first N entries (debugging).",
    )
    ap.add_argument(
        "--skip-smoke",
        action="store_true",
        help="Skip the smoke inference; only run the info-endpoint check.",
    )
    ap.add_argument(
        "--wake-sleeping",
        action="store_true",
        help="Wake sleeping spaces and wait for them to start before validating.",
    )
    ap.add_argument(
        "--workers",
        type=int,
        default=4,
        help="Parallel workers for the info-check phase.",
    )
    args = ap.parse_args()

    hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HF_JOBS_TOKEN")

    payload, src = load_manifest(args.source, hf_token)
    items = payload.get("items")
    if not isinstance(items, list):
        logger.error("manifest at %s is malformed (no items array)", src)
        return 2
    if args.limit:
        items = items[: args.limit]

    spaces = [e for e in items if e.get("kind") == "space"]
    models = [e for e in items if e.get("kind") == "model"]
    logger.info(
        "loaded %d entries from %s (%d spaces, %d models)",
        len(items),
        src,
        len(spaces),
        len(models),
    )

    new_items: list[dict] = []
    with ThreadPoolExecutor(max_workers=args.workers) as pool:
        futures = {}
        for e in items:
            if e.get("kind") == "space":
                futures[pool.submit(validate_space, e, hf_token, args.skip_smoke, args.wake_sleeping)] = e
            elif e.get("kind") == "model":
                futures[pool.submit(validate_model, e, hf_token)] = e
            else:
                new_items.append(e)
        for fut in as_completed(futures):
            entry = futures[fut]
            try:
                result = fut.result()
            except Exception as e:
                result = {
                    "last_checked": now_iso(),
                    "status": "broken",
                    "error": f"validator crashed: {e}",
                }
            updated = dict(entry)
            updated["validation"] = result
            new_items.append(updated)

    order = {e.get("id", ""): i for i, e in enumerate(items)}
    new_items.sort(key=lambda e: order.get(e.get("id", ""), len(items)))

    statuses: dict[str, int] = {}
    for e in new_items:
        s = (e.get("validation") or {}).get("status", "unknown")
        statuses[s] = statuses.get(s, 0) + 1
    logger.info("results: %s", statuses)

    out_payload = {**payload, "items": new_items, "fetched_at": now_iso()}

    if args.dry_run:
        json.dump(out_payload, sys.stdout, indent=2)
        sys.stdout.write("\n")
        return 0

    if not hf_token:
        logger.error("no HF_TOKEN / HF_JOBS_TOKEN — cannot upload (use --dry-run to preview)")
        return 3
    upload_manifest(out_payload, hf_token)
    logger.info("uploaded manifest to %s", CURATED_DATASET)
    return 0


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