Spaces:
Running

hub-galaxy / README_DEV.md
thomwolf's picture
thomwolf HF Staff
Hub galaxy explorer
6aaa687 verified
|
Raw History Blame Contribute Delete
6.48 kB
# Hub galaxy Space: developer notes
A static site (Space SDK `static`, no server). Plain HTML + WebGL2 + one Web Worker, no build step for the front end.
```
index.html page + UI markup
css/style.css look (fonts bundled in fonts/: Inter variable, IBM Plex Mono 400/500/600)
js/app.js renderer (port of video/galaxy.js: soft specks, crisp bodies, halo variant "a", family cores,
star field, bloom pyramid, filmic composite), camera, picking, labels, UI
js/worker.js search + id resolution (all id data lives in the worker)
js/vendor/fzstd.js zstd decoder (MIT, 8 kB) for the id buckets
data/ generated by build/build_data.py (below)
build/build_data.py data build (deterministic)
build/shoot.js local test: static server + headless Chrome (SwiftShader) + screenshots
```
## Rebuild the data
From the repository root (`state-of-os-video/`), about 2 minutes (most of it is zstd -19 on the id buckets):
```
source/.venv/bin/python -I space/build/build_data.py
```
Inputs, read only: `video/galaxy/{pos.f32,birth.f32,meta.u8,families.json,anchors.json,labels.json,stats.json}` and
`source/raw/hub-stats-raw/models_slim.parquet` (columns `id`, `createdAt`, `likes`). The script rebuilds the
birth-sorted order exactly like `source/build_galaxy/build.py` (`lexsort((row, fractional_year(createdAt)))`) and
asserts that it matches `birth.f32`, the ids in `labels.json` and the `likesLog` byte of `meta.u8`. Needs the `zstd` CLI.
gzip files are written with `mtime=0`, so a rebuild is byte-identical.
## Index spaces
- `i`: index in the film's birth-sorted arrays (`video/galaxy/*`).
- `g`: **global index** used everywhere in the Space. Points are split into layers; `g` runs through layer 0, then
layer 1, and so on, and inside each layer points stay in birth order. So "points born by date d" is a prefix of each layer,
and the renderer draws each layer with one `drawArrays(0, k)`.
## Layers (`data/points-L.bin.gz`)
| L | kind | points | content |
|---|---|---|---|
| 0 | preview | 200,000 | every tree root, every model with >= 100 likes, the 73 anchors, plus a hash-uniform sample of the rest. Drawn first (1.25 MB). |
| 1 | family | 859,972 | every remaining point with a top-255 family index. Always drawn. |
| 2-4 | dust | ~742k each | the remaining dust (no top-255 family), split in 3 hash-interleaved layers. Dropped on slow GPUs. |
Each file is gzip of a planar little-endian buffer for its `n` points:
`x int16[n] | y int16[n] | z int16[n] | meta uint8[4n]`. Position = int16 * `posScale` (1100/32767 galaxy units,
0.034 units per step). `meta` is the film's 4 bytes `[familyIdx, countryIdx, flags, likesLog]` (see video/galaxy/README.md).
Birth is not stored per point. `manifest.layers[L]` has `day0` and `counts[]` = number of points born on each UTC day
(day = days since 2022-01-01), from `day0` on. The client rebuilds the per-point birth (`day + (rank in day + 0.5)/count`)
and the number of points born by any date. The counter sums all layers, so it is exact even when dust is not drawn.
## `data/manifest.json`
`{version, asOf, N, day0, ndays, posScale, nbucket, legacyDay, layers:[{file, n, offset (first g), bytes (decoded), day0, counts, kind}],
anchors:[{id, label, g, family, likes, why}], families:[{idx, label, org, country, count, rootId, firstBirth, center, radius}] (top 255),
stats:{inFamily, dust, robotics, families, legacy}}`.
## Ids (loaded lazily by the worker)
- `data/buckets.u8.gz`: one byte per point in `g` order, the id bucket `fnv1a32(lowercase namespace) % 256`.
The namespace is the part before `/` (empty for legacy ids without one).
- `data/ids/NNN.txt.zst`: bucket NNN, one line per model in increasing `g`: `id` or `id\tlikes` (likes omitted when 0).
zstd -19, decoded by fzstd. 256 files, 31.4 MB in total, 90 to 630 kB each.
- g -> id: bucket `b = B[g]`, rank `k` = number of points before g in that bucket (the worker builds per-bucket g lists once,
13 MB of Uint32 in the worker), line `k` of bucket `b`.
- exact id -> g: hash the namespace, load that one bucket, find the line, map rank `k` back to `g`.
So exact lookup over all 3.29M ids costs one bucket download, not the whole table.
- `data/top.txt.gz`: autocomplete, the 50k most-liked models, `id\tg\tlikes` per line, sorted by likes.
Substring search runs on this list (in the worker); exact ids outside it go through the buckets.
- `data/labels.json.gz`: ambient callout candidates from the film's `labels.json`, `[[g, id, score, robotics], ...]`.
The fetch helpers check magic bytes, so they work whether or not a server or CDN already decoded the gzip.
## Rendering notes
- One VAO per layer: three `SHORT` attributes into the planar position buffer, a float birth buffer, `uvec4` meta.
- Picking: on hover (throttled to one pass per 120 ms, only when the pointer rests), the visible set is drawn again with a
pick shader into an RGBA8 + depth target, scissored to a 19x19 px window under the cursor (33x33 on touch). The colour is
`g + 1`, nearest depth wins, and the hit nearest the cursor is taken. No CPU scan of the points.
- The GL scene is only re-rendered when the camera, date, mode or data change. The 2D overlay (labels, markers) runs every frame.
- Tier probe: after layers 0 and 1, 3 frames are timed (readPixels forces a GPU sync) and extrapolated to the dust layers.
`high` = all 3 dust layers, `mid` = 1 or 2, `low` = none plus 1x pixel ratio. Undrawn dust layers are not downloaded
(a search that lands in one loads it on demand). Brightness is renormalised so a decimated galaxy keeps the same look.
- URL params: `?tier=high|mid|low` forces a tier, `?debug` exposes `window.APP` (used by build/shoot.js).
## Test locally
```
node space/build/shoot.js # all scenarios -> space/screenshots/*.png
node space/build/shoot.js only=01-initial,10-selftest
```
It serves `space/` on 127.0.0.1 (random port) and launches `/usr/bin/google-chrome` headless with its own temp profile,
the pipe transport (no debugging port, so it never touches a Chrome on 9222) and `--use-angle=swiftshader
--enable-unsafe-swiftshader`. puppeteer-core is loaded from `video/node_modules`. `10-selftest` checks g -> id and
id -> g through the worker against values computed in Python, and a GPU pick round trip.
For a manual look: `cd space && python3 -m http.server 8000 --bind 127.0.0.1`, then open http://127.0.0.1:8000/.