Spaces:
Sleeping
Sleeping
Download ChatMixtral.py from Peyrusaubes/test: direct link, hf CLI and curl.
- Browser
- Download file 875 Bytes
-
https://huggingface.co/spaces/Peyrusaubes/test/resolve/main/ChatMixtral.py
- Command line
-
hf download hf://spaces/Peyrusaubes/test/ChatMixtral.py
-
curl -L -o ChatMixtral.py https://huggingface.co/spaces/Peyrusaubes/test/resolve/main/ChatMixtral.py
875 Bytes
| from transformers import AutoModelForCausalLM | |
| from transformers import AutoTokenizer | |
| class ChatMixtral: | |
| def __init__(self): | |
| model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1" | |
| self.model = AutoModelForCausalLM.from_pretrained( | |
| model_id, device_map="auto", load_in_4bit=True | |
| ) | |
| self.tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1", padding_side="left") | |
| self.device = "cuda" if torch.cuda.is_available() else "cpu" | |
| print("using device:", self.device) | |
| def ask(self, query: str): | |
| model_inputs = self.tokenizer(["A list of colors: red, blue"], return_tensors="pt").to("cuda") | |
| generated_ids = self.model.generate(**model_inputs) | |
| return self.tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] | |
| def clear(self): | |
| printf("cleaning") | |