yamura4 commited on
Commit
4a403c5
·
verified ·
1 Parent(s): 81c417d

Use the combined main + char-ngram gate for pure v5

Browse files
Files changed (1) hide show
  1. gate.py +29 -4
gate.py CHANGED
@@ -11,8 +11,11 @@ from pathlib import Path
11
  import numpy as np
12
  import onnxruntime as ort
13
  from tokenizers import Tokenizer
 
14
 
15
  DEFAULT_THRESHOLD = 0.94
 
 
16
  MAX_LEN = 128
17
  CHAR_LEN = 256
18
 
@@ -28,12 +31,21 @@ class SendGate:
28
  def __init__(self, model_path: str | Path = "model.onnx",
29
  tokenizer_path: str | Path = "tokenizer.json",
30
  threshold: float = DEFAULT_THRESHOLD,
 
 
31
  providers: list[str] | None = None):
32
  self.tok = Tokenizer.from_file(str(tokenizer_path))
33
  self.sess = ort.InferenceSession(
34
  str(model_path), providers=providers or ["CPUExecutionProvider"]
35
  )
36
  self.threshold = threshold
 
 
 
 
 
 
 
37
 
38
  def _encode(self, text: str):
39
  ids = self.tok.encode(text).ids[:MAX_LEN]
@@ -46,7 +58,7 @@ class SendGate:
46
  return (np.array([ids], dtype=np.int64), np.array([mask], dtype=np.int64),
47
  np.array([chars], dtype=np.int64), np.array([cmask], dtype=np.int64))
48
 
49
- def score(self, text: str) -> float:
50
  ids, mask, chars, cmask = self._encode(text)
51
  logit = self.sess.run(None, {
52
  "input_ids": ids,
@@ -56,7 +68,20 @@ class SendGate:
56
  })[0].flatten()[0]
57
  return float(1.0 / (1.0 + np.exp(-logit)))
58
 
 
 
 
 
 
 
 
 
59
  def check(self, text: str) -> Result:
60
- p = self.score(text)
61
- return Result(blocked=p >= self.threshold, score=p,
62
- reason="model" if p >= self.threshold else None)
 
 
 
 
 
 
11
  import numpy as np
12
  import onnxruntime as ort
13
  from tokenizers import Tokenizer
14
+ from sklearn.feature_extraction.text import HashingVectorizer
15
 
16
  DEFAULT_THRESHOLD = 0.94
17
+ DEFAULT_NGRAM_THRESHOLD = 0.95
18
+ NGRAM_FEATURES = 2 ** 18
19
  MAX_LEN = 128
20
  CHAR_LEN = 256
21
 
 
31
  def __init__(self, model_path: str | Path = "model.onnx",
32
  tokenizer_path: str | Path = "tokenizer.json",
33
  threshold: float = DEFAULT_THRESHOLD,
34
+ ngram_threshold: float = DEFAULT_NGRAM_THRESHOLD,
35
+ ngram_model_path: str | Path = "ngram_model.npz",
36
  providers: list[str] | None = None):
37
  self.tok = Tokenizer.from_file(str(tokenizer_path))
38
  self.sess = ort.InferenceSession(
39
  str(model_path), providers=providers or ["CPUExecutionProvider"]
40
  )
41
  self.threshold = threshold
42
+ self.ngram_threshold = ngram_threshold
43
+ self.vec = HashingVectorizer(analyzer="char", ngram_range=(2, 5),
44
+ n_features=NGRAM_FEATURES,
45
+ alternate_sign=False, norm=None)
46
+ z = np.load(str(ngram_model_path))
47
+ self.ngram_coef = z["coef"]
48
+ self.ngram_intercept = float(z["intercept"])
49
 
50
  def _encode(self, text: str):
51
  ids = self.tok.encode(text).ids[:MAX_LEN]
 
58
  return (np.array([ids], dtype=np.int64), np.array([mask], dtype=np.int64),
59
  np.array([chars], dtype=np.int64), np.array([cmask], dtype=np.int64))
60
 
61
+ def main_score(self, text: str) -> float:
62
  ids, mask, chars, cmask = self._encode(text)
63
  logit = self.sess.run(None, {
64
  "input_ids": ids,
 
68
  })[0].flatten()[0]
69
  return float(1.0 / (1.0 + np.exp(-logit)))
70
 
71
+ def ngram_score(self, text: str) -> float:
72
+ x = self.vec.transform([text])
73
+ logit = float(x @ self.ngram_coef + self.ngram_intercept)
74
+ return float(1.0 / (1.0 + np.exp(-logit)))
75
+
76
+ def score(self, text: str) -> float:
77
+ return max(self.main_score(text), self.ngram_score(text))
78
+
79
  def check(self, text: str) -> Result:
80
+ main = self.main_score(text)
81
+ ngram = self.ngram_score(text)
82
+ blocked = main >= self.threshold or ngram >= self.ngram_threshold
83
+ if blocked:
84
+ reason = "main" if main >= self.threshold else "ngram"
85
+ else:
86
+ reason = None
87
+ return Result(blocked=blocked, score=max(main, ngram), reason=reason)