# Plan: open in-browser models for stac-zap Date: 2026-11-26. Status: a working demo. stac-zap (branch `phase0-local-model`) has a model list: Jev + Clairic, or the local open models of this repo (run `v5`: retriever, decider, tagger, Granite 30M, int8). The catalog follows from the collection: no catalog question. Results: `docs/local-model.md` in stac-zap. Changes from the plan below: the judge is Qwen3.8-27B (MiniMax licence), there is a trained retriever, and int8 is weight-only. ## Goal Replace both hosted models of [stac-zap](https://github.com/developmentseed/stac-zap) with small open models that run in the browser tab: - **Jev** (decisions): catalog, collection, place pick among Nominatim results, and the fallback option fields. - **Clairic** (extraction): place words, time words, start and end date, maximum cloud cover. Nominatim stays: it is a geocoder, not a model. Release requirement: the models, the training data and the code are open source. Thus: - No output of Jev, Clairic or another closed model is used as a label, a soft target or a paraphrase. The Jev and Clairic results in stac-zap's `eval/results/` are baselines to compare against, nothing more. - Labels come from the data generator (exact by construction), from open-weight models on OpenRouter, and from hand review. - Each training row records its source, generator model and OpenRouter provider. Providers whose terms forbid training on outputs are excluded at generation time (gazet lesson: Z.ai-served pairs had to be removed and every model retrained, see gazet `dataset/generative/eval/public_release/report.txt`). ## Repositories | Where | What | |---|---| | `developmentseed/stac-zap-models` (this repo, HF model) | ONNX models (int8), tokenizers, calibration, `sizes.json`, model card, this plan | | `developmentseed/stac-zap-data` (HF dataset, to create in phase 1) | Training, dev and test data, STAC snapshot, licences | | `developmentseed/stac-zap` (GitHub) | Data generator, training scripts, eval, browser integration | Model files go in a folder per version named by content hash (`//`), as gazetini does, so a deployed file never changes and can stay in the browser's Cache Storage. ## What the models must do | Question | Now | Options | Note | |---|---|---|---| | Catalog | Jev | ~13 curated, more with STAC Index | Topic requests, e.g. "crop types in Lower Saxony" -> Thünen | | Collection | Jev | up to 250 | Jev 27/27, others 22-25: the hardest question | | Place pick | Jev | Nominatim results + "None of these" | Same names in different places (Everest, Nepal vs Florida) | | Fallback period / season / cloud | Jev | 10-20 | Only when extraction fails | | Place, time, cloud cover | Clairic | free values | Must read values that no option list holds: "under 12% clouds", "between March 2023 and June 2024" | The decision contract stays the one of `src/zap/decide.ts`: one choice per question and a probability for each option, `keep` included. Then `readAnswers()`, the thresholds, the cloud cover median and the probability panel work without change. The extraction contract stays the one of `src/zap/extract.ts` (value or null, `p_null`). The models must choose from the option texts, not from remembered IDs: catalogs change, and the STAC Index switch shows collections that no training set has. ## Design A generative model does not fit. 250 collection descriptions are about 10k prompt tokens; gazet's 135M model reads about 110 tokens/s in browser WASM, so one collection question would take about 90 s. Encoders score all options in one batch. This is also how geoparse replaced Jev: a fine-tuned MiniLM cross-encoder with one temperature per decision, 91.2% against Jev's 89.1% on its LLM test set (`sunu/in-browser-geocoding`, `reports/local-model-report.md`). | Model | Job | Size goal (int8) | |---|---|---| | **retriever** (bi-encoder) | Top ~16 catalogs and collections for the request. Embeddings of the curated collections are made at build time (~1k x 384 int8, <0.5 MB) and shipped; STAC Index collections are embedded at page load. Replaces the keyword `shortlist()`. | ~25 MB | | **decider** (cross-encoder) | Scores (situation, option) pairs; softmax with a calibrated temperature per question type gives the probabilities. Catalog, collection, place pick, fallback fields, always with `keep`. | ~25-35 MB | | **extractor** (token classification) | BIO tags for place, time and cloud words in one pass. Code makes the values: [chrono-node](https://github.com/wanasit/chrono) (MIT) for dates, the existing `toRange` rules for seasons, quarters, "recent", and a number parser for cloud cover. `p_null` from the tag probabilities. | ~25 MB | Fallback for the extractor, only if dates stay weak: a t5-small frame model (`place=...|when=...|start=...|end=...| cloud=...`) with the encoder and the one-step decoder with key-value cache of gazetini (`scripts/export_onnx.py`, `web/src/models/t5.ts`). If the three models share one base, the page downloads one tokenizer, and a later step can merge them into one backbone with three heads. ### Base model candidates (to confirm in the licence review) The base must be Apache-2.0 or MIT **and** its training data must allow commercial use. Many small rerankers (`cross-encoder/ms-marco-*`) are fine-tuned on MS MARCO, whose terms are non-commercial. Candidates: - `ibm-granite/granite-embedding-30m-english` (Apache-2.0; IBM states it is trained on commercially usable data, without MS MARCO). First choice; check the claim. - `microsoft/MiniLM-L12-H384-uncased` (MIT, pre-trained only, no MS MARCO), as a fallback. - `BAAI/bge-small-en-v1.5` (MIT), only if its fine-tuning data passes the review. ### Browser delivery (from gazetini and gazet) - ONNX Runtime Web, int8, in a Web Worker; models load on page idle, not on the first keystroke. - Files served with brotli (tokenizers and graphs compress well, int8 weights less) plus `sizes.json` for progress. - Cache Storage (`cachedFetch`), so a second visit starts at once and works offline. - Hosted on this HF repo (CORS and range requests work, as for gazet). - GitHub Pages cannot send COOP/COEP: run ONNX Runtime single-threaded (small encoders are fast enough), or add `coi-serviceworker` as gazet does. - Provider switch in stac-zap: `VITE_DECIDER=local|jev`, `VITE_EXTRACTOR=local|clairic`. Default `local` after phase 5; the Worker stays as an optional fallback. ## Training data (all open) 1. **STAC snapshot.** Catalogs and collections (id, title, description, keywords, extent) from the curated catalogs and a sample of STAC Index catalogs. Kept with the source URL of each catalog; licence of the metadata checked per catalog. 2. **Places.** Overture divisions (CDLA-Permissive-2.0 / ODbL by source) and Natural Earth (public domain) for names, types, countries, bbox and population. For the place-pick question, candidate lists are built offline from these, in the form of Nominatim results (name, type, class, country, importance). No bulk Nominatim calls (usage policy), and no ODbL data unless the dataset is marked ODbL. 3. **Synthetic requests with exact labels.** Templates combine collection phrases (from title, keywords and an open-model summary of the description: "wildfire", "crop types", "lidar"), places, time phrases (years, months, seasons, ranges, "since", "last N weeks", single days, quarters), cloud phrases ("under 12%", "mostly clear", "any clouds"), and requests with none of these. The generator knows every label and every span. 4. **Open-model paraphrases.** Open-weight models on OpenRouter (for example DeepSeek, Qwen, GLM through providers whose terms allow training) rewrite requests into informal, indirect and misspelled forms. The structured slots are kept, so the labels stay exact; an open-weight judge (as gazet's MiniMax judge) checks that a paraphrase keeps its meaning, and rejected rows are dropped. 5. **Topic requests without a dataset name.** Open models write requests from a collection's description ("burn scars after the LA fires" -> the eoAPI wildfire collection). Each is checked by the judge against the options and by hand on a sample. 6. **Multi-turn state.** Rows with a current catalog, collection and place, so `keep` and follow-ups ("now the elevation") are learned. 7. **Robustness.** Shuffled options, rewritten descriptions, typos, hard negatives (Sentinel-2 L1C vs L2A, DEM variants, the ArcticDEM versions). 8. **Splits.** Held out from all training, by request and by catalog: - stac-zap's 30 field-test requests, 16 unparsed requests and 36 extraction dev requests (written by a person); - 2-3 whole catalogs, to measure unseen collections; - a new hand-reviewed test set of ~200 requests, written and checked by people, not by a model. Cost: open-model paraphrase and judge calls at gazet's rates (~$3 per 1k accepted rows) give 20-30k rows for well under $100. ## Evaluation - A `local` provider in stac-zap's `eval/providers.ts` that runs the ONNX models in Node (onnxruntime-node), so the field, unparsed and extraction tests compare it directly with the Jev and Clairic baselines already in `eval/results/`. - A parity check: browser int8 against Python fp32 (as geoparse `web/scripts/parity.ts`). - A Playwright smoke test of full zaps in headless Chromium, with time per zap and bytes downloaded. | Metric | Baseline (closed) | Target | |---|---|---| | Field test | Jev 1.13: 166/172 | >= 160/172 | | Collection | 27/27 | >= 25/27 | | Extraction: place, time, cloud | Clairic app calls: 84/85 | >= 82/85 | | Unparsed requests | Clairic: 16/16, Jev: 0/16 | >= 14/16 | | Held-out catalogs | - | report, no target yet | | Silent wrong answers | - | no rise against Jev | | Model time per zap | ~1 s (network) | < 1 s on a laptop, cached start | | First download | - | <= 80 MB | ## Phases | # | Step | Check | |---|---|---| | 0 | Spike: untuned base cross-encoder and tagger on the field test in Node; 250 options in browser WASM | Latency go/no-go; baseline score | | 1 | Licence review of bases, generators, providers, data sources; create `stac-zap-data`; generator and splits | Review notes in the dataset card; sample of labels checked by hand | | 2 | Open-model paraphrases and topic requests with the judge | Judge calibrated on ~200 hand labels, as in gazet | | 3 | Train retriever and decider (short GPU job, HF Jobs); calibrate temperatures | Field test, collection, held-out catalogs | | 4 | Train extractor; chrono-node and rules in TypeScript | Extraction test and unparsed test | | 5 | ONNX int8 export, parity, upload here; stac-zap integration behind the switch | Parity >= 98%; Playwright smoke test | | 6 | Report in stac-zap `docs/`, model card, make this repo and the dataset public | Release checklist | ## Risks - **Place pick needs world knowledge.** Jev is far better than the other hosted models here (110 km median error against >= 478 km). The decider uses Nominatim's type, class, country and importance in the option text; a rule (highest importance whose type fits the request) is the fallback. Measured on its own. - **Unseen STAC Index catalogs** can score lower than the curated ones; the held-out catalog split measures it. - **Phone latency** with single-threaded WASM is not measured yet (phase 0). - **Licence of STAC metadata** in the dataset: if a catalog's terms are unclear, keep only its ids and links.