# SPDX-License-Identifier: MIT # Copyright (c) 2026 ThakiCloud """BanCode v2 inference wrapper (CPU or GPU). The tokenizer is NOT part of this repository. It is loaded from jhu-clsp/mmBERT-small at the exact revision pinned in bancode_v2_profile.json and keeps its own terms (see NOTICE.md). python bancode.py "text one" "text two" """ import json import os import sys import torch from transformers import AutoModelForSequenceClassification, AutoTokenizer PROFILE = "bancode_v2_profile.json" def _profile_path(model, revision=None): if os.path.isdir(model): return os.path.join(model, PROFILE) from huggingface_hub import hf_hub_download return hf_hub_download(model, PROFILE, revision=revision) class BanCodeV2: def __init__(self, model=".", revision=None, device="cpu"): with open(_profile_path(model, revision), encoding="utf-8") as f: self.profile = json.load(f) t = self.profile["tokenizer"] self.tokenizer = AutoTokenizer.from_pretrained(t["repo"], revision=t["revision"]) self.model = AutoModelForSequenceClassification.from_pretrained(model, revision=revision).eval().to(device) self.device = device self.code_idx = int(self.model.config.label2id[self.profile["code_label"]]) def logits(self, texts): enc = self.tokenizer(list(texts), truncation=True, max_length=self.profile["max_length"], padding=True, return_tensors="pt").to(self.device) with torch.inference_mode(): return self.model(**enc).logits.float() def p_code(self, texts): return torch.softmax(self.logits(texts), -1)[:, self.code_idx].tolist() def scan(self, prompt): """LLM Guard scanner contract: (sanitized_prompt, is_valid, risk_score). Raw text, no preprocessing.""" if prompt.strip() == "": return prompt, True, -1.0 s, t = self.p_code([prompt])[0], self.profile["threshold"] risk = round((s - t) / (1 - t), 1) if s > t else round((s - t) / t, 1) return prompt, not (s > t), min(max(risk, -1.0), 1.0) if __name__ == "__main__": m = BanCodeV2(sys.argv[1] if len(sys.argv) > 1 else ".") for text in sys.argv[2:] or ["Please summarise the meeting notes from Thursday.", "Run this:\n```bash\npip install requests\n```"]: _, ok, risk = m.scan(text) print(json.dumps({"code_detected": not ok, "risk_score": risk, "text": text[:40]}, ensure_ascii=False))