|
| 1 | +"""V8-R09: ``data.hf_quickstart`` RPC handler. |
| 2 | +
|
| 3 | +Wraps :func:`scripts.data.hf_quickstart.hf_quickstart` for the GUI. |
| 4 | +The handler runs synchronously — progress arrives on the |
| 5 | +``/ws/data/{job_id}`` WebSocket from the underlying data_event_bus. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import os |
| 11 | +from pydantic import BaseModel, ConfigDict |
| 12 | + |
| 13 | +from cppmega_v4.jsonrpc.cache import LRUCache |
| 14 | +from cppmega_v4.jsonrpc.methods import _cache_lookup, _cache_store |
| 15 | + |
| 16 | + |
| 17 | +__all__ = [ |
| 18 | + "HfQuickstartParams", |
| 19 | + "HfQuickstartResult", |
| 20 | + "hf_quickstart_method", |
| 21 | +] |
| 22 | + |
| 23 | + |
| 24 | +class HfQuickstartParams(BaseModel): |
| 25 | + """Input — HF dataset + tokenizer + token budget.""" |
| 26 | + |
| 27 | + model_config = ConfigDict(extra="forbid") |
| 28 | + |
| 29 | + dataset_id: str |
| 30 | + split: str = "train" |
| 31 | + tokenizer: str = "cppmega_v3" |
| 32 | + n_tokens: int = 100_000 |
| 33 | + job_id: str | None = None |
| 34 | + out_dir: str | None = None |
| 35 | + text_field: str = "text" |
| 36 | + |
| 37 | + |
| 38 | +class HfQuickstartResult(BaseModel): |
| 39 | + """Output — parquet path + token / doc counters + elapsed wall.""" |
| 40 | + |
| 41 | + model_config = ConfigDict(extra="forbid") |
| 42 | + |
| 43 | + parquet_path: str |
| 44 | + n_tokens_written: int |
| 45 | + n_docs_seen: int |
| 46 | + elapsed_ms: float |
| 47 | + |
| 48 | + |
| 49 | +def hf_quickstart_method( |
| 50 | + params: HfQuickstartParams, *, cache: LRUCache | None = None, |
| 51 | +) -> HfQuickstartResult: |
| 52 | + """Run the HF quickstart and return the resulting parquet shard.""" |
| 53 | + # Cache only on full param-key equality; HF jobs aren't typically |
| 54 | + # repeated with identical params so the hit rate is near-zero. |
| 55 | + key, hit = _cache_lookup(cache, "data.hf_quickstart", params) |
| 56 | + if hit is not None: |
| 57 | + return hit |
| 58 | + |
| 59 | + if os.environ.get("VBGUI_DISABLE_NETWORK") == "1": |
| 60 | + raise RuntimeError( |
| 61 | + "HF Hub network access disabled via VBGUI_DISABLE_NETWORK") |
| 62 | + |
| 63 | + from scripts.data.hf_quickstart import hf_quickstart as _hf |
| 64 | + r = _hf( |
| 65 | + dataset_id=params.dataset_id, |
| 66 | + split=params.split, |
| 67 | + tokenizer=params.tokenizer, |
| 68 | + n_tokens=params.n_tokens, |
| 69 | + job_id=params.job_id, |
| 70 | + out_dir=params.out_dir, |
| 71 | + text_field=params.text_field, |
| 72 | + ) |
| 73 | + out = HfQuickstartResult( |
| 74 | + parquet_path=r.parquet_path, |
| 75 | + n_tokens_written=r.n_tokens_written, |
| 76 | + n_docs_seen=r.n_docs_seen, |
| 77 | + elapsed_ms=r.elapsed_ms, |
| 78 | + ) |
| 79 | + _cache_store(cache, key, out) |
| 80 | + return out |
0 commit comments