Download tagger.js from Project-Bastion/attack-technique-tagger-demo: direct link, hf CLI and curl.
- Browser
- Download file 2.2 kB
-
https://huggingface.co/spaces/Project-Bastion/attack-technique-tagger-demo/resolve/main/tagger.js
- Command line
-
hf download hf://spaces/Project-Bastion/attack-technique-tagger-demo/tagger.js
-
curl -L -o tagger.js https://huggingface.co/spaces/Project-Bastion/attack-technique-tagger-demo/resolve/main/tagger.js
2.2 kB
| // In-browser ATT&CK technique tagger glue (no DOM). Mirrors scripts/ttp/train_ttp.py input format: sentence + (prev[-300:] + " | " + next[:300]) as text_pair, max_length 256. | |
| export const THRESHOLD = 0.2; // chosen on the validation split | |
| export function ctxFor(prev, next) { return (prev || "").slice(-300) + " | " + (next || "").slice(0, 300); } | |
| export function splitSentences(text) { // simple splitter for the demo (the training data used pre-split report sentences) | |
| return text.replace(/\s+/g, " ").trim().split(/(?<=[.!?])\s+(?=[A-Z0-9"'(])/).filter((s) => s.length > 0); | |
| } | |
| // transformers.js keeps a text token (not the closing [SEP]) when it truncates; restore it so the ids equal the Python tokenizer's. | |
| export function encodeFixed(tokenizer, a, b, maxLen, Tensor) { | |
| const enc = b === undefined ? tokenizer(a, { truncation: true, max_length: maxLen }) : tokenizer(a, { text_pair: b, truncation: true, max_length: maxLen }); | |
| const ids = Array.from(enc.input_ids.data, Number); | |
| if (ids.length >= maxLen) { const sep = tokenizer.sep_token_id; if (sep === undefined) throw new Error("tokenizer.sep_token_id missing"); ids[ids.length - 1] = sep; } | |
| const n = ids.length; | |
| return { input_ids: new Tensor("int64", BigInt64Array.from(ids.map(BigInt)), [1, n]), attention_mask: new Tensor("int64", new BigInt64Array(n).fill(1n), [1, n]) }; | |
| } | |
| const sigmoid = (x) => 1 / (1 + Math.exp(-x)); | |
| export async function tagSentence(sentence, prev, next, tokenizer, model, threshold = THRESHOLD, Tensor) { | |
| const enc = encodeFixed(tokenizer, sentence, ctxFor(prev, next), 256, Tensor); | |
| const { logits } = await model(enc); | |
| const probs = Array.from(logits.data, (v) => sigmoid(v)); const id2 = model.config.id2label; | |
| const hits = probs.map((p, i) => ({ technique: id2[i], prob: p })).filter((h) => h.prob >= threshold).sort((a, b) => b.prob - a.prob); | |
| return { probs, hits }; | |
| } | |
| export async function tagReport(text, tokenizer, model, threshold = THRESHOLD, Tensor) { | |
| const S = splitSentences(text); const out = []; | |
| for (let i = 0; i < S.length; i++) out.push({ sentence: S[i], ...(await tagSentence(S[i], S[i - 1], S[i + 1], tokenizer, model, threshold, Tensor)) }); | |
| return out; | |
| } | |