RGPalgrave commited on
Commit
51dd783
Β·
verified Β·
1 Parent(s): fff0d09

add API brief

Browse files
Files changed (1) hide show
  1. API.md +163 -0
API.md ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # TopG API β€” brief for another agent
2
+
3
+ Everything needed to call the TopG structure generator. Self-contained: paste this whole file.
4
+
5
+ ## What it does
6
+
7
+ TopG takes a **coordination query** β€” a description of each inequivalent metal site's local
8
+ environment β€” and returns **complete crystal structures**: space group, Wyckoff positions, free
9
+ coordinates and cell shape. It is a 4.5M-parameter conditional transformer. **No search or
10
+ refinement is involved; the output is the model's raw emission.**
11
+
12
+ It generates a **metal sublattice**, not a composition. Element identities in the returned CIF are
13
+ arbitrary palette entries and carry no chemical meaning.
14
+
15
+ ## Endpoint and auth
16
+
17
+ - Space `RGPalgrave/TopG-demo` Β· base URL `https://rgpalgrave-topg-demo.hf.space`
18
+ - Endpoint **`/generate`** β€” use this one. (`/run` is the web page's own button and takes
19
+ UI-shaped arguments.)
20
+ - **The Space is private, so every call needs a HuggingFace token** with read access to it.
21
+ Pass it via the `HF_TOKEN` environment variable; do not hard-code it.
22
+
23
+ ## Python
24
+
25
+ ```python
26
+ import json, os
27
+ from gradio_client import Client # pip install gradio_client
28
+
29
+ c = Client("RGPalgrave/TopG-demo", token=os.environ["HF_TOKEN"])
30
+ r = c.predict(json.dumps({
31
+ "sites": [
32
+ {"H": "4/mmm", "cs": [4, 2], "ratios": [1.0, 1.15], "orbit_size": 2},
33
+ {"H": "mmm", "cs": [2, 4], "ratios": [1.0, 1.02], "orbit_size": 2}
34
+ ],
35
+ "n_atoms": 4, "model": "H_pq0", "candidates": 8, "seed": 0,
36
+ "score": True, "cif": True
37
+ }), api_name="/generate")
38
+ r = json.loads(r) if isinstance(r, str) else r
39
+ ```
40
+
41
+ ## curl
42
+
43
+ POST returns an `event_id`; GET streams the result.
44
+
45
+ ```bash
46
+ EV=$(curl -s -X POST "https://rgpalgrave-topg-demo.hf.space/gradio_api/call/generate" \
47
+ -H "Authorization: Bearer $HF_TOKEN" -H "Content-Type: application/json" \
48
+ -d '{"data":["{\"sites\":[{\"H\":\"m-3m\",\"cs\":[6,12],\"ratios\":[1.0,1.414],\"orbit_size\":4}],\"n_atoms\":4}"]}' \
49
+ | python -c "import sys,json;print(json.load(sys.stdin)['event_id'])")
50
+ curl -s -N "https://rgpalgrave-topg-demo.hf.space/gradio_api/call/generate/$EV" \
51
+ -H "Authorization: Bearer $HF_TOKEN" | grep -m1 '^data:'
52
+ ```
53
+
54
+ ## Request fields
55
+
56
+ | field | required | meaning |
57
+ |---|---|---|
58
+ | `sites` | **yes** | one object per *inequivalent* site (below). 1–12 of them |
59
+ | `n_atoms` | no | metal atoms in the **conventional** cell, 1–192. Defaults to the sum of `orbit_size`. **Orbit sizes must sum to it** β€” a mismatch is accepted but returns a `warning` and usually few or no structures |
60
+ | `model` | no | `H_pq0` (default), `H_pq1`, `H_pq2`, `I_full0`, `I_full1`, `I_full2` |
61
+ | `candidates` | no | beams to draw, default 8. More candidates β†’ more distinct structures, linear cost |
62
+ | `seed` | no | default 0 |
63
+ | `score` | no | default `true`; scores each structure against the query. The slow part for large cells. `false` returns `best_rung: null` |
64
+ | `cif` | no | default `true`; `false` omits CIF text |
65
+
66
+ Each entry of `sites`:
67
+
68
+ | field | meaning |
69
+ |---|---|
70
+ | `H` | site symmetry, Hermann–Mauguin (table below) |
71
+ | `cs` | coordination sequence β€” metal neighbours in each successive shell, e.g. `[4, 2]` |
72
+ | `ratios` | each shell's distance **relative to the nearest**, so `ratios[0]` is always `1.0` |
73
+ | `orbit_size` | atoms this site contributes to the conventional cell |
74
+
75
+ `cs` and `ratios` must be the same length: **1 to 6 shells**. Fewer than 6 is a *partial query* β€”
76
+ supported, and easier for the model β€” but **use an `H_pq` model for it**. `I_full` is trained on
77
+ full-depth queries only and collapses on short ones (~10% vs ~50% skeleton accuracy).
78
+
79
+ ## Accepted site symmetries (36)
80
+
81
+ Any symmetry may be used in any query; the row says where it actually occurs among the 230 space groups.
82
+
83
+ | system | site symmetries |
84
+ |---|---|
85
+ | triclinic | `1` `-1` |
86
+ | monoclinic | `1` `-1` `2` `m` `2/m` |
87
+ | orthorhombic | `1` `-1` `2` `m` `2/m` `222` `2mm` `m2m` `mm2` `mmm` |
88
+ | tetragonal | `1` `-1` `2` `m` `-4` `2/m` `222` `2mm` `4` `m2m` `-42m` `-4m2` `4/m` `422` `4mm` `mmm` `4/mmm` |
89
+ | trigonal | `1` `-1` `2` `m` `3` `2/m` `-3` `32` `3m` `-3m` |
90
+ | hexagonal | `1` `-1` `2` `m` `3` `2/m` `222` `2mm` `m2m` `mm2` `-3` `-6` `32` `3m` `6` `mmm` `-3m` `-62m` `-6m2` `6/m` `622` `6mm` `6/mmm` |
91
+ | cubic | `1` `2` `m` `3` `-4` `2/m` `222` `2mm` `4` `mm2` `-3` `32` `3m` `-42m` `-4m2` `4/m` `422` `4mm` `mmm` `-3m` `23` `4/mmm` `-43m` `432` `m-3` `m-3m` |
92
+
93
+ **Not usable:** `312` `31m` `321` `3m1` `-31m` `-3m1` β€” these are in the model's vocabulary but occur in no Wyckoff table, so no
94
+ training example ever contained one. The API rejects them; use the form without the axis suffix
95
+ (`321`β†’`32`, `3m1`β†’`3m`, `-3m1`β†’`-3m`).
96
+
97
+ ## Response
98
+
99
+ ```json
100
+ {"ok": true,
101
+ "query": {"n_atoms": 4, "k": 2, "depth": 2, "sites": [...]},
102
+ "candidates_built": 8, "distinct_structures": 2, "warning": null,
103
+ "provenance": {"model": "H_pq0", "tokenizer_fingerprint": "e1db473d06e14c82",
104
+ "device": "cpu", "note": "pure model output; no search or refinement"},
105
+ "structures": [
106
+ {"rank": 1, "space_group": 139, "crystal_system": "tetragonal",
107
+ "orbits": "2a 2b", "cell_shape": "c_over_a=1.4714",
108
+ "best_rung": "r0_2sh_noratio", "times_drawn": 6, "cif": "# generated using pymatgen..."}
109
+ ]}
110
+ ```
111
+
112
+ Errors never raise β€” they return `{"ok": false, "error": "..."}`, with `expected` carrying the
113
+ schema when the payload was malformed.
114
+
115
+ - `structures` are **deduplicated** and ranked: scored hits first, then by `times_drawn`.
116
+ - `times_drawn` is how many of the `candidates` beams produced that same structure β€” a rough
117
+ confidence signal, not a probability.
118
+ - `cell_shape` lists only the **free** shape parameters of the crystal system (cubic none;
119
+ tetragonal/trigonal/hexagonal `c_over_a`; orthorhombic 2, monoclinic 3, triclinic 5).
120
+ **No absolute length is emitted** β€” a scale-free query cannot determine one, so the CIF's absolute
121
+ cell size is a placeholder and only ratios are meaningful.
122
+ - The **CIF is written in P1** with every atom explicit, so the file says `P 1` while `space_group`
123
+ gives the real symmetry. The symmetry is in the coordinates.
124
+
125
+ ## `best_rung` β€” how well a structure answers the query
126
+
127
+ Nested criteria, each stricter than the last. `null` means it passed none.
128
+
129
+ | rung | shells checked | distance tolerance |
130
+ |---|---|---|
131
+ | `r0_2sh_noratio` | 2 | ignored |
132
+ | `r1_3sh_noratio` | 3 | ignored |
133
+ | `r2_3sh_t04` | 3 | 4% |
134
+ | `r2a_3sh_t08` | 3 | 8% |
135
+ | `r5_6sh_noratio` | 6 | ignored |
136
+ | **`r3_6sh_t04`** | **6** | **4%** β€” the project's historical criterion |
137
+ | `r3a_6sh_t08` | 6 | 8% |
138
+ | `r3b_6sh_t15` | 6 | 15% |
139
+
140
+ ## How much to trust the output
141
+
142
+ Measured on a frozen, skeleton-disjoint evaluation set; pure model, no search:
143
+
144
+ - The **discrete** answer (space group + Wyckoff labels + N) is exactly right **~54–59%** of the time.
145
+ - The **geometry** meets the strict `r3_6sh_t04` criterion **~2–3%** of the time, and essentially
146
+ never above ~9 free parameters. **Treat returned coordinates as a starting point, not an answer.**
147
+ - A *perfect* model would score **95.67%**, not 100% β€” that is the emission grid's own ceiling.
148
+ - A structure passing no rung still answers the query approximately; `best_rung` says which
149
+ criterion it actually meets.
150
+
151
+ ## Gotchas
152
+
153
+ - **`seed` reproduces within one deployment, not across environments.** The same seed on this Space
154
+ and on a local checkout gives the same structures but different draw counts, because the Python
155
+ and torch builds differ. Do not use `seed` as a portable identifier.
156
+ - **The first call after the Space wakes is slow** β€” it downloads the checkpoint once, then caches.
157
+ A small query (N ≀ 8, 8 candidates) then takes a few seconds.
158
+ - Cost grows with **cell size**, not with hardware; the model is too small for a GPU to help.
159
+ For N > 40 send `"score": false`.
160
+ - Sites are **inequivalent sites, not atoms**: `k` is the number of orbits, `n_atoms` the number of
161
+ atoms, and `orbit_size` links them.
162
+
163
+ Model card: `RGPalgrave/TopG` Β· code and full record: <https://github.com/rgpalgrave/TopG>