zzz99 commited on
Commit
c33ddba
1 Parent(s): ac2cb50

Upload 2 files

Browse files
Files changed (2) hide show
  1. handler.py +52 -0
  2. requirements.txt +5 -0
handler.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Dict
2
+
3
+ import torch
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+
6
+ from peft import PeftConfig, PeftModel
7
+
8
+
9
+ class EndpointHandler:
10
+ def __init__(self, path=""):
11
+ # load model and processor from path
12
+ self.tokenizer = AutoTokenizer.from_pretrained(path)
13
+ # try:
14
+ config = PeftConfig.from_pretrained(path)
15
+ model = AutoModelForCausalLM.from_pretrained(
16
+ config.base_model_name_or_path,
17
+ # return_dict=True,
18
+ # load_in_8bit=True,
19
+ device_map="auto",
20
+ torch_dtype=torch.float16,
21
+ trust_remote_code=True,
22
+ )
23
+ # model.resize_token_embeddings(len(self.tokenizer))
24
+ model = PeftModel.from_pretrained(model, path)
25
+ # except Exception:
26
+ # model = AutoModelForCausalLM.from_pretrained(
27
+ # path, device_map="auto", load_in_8bit=True, torch_dtype=torch.float16, trust_remote_code=True
28
+ # )
29
+ self.model = model
30
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
31
+
32
+ def __call__(self, data: Dict[str, Any]) -> Dict[str, str]:
33
+ # process input
34
+ inputs = data.pop("inputs", data)
35
+ parameters = data.pop("parameters", None)
36
+ messages=[{ 'role': 'user', 'content': inputs}]
37
+
38
+ # preprocess
39
+ # inputs = self.tokenizer(inputs, return_tensors="pt").to(self.device)
40
+ inputs = self.tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(self.device)
41
+
42
+ # pass inputs with all kwargs in data
43
+ if parameters is not None:
44
+ outputs = self.model.generate(inputs, num_return_sequences=1, eos_token_id=self.tokenizer.eos_token_id, **parameters) # removed max_new_tokens=880, will test and pass in max_new_tokens in config later
45
+ else:
46
+ outputs = self.model.generate(inputs, num_return_sequences=1, eos_token_id=self.tokenizer.eos_token_id)
47
+
48
+ # postprocess the prediction
49
+ prediction = self.tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)
50
+ # prediction = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
51
+
52
+ return [{"generated_text": prediction}]
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ peft
2
+ accelerate
3
+ bitsandbytes
4
+ transformers==4.37.2
5
+ jinja2