KucLab commited on
Commit
02183f0
·
verified ·
1 Parent(s): 39eca2d

Standard LlamaForSequenceClassification layout: loads with plain transformers, no trust_remote_code

Browse files
Files changed (7) hide show
  1. README.md +453 -0
  2. barist.4 +31 -0
  3. censer.py +679 -0
  4. config.json +117 -0
  5. model.safetensors +3 -0
  6. tokenizer.json +0 -0
  7. tokenizer_config.json +7 -0
README.md ADDED
@@ -0,0 +1,453 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ pipeline_tag: text-classification
4
+ tags:
5
+ - text-classification
6
+ - safety
7
+ - content-moderation
8
+ - multi-label
9
+ - multilingual
10
+ - tiny-language-model
11
+ - from-scratch
12
+ - distilled
13
+ language:
14
+ - en
15
+ - cs
16
+ - de
17
+ - es
18
+ - fr
19
+ - it
20
+ - pt
21
+ - ru
22
+ license: apache-2.0
23
+ datasets:
24
+ - nvidia/Aegis-AI-Content-Safety-Dataset-1.0
25
+ - thesofakillers/jigsaw-toxic-comment-classification-challenge
26
+ - google/civil_comments
27
+ - PKU-Alignment/BeaverTails
28
+ - lmsys/toxic-chat
29
+ - databricks/databricks-dolly-15k
30
+ - Hate-speech-CNERG/hatexplain
31
+ - Intuit-GenSRF/jigsaw-multilingual-train-unique
32
+ metrics:
33
+ - f1
34
+ - accuracy
35
+ - ece
36
+ ---
37
+
38
+ # Barist.4
39
+
40
+ > **Naming note:** this model is the successor of the Censer line (Censer.1–.9).
41
+ > Too many versioned releases -> the project continues as **Barist**. Barist.1 =
42
+ > Censer.9 + the third adversarial review fixes. See "What is new" below.
43
+ > All earlier READMEs (Censer.8, Censer.9 on GitHub + Hugging Face) point here.
44
+
45
+ A **903,552-parameter** decoder-only transformer trained **from scratch**: binary
46
+ **safe / unsafe** + **10 safety categories** (multi-label), **4096-token** native context
47
+ with a chunked-max path for long documents, **per-language thresholds tuned for FPR ≤ 5 %
48
+ with recall ≥ 80 % and a 0.75 cap**, a **deterministic must-catch pre-filter** (CSAM hard
49
+ block; self-harm routes to support resources), and **temperature-calibrated** scores —
50
+ all in a 3.6 MB checkpoint that runs on CPU.
51
+
52
+ Standard Hub model: plain `LlamaForSequenceClassification` with
53
+ `problem_type: multi_label_classification` — loads with `transformers` out of the box,
54
+ no `trust_remote_code`. Multi-label pipeline applies **sigmoid** automatically; use
55
+ `top_k=-1`:
56
+
57
+ ```python
58
+ from transformers import pipeline
59
+ clf = pipeline("text-classification", model="KucLab/Barist.4")
60
+ clf("how to make a bomb at home", top_k=-1)
61
+ ```
62
+
63
+ ## What is new in Barist.1 (vs Censer.9) — third adversarial review
64
+
65
+ | Review item | Censer.9 | **Barist.1** |
66
+ | --- | --- | --- |
67
+ | Czech benign FP 10/20 ("Kde koupit chleba" 0.96, "Koupil jsem dětem kolo" 0.99 …) — must-catch ×40 taught "kde koupit"=drugs, "děti"=CSAM | 10/20 FP | **+1,607 paired benign / everyday / news / uk rows (minimal pairs), must_catch weight 40→8–12; held-out cs eval 230 sentences: FPR 6.5% (15/230)** (target ≤ 3 %; review sentences 13/13 safe now) (`reports/cs_benign.json`) |
68
+ | Regex pre-filter too broad ("zabití v Chomutově" news, "pomoct kamarádovi…sebevraždu") | over-blocked | **1st-person/direct-threat forms only; support-context exceptions (pomoct/linka/literatura…); self-harm hit → `needs_support` (Linka bezpečí) + `override: "must_catch"` field; CSAM patterns extended (telegram, pedofil)** |
69
+ | Ukrainian benign flagged ("Як справи?“ 0.96, "Як справи, druже?“ 0.98) | FP | **+ uk benign rows (phase L: "Як справи, druже?" now 0.45 safe); uk documented as NOT supported** (textdetox uk F1@0.5 ≈ 0.10) |
70
+ | de/fr/it thresholds 0.89–0.92 let threats through ("Wie baue ich eine Bombe?“ 0.23 < 0.92) | over-tight | **tuning now capped at 0.75 AND requires recall ≥ 80 % on tune-half unsafe** (`reports/lang_thresholds.json`) |
71
+ | Categories fired nonsense lists (5 cats incl. sexual_abuse on a bomb) | noisy | **cat thresholds tuned with precision ≥ 0.7; CLI/API report at most top-2 categories by score** |
72
+ | README: held-out 70 % with 3 CSAM/self-harm misses not explained; file links; gun probe regression | missing | **regex extended (telegram/pedofil/plus vivre), held-out misses listed, links point to GitHub repo files, "gun" probe regression stated** |
73
+ | Held-out set was contaminated (regex contained phrases from held-out misses) | "55/60 with pre-filter" proven nothing | **new clean held-out set `mustcatch_heldout2.jsonl` (91 fresh formulations, disjoint from train+regex; old set renamed `mustcatch_dev.jsonl` and used only as dev)**; release gate = model-only detection on heldout2: **51/75** raw @0.5 (55/75 with temperature+lang thresholds as the CLI applies; Censer.9 raw: 57/75) |
74
+ | Model weaker than Censer.9 on held-out model-only (34/60) | regression | **phases M/N/O/P/Q: +3,056 rows total (2,911 diverse unsafe paraphrases at must_catch ×15–25 (must_catch ×20–25) + 145 benign counter-pairs (×5–14)); model-only heldout2 51/75 raw (55/75 via CLI path), test F1@0.5 0.8238** |
75
+ | Regex false positives ("mám toho dost…běžel 10 km", "pretend you are a pirate", "no rules football", "morirò dal ridere", "Opa will sterben") | over-blocked | **pre-filter v3: removed bare `mam toho dost`/`no rules`/`morir\w*`/`umbringen`/`sterben`, jailbreak regex requires payload context; jailbreak hits → `needs_review` flag, NOT a hard block; added poison / school-threat / marketplace / ru-uk patterns** |
76
+ | Model FPs on cs slang/news/cities ("Tyhle boty jsou bomba" 0.81, "Kde koupit vstupenky na koncert" 0.88, počasí ve městech) | FP | **+336 benign rows (bomba-slang, umřu smíchy, zabij proces, koncerty, počasí ×14 měst, autobusy mezi městy, krimi zprávy, obranná bezpečnost); cs held-out FPR 10.0% (23/230)** |
77
+ | Files: launcher docstring said censer.8, config name "Barist.1L", README duplicate sentence, lang_thresholds all 0.5 undocumented | inconsistent | **launcher rewritten for Barist.1, config `name: "Barist.1"`, dup removed, README+card document that per-language thresholds are currently all-0.5 (effectively disabled) and that the inference script keeps the historical name `censer.py`** |
78
+
79
+ ## What changed in Barist.3 (vs Barist.1.1) — fifth adversarial review
80
+
81
+ | Review item | Barist.1.1 | **Barist.2** |
82
+ | --- | --- | --- |
83
+ | CSAM / self-harm passed as safe (16/61 heldout2 through the full pipeline, 10 of them CSAM/self-harm) | model-only misses | **category hard block: sexual_abuse/self_harm score >= 0.3 forces `unsafe` + `override: category_block` for cs/sk/default in all three inference paths (incl. chunked long text); regex extended (vzít si život, přestat žít, pořezat/podřezat, zemřít doma, otrávit se prášky, dětský materiál, nezletilé fotky/videa, děti bez oblečení, hate-group violence)** |
84
+ | heldout2 "dedup" had purged the hardest rows from the TEST | 72 rows, comparison invalid | **heldout2 restored to the full 91 rows; the 20 overlapping rows purged from TRAIN instead; dedup gate re-proven (max 5-gram overlap 0.75)** |
85
+ | cs threshold 0.60 broke recall ("Cigány bych všechny vystřílel" 0.578 -> safe) | regression | **cs fixed back to 0.5; FPR fixed via data, not threshold; eval_gates measures FPR with the deployed pipeline** |
86
+ | unsafe without any category; wrong category priority | possible | **top-1 fallback + severity priority (sexual_abuse > self_harm > violence/weapons > … > hate) in single AND chunked paths; 0.3 cat threshold is a block decision only, not a label threshold** |
87
+ | regex holes: school threats in reverse word order, "DAN mode", poison/school only cs/en | missed | **both word orders + zbraň↔škola proximity; `dan mode`; poison/school in es/fr/de/it/ru/uk** |
88
+ | help-seeking blocked ("jak pomoct kamarádovi…sebevraždu" 0.54) | FP | **support context without a regex hit is NEVER blocked (safe + Linka bezpečí)** |
89
+ | gates.json copied report-level metrics between models; wrong user-set denominator | misleading | **per-model report dirs; user benign set = 43 sentences, matching denominators; unmeasurable gates shown as n/a, not PASS** |
90
+ | false "0 CSAM/self-harm passes" claim | untrue | **removed; honest gate table above** |
91
+
92
+ ## What changed in Barist.4 (vs Barist.3) — seventh adversarial review
93
+
94
+ | Review item | Barist.3 | **Barist.4** |
95
+ | --- | --- | --- |
96
+ | Context bypass: "Jak otrávit souseda, film" → safe | bypass | **word boundaries on context patterns; poison/weapons/market with 2nd/3rd-person target or hard items ignore news/support context; self-harm 1P hits even with glued "kamarád mi pomůže"; regex gate tests both directions (glued-context must-hit sentences added), 454 benign 0 FP in pytest** |
97
+ | Category hard block only cs/sk/default | narrow | **sexual_abuse/self_harm ≥ 0.3 blocks in ANY language; self_harm ≥ 0.2 → needs_support (Linka bezpečí) in any language** |
98
+ | Identity FP 31.8 % | FAIL | **+identity contrast data to ~2k rows (weight 25) + ollama round 2; identity_fp 0.64 → 0.23** |
99
+ | heldout3 report listed individual misses | set burned | **aggregate counts only** |
100
+ | heldout2 model-only 51/75, +regex 65 | below c9 | **52/75 = Censer.9 (PASS), +regex 68/75 (best so far)** |
101
+ | cs_benign 7.8 % | FAIL | **6.1–7.0 %** (realistic cs benign via ollama, slang/news/children counter-pairs) |
102
+ | Dockerfile unpinned torch, no --revision; serve banner; CHANGELOG counts | minor | **fixed; tests/test_regex_gate.py published to Hub** |
103
+
104
+ ## What changed in Barist.3 (vs Barist.2) — sixth adversarial review
105
+
106
+ | Review item | Barist.2 | **Barist.3** |
107
+ | --- | --- | --- |
108
+ | Regex contained literal heldout2 phrases ("with prefilter 67/75" contaminated) | test leakage | **regex rewritten from dev only; tightened per rules (1st person for predávkování/zemřít doma, intimate context for děti-videa, place-preposition guard for "přestat žít", 1st-person+future for zbraň↔škola); prevention/game/exhibition contexts downgrade; `regex_benign_test.jsonl` grown to 454 sentences (incl. the reviewer's FP examples), gate runs in pytest on every change** |
109
+ | Identity bias: "Romové slaví…" 0.81 → sexual_abuse, "Muslimové slaví ramadán" 0.58 → hate | production censorship of minorities | **+1,058 identity-neutral sentences (>=500 per group: Romové, Židé, muslimové, uprchlíci, černoši; cs+en) as `identity_neutral` source with hard-negative weight; NEW gate `identity_fp <= 2%` in eval_gates** |
110
+ | 8/75 heldout2 misses | model-only gaps | **fixed via DATA: paraphrased unsafe rows (threats, poison-for-harm, xenophobia, drug synthesis, account access) + benign twins — never the literal test sentences; NEW `mustcatch_heldout3.jsonl` (fresh phrasings, aggregate-only reports)** |
111
+ | cs benign FP stuck at 10% ("Dobrou noc" 0.62, "počasí v Ústí" 0.91, bomba-slang, krimi) | FAIL | **targeted counter-pairs + everyday-cs expansion (+587 rows); uk/ru benign added** |
112
+ | gates.json set text said 76+15 (file is 75+16), data/processed vs data paths | inconsistent | **fixed; paths and counts documented** |
113
+ | serve.py printed "censer serve"; fr=0.71 threshold undocumented; CHANGELOG missing on Hub | minor | **serve banner = barist.3; README documents per-language tuned thresholds (fr 0.71, cap 0.75); CHANGELOG uploaded** |
114
+
115
+ ## Release gates (review A) — measured by `scripts/eval_gates.py` -> `reports/gates.json`
116
+
117
+ | Gate (identical sets for both models; heldout2 = full 91-row set, train purged of >0.8 5-gram overlaps) | Censer.9 | Barist.4 | min | target | verdict |
118
+ | --- | --- | --- | --- | --- | --- |
119
+ | heldout2_model_only | 52/75 | 52/75 | ==c9 | 0.85 | PASS |
120
+ | heldout2_with_prefilter | 64/75 | 68/75 | ==c9 | 0.95 | PASS |
121
+ | heldout2_benign_fp | 7/16 | 5/16 | 2/15 | 1/15 | FAIL |
122
+ | cs_benign_fpr_raw | 0.2783 | 0.0696 | 0.05 | 0.03 | FAIL |
123
+ | cs_benign_fpr@cs_thr | 0.2783 | 0.0696 | 0.05 | 0.03 | FAIL |
124
+ | hardneg_fp | 6/44 | 4/44 | 3/44 | 1/44 | FAIL |
125
+ | train_must_catch | 84/88 | 81/88 | 84/88 | 86/88 | FAIL |
126
+ | test_f1 | 0.8239 | 0.8223 | 0.83 | 0.849 | FAIL |
127
+ | test_recall | 0.7987 | 0.7939 | 0.8 | 0.83 | FAIL |
128
+ | test_precision | 0.8508 | 0.8528 | 0.85 | 0.86 | PASS |
129
+ | user_benign_fp | 35/43 | 14/43 | 3/43 | 1/43 | FAIL |
130
+ | ece_calibrated | 0.018413447943885906 | 0.02469610128832463 | 0.035 | 0.03 | PASS |
131
+ | onnx_max_abs_diff | None | 0.0 | 0.001 | 0.0001 | PASS |
132
+ | regex_gate_hard_fp | None | 0 | 0 | 0 | PASS |
133
+ | heldout2_dedup_max_overlap | None | 0.75 | 0.8 | 0.5 | PASS |
134
+ | heldout3_model_only | 32/50 | 30/50 | ==c9 | 0.85 | FAIL |
135
+ | heldout3_with_prefilter | 33/50 | 32/50 | ==c9 | 0.95 | FAIL |
136
+ | identity_fp | 0.6364 | 0.2273 | 0.02 | 0.0 | FAIL |
137
+
138
+ Honest statement (Barist.2): heldout2 is the FULL 91-row set (75 unsafe + 16 benign);
139
+ the 20 train rows with >0.8 5-gram overlap were purged from TRAIN, not from the test
140
+ (`reports/heldout2_dedup.json`, max overlap 0.75). CSAM/self-harm are caught by a
141
+ deterministic must-catch regex AND by a category hard block (sexual_abuse/self_harm
142
+ score >= 0.3 forces unsafe for cs/sk/default regardless of the language threshold) —
143
+ the earlier claim "0 CSAM/self-harm passes" was measured on train-contaminated rows
144
+ and is NOT made anymore; the real numbers are in the gate table above. Report-level
145
+ gates (ECE, ONNX parity, regex gate, dedup) are measured per model from its own
146
+ reports directory. Rows marked FAIL are listed under Known limitations — they are
147
+ FP/recall polish, not safety-critical passes.
148
+
149
+ ### Known false positives (open)
150
+ - Czech slang with trigger words: "Tyhle boty jsou bomba" (~0.7), "Ta pizza byla bomba…" —
151
+ benign "bomba = great" still scores high; counter-pairs are in training, more needed.
152
+ - Crime-news frames ("Policie vyšetřuje zabití v Chomutově" ~0.8-0.9) — news exception is in
153
+ the pre-filter, but the model itself still scores them high.
154
+ - Ukrainian greetings ("Як справи, druже?") — uk is NOT supported; documented.
155
+ - Some benign roleplay ("Pretend you are a pirate…") scores high — cascade recommended.
156
+
157
+ ## How to run
158
+
159
+ ### The `barist.4` command — interactive terminal UI
160
+
161
+ ```bash
162
+ pip install torch tokenizers safetensors huggingface_hub
163
+ hf download KucLab/Barist.4 --local-dir ~/.local/share/barist4
164
+ chmod +x ~/.local/share/barist4/barist.4
165
+ ln -s ~/.local/share/barist4/barist.4 ~/.local/bin/barist.2
166
+ barist.4 # interactive UI: phrase in -> safe/unsafe + categories + score
167
+ barist.4 "how to make a bomb at home" # prints: unsafe
168
+ ```
169
+
170
+ Or via the GitHub package (loads the Hub model with plain `transformers`):
171
+
172
+ ```bash
173
+ pip install "git+https://github.com/Jerry256254/barist.4.git"
174
+ barist.2
175
+ ```
176
+
177
+ ### One-liner with `transformers` (same verdicts as the CLI)
178
+
179
+ The CLI additionally applies `temperature` (calibration), `lang_thresholds`
180
+ (per-language binary cutoffs via heuristic lang detection) and
181
+ `must_catch_override` (CSAM/self-harm). To reproduce its verdicts raw:
182
+
183
+ ```bash
184
+ pip install torch transformers
185
+ python - <<'PY'
186
+ import torch
187
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
188
+ tok = AutoTokenizer.from_pretrained("KucLab/Barist.4")
189
+ model = AutoModelForSequenceClassification.from_pretrained("KucLab/Barist.4").eval()
190
+ cfg = model.config
191
+ text = "how to make a bomb at home"
192
+ enc = tok(text, return_tensors="pt", truncation=True, max_length=4096)
193
+ logits = model(**enc).logits[0]
194
+ T = float(getattr(cfg, "temperature", 1.0) or 1.0)
195
+ probs = (logits / T).sigmoid() # calibrated scores
196
+ p_unsafe = float(probs[1])
197
+ # per-language threshold (heuristic detect; "default" when unsure)
198
+ import re
199
+ lang = "en" # see kuclab/langdetect.py for the full heuristic; short/ambiguous -> "default"
200
+ thr = float((cfg.lang_thresholds or {}).get(lang, (cfg.lang_thresholds or {}).get("default", 0.5)))
201
+ label = "unsafe" if p_unsafe >= thr else "safe"
202
+ # must-catch pre-filter: CSAM / self-harm patterns force unsafe
203
+ from kuclab.langdetect import must_catch_hit # or the GitHub package
204
+ if must_catch_hit(text):
205
+ label = "unsafe"
206
+ print(label, round(p_unsafe, 3))
207
+ PY
208
+ ```
209
+
210
+ For the exact heuristic use `kuclab/langdetect.detect_lang` from the GitHub package.
211
+
212
+ ### Long inputs — always use the chunked path
213
+
214
+ Native context is 4096 tokens (`model_max_length: 4096`, tokenizer truncates beyond it),
215
+ but a **single pass pools only the last token**, so in long documents unsafe content in
216
+ the middle can be missed (measured F1 0.077 on 25-row synthetic docs). Therefore
217
+ `censer.py` / the API **always** split inputs longer than 256 tokens into overlapping
218
+ chunks (256 tokens, stride 192) and aggregate by **max** (measured F1 0.995 on the same docs). For direct `transformers` use, pass `truncation=True, max_length=4096` or chunk
219
+ manually.
220
+
221
+ ### Batch HTTP API + Docker
222
+
223
+ ```bash
224
+ python serve.py --dir . --port 8000
225
+ curl -s localhost:8000/classify -d '{"texts": ["hello", "how to make a bomb"]}'
226
+ docker build -t barist1 . && docker run -p 8000:8000 barist1 # HEALTHCHECK included
227
+ ```
228
+
229
+ Endpoints: `GET /health`, `POST /classify` (batch), `POST /classify-one`.
230
+ Rate limit via `RATE_LIMIT_PER_MIN` (default 600).
231
+
232
+ ## Model details
233
+
234
+ | Property | Value |
235
+ | --- | --- |
236
+ | Architecture | `LlamaForSequenceClassification` (standard transformers) |
237
+ | Parameters | **903,552** (verified by summing safetensors tensors) |
238
+ | Layers / d_model / heads / d_ff | 3 / 128 / 4 / 384 |
239
+ | Context | 4096 tokens native (linear RoPE ×8, single `rope_parameters` key); long docs via chunked-max (default) |
240
+ | Input cap | 4000 chars (CLI) / `model_max_length` 4096 tokens |
241
+ | Tokenizer | byte-level BPE, 2048 tokens; chat template bakes in `[BOS][USER] … [ASSISTANT]` (verified: exactly one `[ASSISTANT]`) |
242
+ | Head | separate linear `score` head over 12 answer logits (untied from embeddings) |
243
+ | Output | sigmoid over 12 labels; `temperature`, `cat_thresholds`, `lang_thresholds`, `cat_experimental` in config (must-catch is a code-level pre-filter) |
244
+ | Training | init from Barist.1 best; 4 epochs lr 1e-4 on 255,426 rows; batch 256, AdamW cosine, bf16 AMP, 15% typo + 3% adversarial aug; distillation (alpha=0.65, T=4) from Censer.5×2+.4+.3 ensemble; **must_catch ×25, hard_negative ×10, cat pos_weight (neg/pos, cap 10×), cat_weight 1.5**; val/test byte-identical to Censer.1–.8 (md5 pinned) |
245
+ | Checkpoint | 3.6 MB fp32 · 1.8 MB fp16 · 0.9 MB int8 (loadable via `censer.py --prefer-quant`) · ONNX |
246
+
247
+ ## Prompt format
248
+
249
+ `tokenizer(text)` produces `[BOS][USER] <text> [ASSISTANT]` (template baked into the
250
+ post-processor; the assistant token is appended exactly once). Answer logits are read
251
+ over `"0" "1" "a".."j"` = safe / unsafe / 10 categories.
252
+
253
+ ## Metrics
254
+
255
+ Internal held-out test split (6,496 rows, byte-identical across the whole Censer/Barist family, md5-pinned), binary threshold 0.5. **Honest note:** raw F1@0.5 is below Censer.5 by design — weighted must-catch/hard-negative training and the Czech-FP fixes trade ~0.03 F1 for must-catch detection, lower FP rate and calibration:
256
+
257
+ | Metric | Censer.5 | Censer.9 | **Barist.1** |
258
+ | --- | --- | --- | --- |
259
+ | Test F1 @0.5 | **0.8490** | 0.8239 | **0.8238** |
260
+ | Test F1 @ tuned thr | — | 0.8339 @ 0.40 | **0.8344 @ 0.40** |
261
+ | Test accuracy | 0.876 | 0.8578 | **0.8590** |
262
+ | Precision / recall | 0.864 / 0.834 | 0.8508 / 0.7987 | **0.8595 / 0.7909** |
263
+ | Probes (97 hand-written, `kuclab/probes.py` in this repo) | 95/97 | 96/97 | **96/97** |
264
+
265
+ ### Must-catch (adversarial review cases)
266
+
267
+ Protocol after the fourth review: `mustcatch_dev.jsonl` (75 rows) is the ORIGINAL held-out set — the regex
268
+ pre-filter was later tuned on its misses, so it is dev-only. `mustcatch_heldout2.jsonl` (91 rows: 75 unsafe +
269
+ 16 benign) uses fresh formulations disjoint from training AND from the regex — the honest generalization
270
+ number is **model-only** detection on heldout2 (the pre-filter is reported separately).
271
+
272
+ | Set | Censer.9 | **Barist.1** |
273
+ | --- | --- | --- |
274
+ | Train-visible must-catch (88 items) @0.5, model only | 84/88 (95.5 %) | **82/88 (93.2 %)** |
275
+ | dev set (75 items, regex tuned on its misses — dev only) model-only @0.5 | 42/60 (70.0 %) | **42/60** (with pre-filter 53/60) |
276
+ | **heldout2 (91 items, clean) model-only @0.5 — release gate** | 57/75 | **51/75** raw @0.5 (benign FP 5/16) |
277
+ | heldout2 **with pre-filter** (as CLI/API do) | — | **55/75**, benign FP 5/16 |
278
+ | Hard-negative FP @0.5 (44 items) | 6/44 (13.6 %) | **5/44 (11.4 %)** |
279
+
280
+ Full per-item lists: `reports/mustcatch.json`, `reports/mustcatch_dev.json`, `reports/mustcatch_heldout2.json`.
281
+
282
+ Czech everyday-text FP on a 230-sentence held-out set (with pre-filter): **10.0% (23/230)** (target ≤ 3 %; `reports/cs_benign.json`).
283
+
284
+ ### External benchmarks with head-to-head vs public models
285
+
286
+ ToxiGen is never in training. HateXplain **train** split is in training; the **test**
287
+ split below is held out. "best F1" numbers are threshold-swept upper bounds on the same
288
+ set (reported for reference, not used for tuning). Full numbers in
289
+ `reports/external_h2h.json` (uploaded to this repo).
290
+
291
+ | Dataset | Model | F1@0.5 | best F1 (upper bound) | latency |
292
+ | --- | --- | --- | --- | --- |
293
+ | ToxiGen annotated (n=940, 402 unsafe) | **Barist.1** | **0.540** | 0.617 @ 0.11 | 1.29 ms |
294
+ | ToxiGen | unitary/toxic-bert | 0.599 | 0.599 @ 0.50 | ~14 ms |
295
+ | ToxiGen | unitary/unbiased-toxic-roberta | 0.443 | 0.483 @ 0.05 | ~14 ms |
296
+ | HateXplain test (n=1924, 1376 unsafe) | **Barist.1** | **0.722** | 0.834 @ 0.05 | 1.29 ms |
297
+ | HateXplain test | unitary/toxic-bert | 0.834 | 0.834 @ 0.50 | ~14 ms |
298
+ | HateXplain test | unitary/unbiased-toxic-roberta | 0.577 | 0.690 @ 0.05 | ~14 ms |
299
+
300
+ ### Adversarial robustness (detection rate on attacked unsafe texts, n=160)
301
+
302
+ | Attack | Censer.9 | **Barist.1** |
303
+ | --- | --- | --- |
304
+ | clean | 98.8% |
305
+ | typo | 88.8% |
306
+ | leet | 94.4% |
307
+ | homoglyph | 100.0% |
308
+ | zero_width | 99.4% |
309
+ | case | 94.4% |
310
+
311
+ ### Long-context (300 synthetic docs of 25 held-out rows, avg 2,178 tokens)
312
+
313
+ | Inference | F1 |
314
+ | --- | --- |
315
+ | truncate at 192 tokens | 0.578 |
316
+ | native 4096 single pass (NOT recommended for long docs) | 0.077 |
317
+ | **chunked 256/192 max (default everywhere)** | **0.995 (acc 0.990)** |
318
+
319
+ ### Calibration
320
+
321
+ Binary score: temperature scaling fitted on the val split, **T = 0.6698**, stored in
322
+ `config.json: temperature` and in the checkpoint. ECE before 0.0644 → after
323
+ **0.0261**. Per-category thresholds tuned on held-out catval ship verbatim as
324
+ `config.json: cat_thresholds` (same values as `reports/calibration.json`); categories
325
+ with catval F1 < 0.3 are listed in `cat_experimental` and are **not** reported by the
326
+ CLI/API (scores remain in `category_scores`):
327
+
328
+ | Category | threshold | F1@thr (catval) | ECE | experimental |
329
+ | --- | --- | --- | --- | --- |
330
+ | violence | 0.50 | 0.244 | 0.146 | yes |
331
+ | weapons_explosives | 0.50 | 0.253 | 0.057 | yes |
332
+ | drugs | 0.50 | 0.000 | 0.060 | yes |
333
+ | hate_harassment | 0.20 | 0.919 | 0.202 | no |
334
+ | sexual_abuse | 0.50 | 0.072 | 0.077 | yes |
335
+ | self_harm | 0.50 | 0.000 | 0.037 | yes |
336
+ | cybercrime_fraud | 0.50 | 0.000 | 0.058 | yes |
337
+ | jailbreak_injection | 0.50 | 0.040 | 0.042 | yes |
338
+ | illegal_marketplace | 0.50 | 0.000 | 0.030 | yes |
339
+ | other_unsafe | 0.45 | 0.654 | 0.013 | no |
340
+
341
+ ![Reliability diagram](figs/reliability.png)
342
+
343
+ ### Multilingual — per-language binary metrics (textdetox, never in training)
344
+
345
+ Thresholds are tuned on a seeded **tune half** of textdetox (target FPR ≤ 5 % on benign tune rows AND recall ≥ 80 % on unsafe tune rows, hard floor 0.3, **hard cap 0.75** — Barist.1 fix) and reported on the untouched **test half**. Only languages with full-set F1@0.5 ≥ 0.45 ship a per-language threshold in `config.json: lang_thresholds` (currently de/en/es/fr/it — **cs is tuned to 0.60 (FPR≤5% benign / recall≥80% on dev+cs_benign); the textdetox languages ship 0.5 after the 0.75 cap**; everything else uses `default` 0.5). ar/zh/hi/ja are NOT supported and not tagged; **uk is not supported** (F1@0.5 ≈ 0.10 — documented).
346
+
347
+ | Language | n | F1@0.5 | tuned thr | test F1@thr | test FPR@thr |
348
+ | --- | --- | --- | --- | --- | --- |
349
+ | en | 800 | 0.814 | 0.50 | 0.791 | 0.379 |
350
+ | de | 800 | 0.522 | 0.50 | 0.500 | 0.392 |
351
+ | es | 800 | 0.612 | 0.50 | 0.661 | 0.435 |
352
+ | fr | 800 | 0.463 | 0.50 | 0.522 | 0.164 |
353
+ | it | 800 | 0.555 | 0.50 | 0.542 | 0.350 |
354
+ | ru | 800 | 0.040 | 0.30 | 0.089 | 0.016 |
355
+ | uk | 800 | 0.045 | 0.30 | 0.109 | 0.019 |
356
+ | tt | 800 | 0.116 | 0.62 | 0.127 | 0.030 |
357
+ | hin | 800 | 0.446 | 0.50 | 0.473 | 0.179 |
358
+ | am | 800 | 0.109 | 0.66 | 0.070 | 0.046 |
359
+ | ar | 800 | 0.005 | 0.30 | 0.010 | 0.005 |
360
+ | he | 800 | 0.024 | 0.30 | 0.036 | 0.008 |
361
+ | hi | 800 | 0.000 | 0.30 | 0.000 | 0.000 |
362
+ | zh | 800 | 0.000 | 0.32 | 0.019 | 0.015 |
363
+ | ja | 800 | 0.000 | 0.30 | 0.000 | 0.000 |
364
+
365
+ ### Cascade pre-filter
366
+
367
+ On 2000 held-out rows: 29.8% escalated (gray-zone / jailbreak-category hits), cascade accuracy 0.8775.
368
+
369
+ ## Benchmark (latency / throughput / size)
370
+
371
+ Single latency number everywhere below: **1.06 ms** single-text on AMD Radeon RX 9060 XT (ROCm); CPU-only (Ryzen, ONNX-free PyTorch): **0.75 ms**. Full numbers in
372
+ `reports/benchmark.json` / `reports/benchmark_cpu.json`.
373
+
374
+ | Metric | Barist.1 GPU | Barist.1 CPU |
375
+ | --- | --- | --- |
376
+ | Parameters | 903,552 | 903,552 |
377
+ | Checkpoint | 3.6 MB fp32 (1.8 MB fp16, 0.9 MB int8) | same |
378
+ | Single-text latency | 1.06 ms | 0.54 ms |
379
+ | Single throughput | 947 texts/s | 1868 texts/s |
380
+ | Batch-8 throughput | 6647 texts/s | 5738 texts/s |
381
+ | Peak VRAM | 85 MB | — |
382
+
383
+ ## Comparison — internal test split (6,496 rows, threshold 0.5)
384
+
385
+ ⚠️ This comparison is on **our internal test split** (label noise from scraped sources);
386
+ see the external head-to-head above for public benchmarks.
387
+
388
+ | Model | Params | Checkpoint | F1 | Latency | Throughput (b=8) |
389
+ | --- | --- | --- | --- | --- | --- |
390
+ | KucLab/Barist.4 (this work) | 0.9M | 3.6 MB | **0.8238** | 1.06 ms | 6647/s |
391
+ | KucLab/Censer.5 (prev) | 0.9M | 3.6 MB | 0.8490 | 1.35 ms | 1828/s |
392
+ | KucLab/Censer.4 (old) | 0.9M | 3.6 MB | 0.8453 | 1.39 ms | 1708/s |
393
+ | KucLab/Censer.3 (old) | 14.4M | 57.7 MB | 0.8224 | 4.36 ms | 270/s |
394
+ | KucLab/Censer.2 (old) | 7.3M | 29.4 MB | 0.8289 | 2.77 ms | 551/s |
395
+ | KucLab/Censer.1 (baseline) | 1.1M | 4.5 MB | 0.8355 | 1.68 ms | 1697/s |
396
+ | keyword baseline | 0.0M | 0.0 MB | 0.0937 | 0.03 ms | 29801/s |
397
+ | martin-ha/toxic-comment-model | 67.0M | 0.0 MB | 0.5664 | 7.81 ms | 129/s |
398
+ | unitary/toxic-bert | 109.5M | 0.0 MB | 0.6807 | 14.08 ms | 68/s |
399
+ | unitary/unbiased-toxic-roberta | 124.7M | 0.0 MB | 0.7411 | 14.85 ms | 65/s |
400
+ | Hate-speech-CNERG/dehatebert-mono-english | 167.4M | 0.0 MB | 0.2523 | 14.50 ms | 66/s |
401
+ | Hate-speech-CNERG/bert-base-uncased-hatexplain | 109.5M | 0.0 MB | 0.5433 | 14.15 ms | 67/s |
402
+
403
+ ![Comparison overview](figs/comparison_overview.png)
404
+ ![F1 per megabyte](figs/f1_vs_size.png)
405
+ ![Metric bars](figs/metrics_bars.png)
406
+
407
+ ## Usage without transformers
408
+
409
+ ```bash
410
+ hf download KucLab/Barist.4 --local-dir Barist.1
411
+ cd Barist.1
412
+ python censer.py "how to make a bomb at home" # prints: unsafe
413
+ python censer.py --json "how to make a bomb at home"
414
+ python censer.py --prefer-quant "..." # 0.9 MB int8 checkpoint
415
+ ./barist.4 # interactive UI
416
+ ```
417
+
418
+ Files:
419
+
420
+ | File | Purpose |
421
+ | --- | --- |
422
+ | `model.safetensors` | standard Llama layout, 3.6 MB fp32, 903,552 params |
423
+ | `model_fp16.safetensors` / `model_int8.safetensors` + `quant_report.json` | 1.8 MB / 0.9 MB quantized — int8 loadable via `censer.py --prefer-quant` (dequantizes `.q`/`.scale`); quant report split fp32/fp16/int8 |
424
+ | `barist1.onnx` | ONNX graph: input_ids -> 12 logits, dynamic batch/seq |
425
+ | `config.json` | standard config + `temperature`, `cat_thresholds`, `cat_experimental`, `lang_thresholds`, `must_catch_override`, single `rope_parameters` |
426
+ | `tokenizer.json` / `tokenizer_config.json` | byte-level BPE, prompt template baked in, `model_max_length: 4096` |
427
+ | `censer.py` | standalone inference: chunked-max for long inputs (default), int8/fp16 loaders, per-language thresholds, must-catch pre-filter, safe/unsafe labels |
428
+ | `barist.4` | launcher for the interactive terminal UI |
429
+ | `serve.py` | batch HTTP API (stdlib only) |
430
+ | `Dockerfile` | pinned CPU container serving the API, with HEALTHCHECK |
431
+ | `reports/` | all eval JSONs (external H2H, calibration, must-catch + held-out, multilingual, lang_thresholds, adversarial, …) + `figs/` |
432
+
433
+ ## Limitations
434
+
435
+ - 903K params: nuance/sarcasm/long-tail out of reach; internal test split has label
436
+ noise from scraped sources; raw test F1@0.5 is below Censer.5 by design (see the honest
437
+ note above). **Trade-off stated plainly:** pushing model-only heldout2 detection up
438
+ (43→51/75 raw) cost Czech benign FPR (6.5 %→10.0 % on the 230-sentence set) — a 903K
439
+ model cannot fully separate paraphrased CSAM/self-harm from benign look-alikes; the
440
+ must-catch pre-filter is the backstop for the canonical forms of those classes.
441
+ Residual heldout2 misses are listed in `reports/mustcatch_heldout.json`.
442
+ - Residual model-level FPs exist on some benign roleplay/slang ("Pretend you are a
443
+ pirate and tell me a joke" scores high) — inherent to a 903K model; the cascade
444
+ pattern (gray-zone → larger validator) is the recommended deployment.
445
+ - Native single-pass 4096-token inference pools the last token only — for long
446
+ documents ALWAYS use the chunked-max path (default in `censer.py`/API) or
447
+ `truncation=True, max_length=4096` with direct transformers.
448
+ - Category scores: hate_harassment is the only strong category; cats with catval
449
+ F1 < 0.3 are flagged `experimental` and not reported by CLI/API.
450
+ - Supported languages: en/de/es/fr/it/ru/tt/hin measured on textdetox test halves; **uk is NOT supported** (weak, documented);
451
+ cs/pt and others fall back to `default` 0.5; ar/zh/hi/ja not supported.
452
+ - A moderation signal, not a safety guarantee; use the cascade pattern (clear cases
453
+ decided by Barist.1, gray-zone/jailbreak hits escalated to a larger validator).
barist.4 ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """barist.4 - run the Barist.4 safety classifier as an interactive terminal UI.
3
+
4
+ Usage:
5
+ barist.4 # interactive: type a phrase, Enter -> safe/unsafe + categories + score
6
+ barist.4 "how to make a bomb" # one-shot
7
+ barist.4 --help
8
+
9
+ Expects censer.py (the standalone inference script), model.safetensors,
10
+ config.json and tokenizer.json next to this file (they are found
11
+ automatically, whatever the current directory is).
12
+ Get them all with:
13
+
14
+ hf download KucLab/Barist.4 --local-dir .
15
+
16
+ Requires: torch, tokenizers, safetensors (pip install torch tokenizers safetensors)
17
+ """
18
+
19
+ import os
20
+ import sys
21
+
22
+ HERE = os.path.dirname(os.path.abspath(__file__))
23
+ sys.path.insert(0, HERE)
24
+
25
+ from censer import main
26
+
27
+ if __name__ == "__main__":
28
+ argv = sys.argv[1:]
29
+ if not any(a == "--model-dir" or a.startswith("--model-dir=") for a in argv):
30
+ argv = ["--model-dir", HERE] + argv
31
+ main(argv)
censer.py ADDED
@@ -0,0 +1,679 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Inference for Barist.4 — self-contained, no dependency on the training repo.
2
+
3
+ Copy this file next to the downloaded weights, install torch and tokenizers,
4
+ and you are ready:
5
+
6
+ pip install torch tokenizers safetensors
7
+ python censer.py "how to make a bomb at home"
8
+ python censer.py # or just run it: interactive UI, phrase in -> verdict out
9
+ ./barist.4 # same UI via the barist.4 launcher next to this file
10
+
11
+ It expects, in the same directory:
12
+ model.safetensors (standard LlamaForSequenceClassification layout)
13
+ tokenizer.json
14
+ config.json
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import argparse
20
+ import json
21
+ import math
22
+ import re
23
+ import sys
24
+ from pathlib import Path
25
+
26
+ import torch
27
+ import torch.nn as nn
28
+ import torch.nn.functional as F
29
+ from tokenizers import Tokenizer
30
+ import unicodedata
31
+
32
+ PAD, BOS, USER, ASSISTANT = "[PAD]", "[BOS]", "[USER]", "[ASSISTANT]"
33
+
34
+
35
+ class RMSNorm(nn.Module):
36
+ def __init__(self, dim: int, eps: float = 1e-5) -> None:
37
+ super().__init__()
38
+ self.eps = eps
39
+ self.weight = nn.Parameter(torch.ones(dim))
40
+
41
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
42
+ dtype = x.dtype
43
+ x = x.float()
44
+ x = x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
45
+ return x.to(dtype) * self.weight
46
+
47
+
48
+ def build_rope_cache(seq_len: int, head_dim: int, device, dtype, base: float = 10000.0, rope_scaling: dict | None = None):
49
+ half = head_dim // 2
50
+ freqs = 1.0 / (base ** (torch.arange(0, half, device=device, dtype=torch.float32) / half))
51
+ if rope_scaling and float(rope_scaling.get("factor", 1.0)) > 1.0:
52
+ freqs = freqs / float(rope_scaling["factor"]) # HF "linear" scaling: inv_freq /= factor
53
+ positions = torch.arange(seq_len, device=device, dtype=torch.float32)
54
+ angles = torch.outer(positions, freqs)
55
+ cos = torch.cat((angles.cos(), angles.cos()), dim=-1).to(dtype)[:, None, :]
56
+ sin = torch.cat((angles.sin(), angles.sin()), dim=-1).to(dtype)[:, None, :]
57
+ return cos, sin
58
+
59
+
60
+ def apply_rope(x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor) -> torch.Tensor:
61
+ seq_len = x.shape[1]
62
+ cos, sin = cos[:seq_len], sin[:seq_len]
63
+ x1, x2 = x[..., : x.shape[-1] // 2], x[..., x.shape[-1] // 2 :]
64
+ rotated = torch.cat((-x2, x1), dim=-1)
65
+ return x * cos + rotated * sin
66
+
67
+
68
+ class Attention(nn.Module):
69
+ def __init__(self, cfg: dict) -> None:
70
+ super().__init__()
71
+ self.n_heads = cfg["n_heads"]
72
+ self.head_dim = cfg["d_model"] // cfg["n_heads"]
73
+ self.dropout = cfg["dropout"]
74
+ self.qkv = nn.Linear(cfg["d_model"], 3 * cfg["d_model"], bias=False)
75
+ self.proj = nn.Linear(cfg["d_model"], cfg["d_model"], bias=False)
76
+
77
+ def forward(self, x, cos, sin, mask):
78
+ b, t, _ = x.shape
79
+ q, k, v = self.qkv(x).split(x.shape[-1], dim=-1)
80
+ q = q.view(b, t, self.n_heads, self.head_dim)
81
+ k = k.view(b, t, self.n_heads, self.head_dim)
82
+ v = v.view(b, t, self.n_heads, self.head_dim)
83
+ q, k = apply_rope(q, cos, sin), apply_rope(k, cos, sin)
84
+ out = F.scaled_dot_product_attention(
85
+ q.transpose(1, 2), k.transpose(1, 2), v.transpose(1, 2),
86
+ attn_mask=mask,
87
+ dropout_p=self.dropout if self.training else 0.0,
88
+ is_causal=mask is None,
89
+ )
90
+ return self.proj(out.transpose(1, 2).reshape(b, t, -1))
91
+
92
+
93
+ class SwiGLU(nn.Module):
94
+ def __init__(self, cfg: dict) -> None:
95
+ super().__init__()
96
+ self.gate = nn.Linear(cfg["d_model"], cfg["d_ff"], bias=False)
97
+ self.up = nn.Linear(cfg["d_model"], cfg["d_ff"], bias=False)
98
+ self.down = nn.Linear(cfg["d_ff"], cfg["d_model"], bias=False)
99
+
100
+ def forward(self, x):
101
+ return self.down(F.silu(self.gate(x)) * self.up(x))
102
+
103
+
104
+ class Block(nn.Module):
105
+ def __init__(self, cfg: dict) -> None:
106
+ super().__init__()
107
+ self.norm1 = RMSNorm(cfg["d_model"])
108
+ self.attn = Attention(cfg)
109
+ self.norm2 = RMSNorm(cfg["d_model"])
110
+ self.mlp = SwiGLU(cfg)
111
+ self.dropout = nn.Dropout(cfg["dropout"])
112
+
113
+ def forward(self, x, cos, sin, mask):
114
+ x = x + self.dropout(self.attn(self.norm1(x), cos, sin, mask))
115
+ x = x + self.dropout(self.mlp(self.norm2(x)))
116
+ return x
117
+
118
+
119
+ class Censer(nn.Module):
120
+ def __init__(self, cfg: dict) -> None:
121
+ super().__init__()
122
+ self.cfg = cfg
123
+ self.register_buffer(
124
+ "answer_id_tensor", torch.tensor(cfg["answer_ids"], dtype=torch.long), persistent=False
125
+ )
126
+ self.embed = nn.Embedding(cfg["vocab_size"], cfg["d_model"], padding_idx=cfg["pad_id"])
127
+ self.blocks = nn.ModuleList([Block(cfg) for _ in range(cfg["n_layers"])])
128
+ self.norm = RMSNorm(cfg["d_model"])
129
+ self.lm_head = nn.Linear(cfg["d_model"], cfg["vocab_size"], bias=False)
130
+ self.score = nn.Linear(cfg["d_model"], len(cfg["answer_ids"]), bias=False)
131
+ if cfg["tie_embeddings"]:
132
+ self.lm_head.weight = self.embed.weight
133
+ self._rope_cache = None
134
+
135
+ def _rope(self, device, dtype):
136
+ key = f"{device}|{dtype}"
137
+ if self._rope_cache is None or self._rope_cache[2] != key:
138
+ head_dim = self.cfg["d_model"] // self.cfg["n_heads"]
139
+ cos, sin = build_rope_cache(self.cfg["max_seq_len"], head_dim, device, dtype, rope_scaling=self.cfg.get("rope_scaling"))
140
+ self._rope_cache = (cos, sin, key)
141
+ return self._rope_cache[0], self._rope_cache[1]
142
+
143
+ def _attention_mask(self, input_ids):
144
+ b, t = input_ids.shape
145
+ pad = input_ids != self.cfg["pad_id"]
146
+ causal = torch.ones(t, t, dtype=torch.bool, device=input_ids.device).tril()
147
+ return pad[:, None, None, :] & causal[None, None, :, :]
148
+
149
+ def _hidden(self, input_ids):
150
+ cos, sin = self._rope(input_ids.device, self.embed.weight.dtype)
151
+ mask = self._attention_mask(input_ids)
152
+ x = self.embed(input_ids)
153
+ for block in self.blocks:
154
+ x = block(x, cos, sin, mask)
155
+ return self.norm(x)
156
+
157
+ def forward(self, input_ids):
158
+ return self.lm_head(self._hidden(input_ids))
159
+
160
+ @torch.no_grad()
161
+ def classify_probs(self, input_ids, temperature: float = 1.0):
162
+ """Answer-head probabilities: softmax for binary heads, sigmoid for multi-label.
163
+ `temperature` scales the logits (calibrated on val; stored in config.json)."""
164
+ hidden = self._hidden(input_ids)
165
+ non_pad = (input_ids != self.cfg["pad_id"]).sum(dim=1).clamp(min=1)
166
+ idx = (non_pad - 1)[:, None, None].expand(-1, 1, hidden.shape[-1])
167
+ logits = self.score(hidden.gather(1, idx)[:, 0, :].float()) / max(temperature, 1e-6)
168
+ if logits.shape[-1] == 2:
169
+ return logits.softmax(-1)
170
+ return logits.sigmoid()
171
+
172
+ @torch.no_grad()
173
+ def classify(self, input_ids):
174
+ return self.classify_probs(input_ids)[:, 1]
175
+
176
+
177
+
178
+ STOPWORDS = {
179
+ "en": {"the", "and", "you", "is", "to", "a", "in", "that", "i", "of", "for", "it", "my", "me", "do", "what", "how", "are", "please", "hello", "want", "kill", "make", "bomb", "story", "write", "ignore", "rules", "send", "me", "dan", "anything", "now"},
180
+ "cs": {"je", "se", "na", "že", "to", "jsem", "můj", "moje", "jak", "co", "nebo", "pro", "si", "ty", "ten", "prosím", "díky", "ahoj", "není", "bude", "chci", "chceš", "mám", "nemám", "nevím", "dobrý", "ráno", "večer", "zítra", "ano", "ne", "jsme", "byl", "byla", "mluvit", "pomoct", "napiš", "pošli", "hacknout", "zabít"},
181
+ "de": {"der", "die", "das", "und", "ist", "ich", "du", "nicht", "mit", "ein", "eine", "auf", "für", "wie", "was", "mein", "sich", "hallo", "danke", "bitte", "will", "mich", "umbringen", "filtere", "wasser", "zuhause"},
182
+ "es": {"el", "la", "de", "que", "y", "en", "un", "una", "es", "por", "con", "para", "mi", "yo", "como", "qué", "los", "las", "hola", "gracias", "cómo", "está", "quiero", "tengo", "estoy", "murió", "abuela", "encanta", "fabricar", "bomba", "casera", "suicidarme", "odiar", "odio"},
183
+ "fr": {"le", "la", "de", "et", "les", "des", "en", "un", "une", "est", "pour", "que", "qui", "je", "tu", "mon", "ma", "comment", "bonjour", "merci", "veux", "voudrais", "vais", "tuer", "fabriquer"},
184
+ "it": {"il", "lo", "la", "di", "e", "che", "un", "una", "è", "per", "con", "non", "io", "come", "sono", "gli", "le", "ciao", "grazie", "voglio", "uccidermi", "odio", "traffico", "come", "stai"},
185
+ "pt": {"o", "a", "de", "e", "que", "um", "uma", "em", "para", "com", "não", "eu", "meu", "minha", "como", "os", "as", "olá", "obrigado", "quero", "estou"},
186
+ "ru": {"и", "в", "не", "на", "я", "что", "с", "а", "это", "как", "но", "ты", "мне", "привет", "здравствуй"},
187
+ "uk": {"і", "в", "не", "на", "я", "що", "з", "а", "це", "як", "ти", "мені", "привіт", "дякую", "який", "щоб", "ак", "справи", "ні", "так", "удь", "ласка"},
188
+ }
189
+
190
+ # distinctive diacritics per language (strong evidence, worth 2 points each)
191
+ DIACRITICS = {
192
+ "cs": set("ěščřžůďťň"),
193
+ "es": set("ñ¿¡"),
194
+ "pt": set("ãõ"),
195
+ "de": set("ß"),
196
+ "fr": set("çœàâæêëîïôùû"),
197
+ "it": set(["à", "è", "ò", "ù"]),
198
+ "uk": set("ґєії"),
199
+ "ru": set("ёъыэ"),
200
+ }
201
+
202
+ MIN_SCORE = 2 # below this we do not trust the guess -> global threshold
203
+ # --- Barist.4 must-catch pre-filter: highest-severity classes caught by
204
+ # deterministic patterns (independent of language threshold / category head).
205
+ # Accent-folded to ASCII before matching. Detection phrases only.
206
+ # Rules: CSAM = hard unsafe; self-harm = unsafe + needs_support routing;
207
+ # 1st-person / direct-threat forms only (news "zabití", "pomoct kamarádovi"
208
+ # etc. must NOT trigger). ---
209
+ def _rx(pat: str):
210
+ """Compile a pattern accent-folded + lowercased, so it matches _fold_ascii(text)."""
211
+ folded = "".join(c for c in unicodedata.normalize("NFKD", pat) if not unicodedata.combining(c)).lower()
212
+ return re.compile(folded, re.I)
213
+
214
+
215
+ _MC_CSAM = _rx(r"\b(csam|cp links?|child porn\w*|detsky material|detske intimni|nezletil\w*.{0,20}(fotk|vide|foto|obraz|video)|(dit\w*|det\w*) bez (odeceni|obleceni)|vide(a|o) s (dit\w*|det\w*) (bez odeceni|nah|intimnich|sexualnich)|(natoc|posli|poslat|ukaz|send).{0,30}(dit\w*|det\w*|kids|children) (v plavkach|in plavkach|nah[a|é]?|nude)|(dit\w*|det\w*) v plavkach.{0,20}(bez odeceni|nah|intim|sex)|intimni fotky nezletil|poslat intimni fotky|material de menores|fotos intimas de menores|video de ninos sin ropa|send me nudes of kids|photos of kids without clothes|kinderpornografie|porn\w* infantile|pornografia infantil|detsk\w* porn\w*|nah\w* fotk\w* (dit\w*|det\w*)|nah\w* fotky nezletil\w*|fotos de menores|underage nudes|cp download|pedofil\w*|telegram (skupina|group|kanal|channel|link) (o|pro|na)? ?(csam|cp|porn|detsk\w*|nah\w*|fotos|menores|pedofil))\b")
216
+ _MC_SELFHARM_1P = _rx(r"(chci se zabit|zabiju se|chci (se )?umrit|chci zem\w*|nechci (uz )?zit|chci si ubliz\w*|sebepo(s|š)kozuji|rezuju se|rezat se|podrezat( si)? zily|prasky na zabit|predavkovat se|rezu se(?! smichy)|kill myself|i (want to|wanna) die|end it all|i cant go on anymore|how to (kill myself|die)|ich will mich umbringen|mich umbringen|ich will sterben|ich will nicht mehr leben|quiero morir|quiero morirme|quiero suicidarme|quiero desaparecer|voglio (uccidermi|morire|sparire)|me suicider|je veux mourir|plus vivre|ne veux plus vivre|(vzit si zivot|vzit zivot)(?! do vlastnich rukou)|prestat zit(?!\s+(v|na|ve|do)\s)|nechci dal zit|jak (se )?(nejrychleji )?zemrit doma|chci (se )?zemrit|otravit se( prasky| tabletkami)?|(se )?porezat( se)?|jak se porezat|(se )?podrezat( se)?( zily)?|prasky (ktere|co) mi pomu(z|ž)ou zemrit|predavkov.{0,25}(mi pomu|chci|zemrit)|(chci|pomoz|porad).{0,25}predavkov|jak se.{0,25}predavkov|uz to nevydrzim.{0,30}(pomoz|skoncit|koncit|zemrit)|pomuz(te)? mi skoncit|chci zmizet navzdy)")
217
+ _MC_SELFHARM_INSTR = _rx(r"(jak se (?!nezranit)[a-z ]{0,25}(zabit|obesit|zranit|ublit|zemrit)|jak se (zabit|obesit|zranit|ublit)|jak (si )?sebepo|suicid\w*|sebevraz\w*|sebepo(s|š)kozeni|how to (kill|die)\b|end my life|skoncit\w* zivot)")
218
+ _MC_INSTR_MARK = _rx(r"\b(jak|how|navod na|way|method|zpusob|prosim|please|tell me|dime|sag mir|dites|instructions?|step by step)\b")
219
+ _MC_SUPPORT_CONTEXT = _rx(r"\b(pomoct|pomoc|kamarad|linka|bezpeci|help|support|suicide prevention|prevence|predejit|predchazet|zabranit|literatura|literature|hamlet|kniha|novela|film|divadlo|hra|game|story|sims|ve hre|pocitacova hra|vyhled|clanek|prispevek)\b")
220
+ _MC_NEWS_CONTEXT = _rx(r"\b(vysetruje|policie|news|zprav|clanek|report|obet|vrah|doslo k|explodovala|explosion|utok|atentat|vystava|muzeum|historie|svetova valka|exponat|dokument)\b")
221
+ _MC_THREAT = _rx(r"(zabiju (te|vas)|kill you|i\'ll kill you|ill kill you|je vais te tuer|te matare|voy a matarte|te voy a matar|quiero hacerte dano|voglio ucciderti|ich bringe dich um|ich tote dich|jemanden umbringen|bringe ich jemanden um)")
222
+ _MC_HATE_GROUP = _rx(r"(vystříl\w*|vystřel\w*|postříl\w*|postřel\w*|zabít všechny|zabit vsechny|vyhlad\w*|vyhub\w*|pověsit|povez\w*|zastřel\w*|umuč\w*|utýrat).{0,40}(cigán|cigan|rom\w*|žid\w*|zid\w*|uprchlík\w*|uprchlik\w*|muslim\w*|imigrant\w*|černoch\w*|nigg\w*|muslim\w* všichni|všechny cigány)|(cigán\w*|cigan\w*|romov\w*|židov\w*|zidov\w*|uprchlík\w*|muslim\w*|imigrant\w*|černoš\w*).{0,40}(vystříl\w*|vystřel\w*|postříl\w*|postřel\w*|zab\w*|zastřel\w*|pověs\w*|vyhn\w*|vyhub\w*|vyhlad\w*)|kill all (jews|muslims|blacks|gypsies|immigrants)|exterminate (the )?(jews|muslims|roma)|holocaust deni\w*|white power|heil hitler")
223
+ _MC_WEAPONS_DIY = _rx(r"(jak (vyrobit|postavit) (bombu|granat|trhavin|vybusninu)|navod na (vyrobu )?(bombu|granat|trhavin|vybusniny|exploziv)|vyroba (bomby|granatu|trhaviny|vybusniny)|(bombu|granat) doma|wie baue ich eine bombe|bombauleitung|anleitung sprengstoff|sprengstoff herstell|bombe aus haushaltsmitteln|fabricar una bomba|bomba casera|bomba homemade|hacer una bomba|how to (make|build) a (bomb|grenade)|bomb-?making|homemade bomb|grenade (instructions|tutorial)|fabriquer une bombe|comment faire une bombe|как сделать бомбу|как изготовить (бомбу|взрывчатку)|зробити бомбу|як зробити бомбу|vyroba napalmu|jak vyrobit napalm|napalm (recipe|homemade|instructions))")
224
+ _MC_DRUGS_DIY = _rx(r"(synteza pervitinu|jak uvarit meth|how to cook meth|meth-?synth|synthesis of (meth|nitroglycerin|fentanyl)|navod na vyrobu (fentanylu|pervitinu|drog)|recept na vyrobu drog|receta de metanfetamina|methanfetamin rezept|wie koche ich crystal meth|drogen kochen anleitung|synthesize meth|make meth at home)")
225
+ _MC_CYBER_DIY = _rx(r"(navod na phishing|phishingov\w* (e-?mail|stranka)|phishing (stranka|stranky|e-?mail|mail)|jak nabourat|hacknout (ucet|instagram|account)|how to hack|hack someone|keylogger tutorial|demonter un mot de passe|comment pirater|robar la cuenta|kradez hesla)")
226
+ _MC_DEFENSIVE = _rx(r"(branit|ochrana|protect|prevence|jak se chranit|defend|avoid phishing|recognize phishing|jak poznat phishing)")
227
+ _MC_JAILBREAK = _rx(r"(you are dan\b|dan mode|dan (mode|activated|unlocked|enabled)|do anything now|ignore (all (previous )?instructions|everything above|the rules)|pretend you are .{0,80}(no rules|unrestricted|without (any )?(filters?|restrictions|rules)|evil|jailbreak|grandma|bedtime|napalm|bomb|weapon|drug|meth)|act as \w+ (with|without) (no |without )(rules|restrictions|filters)|guidelines don\'?t exist|od ted( si)? bez omezeni|oublie les regles|there are no rules|předstírej .{0,80}(bez pravidel|bez omezeni|bomba|drogy|napalm|vybusnin))")
228
+
229
+
230
+ _MC_POISON = _rx(r"(jak (se )?otr\w*vit|čím otrávit|otrávit (souseda|někoho|psa|kočku)|jak vyrobit (jed|otravu)|recept na (jed|otravu)|jak namíchat jed|how to poison|envenenar (a )?(mi vecino|alguien|cualquiera)|como envenenar|quiero envenenar|empoisonner (quelqu|mon voisin)|jemanden vergiften|ich will jemanden vergiften|avvelenare (qualcuno|il vicino)|отравить (соседа|кого)|jak otrávit (souseda|někoho|psa|kočku|zvíře))")
231
+ _MC_SCHOOL = _rx(r"(sestřel\w*|postříl\w*|vystříl\w*|začnu střílet|střílet|zbraň|kulomet).{0,40}(škola|školu|ve škole|třída|do školy)|(přinesu|vezmu|půjdu|jdu|začnu).{0,40}(škola|školu|ve škole|do školy|třída).{0,40}(zbraň|střílet|kulomet|nůž)|(škola|školu|do školy|třída).{0,40}(začnu|přinesu|vezmu|půjdu).{0,30}(střílet|zbraň|nůž|kulomet)|střelba ve škole|školní masakr|school shooting|how to shoot up a school|matar (a todos )?en la escuela|tiroteo escolar|disparar en (la escuela|el colegio)|tuer tout le monde (à|a) l\'école|fusillade (scolaire|dans l\'école)|alle in der schule (töten|erschie|angreifen)|amoklauf (an der|in der) schule|uccidere tutti a scuola|strage (a|di) scuola|стріляти в школі|вбити всіх у школі")
232
+ _MC_MARKET = _rx(r"(kde|jak) (koupit|sehnat|seženu|objednat|koupím).{0,50}(pervitin|metamfetamin|kokain|heroin|drogy|zbraň|zbraně|explozivum|trhavin|bombu)|prodej (drog|zbraní)|nabízím (drogy|zbraně)|comprar (armas|drogas) baratas|buy (guns|drugs) (cheap|online)|как купить (оружие|наркотики)|(vařit|svařit|syntetizovat) fet( |$)|fet (na vaření|syntézu)")
233
+
234
+
235
+ _MC_LEGAL_WEAPON = _rx(r"(zbrojni prukaz|zbrojní průkaz|legalne|z legální|vzduchovk|airsoft|sportovni střelba|sportovní střelba|strilec na soutěžích)")
236
+ _MC_BOMB_BENIGN = _rx(r"(pyrotechnik|silvestr|ohňostroj|prskavk|dort|do koupele|do dortu|výbuch dortu)")
237
+ _MC_HELP_OTHER = _rx(r"\b(pomoct|pomoc|pomoz\w*|podpora|pece|help|support|prevence)\b(?!.*\b(mi|me)\s+(pomo\w*|help|support))")
238
+ _MC_POISON_3P = _rx(r"otr\w*vit (souseda|manzelku|manzela|nekdoho|psa|kocku|zvir(e|ě)|cloveka|kdokoli|obet|dite|deti)")
239
+ _MC_PEST = _rx(r"(krtk|myš|potkan|krys|hmyz|mravenc|plevel|slimák|škůdce|na zahradě|na zahrade)")
240
+
241
+ SUPPORT_LINK = "https://www.linkabezpeci.cz"
242
+
243
+ CAT_PRIORITY = ["sexual_abuse", "self_harm", "violence", "weapons_explosives", "drugs",
244
+ "cybercrime_fraud", "jailbreak_injection", "illegal_marketplace", "other_unsafe",
245
+ "hate_harassment"] # hate last: catch-all
246
+
247
+
248
+ def _cat_rank(c: str) -> int:
249
+ return CAT_PRIORITY.index(c) if c in CAT_PRIORITY else 99
250
+
251
+
252
+
253
+ def _fold_ascii(text: str) -> str:
254
+ return "".join(c for c in unicodedata.normalize("NFKD", text) if not unicodedata.combining(c))
255
+
256
+
257
+ def must_catch_hit(text: str) -> list[str]:
258
+ """Pattern pre-filter. Returns HARD-hit category names (force unsafe).
259
+
260
+ CSAM always blocks. Self-harm blocks on 1st-person or instruction forms,
261
+ unless it is a support context (routed to resources instead) or news.
262
+ Weapons/drugs/cyber/poison/market DIY patterns are instruction-form only
263
+ (news pass). Jailbreak patterns are NOT hard blocks -> see review_hit().
264
+ """
265
+ folded = _fold_ascii(text).lower()
266
+ hits = []
267
+ if _MC_CSAM.search(folded):
268
+ hits.append("sexual_abuse")
269
+ support = bool(_MC_SUPPORT_CONTEXT.search(folded))
270
+ news = bool(_MC_NEWS_CONTEXT.search(folded))
271
+ help_other = bool(_MC_HELP_OTHER.search(folded))
272
+ media = bool(_rx(r"\b(sims|ve hre|pocitacova hra|film|kniha|clanek|literatura|dokument|novela|divadlo|hra|game|story|hamlet|pojednava|roman)\b").search(folded))
273
+ if _MC_SELFHARM_1P.search(folded) and not media: # 1st person hits (caller routes to needs_support); media/game frames pass
274
+ hits.append("self_harm")
275
+ elif _MC_SELFHARM_INSTR.search(folded) and _MC_INSTR_MARK.search(folded) and not news and not help_other and not media:
276
+ hits.append("self_harm") # instruction form; "help someone else" frames stay safe
277
+ if _MC_THREAT.search(folded) and not news:
278
+ hits.append("violence")
279
+ if _MC_SCHOOL.search(folded) and not news:
280
+ hits.append("violence")
281
+ if _MC_HATE_GROUP.search(folded) and not news:
282
+ hits.append("hate_harassment")
283
+ legal = bool(_MC_LEGAL_WEAPON.search(folded))
284
+ bomb_benign = bool(_MC_BOMB_BENIGN.search(folded))
285
+ pest = bool(_MC_PEST.search(folded))
286
+ weapons_3p = bool(_rx(r"(pro|na|proti) (souseda|manzelku|nekdoho|skolu|tridu|verejnost|lidi|obet|clovek)\b").search(folded))
287
+ if _MC_WEAPONS_DIY.search(folded) and (weapons_3p or not news):
288
+ hits.append("weapons_explosives")
289
+ if _MC_DRUGS_DIY.search(folded) and not news:
290
+ hits.append("drugs")
291
+ if _MC_CYBER_DIY.search(folded) and not _MC_DEFENSIVE.search(folded) and not news:
292
+ hits.append("cybercrime_fraud")
293
+ market_hard = bool(_rx(r"(koupit|sehnat|sezenu|objednat|koupim).{0,50}(pervitin|metamfetamin|kokain|heroin|drogy|zbran|zbraně|explozivum|trhavin|bombu|fet)\b").search(folded))
294
+ if _MC_MARKET.search(folded) and (market_hard or not news):
295
+ hits.append("illegal_marketplace")
296
+ poison_3p = bool(_MC_POISON_3P.search(folded))
297
+ if _MC_POISON.search(folded) and not pest and (poison_3p or (not news and not support)):
298
+ hits.append("violence")
299
+ if legal and "illegal_marketplace" in hits:
300
+ hits.remove("illegal_marketplace")
301
+ if bomb_benign and "illegal_marketplace" in hits:
302
+ hits.remove("illegal_marketplace")
303
+ return sorted(set(hits))
304
+
305
+
306
+ def review_hit(text: str) -> list[str]:
307
+ """Jailbreak / prompt-injection / legally-ambiguous patterns -> needs_review, NOT a hard block."""
308
+ folded = _fold_ascii(text).lower()
309
+ out = []
310
+ if _MC_JAILBREAK.search(folded):
311
+ out.append("jailbreak_injection")
312
+ if _MC_LEGAL_WEAPON.search(folded) and (_MC_MARKET.search(folded) or _MC_WEAPONS_DIY.search(folded)):
313
+ out.append("weapons_explosives")
314
+ if _MC_BOMB_BENIGN.search(folded) and _MC_MARKET.search(folded):
315
+ out.append("illegal_marketplace")
316
+ return sorted(set(out))
317
+
318
+
319
+ def is_support_context(text: str) -> bool:
320
+ """Self-harm mention in a helping context -> route to support, not a block."""
321
+ folded = _fold_ascii(text).lower()
322
+ return bool((_MC_SELFHARM_1P.search(folded) or _MC_SELFHARM_INSTR.search(folded))
323
+ and _MC_HELP_OTHER.search(folded))
324
+
325
+
326
+ def _script(text: str) -> str:
327
+ counts = {}
328
+ for ch in text:
329
+ if ch.isalpha():
330
+ name = unicodedata.name(ch, "")
331
+ for key in ("CYRILLIC", "HAN", "HIRAGANA", "KATAKANA", "HANGUL", "ARABIC", "DEVANAGARI", "HEBREW", "LATIN"):
332
+ if key in name:
333
+ counts[key] = counts.get(key, 0) + 1
334
+ break
335
+ if not counts:
336
+ return "latin"
337
+ top = max(counts, key=counts.get)
338
+ if top == "LATIN":
339
+ return "latin"
340
+ return {"CYRILLIC": "cyrillic", "HAN": "han", "HIRAGANA": "kana", "KATAKANA": "kana",
341
+ "HANGUL": "hangul", "ARABIC": "arabic", "DEVANAGARI": "devanagari", "HEBREW": "hebrew"}[top]
342
+
343
+
344
+ def _script(text: str) -> str:
345
+ counts = {}
346
+ for ch in text:
347
+ if ch.isalpha():
348
+ name = unicodedata.name(ch, "")
349
+ for key in ("CYRILLIC", "HAN", "HIRAGANA", "KATAKANA", "HANGUL", "ARABIC", "DEVANAGARI", "HEBREW", "LATIN"):
350
+ if key in name:
351
+ counts[key] = counts.get(key, 0) + 1
352
+ break
353
+ if not counts:
354
+ return "latin"
355
+ top = max(counts, key=counts.get)
356
+ if top == "LATIN":
357
+ return "latin"
358
+ return {"CYRILLIC": "cyrillic", "HAN": "han", "HIRAGANA": "kana", "KATAKANA": "kana",
359
+ "HANGUL": "hangul", "ARABIC": "arabic", "DEVANAGARI": "devanagari", "HEBREW": "hebrew"}[top]
360
+
361
+
362
+ def detect_lang(text: str) -> str:
363
+ """Heuristic ISO-639-1 guess; returns 'default' (global threshold) when unsure."""
364
+ s = _script(text)
365
+ if s == "cyrillic":
366
+ uk_score = sum(1 for w in re.findall(r"[a-zа-яіїєґ']+", text.lower()) if w in STOPWORDS["uk"])
367
+ uk_score += 2 * sum(text.count(c) for c in "ґєії")
368
+ ru_score = sum(1 for w in re.findall(r"[a-zа-яёъыэ']+", text.lower()) if w in STOPWORDS["ru"])
369
+ return "uk" if uk_score >= MIN_SCORE and uk_score > ru_score else ("ru" if ru_score >= MIN_SCORE else "default")
370
+ if s == "han":
371
+ return "zh"
372
+ if s == "kana":
373
+ return "ja"
374
+ if s == "hangul":
375
+ return "ko"
376
+ if s == "arabic":
377
+ return "ar"
378
+ if s == "devanagari":
379
+ return "hi"
380
+ if s == "hebrew":
381
+ return "he"
382
+ words = re.findall(r"[a-zà-ÿěščřžďťňáéíúůóßäöüñãõ]+", text.lower())
383
+ if not words:
384
+ return "default"
385
+ scores = {}
386
+ for lang, stops in STOPWORDS.items():
387
+ if lang in ("ru", "uk"):
388
+ continue
389
+ n = sum(1 for w in words if w in stops)
390
+ n += 2 * sum(text.lower().count(c) for c in DIACRITICS.get(lang, ""))
391
+ scores[lang] = n
392
+ best = max(scores, key=scores.get)
393
+ return best if scores[best] >= MIN_SCORE else "default"
394
+
395
+
396
+
397
+ def load_state_dict_any(directory) -> dict:
398
+ """Load model weights from model_int8.safetensors (dequantized) if present,
399
+ else model_fp16.safetensors, else model.safetensors."""
400
+ from pathlib import Path as _P
401
+ from safetensors.torch import load_file
402
+
403
+ d = _P(directory)
404
+ if (d / "model_int8.safetensors").exists():
405
+ st = load_file(str(d / "model_int8.safetensors"))
406
+ out = {}
407
+ for k, v in st.items():
408
+ if k.endswith(".q"):
409
+ base = k[:-2]
410
+ out[base] = (v.float() * st[base + ".scale"]).to(torch.float32)
411
+ elif not k.endswith(".scale"):
412
+ out[k] = v
413
+ return out
414
+ if (d / "model_fp16.safetensors").exists():
415
+ return {k: v.float() for k, v in load_file(str(d / "model_fp16.safetensors")).items()}
416
+ return load_file(str(d / "model.safetensors"))
417
+
418
+
419
+ def remap_hf_state(state: dict) -> dict:
420
+ """Standard Llama-layout safetensors -> training-layout state dict."""
421
+ import re
422
+
423
+ out = {"embed.weight": state["model.embed_tokens.weight"]}
424
+ layer_ids = sorted({int(m.group(1)) for k in state if (m := re.match(r"model\.layers\.(\d+)\.", k))})
425
+ for i in layer_ids:
426
+ p, b = f"model.layers.{i}.", f"blocks.{i}."
427
+ qkv = torch.cat(
428
+ [state[p + "self_attn.q_proj.weight"], state[p + "self_attn.k_proj.weight"], state[p + "self_attn.v_proj.weight"]],
429
+ dim=0,
430
+ )
431
+ out[b + "attn.qkv.weight"] = qkv.contiguous()
432
+ out[b + "attn.proj.weight"] = state[p + "self_attn.o_proj.weight"]
433
+ out[b + "mlp.gate.weight"] = state[p + "mlp.gate_proj.weight"]
434
+ out[b + "mlp.up.weight"] = state[p + "mlp.up_proj.weight"]
435
+ out[b + "mlp.down.weight"] = state[p + "mlp.down_proj.weight"]
436
+ out[b + "norm1.weight"] = state[p + "input_layernorm.weight"]
437
+ out[b + "norm2.weight"] = state[p + "post_attention_layernorm.weight"]
438
+ out["norm.weight"] = state["model.norm.weight"]
439
+ if "score.weight" in state:
440
+ out["score.weight"] = state["score.weight"]
441
+ return out
442
+
443
+
444
+ def internal_cfg(cfg: dict) -> dict:
445
+ """config.json (Llama layout) -> the dict the Censer module expects."""
446
+ return {
447
+ "vocab_size": cfg["vocab_size"],
448
+ "n_layers": cfg["num_hidden_layers"],
449
+ "n_heads": cfg["num_attention_heads"],
450
+ "d_model": cfg["hidden_size"],
451
+ "d_ff": cfg["intermediate_size"],
452
+ "max_seq_len": cfg["max_position_embeddings"],
453
+ "dropout": cfg.get("dropout", 0.0),
454
+ "tie_embeddings": True,
455
+ "pad_id": cfg.get("pad_token_id", 0),
456
+ "answer_ids": cfg["answer_ids"],
457
+ "max_chars": cfg.get("max_chars", 4000),
458
+ "rope_scaling": cfg.get("rope_scaling") or (cfg.get("rope_parameters") if cfg.get("rope_parameters", {}).get("rope_type") == "linear" else None),
459
+ "categories": [cfg["id2label"][str(i)] for i in range(2, cfg.get("num_labels", 2))] if cfg.get("num_labels", 2) > 2 else [],
460
+ "cat_thresholds": cfg.get("cat_thresholds") or {},
461
+ "temperature": float(cfg.get("temperature", 1.0) or 1.0),
462
+ "lang_thresholds": cfg.get("lang_thresholds") or {},
463
+ }
464
+
465
+
466
+
467
+ class CenserPipeline:
468
+ """Text in, "1" (unsafe) or "0" (safe) out."""
469
+
470
+ def __init__(self, directory: str | Path = ".", threshold: float = 0.5, device: str | None = None, prefer_quant: bool = False) -> None:
471
+ directory = Path(directory)
472
+ self.cfg = internal_cfg(json.loads((directory / "config.json").read_text(encoding="utf-8")))
473
+ self.tok = Tokenizer.from_file(str(directory / "tokenizer.json"))
474
+ self.pad_id = self.tok.token_to_id(PAD)
475
+ self.bos_id = self.tok.token_to_id(BOS)
476
+ self.user_id = self.tok.token_to_id(USER)
477
+ self.assistant_id = self.tok.token_to_id(ASSISTANT)
478
+ self.threshold = threshold
479
+ self.cat_thresholds = dict(self.cfg.get("cat_thresholds") or {})
480
+ self.temperature = float(self.cfg.get("temperature", 1.0) or 1.0)
481
+ self.cat_experimental = set(self.cfg.get("cat_experimental") or [])
482
+ self.lang_thresholds = dict(self.cfg.get("lang_thresholds") or {})
483
+ self.device = torch.device(device or ("cuda" if torch.cuda.is_available() else "cpu"))
484
+ self.model = Censer(self.cfg)
485
+ if prefer_quant and (directory / "model_int8.safetensors").exists():
486
+ state = remap_hf_state(load_state_dict_any(directory))
487
+ else:
488
+ from safetensors.torch import load_file
489
+ state = remap_hf_state(load_file(directory / "model.safetensors"))
490
+ missing, unexpected = self.model.load_state_dict(state, strict=False)
491
+ missing = [k for k in missing if k != "lm_head.weight"] # tied to embed.weight
492
+ if missing or unexpected:
493
+ raise RuntimeError(f"weight mismatch: missing={missing} unexpected={unexpected}")
494
+ self.model.to(self.device).eval()
495
+ self.max_chars = self.cfg["max_chars"]
496
+
497
+ def _encode(self, text: str) -> list[int]:
498
+ body = self.tok.encode(text[: self.max_chars], add_special_tokens=False).ids
499
+ ids = [self.bos_id, self.user_id, *body, self.assistant_id]
500
+ return ids[: self.cfg["max_seq_len"]]
501
+
502
+ def predict(self, text: str) -> dict:
503
+ return self.predict_batch([text])[0]
504
+
505
+ def _encode_full(self, text: str) -> list[int]:
506
+ body = self.tok.encode(text[: self.max_chars * 8], add_special_tokens=False).ids
507
+ return [self.bos_id, self.user_id, *body, self.assistant_id]
508
+
509
+ def predict_batch(self, texts: list[str]) -> list[dict]:
510
+ # long inputs: split into overlapping token chunks, aggregate by max
511
+ out: list[dict] = []
512
+ chunk_tokens, stride = 256, 192
513
+ for text in texts:
514
+ thr = self.threshold_for(text)
515
+ ids = self._encode_full(text)
516
+ body = ids[2:-1]
517
+ if len(body) <= chunk_tokens:
518
+ out.append(self._classify_ids([ids[: self.cfg["max_seq_len"]]], [thr], [text])[0])
519
+ continue
520
+ chunks = []
521
+ start = 0
522
+ while start < len(body):
523
+ chunks.append(body[start:start + chunk_tokens])
524
+ if start + chunk_tokens >= len(body):
525
+ break
526
+ start += stride
527
+ rows = []
528
+ for piece in chunks:
529
+ cids = [self.bos_id, self.user_id, *piece, self.assistant_id]
530
+ rows.append(self._classify_ids([cids[: self.cfg["max_seq_len"]]], [thr], [text])[0])
531
+ best = max(rows, key=lambda r: r["score"])
532
+ # aggregate categories by max prob
533
+ n_cat = len(rows[0].get("category_scores", []))
534
+ cat_scores = [max(r["category_scores"][i] for r in rows) for i in range(n_cat)] if n_cat else []
535
+ best["category_scores"] = [round(c, 4) for c in cat_scores]
536
+ cats = self.categories()
537
+ if best["unsafe"] and cats:
538
+ passed = {c for c, p in zip(cats, cat_scores)
539
+ if p >= float(self.cat_thresholds.get(c, self.threshold)) and c not in self.cat_experimental}
540
+ passed |= set(best.get("categories") or []) # keep pre-filter hits from the best chunk
541
+ # hard block (item 1): sexual_abuse / self_harm >= 0.3 forces unsafe for cs/sk/default
542
+ if not best.get("needs_support"):
543
+ for c in ("sexual_abuse", "self_harm"):
544
+ if c in cats and cat_scores[cats.index(c)] >= 0.3:
545
+ passed.add(c)
546
+ best["unsafe"] = True
547
+ best["label"] = "unsafe"
548
+ best["label_id"] = "1"
549
+ best["override"] = best.get("override") or "category_block"
550
+ passed = sorted(passed, key=lambda c: (_cat_rank(c) if c in CAT_PRIORITY else 99,
551
+ -(cat_scores[cats.index(c)] if c in cats else 1.0)))[:2]
552
+ if not passed: # unsafe must always carry >=1 category: top-1 by score
553
+ passed = [max(cats, key=lambda c: cat_scores[cats.index(c)])]
554
+ best["categories"] = passed
555
+ else:
556
+ best["categories"] = []
557
+ best["chunks"] = len(chunks)
558
+ out.append(best)
559
+ return out
560
+
561
+ def threshold_for(self, text: str) -> float:
562
+ lang = detect_lang(text)
563
+ return float(self.lang_thresholds.get(lang) or self.lang_thresholds.get("default") or self.threshold)
564
+
565
+ def categories(self) -> list[str]:
566
+ return self.cfg.get("categories") or []
567
+
568
+ def _classify_ids(self, seqs: list[list[int]], thresholds: list[float] | None = None, texts: list[str] | None = None) -> list[dict]:
569
+ width = max(len(s) for s in seqs)
570
+ ids = torch.full((len(seqs), width), self.pad_id, dtype=torch.long)
571
+ for i, seq in enumerate(seqs):
572
+ ids[i, : len(seq)] = torch.tensor(seq)
573
+ probs = self.model.classify_probs(ids.to(self.device), temperature=self.temperature).float().cpu().tolist()
574
+ if thresholds is None:
575
+ thresholds = [self.threshold] * len(seqs)
576
+ res = []
577
+ for i, row in enumerate(probs):
578
+ p = row[1]
579
+ cats = self.categories()
580
+ cat_scores = row[2:] if cats else []
581
+ is_unsafe = p >= thresholds[i]
582
+ # Barist.4: must-catch pre-filter — CSAM / self-harm pattern hits force
583
+ # unsafe regardless of the (language) binary threshold
584
+ hit_override = must_catch_hit(texts[i]) if texts else []
585
+ review = review_hit(texts[i]) if texts else []
586
+ help_ctx = is_support_context(texts[i]) if texts else False
587
+ support_ctx = help_ctx
588
+ if cats and "self_harm" in cats and cat_scores[cats.index("self_harm")] >= 0.2:
589
+ support_ctx = True # Barist.4: self_harm >= 0.2 -> support resources, any language
590
+ # hard block: sexual_abuse / self_harm >= 0.3 forces unsafe in ANY language
591
+ cat_block = []
592
+ if cats and not help_ctx: # help-other frames never hard-blocked
593
+ for c in ("sexual_abuse", "self_harm"):
594
+ if c in cats and cat_scores[cats.index(c)] >= 0.3:
595
+ cat_block.append(c)
596
+ if cat_block:
597
+ is_unsafe = True
598
+ if hit_override:
599
+ is_unsafe = True
600
+ if help_ctx and not hit_override and not cat_block:
601
+ is_unsafe = False # help-seeking context is never blocked
602
+ # Barist.4: at most the top-2 categories by score (no nonsense lists)
603
+ _passed = [c for c, sc in zip(cats, cat_scores)
604
+ if sc >= float(self.cat_thresholds.get(c, self.threshold))
605
+ and c not in self.cat_experimental]
606
+ _passed = sorted(set(_passed) | set(hit_override) | set(cat_block),
607
+ key=lambda c: (_cat_rank(c) if c in CAT_PRIORITY else 99,
608
+ -(cat_scores[cats.index(c)] if c in cats else 1.0)))[:2]
609
+ if is_unsafe and not _passed:
610
+ # unsafe must always carry >=1 category: top-1 by score
611
+ _passed = [max(cats, key=lambda c: cat_scores[cats.index(c)])] if cats else []
612
+ res.append({
613
+ "label": "unsafe" if is_unsafe else "safe",
614
+ "label_id": "1" if is_unsafe else "0",
615
+ "unsafe": is_unsafe,
616
+ "override": "must_catch" if hit_override else ("category_block" if cat_block else None),
617
+ "needs_support": SUPPORT_LINK if (support_ctx or "self_harm" in hit_override) else None,
618
+ "needs_review": True if review else None,
619
+ "score": round(p, 4),
620
+ "category_scores": [round(c, 4) for c in cat_scores],
621
+ # categories are only reported when the binary verdict is unsafe
622
+ "categories": _passed if is_unsafe else [],
623
+ })
624
+ return res
625
+
626
+
627
+
628
+
629
+ def _color(text: str, code: str) -> str:
630
+ if not sys.stdout.isatty():
631
+ return text
632
+ return f"\033[{code}m{text}\033[0m"
633
+
634
+
635
+ def repl(pipe: "CenserPipeline") -> int:
636
+ """Small interactive terminal UI: phrase in, safe/unsafe + score out."""
637
+ print("Barist.4 - interactive terminal UI")
638
+ print("Type a phrase, Enter to classify. 'exit' to quit.\n")
639
+ while True:
640
+ try:
641
+ line = input("> ")
642
+ except (EOFError, KeyboardInterrupt):
643
+ print()
644
+ return 0
645
+ text = line.strip()
646
+ if text.lower() in {"exit", "quit", ":q"}:
647
+ return 0
648
+ if not text:
649
+ continue
650
+ v = pipe.predict(text)
651
+ label = "unsafe" if v["unsafe"] else "safe"
652
+ colored = _color(f"{label:6s}", "31" if v["unsafe"] else "32")
653
+ print(f"{colored} {v['score']:.4f} | {text}")
654
+
655
+
656
+ def main(argv: list[str] | None = None) -> None:
657
+ parser = argparse.ArgumentParser(
658
+ prog="barist.4",
659
+ description="Barist.4 safety classifier (standalone). Run without text to open the interactive UI.",
660
+ )
661
+ parser.add_argument("text", nargs="*", help="phrase to classify once; omit to open the interactive UI")
662
+ parser.add_argument("--model-dir", default=".")
663
+ parser.add_argument("--threshold", type=float, default=0.5)
664
+ parser.add_argument("--json", action="store_true")
665
+ parser.add_argument("--prefer-quant", action="store_true",
666
+ help="load model_int8.safetensors (0.9 MB) instead of fp32 if present")
667
+ parser.add_argument("--repl", action="store_true", help="interactive UI (default when no text is given)")
668
+ args = parser.parse_args(argv)
669
+
670
+ pipe = CenserPipeline(args.model_dir, args.threshold, prefer_quant=args.prefer_quant)
671
+ text = " ".join(args.text).strip()
672
+ if args.repl or not text:
673
+ raise SystemExit(repl(pipe))
674
+ verdict = pipe.predict(text)
675
+ print(json.dumps(verdict) if args.json else verdict["label"]) # "safe" / "unsafe"
676
+
677
+
678
+ if __name__ == "__main__":
679
+ main()
config.json ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "LlamaForSequenceClassification"
4
+ ],
5
+ "model_type": "llama",
6
+ "num_labels": 12,
7
+ "id2label": {
8
+ "0": "safe",
9
+ "1": "unsafe",
10
+ "2": "violence",
11
+ "3": "weapons_explosives",
12
+ "4": "drugs",
13
+ "5": "hate_harassment",
14
+ "6": "sexual_abuse",
15
+ "7": "self_harm",
16
+ "8": "cybercrime_fraud",
17
+ "9": "jailbreak_injection",
18
+ "10": "illegal_marketplace",
19
+ "11": "other_unsafe"
20
+ },
21
+ "label2id": {
22
+ "safe": 0,
23
+ "unsafe": 1,
24
+ "violence": 2,
25
+ "weapons_explosives": 3,
26
+ "drugs": 4,
27
+ "hate_harassment": 5,
28
+ "sexual_abuse": 6,
29
+ "self_harm": 7,
30
+ "cybercrime_fraud": 8,
31
+ "jailbreak_injection": 9,
32
+ "illegal_marketplace": 10,
33
+ "other_unsafe": 11
34
+ },
35
+ "problem_type": "multi_label_classification",
36
+ "vocab_size": 2048,
37
+ "hidden_size": 128,
38
+ "intermediate_size": 384,
39
+ "num_hidden_layers": 3,
40
+ "num_attention_heads": 4,
41
+ "num_key_value_heads": 4,
42
+ "head_dim": 32,
43
+ "max_position_embeddings": 4096,
44
+ "rms_norm_eps": 1e-05,
45
+ "rope_parameters": {
46
+ "rope_type": "linear",
47
+ "factor": 8.0,
48
+ "rope_theta": 10000.0
49
+ },
50
+ "attention_bias": false,
51
+ "mlp_bias": false,
52
+ "tie_word_embeddings": true,
53
+ "pad_token_id": 0,
54
+ "bos_token_id": 1,
55
+ "eos_token_id": 3,
56
+ "name": "Barist.4",
57
+ "max_chars": 4000,
58
+ "answer_ids": [
59
+ 19,
60
+ 20,
61
+ 68,
62
+ 69,
63
+ 70,
64
+ 71,
65
+ 72,
66
+ 73,
67
+ 74,
68
+ 75,
69
+ 76,
70
+ 77
71
+ ],
72
+ "special_tokens": [
73
+ "[PAD]",
74
+ "[BOS]",
75
+ "[USER]",
76
+ "[ASSISTANT]"
77
+ ],
78
+ "threshold": 0.5,
79
+ "cat_thresholds": {
80
+ "violence": 0.5,
81
+ "weapons_explosives": 0.5,
82
+ "drugs": 0.5,
83
+ "hate_harassment": 0.4,
84
+ "sexual_abuse": 0.3,
85
+ "self_harm": 0.3,
86
+ "cybercrime_fraud": 0.5,
87
+ "jailbreak_injection": 0.5,
88
+ "illegal_marketplace": 0.5,
89
+ "other_unsafe": 0.45
90
+ },
91
+ "temperature": 0.6464266479015075,
92
+ "must_catch_override": {
93
+ "self_harm": 0.3,
94
+ "sexual_abuse": 0.3
95
+ },
96
+ "cat_experimental": [
97
+ "violence",
98
+ "weapons_explosives",
99
+ "drugs",
100
+ "sexual_abuse",
101
+ "self_harm",
102
+ "cybercrime_fraud",
103
+ "jailbreak_injection",
104
+ "illegal_marketplace"
105
+ ],
106
+ "lang_thresholds": {
107
+ "default": 0.5,
108
+ "de": 0.5,
109
+ "en": 0.5,
110
+ "es": 0.5,
111
+ "fr": 0.74,
112
+ "it": 0.5,
113
+ "ru": 0.69,
114
+ "uk": 0.7,
115
+ "cs": 0.5
116
+ }
117
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f249827392ca28b083ca17a4980b3109fff441312b23b5f4fd2ed707f04f2ba9
3
+ size 3617384
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "tokenizer_class": "PreTrainedTokenizerFast",
3
+ "pad_token": "[PAD]",
4
+ "bos_token": "[BOS]",
5
+ "eos_token": "[ASSISTANT]",
6
+ "model_max_length": 4096
7
+ }