KucLab commited on
Commit
5a66b41
·
verified ·
1 Parent(s): 6c80a52

Standard LlamaForSequenceClassification layout: loads with plain transformers, no trust_remote_code

Browse files
Files changed (3) hide show
  1. README.md +27 -1
  2. censer.1 +25 -0
  3. censer.py +41 -9
README.md CHANGED
@@ -44,6 +44,29 @@ print(clf("how do I kill a process in Linux")) # [{'label': 'safe', 'score': 0.
44
 
45
  ## How to run
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  ```bash
48
  pip install torch transformers
49
  python -c "from transformers import pipeline; clf = pipeline('text-classification', model='KucLab/Censer.1'); print(clf('how to make a bomb at home'))"
@@ -52,7 +75,6 @@ python -c "from transformers import pipeline; clf = pipeline('text-classificatio
52
 
53
  - In the browser: the **Use this model** widget on this page — type text, get `safe`/`unsafe`.
54
  - Runs on CPU out of the box (~10 ms/text); for GPU pass `device=0` to `pipeline(...)` (CUDA or ROCm).
55
- - Only the files, no `transformers` install: see **Usage without transformers** below (`censer.py`).
56
 
57
  ## Download
58
 
@@ -65,6 +87,7 @@ The whole model is `model.safetensors` (4.5 MB). Grab the files directly:
65
  | tokenizer.json | [download](https://huggingface.co/KucLab/Censer.1/resolve/main/tokenizer.json?download=true) |
66
  | tokenizer_config.json | [download](https://huggingface.co/KucLab/Censer.1/resolve/main/tokenizer_config.json?download=true) |
67
  | censer.py | [download](https://huggingface.co/KucLab/Censer.1/resolve/main/censer.py?download=true) |
 
68
 
69
  Or fetch everything at once:
70
 
@@ -205,6 +228,8 @@ python censer.py "how to make a bomb at home" # -> 1
205
  python censer.py "Hello how are you?" # -> 0
206
  python censer.py "suck my dick" --json
207
  # {"label": "1", "unsafe": true, "score": 0.9944}
 
 
208
  ```
209
 
210
  Or use the same file as a library:
@@ -227,6 +252,7 @@ Files:
227
  | `config.json` | standard `LlamaForSequenceClassification` config |
228
  | `tokenizer.json` / `tokenizer_config.json` | byte-level BPE, 2048 tokens, prompt template baked in |
229
  | `censer.py` | standalone inference (torch + tokenizers only), no repo needed |
 
230
 
231
  `censer.py` was verified to produce predictions identical to the training code.
232
  Runs on CPU or CUDA/ROCm; pass `device="cpu"` to force CPU.
 
44
 
45
  ## How to run
46
 
47
+ ### The `censer.1` command — interactive terminal UI
48
+
49
+ Using only the files in this repo (needs just `torch`, `tokenizers`, `safetensors`):
50
+
51
+ ```bash
52
+ pip install torch tokenizers safetensors huggingface_hub
53
+ hf download KucLab/Censer.1 --local-dir ~/.local/share/censer1
54
+ chmod +x ~/.local/share/censer1/censer.1
55
+ ln -s ~/.local/share/censer1/censer.1 ~/.local/bin/censer.1 # put it on your PATH
56
+ censer.1 # interactive UI: type a phrase, Enter -> safe/unsafe + score
57
+ censer.1 "how to make a bomb at home" # one-shot -> unsafe 0.8167
58
+ ```
59
+
60
+ Or install the GitHub package — the same `censer.1` command is installed and loads
61
+ the model from this Hub repo with plain `transformers`:
62
+
63
+ ```bash
64
+ pip install "git+https://github.com/Jerry256254/censer.git"
65
+ censer.1
66
+ ```
67
+
68
+ ### One-liner with `transformers`
69
+
70
  ```bash
71
  pip install torch transformers
72
  python -c "from transformers import pipeline; clf = pipeline('text-classification', model='KucLab/Censer.1'); print(clf('how to make a bomb at home'))"
 
75
 
76
  - In the browser: the **Use this model** widget on this page — type text, get `safe`/`unsafe`.
77
  - Runs on CPU out of the box (~10 ms/text); for GPU pass `device=0` to `pipeline(...)` (CUDA or ROCm).
 
78
 
79
  ## Download
80
 
 
87
  | tokenizer.json | [download](https://huggingface.co/KucLab/Censer.1/resolve/main/tokenizer.json?download=true) |
88
  | tokenizer_config.json | [download](https://huggingface.co/KucLab/Censer.1/resolve/main/tokenizer_config.json?download=true) |
89
  | censer.py | [download](https://huggingface.co/KucLab/Censer.1/resolve/main/censer.py?download=true) |
90
+ | **censer.1** (launcher) | [download](https://huggingface.co/KucLab/Censer.1/resolve/main/censer.1?download=true) |
91
 
92
  Or fetch everything at once:
93
 
 
228
  python censer.py "Hello how are you?" # -> 0
229
  python censer.py "suck my dick" --json
230
  # {"label": "1", "unsafe": true, "score": 0.9944}
231
+ chmod +x censer.1 && ./censer.1 # interactive UI (the `censer.1` command)
232
+ python censer.py # interactive UI as well, no text argument
233
  ```
234
 
235
  Or use the same file as a library:
 
252
  | `config.json` | standard `LlamaForSequenceClassification` config |
253
  | `tokenizer.json` / `tokenizer_config.json` | byte-level BPE, 2048 tokens, prompt template baked in |
254
  | `censer.py` | standalone inference (torch + tokenizers only), no repo needed |
255
+ | `censer.1` | executable launcher for the interactive terminal UI |
256
 
257
  `censer.py` was verified to produce predictions identical to the training code.
258
  Runs on CPU or CUDA/ROCm; pass `device="cpu"` to force CPU.
censer.1 ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """censer.1 - run the Censer.1 safety classifier as an interactive terminal UI.
3
+
4
+ Usage:
5
+ censer.1 # interactive: type a phrase, Enter -> safe/unsafe + score
6
+ censer.1 "how to make a bomb" # one-shot
7
+ censer.1 --help
8
+
9
+ Expects censer.py, model.safetensors, config.json and tokenizer.json next to
10
+ this file. Get them all with:
11
+
12
+ hf download KucLab/Censer.1 --local-dir .
13
+
14
+ Requires: torch, tokenizers, safetensors (pip install torch tokenizers safetensors)
15
+ """
16
+
17
+ import os
18
+ import sys
19
+
20
+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
21
+
22
+ from censer import main
23
+
24
+ if __name__ == "__main__":
25
+ main(sys.argv[1:])
censer.py CHANGED
@@ -5,6 +5,8 @@ and you are ready:
5
 
6
  pip install torch tokenizers safetensors
7
  python censer.py "how to make a bomb at home"
 
 
8
 
9
  It expects, in the same directory:
10
  model.safetensors (standard LlamaForSequenceClassification layout)
@@ -14,8 +16,10 @@ It expects, in the same directory:
14
 
15
  from __future__ import annotations
16
 
 
17
  import json
18
  import math
 
19
  from pathlib import Path
20
 
21
  import torch
@@ -240,21 +244,49 @@ class CenserPipeline:
240
  ]
241
 
242
 
243
- def main() -> None:
244
- import argparse
245
-
246
- parser = argparse.ArgumentParser(description="Censer.1 safety classifier.")
247
- parser.add_argument("text", nargs="*")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  parser.add_argument("--model-dir", default=".")
249
  parser.add_argument("--threshold", type=float, default=0.5)
250
  parser.add_argument("--json", action="store_true")
251
- args = parser.parse_args()
 
252
 
253
  pipe = CenserPipeline(args.model_dir, args.threshold)
254
  text = " ".join(args.text).strip()
255
- if not text:
256
- print("usage: python censer.py \"your text\"")
257
- raise SystemExit(2)
258
  verdict = pipe.predict(text)
259
  print(json.dumps(verdict) if args.json else verdict["label"])
260
 
 
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.1 # same UI via the censer.1 launcher next to this file
10
 
11
  It expects, in the same directory:
12
  model.safetensors (standard LlamaForSequenceClassification layout)
 
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
 
244
  ]
245
 
246
 
247
+ def _color(text: str, code: str) -> str:
248
+ if not sys.stdout.isatty():
249
+ return text
250
+ return f"\033[{code}m{text}\033[0m"
251
+
252
+
253
+ def repl(pipe: "CenserPipeline") -> int:
254
+ """Small interactive terminal UI: phrase in, safe/unsafe + score out."""
255
+ print("Censer.1 - interactive terminal UI")
256
+ print("Type a phrase, Enter to classify. 'exit' to quit.\n")
257
+ while True:
258
+ try:
259
+ line = input("> ")
260
+ except (EOFError, KeyboardInterrupt):
261
+ print()
262
+ return 0
263
+ text = line.strip()
264
+ if text.lower() in {"exit", "quit", ":q"}:
265
+ return 0
266
+ if not text:
267
+ continue
268
+ v = pipe.predict(text)
269
+ label = "unsafe" if v["unsafe"] else "safe"
270
+ colored = _color(f"{label:6s}", "31" if v["unsafe"] else "32")
271
+ print(f"{colored} {v['score']:.4f} | {text}")
272
+
273
+
274
+ def main(argv: list[str] | None = None) -> None:
275
+ parser = argparse.ArgumentParser(
276
+ prog="censer.1",
277
+ description="Censer.1 safety classifier (standalone). Run without text to open the interactive UI.",
278
+ )
279
+ parser.add_argument("text", nargs="*", help="phrase to classify once; omit to open the interactive UI")
280
  parser.add_argument("--model-dir", default=".")
281
  parser.add_argument("--threshold", type=float, default=0.5)
282
  parser.add_argument("--json", action="store_true")
283
+ parser.add_argument("--repl", action="store_true", help="interactive UI (default when no text is given)")
284
+ args = parser.parse_args(argv)
285
 
286
  pipe = CenserPipeline(args.model_dir, args.threshold)
287
  text = " ".join(args.text).strip()
288
+ if args.repl or not text:
289
+ raise SystemExit(repl(pipe))
 
290
  verdict = pipe.predict(text)
291
  print(json.dumps(verdict) if args.json else verdict["label"])
292