MichaelAI23 commited on
Commit
2a339d3
·
1 Parent(s): 4d9d623

Add handler and custom requirements.txt

Browse files
Files changed (2) hide show
  1. handler.py +38 -0
  2. requirements.txt +2 -0
handler.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, Any
2
+ from transformers import AutoModelForCausalLM, BitsAndBytesConfig
3
+ import torch
4
+
5
+ class EndpointHandler:
6
+ def __init__(self, path=""):
7
+ model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
8
+ quantization_config = BitsAndBytesConfig(
9
+ load_in_4bit=True,
10
+ bnb_4bit_compute_dtype=torch.float16
11
+ )
12
+ # load model and processor from path
13
+ self.model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=quantization_config)
14
+
15
+ def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
16
+ """
17
+ Args:
18
+ data (:dict:):
19
+ The payload with the text prompt and generation parameters.
20
+ """
21
+ # process input
22
+ inputs = data.pop("inputs", data)
23
+ parameters = data.pop("parameters", None)
24
+ inputs = f"[INST] {inputs} [/INST]"
25
+
26
+ # preprocess
27
+ input_ids = self.tokenizer(inputs, return_tensors="pt").input_ids
28
+
29
+ # pass inputs with all kwargs in data
30
+ if parameters is not None:
31
+ outputs = self.model.generate(input_ids, **parameters)
32
+ else:
33
+ outputs = self.model.generate(input_ids)
34
+
35
+ # postprocess the prediction
36
+ prediction = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
37
+
38
+ return [{"generated_text": prediction}]
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ git+https://github.com/huggingface/transformers
2
+ bitsandbytes