File size: 1,414 Bytes
dbfd080
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import json
import sys

import numpy as np
import onnxruntime as ort
import sentencepiece as spm
from huggingface_hub import hf_hub_download

REPO = "Prosperitytech/lighthouse-keeper"

cfg = json.load(open(hf_hub_download(REPO, "config.json")))
sp = spm.SentencePieceProcessor(model_file=hf_hub_download(REPO, "keeper.spm"))
session = ort.InferenceSession(hf_hub_download(REPO, "keeper.onnx"), providers=["CPUExecutionProvider"])


def score(text):
    ids = [cfg["bos_id"]] + sp.encode(text)[: cfg["max_tokens"] - 1]
    probs = session.run(None, {"input_ids": np.array([ids], dtype=np.int64)})[0][0]
    return {cat: dict(zip(cfg["levels"], map(float, p))) for cat, p in zip(cfg["categories"], probs)}


def flag(text, min_risk=cfg["suggested_min_risk"]):
    priority = cfg["tie_break_order"]
    best = None
    for cat, p in score(text).items():
        level = max(p, key=p.get)
        risk = 1 - p["none"]
        if level != "none" and risk >= min_risk:
            key = (cfg["levels"].index(level), p[level] + 0.06 * (1 - priority.index(cat) / len(priority)))
            if best is None or key > best[1]:
                best = (cat, key, p[level])
    return None if best is None else {"category": best[0], "level": cfg["levels"][best[1][0]], "confidence": round(best[2], 3)}


if __name__ == "__main__":
    print(flag(" ".join(sys.argv[1:]) or "send me your BVN make I help you collect the money"))