KucLab commited on
Commit
739730f
·
verified ·
1 Parent(s): 3234c88

Update serve.py (Barist.2)

Browse files
Files changed (1) hide show
  1. serve.py +122 -0
serve.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Batch classification API for the Barist.1 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
+ Response fields per text: `label` ("safe"/"unsafe"), `unsafe`, `score`,
7
+ `categories` (top-2 by priority: sexual_abuse > self_harm > violence/weapons > ... > hate;
8
+ unsafe always gets >=1 category), `override` ("must_catch" when the deterministic
9
+ pre-filter fired), `needs_review` (jailbreak/legally-ambiguous patterns — review, not a
10
+ block), `needs_support` (Linka bezpečí URL when self-harm is indicated, regex or
11
+ model score >= 0.3).
12
+
13
+ Endpoints:
14
+ GET /health -> {"status": "ok", "model": ...}
15
+ POST /classify -> {"texts": [...]} => {"results": [{label, unsafe, score, categories, chunks}]}
16
+ POST /classify-one -> {"text": "..."} => single result
17
+
18
+ Features: batching, long-input chunking (chunk_tokens/stride), token-bucket
19
+ rate limit (env RATE_LIMIT_PER_MIN, default 600), max body size guard.
20
+
21
+ python serve.py --dir bundle --port 8000
22
+ """
23
+
24
+ from __future__ import annotations
25
+
26
+ import argparse
27
+ import json
28
+ import os
29
+ import time
30
+ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
31
+ from pathlib import Path
32
+
33
+ MAX_BODY = 2 * 1024 * 1024 # 2 MB
34
+
35
+
36
+ class RateLimiter:
37
+ def __init__(self, per_min: float) -> None:
38
+ self.per_min = per_min
39
+ self.tokens = per_min
40
+ self.updated = time.monotonic()
41
+ self.last_deny = 0.0
42
+
43
+ def allow(self, cost: float = 1.0) -> bool:
44
+ now = time.monotonic()
45
+ self.tokens = min(self.per_min, self.tokens + (now - self.updated) * self.per_min / 60.0)
46
+ self.updated = now
47
+ if self.tokens >= cost:
48
+ self.tokens -= cost
49
+ return True
50
+ return False
51
+
52
+
53
+ def make_handler(pipe, limiter: RateLimiter, model_name: str):
54
+ class Handler(BaseHTTPRequestHandler):
55
+ def _json(self, code: int, payload: dict) -> None:
56
+ body = json.dumps(payload, ensure_ascii=False).encode("utf-8")
57
+ self.send_response(code)
58
+ self.send_header("Content-Type", "application/json; charset=utf-8")
59
+ self.send_header("Content-Length", str(len(body)))
60
+ self.end_headers()
61
+ self.wfile.write(body)
62
+
63
+ def log_message(self, fmt, *args): # quiet
64
+ pass
65
+
66
+ def do_GET(self):
67
+ if self.path == "/health":
68
+ self._json(200, {"status": "ok", "model": model_name, "device": str(pipe.device)})
69
+ else:
70
+ self._json(404, {"error": "not found"})
71
+
72
+ def do_POST(self):
73
+ if not limiter.allow():
74
+ self._json(429, {"error": "rate limit exceeded"})
75
+ return
76
+ length = int(self.headers.get("Content-Length") or 0)
77
+ if length > MAX_BODY:
78
+ self._json(413, {"error": "body too large"})
79
+ return
80
+ try:
81
+ payload = json.loads(self.rfile.read(length) or b"{}")
82
+ except Exception:
83
+ self._json(400, {"error": "invalid json"})
84
+ return
85
+ if self.path == "/classify-one":
86
+ text = str(payload.get("text", ""))
87
+ self._json(200, pipe.predict(text))
88
+ elif self.path == "/classify":
89
+ texts = payload.get("texts")
90
+ if not isinstance(texts, list) or not texts or len(texts) > 256:
91
+ self._json(400, {"error": "texts must be a list of 1..256 strings"})
92
+ return
93
+ self._json(200, {"results": pipe.predict_batch([str(t) for t in texts])})
94
+ else:
95
+ self._json(404, {"error": "not found"})
96
+
97
+ return Handler
98
+
99
+
100
+ def main() -> None:
101
+ parser = argparse.ArgumentParser()
102
+ parser.add_argument("--dir", default=os.environ.get("CENSER_DIR", "bundle"))
103
+ parser.add_argument("--port", type=int, default=8000)
104
+ parser.add_argument("--host", default="0.0.0.0")
105
+ parser.add_argument("--device", default=None)
106
+ args = parser.parse_args()
107
+
108
+ import sys
109
+
110
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
111
+ from censer import CenserPipeline
112
+
113
+ pipe = CenserPipeline(args.dir, device=args.device)
114
+ limiter = RateLimiter(float(os.environ.get("RATE_LIMIT_PER_MIN", "600")))
115
+ server = ThreadingHTTPServer((args.host, args.port), make_handler(pipe, limiter, args.dir))
116
+ print(f"censer serve: model={args.dir} device={pipe.device} on http://{args.host}:{args.port}")
117
+ print(" POST /classify {\"texts\": [...]} POST /classify-one {\"text\": ...} GET /health")
118
+ server.serve_forever()
119
+
120
+
121
+ if __name__ == "__main__":
122
+ main()