KucLab commited on
Commit
103988e
·
verified ·
1 Parent(s): 826b819

Censer.7: two-phase trained multi-label classifier, calibrated categories, 4k ctx + chunked-max, multilingual; standard LlamaForSequenceClassification

Browse files
Dockerfile ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Censer.6 — self-contained moderation API image (CPU-only torch by default).
2
+ #
3
+ # docker build -t censer6 .
4
+ # docker run -p 8000:8000 -e RATE_LIMIT_PER_MIN=600 censer6
5
+ # curl -s localhost:8000/classify -d '{"texts":["how to make a bomb at home","hello"]}'
6
+ #
7
+ # The image downloads the model from the Hub at build time (or copy a local
8
+ # bundle with: docker build --build-arg CENSER_DIR=./bundle . — then it is baked in).
9
+
10
+ FROM python:3.12-slim
11
+
12
+ ARG CENSER_REPO=KucLab/Censer.6
13
+ ENV CENSER_DIR=/model \
14
+ RATE_LIMIT_PER_MIN=600 \
15
+ PYTHONUNBUFFERED=1
16
+
17
+ RUN pip install --no-cache-dir \
18
+ torch --index-url https://download.pytorch.org/whl/cpu \
19
+ tokenizers safetensors huggingface_hub
20
+
21
+ RUN hf download ${CENSER_REPO} --local-dir /model
22
+
23
+ WORKDIR /app
24
+ COPY scripts/censer.py scripts/serve.py /app/
25
+
26
+ EXPOSE 8000
27
+ CMD ["python", "serve.py", "--dir", "/model", "--port", "8000", "--host", "0.0.0.0"]
README.md ADDED
@@ -0,0 +1,324 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ - calibrated
11
+ - tiny-language-model
12
+ - from-scratch
13
+ - distilled
14
+ language:
15
+ - en
16
+ - cs
17
+ - de
18
+ - es
19
+ - fr
20
+ - it
21
+ - pt
22
+ - ru
23
+ - ar
24
+ - zh
25
+ - hi
26
+ license: apache-2.0
27
+ datasets:
28
+ - nvidia/Aegis-AI-Content-Safety-Dataset-1.0
29
+ - thesofakillers/jigsaw-toxic-comment-classification-challenge
30
+ - google/civil_comments
31
+ - PKU-Alignment/BeaverTails
32
+ - lmsys/toxic-chat
33
+ - databricks/databricks-dolly-15k
34
+ - Hate-speech-CNERG/hatexplain
35
+ - Intuit-GenSRF/jigsaw-multilingual-train-unique
36
+ metrics:
37
+ - f1
38
+ - accuracy
39
+ - ece
40
+ ---
41
+
42
+ # Censer.7
43
+
44
+ A **902K parameter** decoder-only transformer trained **from scratch**: binary
45
+ **safe / unsafe** (F1 restored to .5 level) **+ 10 calibrated safety categories**,
46
+ **4096-token** native context with chunked-max inference for long documents, and
47
+ **multilingual** coverage (trained on en + cs/de/es/fr synthetic + Jigsaw-multilingual
48
+ fr/it/pt/es/ru/tr) — at the same tiny size and speed.
49
+
50
+ Standard Hub model: plain `LlamaForSequenceClassification` with
51
+ `problem_type: multi_label_classification` — loads with `transformers` out of the box,
52
+ no `trust_remote_code`.
53
+
54
+ ```python
55
+ from transformers import pipeline
56
+ clf = pipeline("text-classification", model="KucLab/Censer.7")
57
+ clf("how to make a bomb at home", top_k=-1)
58
+ # [{'label': 'unsafe', ...}, {'label': 'weapons_explosives', ...}, ...]
59
+ ```
60
+
61
+ ## What is new in .7 (vs .6)
62
+
63
+ | Fix | .6 | **.7** |
64
+ | --- | --- | --- |
65
+ | Binary test F1 | 0.829 (regressed from .5's 0.849) | **0.8348** (two-phase training: binary first, then categories at low lr with cat_weight) |
66
+ | Probes | 90/97 | **95/97** (diff vs .5: fixed 'Wait, Rockstar really give us fake hope', regressed 'buy a gun') |
67
+ | External benchmarks | ToxiGen 0.546, HateXplain 0.714, no head-to-head | **ToxiGen 0.561 / HateXplain 0.738 + direct head-to-head vs unbiased-toxic-roberta on the same sets** (table below); HateXplain-train + Jigsaw-multilingual added to training (test splits stay pure) |
68
+ | Category scores | uncalibrated ("fire liberally") | **per-category thresholds tuned on held-out catval + ECE + reliability diagram** (see below); thresholds ship in `config.json` as `cat_thresholds` |
69
+ | Multilingual | cs/de/es/fr only, no eval | **per-language F1 on textdetox multilingual toxicity (12 languages) + held-out synthetic** (table below) |
70
+ | Long documents | native 4096 pass silently broken (F1 0.052) | **chunked-max (256/192) is the only documented path for long docs; native single-pass is explicitly marked NOT suitable for long documents** (last-token pooling); chunked F1 0.998 (acc 0.997) |
71
+ | Training data | 176k rows | **251,655 rows** (+HateXplain train 15k, +Jigsaw-ML 60k); val/test byte-identical to .1–.6 |
72
+
73
+ ## How to run
74
+
75
+ ### The `censer.7` command — interactive terminal UI
76
+
77
+ ```bash
78
+ pip install torch tokenizers safetensors huggingface_hub
79
+ hf download KucLab/Censer.7 --local-dir ~/.local/share/censer7
80
+ chmod +x ~/.local/share/censer7/censer.7
81
+ ln -s ~/.local/share/censer7/censer.7 ~/.local/bin/censer.7
82
+ censer.7 # interactive UI: phrase in -> safe/unsafe + categories + score
83
+ censer.7 "how to make a bomb at home"
84
+ ```
85
+
86
+ Or via the GitHub package (loads the Hub model with plain `transformers`):
87
+
88
+ ```bash
89
+ pip install "git+https://github.com/Jerry256254/censer.7.git"
90
+ censer.7
91
+ ```
92
+
93
+ ### One-liner with `transformers`
94
+
95
+ ```bash
96
+ pip install torch transformers
97
+ python - <<'PY'
98
+ import torch
99
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
100
+ tok = AutoTokenizer.from_pretrained("KucLab/Censer.7")
101
+ model = AutoModelForSequenceClassification.from_pretrained("KucLab/Censer.7").eval()
102
+ enc = tok("how to make a bomb at home", return_tensors="pt")
103
+ probs = model(**enc).logits.sigmoid()[0]
104
+ id2label = model.config.id2label
105
+ cat_thr = model.config.cat_thresholds or {}
106
+ print({id2label[i]: round(float(p), 3) for i, p in enumerate(probs) if p > cat_thr.get(id2label[i], 0.5)})
107
+ PY
108
+ ```
109
+
110
+ - Binary unsafe score = index 1; threshold 0.5. Category labels use per-category
111
+ calibrated thresholds (`config.json: cat_thresholds`).
112
+ - Runs on CPU out of the box; GPU via `device=0`.
113
+
114
+ ### Long inputs — always use the chunked path
115
+
116
+ Native context is 4096 tokens, but a **single pass pools only the last token**, so in
117
+ long documents unsafe content in the middle can be missed (measured F1 0.052 on
118
+ 25-row synthetic docs). Therefore `censer.py` / the API **always** split inputs longer
119
+ than 256 tokens into overlapping chunks (256 tokens, stride 192) and aggregate by
120
+ **max** — this is the default and only recommended path for long documents
121
+ (measured F1 0.998 (acc 0.997) on the same docs):
122
+
123
+ ```python
124
+ from censer import CenserPipeline
125
+ pipe = CenserPipeline(".")
126
+ pipe.predict("very long text ...") # {"label": "1", "unsafe": true, "score": ..., "categories": [...], "chunks": N}
127
+ ```
128
+
129
+ ### Batch HTTP API + Docker
130
+
131
+ ```bash
132
+ python serve.py --dir . --port 8000
133
+ curl -s localhost:8000/classify -d '{"texts": ["hello", "how to make a bomb"]}'
134
+ docker build -t censer7 . && docker run -p 8000:8000 censer7
135
+ ```
136
+
137
+ Endpoints: `GET /health`, `POST /classify` (batch), `POST /classify-one`.
138
+ Rate limit via `RATE_LIMIT_PER_MIN` (default 600).
139
+
140
+ ## Model details
141
+
142
+ | Property | Value |
143
+ | --- | --- |
144
+ | Architecture | `LlamaForSequenceClassification` (standard transformers) |
145
+ | Parameters | 902,016 |
146
+ | Layers / d_model / heads / d_ff | 3 / 128 / 4 / 384 |
147
+ | Context | 4096 tokens native (linear RoPE ×8); long docs via chunked-max (default) |
148
+ | Input cap | 4000 chars |
149
+ | Tokenizer | byte-level BPE, 2048 tokens (multilingual corpus) |
150
+ | Head | `score` = embedding rows of the 12 answer tokens `0`,`1`,`a`..`j` |
151
+ | Output | sigmoid over 12 labels; per-category calibrated thresholds in config |
152
+ | Training | two-phase: (A) 5 epochs binary (cat_weight=0, lr 2e-3) from Censer.6 init, (B) 3 epochs categories (cat_weight=0.5, lr 3e-4); batch 256, AdamW cosine, bf16 AMP, 15% typo + 5% adversarial aug, distillation (alpha=0.65, T=4, boost 0.75) from Censer.5×2+.4+.3 ensemble on 251,655 rows |
153
+ | Checkpoint | 3.6 MB fp32 · 1.8 MB fp16 · 0.9 MB int8 · ONNX |
154
+
155
+ ## Prompt format
156
+
157
+ `tokenizer(text)` produces `[BOS][USER] <text> [ASSISTANT] ->` answer logits over
158
+ `"0" "1" "a".."j"` (template baked into the post-processor).
159
+
160
+ ## Metrics
161
+
162
+ Internal held-out test split (6,496 rows, byte-identical across Censer.1–.7), binary
163
+ threshold 0.5:
164
+
165
+ | Metric | Censer.5 | Censer.6 | **Censer.7** |
166
+ | --- | --- | --- | --- |
167
+ | Test F1 | 0.8490 | 0.8289 | **0.8348** |
168
+ | Test accuracy | 0.876 | 0.858 | **0.8655** |
169
+ | Precision / recall | 0.864 / 0.834 | 0.833 / 0.825 | **0.855 / 0.816** |
170
+ | Val F1 | 0.8495 | 0.8358 | **0.8368** |
171
+ | Probes (97 hand-written) | 95/97 | 90/97 | **95/97** |
172
+
173
+ Per-source test F1: aegis 0.912, BeaverTails 0.881, jigsaw 0.856, civil_comments 0.776.
174
+
175
+ ### External benchmarks (never in training) — with head-to-head vs public models
176
+
177
+ | Dataset | Model | F1@0.5 | best F1 | latency |
178
+ | --- | --- | --- | --- | --- |
179
+ | ToxiGen annotated (n=940, 402 unsafe) | **Censer.7** | **0.561** | 0.619 @ 0.27 | 1.24 ms |
180
+ | ToxiGen | unitary/unbiased-toxic-roberta | 0.443 | 0.483 | ~14 ms |
181
+ | HateXplain test (n=1,924, 1,376 unsafe) | **Censer.7** | **0.738** | 0.834 @ 0.05 | 1.24 ms |
182
+ | HateXplain test | unitary/unbiased-toxic-roberta | 0.577 | 0.690 | ~14 ms |
183
+
184
+ (full numbers in `reports/external_h2h.json`)
185
+
186
+ ### Adversarial robustness (detection rate on attacked unsafe texts, n=160)
187
+
188
+ | Attack | .6 | **.7** |
189
+ | --- | --- | --- |
190
+ | clean | 91.3% | 93.8% |
191
+ | typo | 89.4% | 88.8% |
192
+ | leetspeak | 92.5% | 93.1% |
193
+ | homoglyph | 98.1% | 98.1% |
194
+ | zero-width | 95.0% | 97.5% |
195
+ | case flip | 92.5% | 91.9% |
196
+
197
+ ### Long-context (300 synthetic docs of 25 held-out rows, avg 2,178 tokens)
198
+
199
+ | Inference | F1 |
200
+ | --- | --- |
201
+ | truncate at 192 tokens | 0.571 |
202
+ | native 4096 single pass (NOT recommended for long docs) | 0.052 |
203
+ | **chunked 256/192 max (default everywhere)** | **0.998 (acc 0.997)** |
204
+
205
+ ### Category calibration (held-out HateXplain val, n=1,922)
206
+
207
+ Binary-score ECE = **0.1798**. Per-category tuned thresholds (F1-maximizing) ship in
208
+ `config.json: cat_thresholds`:
209
+
210
+ | Category | threshold | F1@thr | ECE |
211
+ | --- | --- | --- | --- |
212
+ | violence | 0.20 | 0.000 | 0.195 |
213
+ | weapons_explosives | 0.20 | 0.000 | 0.140 |
214
+ | drugs | 0.20 | 0.000 | 0.103 |
215
+ | hate_harassment | 0.20 | 0.834 | 0.112 |
216
+ | sexual_abuse | 0.20 | 0.000 | 0.124 |
217
+ | self_harm | 0.20 | 0.000 | 0.095 |
218
+ | cybercrime_fraud | 0.20 | 0.000 | 0.130 |
219
+ | jailbreak_injection | 0.20 | 0.000 | 0.088 |
220
+ | illegal_marketplace | 0.20 | 0.000 | 0.083 |
221
+ | other_unsafe | 0.20 | 0.000 | 0.147 |
222
+
223
+ ![Reliability diagram](figs/reliability.png)
224
+
225
+ ### Multilingual — per-language binary F1 (textdetox multilingual toxicity, never in training; + held-out synthetic)
226
+
227
+ | Language | n | unsafe | F1@0.5 | best F1 |
228
+ | --- | --- | --- | --- | --- |
229
+ | synth_cs | 300 | 150 | 1.000 | 1.000 |
230
+ | synth_de | 300 | 150 | 1.000 | 1.000 |
231
+ | synth_es | 300 | 150 | 1.000 | 1.000 |
232
+ | synth_fr | 300 | 150 | 1.000 | 1.000 |
233
+ | textdetox_am | 800 | 385 | 0.141 | 0.646 |
234
+ | textdetox_ar | 800 | 387 | 0.000 | 0.049 |
235
+ | textdetox_de | 800 | 397 | 0.596 | 0.690 |
236
+ | textdetox_en | 800 | 391 | 0.820 | 0.842 |
237
+ | textdetox_es | 800 | 408 | 0.543 | 0.674 |
238
+ | textdetox_fr | 800 | 391 | 0.512 | 0.676 |
239
+ | textdetox_he | 800 | 332 | 0.063 | 0.305 |
240
+ | textdetox_hi | 800 | 409 | 0.000 | 0.000 |
241
+ | textdetox_hin | 800 | 394 | 0.543 | 0.639 |
242
+ | textdetox_it | 800 | 409 | 0.607 | 0.677 |
243
+ | textdetox_ja | 800 | 409 | 0.000 | 0.010 |
244
+ | textdetox_ru | 800 | 391 | 0.399 | 0.655 |
245
+ | textdetox_tt | 800 | 403 | 0.590 | 0.671 |
246
+ | textdetox_uk | 800 | 391 | 0.393 | 0.657 |
247
+ | textdetox_zh | 800 | 409 | 0.010 | 0.190 |
248
+
249
+ ### Cascade pre-filter
250
+
251
+ On 2,000 held-out rows: 42.8% escalated (gray-zone / jailbreak-category hits),
252
+ cascade accuracy 0.8600.
253
+
254
+ ## Benchmark (latency / throughput / size)
255
+
256
+ AMD Radeon RX 9060 XT (ROCm) and CPU-only:
257
+
258
+ | Metric | .7 GPU | .7 CPU | .5 GPU |
259
+ | --- | --- | --- | --- |
260
+ | Parameters | 902,016 | 902,016 | 902,016 |
261
+ | Checkpoint | 3.6 MB | 3.6 MB (int8 0.9 MB) | 3.6 MB |
262
+ | Single-text latency | 1.24 ms | 0.61 ms | 1.43 ms |
263
+ | Single throughput | 804 texts/s | 1628 texts/s | 823/s |
264
+ | Batch-8 throughput | 5348 texts/s | 4603 texts/s | 5,709/s |
265
+ | Peak VRAM | 85.1 MB | — | 105 MB |
266
+
267
+ ## Comparison on the internal test split (6,496 rows, threshold 0.5)
268
+
269
+ | Model | Params | Checkpoint | F1 | Latency | Throughput (b=8) |
270
+ | --- | --- | --- | --- | --- | --- |
271
+ | **Censer.7 (ours)** | **0.9M** | **3.6 MB** | **0.8348** | **1.39 ms** | **1761/s** |
272
+ | KucLab/Censer.5 | 0.9M | 3.6 MB | 0.849 | 1.27 ms | 1,978/s |
273
+ | KucLab/Censer.6 | 0.9M | 3.6 MB | 0.829 | 1.22 ms | 1,960/s |
274
+ | KucLab/Censer.4 | 0.9M | 3.6 MB | 0.845 | 1.17 ms | 2,229/s |
275
+ | KucLab/Censer.1 | 1.1M | 4.5 MB | 0.836 | 1.43 ms | 1,853/s |
276
+ | KucLab/Censer.2 | 7.3M | 29.4 MB | 0.829 | 2.70 ms | 508/s |
277
+ | KucLab/Censer.3 | 14.4M | 57.7 MB | 0.822 | 4.27 ms | 263/s |
278
+ | unitary/unbiased-toxic-roberta | 124.7M | 498.7 MB | 0.741 | 14.7 ms | 67/s |
279
+ | unitary/toxic-bert | 109.5M | 876.0 MB | 0.681 | 14.2 ms | 68/s |
280
+ | martin-ha/toxic-comment-model | 67.0M | 267.9 MB | 0.566 | 7.2 ms | 133/s |
281
+ | Hate-speech-CNERG/bert-base-uncased-hatexplain | 109.5M | 438.0 MB | 0.543 | 14.7 ms | 67/s |
282
+ | Hate-speech-CNERG/dehatebert-mono-english | 167.4M | 669.5 MB | 0.252 | 14.3 ms | 68/s |
283
+ | mistralai/Shieldstral-1.0-3B | 3.85B | 7.7 GB | 0.318 | 51.6 ms | 24/s |
284
+ | keyword baseline | — | — | 0.094 | 0.03 ms | 34,784/s |
285
+
286
+ ![Comparison overview](figs/comparison_overview.png)
287
+ ![F1 per megabyte](figs/f1_vs_size.png)
288
+ ![Metric bars](figs/metrics_bars.png)
289
+
290
+ ## Usage without transformers
291
+
292
+ ```bash
293
+ hf download KucLab/Censer.7 --local-dir Censer.7
294
+ cd Censer.7
295
+ python censer.py "how to make a bomb at home" --json
296
+ ./censer.7 # interactive UI
297
+ ```
298
+
299
+ Files:
300
+
301
+ | File | Purpose |
302
+ | --- | --- |
303
+ | `model.safetensors` | standard Llama layout, 3.6 MB |
304
+ | `model_fp16.safetensors` / `model_int8.safetensors` + `quant_report.json` | 1.8 MB / 0.9 MB quantized |
305
+ | `censer7.onnx` | ONNX graph: input_ids -> 12 logits, dynamic batch/seq |
306
+ | `config.json` | standard config + `cat_thresholds` (calibrated category cutoffs) |
307
+ | `tokenizer.json` / `tokenizer_config.json` | byte-level BPE, prompt template baked in |
308
+ | `censer.py` | standalone inference, chunked-max for long inputs (default) |
309
+ | `censer.7` | launcher for the interactive terminal UI |
310
+ | `serve.py` | batch HTTP API (stdlib only) |
311
+ | `Dockerfile` | CPU container serving the API |
312
+
313
+ ## Limitations
314
+
315
+ - 902K params: nuance/sarcasm/long-tail out of reach; internal test split has label
316
+ noise from scraped sources.
317
+ - Native single-pass 4096-token inference pools the last token only — for long
318
+ documents ALWAYS use the chunked-max path (default in `censer.py`/API).
319
+ - Category scores use calibrated per-category thresholds, but remain less reliable
320
+ than the binary verdict.
321
+ - Multilingual accuracy is best for the trained languages (en/cs/de/es/fr + Jigsaw-ML
322
+ languages); see the per-language table — low-resource languages are weaker.
323
+ - A moderation signal, not a safety guarantee; use the cascade pattern (clear cases
324
+ decided by Censer.7, gray-zone/jailbreak hits escalated to a larger validator).
censer.7 ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """censer.7 - run the Censer.7 safety classifier as an interactive terminal UI.
3
+
4
+ Usage:
5
+ censer.7 # interactive: type a phrase, Enter -> safe/unsafe + categories + score
6
+ censer.7 "how to make a bomb" # one-shot
7
+ censer.7 --help
8
+
9
+ Expects censer.py, model.safetensors, config.json and tokenizer.json next to
10
+ this file (they are found automatically, whatever the current directory is).
11
+ Get them all with:
12
+
13
+ hf download KucLab/Censer.7 --local-dir .
14
+
15
+ Requires: torch, tokenizers, safetensors (pip install torch tokenizers safetensors)
16
+ """
17
+
18
+ import os
19
+ import sys
20
+
21
+ HERE = os.path.dirname(os.path.abspath(__file__))
22
+ sys.path.insert(0, HERE)
23
+
24
+ from censer import main
25
+
26
+ if __name__ == "__main__":
27
+ argv = sys.argv[1:]
28
+ if not any(a == "--model-dir" or a.startswith("--model-dir=") for a in argv):
29
+ argv = ["--model-dir", HERE] + argv
30
+ main(argv)
censer.py ADDED
@@ -0,0 +1,355 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Inference for Censer.7 — 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
+ ./censer.5 # same UI via the censer.5 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 sys
23
+ from pathlib import Path
24
+
25
+ import torch
26
+ import torch.nn as nn
27
+ import torch.nn.functional as F
28
+ from tokenizers import Tokenizer
29
+
30
+ PAD, BOS, USER, ASSISTANT = "[PAD]", "[BOS]", "[USER]", "[ASSISTANT]"
31
+
32
+
33
+ class RMSNorm(nn.Module):
34
+ def __init__(self, dim: int, eps: float = 1e-5) -> None:
35
+ super().__init__()
36
+ self.eps = eps
37
+ self.weight = nn.Parameter(torch.ones(dim))
38
+
39
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
40
+ dtype = x.dtype
41
+ x = x.float()
42
+ x = x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
43
+ return x.to(dtype) * self.weight
44
+
45
+
46
+ def build_rope_cache(seq_len: int, head_dim: int, device, dtype, base: float = 10000.0, rope_scaling: dict | None = None):
47
+ half = head_dim // 2
48
+ freqs = 1.0 / (base ** (torch.arange(0, half, device=device, dtype=torch.float32) / half))
49
+ if rope_scaling and float(rope_scaling.get("factor", 1.0)) > 1.0:
50
+ freqs = freqs / float(rope_scaling["factor"]) # HF "linear" scaling: inv_freq /= factor
51
+ positions = torch.arange(seq_len, device=device, dtype=torch.float32)
52
+ angles = torch.outer(positions, freqs)
53
+ cos = torch.cat((angles.cos(), angles.cos()), dim=-1).to(dtype)[:, None, :]
54
+ sin = torch.cat((angles.sin(), angles.sin()), dim=-1).to(dtype)[:, None, :]
55
+ return cos, sin
56
+
57
+
58
+ def apply_rope(x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor) -> torch.Tensor:
59
+ seq_len = x.shape[1]
60
+ cos, sin = cos[:seq_len], sin[:seq_len]
61
+ x1, x2 = x[..., : x.shape[-1] // 2], x[..., x.shape[-1] // 2 :]
62
+ rotated = torch.cat((-x2, x1), dim=-1)
63
+ return x * cos + rotated * sin
64
+
65
+
66
+ class Attention(nn.Module):
67
+ def __init__(self, cfg: dict) -> None:
68
+ super().__init__()
69
+ self.n_heads = cfg["n_heads"]
70
+ self.head_dim = cfg["d_model"] // cfg["n_heads"]
71
+ self.dropout = cfg["dropout"]
72
+ self.qkv = nn.Linear(cfg["d_model"], 3 * cfg["d_model"], bias=False)
73
+ self.proj = nn.Linear(cfg["d_model"], cfg["d_model"], bias=False)
74
+
75
+ def forward(self, x, cos, sin, mask):
76
+ b, t, _ = x.shape
77
+ q, k, v = self.qkv(x).split(x.shape[-1], dim=-1)
78
+ q = q.view(b, t, self.n_heads, self.head_dim)
79
+ k = k.view(b, t, self.n_heads, self.head_dim)
80
+ v = v.view(b, t, self.n_heads, self.head_dim)
81
+ q, k = apply_rope(q, cos, sin), apply_rope(k, cos, sin)
82
+ out = F.scaled_dot_product_attention(
83
+ q.transpose(1, 2), k.transpose(1, 2), v.transpose(1, 2),
84
+ attn_mask=mask,
85
+ dropout_p=self.dropout if self.training else 0.0,
86
+ is_causal=mask is None,
87
+ )
88
+ return self.proj(out.transpose(1, 2).reshape(b, t, -1))
89
+
90
+
91
+ class SwiGLU(nn.Module):
92
+ def __init__(self, cfg: dict) -> None:
93
+ super().__init__()
94
+ self.gate = nn.Linear(cfg["d_model"], cfg["d_ff"], bias=False)
95
+ self.up = nn.Linear(cfg["d_model"], cfg["d_ff"], bias=False)
96
+ self.down = nn.Linear(cfg["d_ff"], cfg["d_model"], bias=False)
97
+
98
+ def forward(self, x):
99
+ return self.down(F.silu(self.gate(x)) * self.up(x))
100
+
101
+
102
+ class Block(nn.Module):
103
+ def __init__(self, cfg: dict) -> None:
104
+ super().__init__()
105
+ self.norm1 = RMSNorm(cfg["d_model"])
106
+ self.attn = Attention(cfg)
107
+ self.norm2 = RMSNorm(cfg["d_model"])
108
+ self.mlp = SwiGLU(cfg)
109
+ self.dropout = nn.Dropout(cfg["dropout"])
110
+
111
+ def forward(self, x, cos, sin, mask):
112
+ x = x + self.dropout(self.attn(self.norm1(x), cos, sin, mask))
113
+ x = x + self.dropout(self.mlp(self.norm2(x)))
114
+ return x
115
+
116
+
117
+ class Censer(nn.Module):
118
+ def __init__(self, cfg: dict) -> None:
119
+ super().__init__()
120
+ self.cfg = cfg
121
+ self.register_buffer(
122
+ "answer_id_tensor", torch.tensor(cfg["answer_ids"], dtype=torch.long), persistent=False
123
+ )
124
+ self.embed = nn.Embedding(cfg["vocab_size"], cfg["d_model"], padding_idx=cfg["pad_id"])
125
+ self.blocks = nn.ModuleList([Block(cfg) for _ in range(cfg["n_layers"])])
126
+ self.norm = RMSNorm(cfg["d_model"])
127
+ self.lm_head = nn.Linear(cfg["d_model"], cfg["vocab_size"], bias=False)
128
+ if cfg["tie_embeddings"]:
129
+ self.lm_head.weight = self.embed.weight
130
+ self._rope_cache = None
131
+
132
+ def _rope(self, device, dtype):
133
+ key = f"{device}|{dtype}"
134
+ if self._rope_cache is None or self._rope_cache[2] != key:
135
+ head_dim = self.cfg["d_model"] // self.cfg["n_heads"]
136
+ cos, sin = build_rope_cache(self.cfg["max_seq_len"], head_dim, device, dtype, rope_scaling=self.cfg.get("rope_scaling"))
137
+ self._rope_cache = (cos, sin, key)
138
+ return self._rope_cache[0], self._rope_cache[1]
139
+
140
+ def _attention_mask(self, input_ids):
141
+ b, t = input_ids.shape
142
+ pad = input_ids != self.cfg["pad_id"]
143
+ causal = torch.ones(t, t, dtype=torch.bool, device=input_ids.device).tril()
144
+ return pad[:, None, None, :] & causal[None, None, :, :]
145
+
146
+ def forward(self, input_ids):
147
+ cos, sin = self._rope(input_ids.device, self.embed.weight.dtype)
148
+ mask = self._attention_mask(input_ids)
149
+ x = self.embed(input_ids)
150
+ for block in self.blocks:
151
+ x = block(x, cos, sin, mask)
152
+ return self.lm_head(self.norm(x))
153
+
154
+ @torch.no_grad()
155
+ def classify_probs(self, input_ids):
156
+ """Answer-token probabilities: softmax for binary heads, sigmoid for multi-label."""
157
+ hidden = self.forward(input_ids)
158
+ non_pad = (input_ids != self.cfg["pad_id"]).sum(dim=1).clamp(min=1)
159
+ idx = (non_pad - 1)[:, None, None].expand(-1, 1, hidden.shape[-1])
160
+ logits = hidden.gather(1, idx)[:, 0, :].float()
161
+ logits = logits.index_select(-1, self.answer_id_tensor)
162
+ if logits.shape[-1] == 2:
163
+ return logits.softmax(-1)
164
+ return logits.sigmoid()
165
+
166
+ @torch.no_grad()
167
+ def classify(self, input_ids):
168
+ return self.classify_probs(input_ids)[:, 1]
169
+
170
+
171
+ def remap_hf_state(state: dict) -> dict:
172
+ """Standard Llama-layout safetensors -> training-layout state dict."""
173
+ import re
174
+
175
+ out = {"embed.weight": state["model.embed_tokens.weight"]}
176
+ layer_ids = sorted({int(m.group(1)) for k in state if (m := re.match(r"model\.layers\.(\d+)\.", k))})
177
+ for i in layer_ids:
178
+ p, b = f"model.layers.{i}.", f"blocks.{i}."
179
+ qkv = torch.cat(
180
+ [state[p + "self_attn.q_proj.weight"], state[p + "self_attn.k_proj.weight"], state[p + "self_attn.v_proj.weight"]],
181
+ dim=0,
182
+ )
183
+ out[b + "attn.qkv.weight"] = qkv.contiguous()
184
+ out[b + "attn.proj.weight"] = state[p + "self_attn.o_proj.weight"]
185
+ out[b + "mlp.gate.weight"] = state[p + "mlp.gate_proj.weight"]
186
+ out[b + "mlp.up.weight"] = state[p + "mlp.up_proj.weight"]
187
+ out[b + "mlp.down.weight"] = state[p + "mlp.down_proj.weight"]
188
+ out[b + "norm1.weight"] = state[p + "input_layernorm.weight"]
189
+ out[b + "norm2.weight"] = state[p + "post_attention_layernorm.weight"]
190
+ out["norm.weight"] = state["model.norm.weight"]
191
+ return out
192
+
193
+
194
+ def internal_cfg(cfg: dict) -> dict:
195
+ """config.json (Llama layout) -> the dict the Censer module expects."""
196
+ return {
197
+ "vocab_size": cfg["vocab_size"],
198
+ "n_layers": cfg["num_hidden_layers"],
199
+ "n_heads": cfg["num_attention_heads"],
200
+ "d_model": cfg["hidden_size"],
201
+ "d_ff": cfg["intermediate_size"],
202
+ "max_seq_len": cfg["max_position_embeddings"],
203
+ "dropout": cfg.get("dropout", 0.0),
204
+ "tie_embeddings": True,
205
+ "pad_id": cfg.get("pad_token_id", 0),
206
+ "answer_ids": cfg["answer_ids"],
207
+ "max_chars": cfg.get("max_chars", 4000),
208
+ "rope_scaling": cfg.get("rope_scaling") or (cfg.get("rope_parameters") if cfg.get("rope_parameters", {}).get("rope_type") == "linear" else None),
209
+ "categories": [cfg["id2label"][str(i)] for i in range(2, cfg.get("num_labels", 2))] if cfg.get("num_labels", 2) > 2 else [],
210
+ "cat_thresholds": cfg.get("cat_thresholds") or {},
211
+ }
212
+
213
+
214
+ class CenserPipeline:
215
+ """Text in, "1" (unsafe) or "0" (safe) out."""
216
+
217
+ def __init__(self, directory: str | Path = ".", threshold: float = 0.5, device: str | None = None) -> None:
218
+ from safetensors.torch import load_file
219
+
220
+ directory = Path(directory)
221
+ self.cfg = internal_cfg(json.loads((directory / "config.json").read_text(encoding="utf-8")))
222
+ self.tok = Tokenizer.from_file(str(directory / "tokenizer.json"))
223
+ self.pad_id = self.tok.token_to_id(PAD)
224
+ self.bos_id = self.tok.token_to_id(BOS)
225
+ self.user_id = self.tok.token_to_id(USER)
226
+ self.assistant_id = self.tok.token_to_id(ASSISTANT)
227
+ self.threshold = threshold
228
+ self.cat_thresholds = dict(self.cfg.get("cat_thresholds") or {})
229
+ self.device = torch.device(device or ("cuda" if torch.cuda.is_available() else "cpu"))
230
+ self.model = Censer(self.cfg)
231
+ state = remap_hf_state(load_file(directory / "model.safetensors"))
232
+ missing, unexpected = self.model.load_state_dict(state, strict=False)
233
+ missing = [k for k in missing if k != "lm_head.weight"] # tied to embed.weight
234
+ if missing or unexpected:
235
+ raise RuntimeError(f"weight mismatch: missing={missing} unexpected={unexpected}")
236
+ self.model.to(self.device).eval()
237
+ self.max_chars = self.cfg["max_chars"]
238
+
239
+ def _encode(self, text: str) -> list[int]:
240
+ body = self.tok.encode(text[: self.max_chars], add_special_tokens=False).ids
241
+ ids = [self.bos_id, self.user_id, *body, self.assistant_id]
242
+ return ids[: self.cfg["max_seq_len"]]
243
+
244
+ def predict(self, text: str) -> dict:
245
+ return self.predict_batch([text])[0]
246
+
247
+ def _encode_full(self, text: str) -> list[int]:
248
+ body = self.tok.encode(text[: self.max_chars * 8], add_special_tokens=False).ids
249
+ return [self.bos_id, self.user_id, *body, self.assistant_id]
250
+
251
+ def predict_batch(self, texts: list[str]) -> list[dict]:
252
+ # long inputs: split into overlapping token chunks, aggregate by max
253
+ out: list[dict] = []
254
+ chunk_tokens, stride = 256, 192
255
+ for text in texts:
256
+ ids = self._encode_full(text)
257
+ body = ids[2:-1]
258
+ if len(body) <= chunk_tokens:
259
+ out.append(self._classify_ids([ids[: self.cfg["max_seq_len"]]])[0])
260
+ continue
261
+ chunks = []
262
+ start = 0
263
+ while start < len(body):
264
+ chunks.append(body[start:start + chunk_tokens])
265
+ if start + chunk_tokens >= len(body):
266
+ break
267
+ start += stride
268
+ rows = []
269
+ for piece in chunks:
270
+ cids = [self.bos_id, self.user_id, *piece, self.assistant_id]
271
+ rows.append(self._classify_ids([cids[: self.cfg["max_seq_len"]]])[0])
272
+ best = max(rows, key=lambda r: r["score"])
273
+ # aggregate categories by max prob
274
+ n_cat = len(rows[0].get("category_scores", []))
275
+ cat_scores = [max(r["category_scores"][i] for r in rows) for i in range(n_cat)] if n_cat else []
276
+ best["category_scores"] = [round(c, 4) for c in cat_scores]
277
+ cats = self.categories()
278
+ best["categories"] = [c for c, p in zip(cats, cat_scores) if p >= float(self.cat_thresholds.get(c, self.threshold))]
279
+ best["chunks"] = len(chunks)
280
+ out.append(best)
281
+ return out
282
+
283
+ def categories(self) -> list[str]:
284
+ return self.cfg.get("categories") or []
285
+
286
+ def _classify_ids(self, seqs: list[list[int]]) -> list[dict]:
287
+ width = max(len(s) for s in seqs)
288
+ ids = torch.full((len(seqs), width), self.pad_id, dtype=torch.long)
289
+ for i, seq in enumerate(seqs):
290
+ ids[i, : len(seq)] = torch.tensor(seq)
291
+ probs = self.model.classify_probs(ids.to(self.device)).float().cpu().tolist()
292
+ res = []
293
+ for row in probs:
294
+ p = row[1]
295
+ cats = self.categories()
296
+ cat_scores = row[2:] if cats else []
297
+ res.append({
298
+ "label": "1" if p >= self.threshold else "0",
299
+ "unsafe": p >= self.threshold,
300
+ "score": round(p, 4),
301
+ "category_scores": [round(c, 4) for c in cat_scores],
302
+ "categories": [c for c, s in zip(cats, cat_scores) if s >= float(self.cat_thresholds.get(c, self.threshold))],
303
+ })
304
+ return res
305
+
306
+
307
+ def _color(text: str, code: str) -> str:
308
+ if not sys.stdout.isatty():
309
+ return text
310
+ return f"\033[{code}m{text}\033[0m"
311
+
312
+
313
+ def repl(pipe: "CenserPipeline") -> int:
314
+ """Small interactive terminal UI: phrase in, safe/unsafe + score out."""
315
+ print("Censer.7 - interactive terminal UI")
316
+ print("Type a phrase, Enter to classify. 'exit' to quit.\n")
317
+ while True:
318
+ try:
319
+ line = input("> ")
320
+ except (EOFError, KeyboardInterrupt):
321
+ print()
322
+ return 0
323
+ text = line.strip()
324
+ if text.lower() in {"exit", "quit", ":q"}:
325
+ return 0
326
+ if not text:
327
+ continue
328
+ v = pipe.predict(text)
329
+ label = "unsafe" if v["unsafe"] else "safe"
330
+ colored = _color(f"{label:6s}", "31" if v["unsafe"] else "32")
331
+ print(f"{colored} {v['score']:.4f} | {text}")
332
+
333
+
334
+ def main(argv: list[str] | None = None) -> None:
335
+ parser = argparse.ArgumentParser(
336
+ prog="censer.5",
337
+ description="Censer.7 safety classifier (standalone). Run without text to open the interactive UI.",
338
+ )
339
+ parser.add_argument("text", nargs="*", help="phrase to classify once; omit to open the interactive UI")
340
+ parser.add_argument("--model-dir", default=".")
341
+ parser.add_argument("--threshold", type=float, default=0.5)
342
+ parser.add_argument("--json", action="store_true")
343
+ parser.add_argument("--repl", action="store_true", help="interactive UI (default when no text is given)")
344
+ args = parser.parse_args(argv)
345
+
346
+ pipe = CenserPipeline(args.model_dir, args.threshold)
347
+ text = " ".join(args.text).strip()
348
+ if args.repl or not text:
349
+ raise SystemExit(repl(pipe))
350
+ verdict = pipe.predict(text)
351
+ print(json.dumps(verdict) if args.json else verdict["label"])
352
+
353
+
354
+ if __name__ == "__main__":
355
+ main()
censer7.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1f470b5603915208c3ab2351678b512f2f5da69914f562f15cf773d33b110286
3
+ size 5778265
config.json ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "original_max_position_embeddings": 512
50
+ },
51
+ "rope_scaling": {
52
+ "rope_type": "linear",
53
+ "factor": 8.0
54
+ },
55
+ "attention_bias": false,
56
+ "mlp_bias": false,
57
+ "tie_word_embeddings": true,
58
+ "pad_token_id": 0,
59
+ "bos_token_id": 1,
60
+ "eos_token_id": 3,
61
+ "name": "Censer.7",
62
+ "max_chars": 4000,
63
+ "answer_ids": [
64
+ 19,
65
+ 20,
66
+ 68,
67
+ 69,
68
+ 70,
69
+ 71,
70
+ 72,
71
+ 73,
72
+ 74,
73
+ 75,
74
+ 76,
75
+ 77
76
+ ],
77
+ "special_tokens": [
78
+ "[PAD]",
79
+ "[BOS]",
80
+ "[USER]",
81
+ "[ASSISTANT]"
82
+ ],
83
+ "threshold": 0.5,
84
+ "cat_thresholds": {
85
+ "violence": 0.5,
86
+ "weapons_explosives": 0.5,
87
+ "drugs": 0.5,
88
+ "hate_harassment": 0.2,
89
+ "sexual_abuse": 0.5,
90
+ "self_harm": 0.5,
91
+ "cybercrime_fraud": 0.5,
92
+ "jailbreak_injection": 0.5,
93
+ "illegal_marketplace": 0.5,
94
+ "other_unsafe": 0.5
95
+ }
96
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5cc81eaab61ca9c04b75b891cef36cdf7c54ab5bd0b407a5a68867a77c7d4f61
3
+ size 3617384
model_fp16.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0e500de1c598dd01b6bcc167eeac27dbf54cb8c58f2c6ab16a6864b932abb5bd
3
+ size 1810256
model_int8.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e6db683ed9109d478ecdeef83df4b0ad9e67e853bcd557321400bbbaf19aa0c2
3
+ size 911740
quant_report.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "model_safetensors_mb": 3.62,
3
+ "model_fp16_mb": 1.81,
4
+ "model_int8_mb": 0.91,
5
+ "max_rel_roundtrip_error": 0.00393700459972024
6
+ }
serve.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Batch classification API for the Censer.6 Hub bundle (stdlib only, no FastAPI).
2
+
3
+ python scripts/serve.py --port 8000 # serves the local HF bundle dir
4
+ CENSER_DIR=/path/to/bundle python scripts/serve.py
5
+
6
+ Endpoints:
7
+ GET /health -> {"status": "ok", "model": ...}
8
+ POST /classify -> {"texts": [...]} => {"results": [{label, unsafe, score, categories, chunks}]}
9
+ POST /classify-one -> {"text": "..."} => single result
10
+
11
+ Features: batching, long-input chunking (chunk_tokens/stride), token-bucket
12
+ rate limit (env RATE_LIMIT_PER_MIN, default 600), max body size guard.
13
+
14
+ .venv/bin/python scripts/serve.py --dir bundle --port 8000
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import argparse
20
+ import json
21
+ import os
22
+ import time
23
+ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
24
+ from pathlib import Path
25
+
26
+ MAX_BODY = 2 * 1024 * 1024 # 2 MB
27
+
28
+
29
+ class RateLimiter:
30
+ def __init__(self, per_min: float) -> None:
31
+ self.per_min = per_min
32
+ self.tokens = per_min
33
+ self.updated = time.monotonic()
34
+ self.last_deny = 0.0
35
+
36
+ def allow(self, cost: float = 1.0) -> bool:
37
+ now = time.monotonic()
38
+ self.tokens = min(self.per_min, self.tokens + (now - self.updated) * self.per_min / 60.0)
39
+ self.updated = now
40
+ if self.tokens >= cost:
41
+ self.tokens -= cost
42
+ return True
43
+ return False
44
+
45
+
46
+ def make_handler(pipe, limiter: RateLimiter, model_name: str):
47
+ class Handler(BaseHTTPRequestHandler):
48
+ def _json(self, code: int, payload: dict) -> None:
49
+ body = json.dumps(payload, ensure_ascii=False).encode("utf-8")
50
+ self.send_response(code)
51
+ self.send_header("Content-Type", "application/json; charset=utf-8")
52
+ self.send_header("Content-Length", str(len(body)))
53
+ self.end_headers()
54
+ self.wfile.write(body)
55
+
56
+ def log_message(self, fmt, *args): # quiet
57
+ pass
58
+
59
+ def do_GET(self):
60
+ if self.path == "/health":
61
+ self._json(200, {"status": "ok", "model": model_name, "device": str(pipe.device)})
62
+ else:
63
+ self._json(404, {"error": "not found"})
64
+
65
+ def do_POST(self):
66
+ if not limiter.allow():
67
+ self._json(429, {"error": "rate limit exceeded"})
68
+ return
69
+ length = int(self.headers.get("Content-Length") or 0)
70
+ if length > MAX_BODY:
71
+ self._json(413, {"error": "body too large"})
72
+ return
73
+ try:
74
+ payload = json.loads(self.rfile.read(length) or b"{}")
75
+ except Exception:
76
+ self._json(400, {"error": "invalid json"})
77
+ return
78
+ if self.path == "/classify-one":
79
+ text = str(payload.get("text", ""))
80
+ self._json(200, pipe.predict(text))
81
+ elif self.path == "/classify":
82
+ texts = payload.get("texts")
83
+ if not isinstance(texts, list) or not texts or len(texts) > 256:
84
+ self._json(400, {"error": "texts must be a list of 1..256 strings"})
85
+ return
86
+ self._json(200, {"results": pipe.predict_batch([str(t) for t in texts])})
87
+ else:
88
+ self._json(404, {"error": "not found"})
89
+
90
+ return Handler
91
+
92
+
93
+ def main() -> None:
94
+ parser = argparse.ArgumentParser()
95
+ parser.add_argument("--dir", default=os.environ.get("CENSER_DIR", "bundle"))
96
+ parser.add_argument("--port", type=int, default=8000)
97
+ parser.add_argument("--host", default="0.0.0.0")
98
+ parser.add_argument("--device", default=None)
99
+ args = parser.parse_args()
100
+
101
+ import sys
102
+
103
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
104
+ from censer import CenserPipeline
105
+
106
+ pipe = CenserPipeline(args.dir, device=args.device)
107
+ limiter = RateLimiter(float(os.environ.get("RATE_LIMIT_PER_MIN", "600")))
108
+ server = ThreadingHTTPServer((args.host, args.port), make_handler(pipe, limiter, args.dir))
109
+ print(f"censer serve: model={args.dir} device={pipe.device} on http://{args.host}:{args.port}")
110
+ print(" POST /classify {\"texts\": [...]} POST /classify-one {\"text\": ...} GET /health")
111
+ server.serve_forever()
112
+
113
+
114
+ if __name__ == "__main__":
115
+ main()
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "tokenizer_class": "PreTrainedTokenizerFast",
3
+ "pad_token": "[PAD]",
4
+ "bos_token": "[BOS]",
5
+ "eos_token": "[ASSISTANT]"
6
+ }