Download chat_runtime.py from Astra228/ChudGPT-Femboy: direct link, hf CLI and curl.
- Browser
- Download file 1.33 kB
-
https://huggingface.co/Astra228/ChudGPT-Femboy/resolve/main/chat_runtime.py
- Command line
-
hf download hf://Astra228/ChudGPT-Femboy/chat_runtime.py
-
curl -L -o chat_runtime.py https://huggingface.co/Astra228/ChudGPT-Femboy/resolve/main/chat_runtime.py
1.33 kB
| import json | |
| from pathlib import Path | |
| import torch | |
| from tokenizers import Tokenizer | |
| from safetensors.torch import load_file | |
| from model import Config, Model, generate | |
| from conversation import conversation_prompt | |
| class Engine: | |
| def __init__(self, checkpoint=None, device='cuda', allow_development=True): | |
| root=Path(__file__).resolve().parent | |
| torch.set_num_threads(2) | |
| if device=='cuda' and not torch.cuda.is_available(): | |
| device='cpu' | |
| self.device=device | |
| cfg=json.loads((root/'config.json').read_text(encoding='utf-8')) | |
| self.model=Model(Config(**cfg['architecture'])) | |
| self.model.load_state_dict(load_file(str(root/'model.safetensors'))) | |
| self.model.to(device).eval() | |
| self.tok=Tokenizer.from_file(str(root/'tokenizer.json')) | |
| def reply(self,messages,max_tokens=96): | |
| ids=conversation_prompt(self.tok,messages,self.model.config.context,max_tokens) | |
| result=generate(self.model,torch.tensor([ids],device=self.device),max_tokens=max_tokens,temperature=0, | |
| eos=self.tok.token_to_id('<eos>'),stop_ids=[[self.tok.token_to_id('<'+r+'>')] for r in ['user','system','assistant']], | |
| valid_vocab_size=self.tok.get_vocab_size()) | |
| return self.tok.decode(result,skip_special_tokens=True).strip() | |