Instructions to use echan24/mistral-finetune with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use echan24/mistral-finetune with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-v0.1") model = PeftModel.from_pretrained(base_model, "echan24/mistral-finetune") - Notebooks
- Google Colab
- Kaggle
Download handler.py from echan24/mistral-finetune: direct link, hf CLI and curl.
- Browser
- Download file 3.12 kB
-
https://huggingface.co/echan24/mistral-finetune/resolve/main/handler.py
- Command line
-
hf download hf://echan24/mistral-finetune/handler.py
-
curl -L -o handler.py https://huggingface.co/echan24/mistral-finetune/resolve/main/handler.py
3.12 kB
| from typing import Dict, List, Any | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig | |
| from peft import PeftModel, PeftConfig | |
| import torch | |
| class EndpointHandler: | |
| def __init__(self, path="rafikixx/mistral-finetune"): | |
| ''' | |
| this method will be called when starting the Endpoint and will receive 1 argument, | |
| a string with the path to your model weights. This allows you to load your model | |
| correctly | |
| inputs | |
| ------ | |
| path: string with path to model weights, this is required by HuggingFace | |
| https://huggingface.co/docs/inference-endpoints/guides/custom_handler | |
| ''' | |
| # load the model | |
| config = PeftConfig.from_pretrained(path) | |
| bnb_config = BitsAndBytesConfig( | |
| load_in_4bit=True, | |
| bnb_4bit_use_double_quant=True, | |
| bnb_4bit_quant_type="nf4", | |
| bnb_4bit_compute_dtype=torch.bfloat16 | |
| ) | |
| self.model = AutoModelForCausalLM.from_pretrained( | |
| config.base_model_name_or_path, | |
| return_dict=True, | |
| load_in_4bit=True, | |
| device_map={"":0}, | |
| trust_remote_code=True, | |
| quantization_config=bnb_config, | |
| ) | |
| self.tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path) | |
| self.tokenizer.pad_token = self.tokenizer.eos_token | |
| self.model = PeftModel.from_pretrained(self.model, path) | |
| def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]: | |
| ''' | |
| method will be called on every request and receive a dictionary with your request body | |
| as a python dictionary. It will always contain the inputs key. | |
| Args: | |
| data: A dictionary with a inputs key e.g. {'inputs': 'What is NFA Rule 2-2?', | |
| 'parameters': {'max_new_tokens': 64}} | |
| returns a list containing a dictionary | |
| ''' | |
| inputs = data.pop('inputs', data) | |
| parameters = data.pop('parameters', None) | |
| prompt = f'''### Question: | |
| {inputs} | |
| ### Answer: | |
| '''.strip() | |
| batch = self.tokenizer( | |
| prompt, | |
| padding=True, | |
| truncation=True, | |
| return_tensors='pt' | |
| ) | |
| batch = batch.to('cuda:0') | |
| generation_config = self.model.generation_config | |
| generation_config.top_p = 0.7 | |
| generation_config.temperature = 0.7 | |
| generation_config.max_new_tokens = parameters.get('max_new_tokens', 32) | |
| generation_config.num_return_sequences = 1 | |
| generation_config.pad_token_id = self.tokenizer.eos_token_id | |
| generation_config.eos_token_id = self.tokenizer.eos_token_id | |
| with torch.cuda.amp.autocast(): | |
| output_tokens = self.model.generate( | |
| input_ids = batch.input_ids, | |
| generation_config=generation_config, | |
| ) | |
| generated_text = self.tokenizer.decode(output_tokens[0]) | |
| prediction = {'answer': generated_text.split('### Answer:')[1].strip()} | |
| return prediction |