Download model.py from inclusionAI/LemonDecider-4B-preview: direct link, hf CLI and curl.
- Browser
- Download file 2.87 kB
-
https://huggingface.co/inclusionAI/LemonDecider-4B-preview/resolve/main/model.py
- Command line
-
hf download hf://inclusionAI/LemonDecider-4B-preview/model.py
-
curl -L -o model.py https://huggingface.co/inclusionAI/LemonDecider-4B-preview/resolve/main/model.py
2.87 kB
| # Author:浅斟 | |
| """LemonJev 4B local Python and CLI entrypoint. See QUICKSTART.md.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import decision_runtime as runtime | |
| class DecisionModel: | |
| """Load one decision checkpoint and answer typed questions.""" | |
| def __init__(self, checkpoint=None, device="auto"): | |
| import torch | |
| from transformers import AutoModel | |
| checkpoint = Path(checkpoint or Path(__file__).resolve().parent).resolve() | |
| if device == "auto": | |
| device = "cuda:0" if torch.cuda.is_available() else "cpu" | |
| self.device = device | |
| self.model = AutoModel.from_pretrained( | |
| str(checkpoint), | |
| trust_remote_code=True, | |
| local_files_only=True, | |
| use_safetensors=True, | |
| dtype=torch.bfloat16, | |
| device_map=device, | |
| attn_implementation="sdpa", | |
| ).eval() | |
| self.processor = runtime.load_processor(checkpoint) | |
| self.config = self.model.config | |
| if self.config.decision_protocol != "autojev-v1": | |
| raise ValueError("Unsupported decision protocol") | |
| if tuple(self.model.readout.weight.shape) != ( | |
| self.config.max_options, | |
| self.config.text_config.hidden_size, | |
| ): | |
| raise ValueError("Readout shape does not match configuration") | |
| for code, token_id in zip(self.config.codes, self.config.token_ids, strict=True): | |
| if self.processor.tokenizer.encode(code, add_special_tokens=False) != [token_id]: | |
| raise ValueError("Tokenizer option mapping mismatch") | |
| def predict(self, request): | |
| state, questions = runtime.validate_request(request) | |
| names = list(questions) | |
| rows = [ | |
| {"state": state, "question": questions[name]} for name in names | |
| ] | |
| result = runtime.predict_batch(self.model, self.processor, rows) | |
| return { | |
| "model": self.config.model_id, | |
| "answers": dict(zip(names, result["answers"])), | |
| "temperature": result["temperature"], | |
| "usage": { | |
| "input_tokens": result["input_tokens"], | |
| "output_tokens": 0, | |
| }, | |
| } | |
| def main(): | |
| parser = argparse.ArgumentParser(description="LemonJev 4B decision inference") | |
| parser.add_argument( | |
| "--checkpoint", default=str(Path(__file__).resolve().parent) | |
| ) | |
| parser.add_argument("--device", default="auto") | |
| parser.add_argument("--input", help="JSON request file; defaults to example.json") | |
| args = parser.parse_args() | |
| request = json.loads( | |
| Path(args.input or Path(__file__).with_name("example.json")).read_text() | |
| ) | |
| model = DecisionModel(args.checkpoint, args.device) | |
| print(json.dumps(model.predict(request), ensure_ascii=False, indent=2)) | |
| if __name__ == "__main__": | |
| main() |