Text Classification
Transformers
Safetensors
Japanese
English
llama
text-generation
decision-model
typed-decisions
systemone
saihai
custom-code
custom_code
text-embeddings-inference
Run this model with HF Jobs
Run hotchpotch/saihai-v1-8b on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/hotchpotch/saihai-v1-8b. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/hotchpotch/saihai-v1-8b on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use hotchpotch/saihai-v1-8b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use hotchpotch/saihai-v1-8b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="hotchpotch/saihai-v1-8b", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("hotchpotch/saihai-v1-8b", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("hotchpotch/saihai-v1-8b", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Add standalone AutoModel System One inference and usage instructions
Browse filesFiles changed (4)
- README.md+25-13
- config.json+4-1
- inference_saihai.pyAdded+373
- release-manifest.json+15-5
11 unmodified lines121314151617181920212223242526272829303132333435363711 unmodified lines4950515253545556575811 unmodified linesA text-only Typed Decision Model based on LLM-jp 4.1 8B. It returns Noul(probability of true), Choice (candidate probabilities), and Score (an expectedrubric level). The release contains a LoRA-merged BF16 model and a self-containedPython package. Inference reads candidate logits without generating reasoningor free-form answers.## PythonUse Python 3.12+ and a CUDA GPU with room for approximately 17.2 GB of weightsplus inference memory. Authenticate with an account that can access this privaterepository, then download and install the included package:```shhf auth loginhf download hotchpotch/saihai-v1-8b --local-dir ./saihai-v1-8bpip install './saihai-v1-8b[inference]'``````pythonfrom saihai import Saihaimodel = Saihai.from_pretrained("./saihai-v1-8b", device="cuda")response = model.systemone({"state": "決済が失敗しています。至急対応してください。","questions": {11 unmodified linesprint(response["answers"])```After installation, loading from the Hub also works:`Saihai.from_pretrained("hotchpotch/saihai-v1-8b", token=True, revision="main")`.Pin a commit revision for reproducibility. A single question can use`model.predict(state, question)`.## Interface and limits11 unmodified lines1213141516171819202122232425262728293031323334353637383940414211 unmodified lines545556575859606162636465666768697011 unmodified linesA text-only Typed Decision Model based on LLM-jp 4.1 8B. It returns Noul(probability of true), Choice (candidate probabilities), and Score (an expectedrubric level). The release contains a LoRA-merged BF16 model and a standaloneTransformers remote-code runtime (plus the original Python package). Inference reads candidate logits without generating reasoningor free-form answers.## PythonUse Python 3.12+ and a CUDA GPU with room for approximately 17.2 GB of weightsplus inference memory. The standalone `inference_saihai.py` runs without thesaihai package or training code. Authenticate for access to this private model.```shhf auth loginpip install torch 'transformers==5.17.0' 'tokenizers>=0.23,<0.24' 'huggingface-hub>=1.0,<2' 'accelerate>=1.0,<2'``````pythonfrom transformers import AutoModelmodel = AutoModel.from_pretrained("hotchpotch/saihai-v1-8b",trust_remote_code=True,token=True,device_map="auto",dtype="auto",)response = model.systemone({"state": "決済が失敗しています。至急対応してください。","questions": {11 unmodified linesprint(response["answers"])```Pin `revision="<full commit hash>"` for reproducibility; the loader uses thatrevision for code, weights, tokenizer and decision contract. A single questioncan use `model.predict(state, question)`. `AutoModel` exposes the decision API;`AutoModelForCausalLM` still loads the standard backbone.For a downloaded export, import `SaihaiModel` from `inference_saihai` and call`SaihaiModel.from_pretrained("./saihai-v1-8b", device="cuda")`. This file mustremain standalone: no saihai installation or adjacent Python files are required.Isolated-process tests block saihai imports and verify standalone inferenceagainst the package implementation. The original included package interface(`from saihai import Saihai`) remains available after package installation.## Interface and limits
30 unmodified lines313233343530 unmodified lines"tie_word_embeddings": false,"transformers_version": "5.17.0","use_cache": true,"vocab_size": 196608}30 unmodified lines313233343536373830 unmodified lines"tie_word_embeddings": false,"transformers_version": "5.17.0","use_cache": true,"vocab_size": 196608,"auto_map": {"AutoModel": "inference_saihai.SaihaiModel"}}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373"""Standalone saihai inference for Transformers AutoModel remote-code loading.
This file MUST run independently, copied unchanged into a model export. Neverimport saihai, its training code, or sibling files. Required dependencies arePyTorch, Transformers, tokenizers, huggingface-hub and (for device_map) accelerate.Keep rendering and response mapping aligned with protocol.py / inference.py;isolated-process tests verify independence and numerical parity."""
import jsonimport mathfrom pathlib import Path
import torchfrom tokenizers import Tokenizerfrom transformers import LlamaConfig, PreTrainedModel, Qwen3_5Configfrom transformers.utils.hub import cached_file
def canonical_json(value): """Match minja's six-significant-digit float JSON representation explicitly.""" if value is None: return "null" if isinstance(value, bool): return "true" if value else "false" if isinstance(value, str): return json.dumps(value, ensure_ascii=False) if isinstance(value, int): return str(value) if isinstance(value, float): if not math.isfinite(value): raise ValueError("Nonfinite input number") return format(value, ".6g") if isinstance(value, list): return "[" + ", ".join(canonical_json(x) for x in value) + "]" if isinstance(value, dict) and all(isinstance(k, str) for k in value): return ( "{" + ", ".join(canonical_json(k) + ": " + canonical_json(v) for k, v in value.items()) + "}" ) raise ValueError("Input must contain JSON values with string object keys")
def render_input(inp, contract): kind = inp["type"] count = len(inp["options"]) if kind not in ("choice", "noul", "score"): raise ValueError("Unsupported decision type") minimum = 1 if kind == "choice" else 2 if count < minimum or (kind == "noul" and count != 2) or (kind == "score" and count > 10): raise ValueError("Invalid native candidate count") if kind == "noul" and [o["label"] for o in inp["options"]] != ["false", "true"]: raise ValueError("Native Noul requires false then true") options = [] if len(inp["options"]) > len(contract["codes"]): raise ValueError("Native candidate capacity exceeded") for code, o in zip(contract["codes"], inp["options"]): item = {"code": code, "label": o["label"], "definition": o["description"]} if inp["type"] == "score": value = o["value"] if ( isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value) ): raise ValueError("Score values must be finite numbers") item["numeric_value"] = value options.append(item) return ( contract["prompt_prefix"] + canonical_json( { "instruction": inp["instructions"], "state": inp["state"], "type": inp["type"], "options": options, } ) + contract["prompt_suffix"] )
class DecisionInterface: """Shared renderer and llama.cpp-style System One response mapping."""
def __init__(self, contract): self.contract_path = Path(contract) if isinstance(contract, (str, Path)) else None self.contract = ( json.loads(self.contract_path.read_text()) if self.contract_path else contract ) ids = self.contract["code_token_ids"] if not ids or len(ids) != len(set(ids)) or len(ids) != len(self.contract["codes"]): raise ValueError("Invalid candidate codebook")
def render(self, inp): if self.contract.get("prompt_version") == "saihai-pplx-json-v1": return render_input(inp, self.contract) options = inp["options"] minimum = 1 if inp["type"] == "choice" else 2 if not minimum <= len(options) <= len(self.contract["codes"]): raise ValueError("Candidate count exceeds contract bounds") if inp["type"] not in {"choice", "noul", "score"}: raise ValueError("Unsupported decision type") mapped = [] for code, option in zip(self.contract["codes"], options): item = {"code": code, "label": option["label"], "definition": option["description"]} if "value" in option: value = option["value"] if ( isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value) ): raise ValueError("Score values must be finite numbers") item["numeric_value"] = value if inp["type"] == "score" and "value" not in option: raise ValueError("Score options require numeric values") mapped.append(item) payload = { "instruction": inp["instructions"], "state": inp["state"], "type": inp["type"], "options": mapped, } return ( self.contract["prompt_prefix"] + json.dumps(payload, ensure_ascii=False, allow_nan=False) + self.contract["prompt_suffix"] )
def systemone(self, request): """Accept TypeSafe-style criteria plus an exact-input options extension.""" if request.get("images") or request.get("videos"): raise ValueError("This model is text-only") state, questions = request["state"], request["questions"] if "model" in request and request["model"] != self.contract["model_id"]: raise ValueError("Requested model does not match the loaded model") if not isinstance(questions, dict) or not 1 <= len(questions) <= 64: raise ValueError("Expected 1..64 named questions") answers, input_tokens, backend_output_tokens = {}, 0, 0 for qid, q in questions.items(): if not isinstance(qid, str) or not qid: raise ValueError("Question IDs must be nonempty strings") kind = q["type"] if "options" in q: options = q["options"] keys = [option["id"] for option in options] elif kind == "choice": criteria = q["criteria"] if not isinstance(criteria, dict): raise ValueError("Choice criteria must be an ordered object") keys = list(criteria) options = [{"label": key, "description": criteria[key]} for key in keys] elif kind == "noul": criteria = q.get("criteria") or {} keys = ["false", "true"] options = [ { "label": key, "description": key if criteria.get(key) is None else criteria[key], } for key in keys ] elif kind == "score": criteria = q["criteria"] if not isinstance(criteria, list): raise ValueError("Score criteria must be an array") keys = [str(i) for i in range(len(criteria))] options = [ { "label": f"option_{i}", "description": value["description"] if isinstance(value, dict) and "value" in value else value, "value": value["value"] if isinstance(value, dict) and "value" in value else i, } for i, value in enumerate(criteria) ] else: raise ValueError("Unsupported decision type") if len(keys) != len(set(keys)) or any(not isinstance(key, str) for key in keys): raise ValueError("Option IDs must be unique strings") if kind == "noul" and set(keys) != {"false", "true"}: raise ValueError("Noul requires false and true options") result = self.predict( { "state": state, "type": kind, "instructions": q.get("instructions", qid), "options": options, } ) ps = result["probabilities"] input_tokens += result["input_tokens"] backend_output_tokens += result.get("backend_output_tokens", 1) answer = {"type": kind} if kind == "noul": answer["noul"] = ps[keys.index("true")] else: answer["probabilities"] = dict(zip(keys, ps, strict=True)) if kind == "choice": mode = max(range(len(ps)), key=ps.__getitem__) answer["choice"] = keys[mode] answer["confidence"] = ( 1.0 if len(ps) == 1 else max(0.0, (ps[mode] - 1 / len(ps)) / (1 - 1 / len(ps))) ) else: mode = max(range(len(ps)), key=ps.__getitem__) distance = sum(p * abs(i - mode) for i, p in enumerate(ps)) uniform_distance = sum( abs(i - (len(ps) - 1) / 2) for i in range(len(ps)) ) / len(ps) answer["confidence"] = max(0.0, 1 - distance / uniform_distance) answer["score"] = sum(i * p for i, p in enumerate(ps)) answer["legend"] = { key: option["description"] for key, option in zip(keys, options, strict=True) } answer["expected_value"] = sum( option["value"] * p for option, p in zip(options, ps, strict=True) ) answer["numeric_values"] = dict( zip(keys, [o["value"] for o in options], strict=True) ) answers[qid] = answer return { "answers": answers, "usage": {"input_tokens": input_tokens, "output_tokens": 0}, "backend_usage": {"output_tokens": backend_output_tokens}, "model": self.contract["model_id"], }
class SaihaiModel(PreTrainedModel): """Typed decision wrapper; standard backbone weights retain their original names."""
config_class = LlamaConfig _supports_sdpa = True
def __init__(self, model, tokenizer, contract): super().__init__(model.config) self.model = model self.tokenizer = tokenizer self.interface = DecisionInterface(contract) self.interface.predict = self._predict_input ids = contract["code_token_ids"] if any(not isinstance(i, int) or not 0 <= i < model.lm_head.out_features for i in ids): raise ValueError("Candidate token IDs exceed model vocabulary") if contract["max_input_tokens"] > model.config.get_text_config().max_position_embeddings: raise ValueError("Contract context limit exceeds model positional capacity") self.token_ids = ids
@classmethod def from_pretrained( cls, pretrained_model_name_or_path, *model_args, config=None, revision="main", token=None, cache_dir=None, local_files_only=False, force_download=False, subfolder="", device=None, dtype=None, device_map=None, attn_implementation="sdpa", **kwargs, ): """Load a local export or Hub revision without installing the saihai package.""" from transformers import AutoConfig, LlamaForCausalLM, Qwen3_5ForConditionalGeneration
hub = dict( revision=revision, token=token, cache_dir=cache_dir, local_files_only=local_files_only, force_download=force_download, subfolder=subfolder, ) if config is None: config = AutoConfig.from_pretrained(pretrained_model_name_or_path, **hub) # AutoModel resolves config first. Reuse that commit for contract, tokenizer # and weights so a moving branch cannot mix artifacts from different releases. if getattr(config, "_commit_hash", None): hub["revision"] = config._commit_hash contract_path = cached_file(pretrained_model_name_or_path, "decision-inference.json", **hub) contract = json.loads(Path(contract_path).read_text()) tokenizer_file = contract["tokenizer_file"] if Path(tokenizer_file).is_absolute() or ".." in Path(tokenizer_file).parts: raise ValueError("Tokenizer must be a file within the model export") tokenizer = Tokenizer.from_file( cached_file(pretrained_model_name_or_path, tokenizer_file, **hub) ) if config.model_type == "llama": backbone_cls = LlamaForCausalLM elif config.model_type == "qwen3_5": backbone_cls = Qwen3_5ForConditionalGeneration else: raise ValueError(f"Unsupported backbone: {config.model_type}") if device is not None and device_map is not None: raise ValueError("Specify device or device_map, not both") if device is not None: device_map = {"": str(device)} if dtype is None: dtype = kwargs.pop("torch_dtype", "auto") for name in ( "trust_remote_code", "_from_auto", "adapter_kwargs", "_commit_hash", "code_revision", ): kwargs.pop(name, None) model = backbone_cls.from_pretrained( pretrained_model_name_or_path, *model_args, config=config, dtype=dtype, device_map=device_map, attn_implementation=attn_implementation, **hub, **kwargs, ).eval() return cls(model, tokenizer, contract).eval()
def forward(self, *args, **kwargs): """Delegate ordinary tensor forward calls to the exported backbone.""" return self.model(*args, **kwargs)
def _predict_input(self, inp): text = self.interface.render(inp) ids = self.tokenizer.encode(text, add_special_tokens=False).ids if not ids or len(ids) > self.interface.contract["max_input_tokens"]: raise ValueError("Input exceeds contract context limit; refusing truncation") inputs = torch.tensor([ids], device=self.model.get_input_embeddings().weight.device) with torch.inference_mode(): backbone = self.model.model if hasattr(backbone, "language_model"): backbone = backbone.language_model hidden = backbone( input_ids=inputs, attention_mask=torch.ones_like(inputs), use_cache=False ).last_hidden_state[:, -1:, :] logits = self.model.lm_head(hidden)[0, 0, self.token_ids].float() probabilities = logits[: len(inp["options"])].softmax(-1).cpu().tolist() return { "probabilities": probabilities, "input_tokens": len(ids), "backend_output_tokens": 0, }
def systemone(self, request): return self.interface.systemone(request)
def predict(self, state, question): return self.systemone({"state": state, "questions": {"decision": question}})["answers"][ "decision" ]
def save_pretrained(self, *args, **kwargs): raise NotImplementedError("Export the original backbone with the saihai export helper")
class SaihaiQwenModel(SaihaiModel): """The same standalone decision interface for Qwen3.5 exports."""
config_class = Qwen3_5Config42 unmodified lines43444546474849504 unmodified lines555657585960616262 unmodified lines12512612712812913013113242 unmodified lines"bytes": 4177},"config.json": {"sha256": "a386cef2c6bb34bca68580b2977358bff51901b906c2c8b4094b8287e5344655","bytes": 747},"training-provenance.json": {"sha256": "453f54d3f2caf67dbd0c0b7673df4b4f232119168620e29651182c6393aadfe3",4 unmodified lines"bytes": 1178},"README.md": {"sha256": "4d704c8bdff38b7ac99a8822aa10f2daed6ee71bc51cd91e3142b8cb0c0b3ed1","bytes": 5897},"generation_config.json": {"sha256": "1e6b0601f84e78ccff0144efd7d07244fe5217a16bcb3a0abb0a340dd845fcbe",62 unmodified lines"saihai/templates/systemone_pplx26.jinja": {"sha256": "a5464a20e1dc7018a25af94c66153d2b6e66af6849de9982ead5514ee3eed8b1","bytes": 2576}},"inference_max_input_tokens": 65536,"training_max_input_tokens": 8192}42 unmodified lines43444546474849504 unmodified lines555657585960616262 unmodified lines12512612712812913013113213313413513613713813914014114242 unmodified lines"bytes": 4177},"config.json": {"bytes": 816,"sha256": "f315b0158e3107ff9b23f90f39f648d128b6390ecaf83791b7862f5e19fef798"},"training-provenance.json": {"sha256": "453f54d3f2caf67dbd0c0b7673df4b4f232119168620e29651182c6393aadfe3",4 unmodified lines"bytes": 1178},"README.md": {"bytes": 6595,"sha256": "7a07c598283cb48c2bf383b6cbd9ca25269ff336cc602fc092674d5696cd9ae9"},"generation_config.json": {"sha256": "1e6b0601f84e78ccff0144efd7d07244fe5217a16bcb3a0abb0a340dd845fcbe",62 unmodified lines"saihai/templates/systemone_pplx26.jinja": {"sha256": "a5464a20e1dc7018a25af94c66153d2b6e66af6849de9982ead5514ee3eed8b1","bytes": 2576},"inference_saihai.py": {"bytes": 15973,"sha256": "f44bc6fb22336a88bcd092427aea25f669c624ee5696e74dfeca7d88cd6bb846"}},"inference_max_input_tokens": 65536,"training_max_input_tokens": 8192,"standalone_inference": {"entry_point": "inference_saihai.SaihaiModel","auto_class": "AutoModel","previous_revision": "c8aa3bbb015471496dbec3a7db2fef86581bd3cf","weights_changed": false}}
README.md+25-13
config.json+4-1
inference_saihai.pyAdded+373
release-manifest.json+15-5