Text Classification
Transformers
ONNX
Safetensors
llama
safety
content-moderation
multi-label
multilingual
tiny-language-model
from-scratch
distilled
text-embeddings-inference
Instructions to use KucLab/Censer.8 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use KucLab/Censer.8 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="KucLab/Censer.8")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification tokenizer = AutoTokenizer.from_pretrained("KucLab/Censer.8") model = AutoModelForSequenceClassification.from_pretrained("KucLab/Censer.8", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Add fp16/int8 quantized weights, ONNX graph, quant report, serve.py, Dockerfile
Browse files- Dockerfile +27 -0
- censer8.onnx +3 -0
- model_fp16.safetensors +3 -0
- model_int8.safetensors +3 -0
- quant_report.json +17 -0
- serve.py +115 -0
Dockerfile
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Censer.8 — self-contained moderation API image (CPU-only torch by default).
|
| 2 |
+
#
|
| 3 |
+
# docker build -t censer8 .
|
| 4 |
+
# docker run -p 8000:8000 -e RATE_LIMIT_PER_MIN=600 censer8
|
| 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.
|
| 8 |
+
|
| 9 |
+
FROM python:3.12-slim
|
| 10 |
+
|
| 11 |
+
ARG CENSER_REPO=KucLab/Censer.8
|
| 12 |
+
ENV CENSER_DIR=/model \
|
| 13 |
+
RATE_LIMIT_PER_MIN=600 \
|
| 14 |
+
PYTHONUNBUFFERED=1
|
| 15 |
+
|
| 16 |
+
RUN pip install --no-cache-dir \
|
| 17 |
+
"torch==2.9.*" --index-url https://download.pytorch.org/whl/cpu \
|
| 18 |
+
"tokenizers>=0.20,<1" "safetensors>=0.4,<1" "huggingface_hub>=0.24,<1" "numpy>=1.26,<3"
|
| 19 |
+
|
| 20 |
+
RUN hf download ${CENSER_REPO} --local-dir /model
|
| 21 |
+
|
| 22 |
+
WORKDIR /app
|
| 23 |
+
COPY censer.py serve.py /app/
|
| 24 |
+
|
| 25 |
+
EXPOSE 8000
|
| 26 |
+
HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8000/health', timeout=4).status==200 else 1)"
|
| 27 |
+
CMD ["python", "serve.py", "--dir", "/model", "--port", "8000", "--host", "0.0.0.0"]
|
censer8.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8213ddd6bddd8e3cb3f86f86bc0b53e6257ce42fff121d6e258b80e37083e6e0
|
| 3 |
+
size 4735540
|
model_fp16.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e8836be152c19c0e63eba7569e65b2fc2cf59bf904311c09d2f10878ade5d53b
|
| 3 |
+
size 1810256
|
model_int8.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:13faee97238efbc8e97dce6edc53338b3c39fa8111a8789293a348f62eb65baa
|
| 3 |
+
size 911740
|
quant_report.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"fp32": {
|
| 3 |
+
"file": "model.safetensors",
|
| 4 |
+
"mb": 3.62
|
| 5 |
+
},
|
| 6 |
+
"fp16": {
|
| 7 |
+
"file": "model_fp16.safetensors",
|
| 8 |
+
"mb": 1.81
|
| 9 |
+
},
|
| 10 |
+
"int8": {
|
| 11 |
+
"file": "model_int8.safetensors",
|
| 12 |
+
"mb": 0.91,
|
| 13 |
+
"scheme": "per-tensor symmetric",
|
| 14 |
+
"max_rel_roundtrip_error": 0.003936994820833206,
|
| 15 |
+
"loader": "censer.load_state_dict_any(dir) dequantizes .q/.scale; or use --prefer-quant"
|
| 16 |
+
}
|
| 17 |
+
}
|
serve.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Batch classification API for the Censer.8 Hub bundle (stdlib only, no FastAPI).
|
| 2 |
+
|
| 3 |
+
python serve.py --port 8000 # serves the local HF bundle dir (run from the bundle root)
|
| 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 |
+
python 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()
|