shalomma commited on
Commit
23f852d
·
1 Parent(s): d1c7a38

Upload handler.py

Browse files
Files changed (1) hide show
  1. handler.py +30 -0
handler.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import llama
2
+ from typing import Dict, List, Any
3
+
4
+ MODEL = 'decapoda-research/llama-7b-hf'
5
+
6
+
7
+ # MODEL = 'decapoda-research/llama-13b-hf'
8
+ # MODEL = 'decapoda-research/llama-30b-hf'
9
+ # MODEL = 'decapoda-research/llama-65b-hf'
10
+
11
+ class EndpointHandler():
12
+ def __init__(self, path=""):
13
+ self.tokenizer = llama.LLaMATokenizer.from_pretrained(MODEL)
14
+ self.model = llama.LLaMAForCausalLM.from_pretrained(MODEL, low_cpu_mem_usage=True, load_in_8bit=True)
15
+ self.model.to('cuda')
16
+
17
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
18
+ inputs = data.pop("inputs", data)
19
+ parameters = data.pop("parameters", None)
20
+
21
+ input_ids = self.tokenizer(inputs, return_tensors="pt", add_special_tokens=False).input_ids.cuda()
22
+
23
+ if parameters is not None:
24
+ outputs = self.model.generate(input_ids, **parameters)
25
+ else:
26
+ outputs = self.model.generate(input_ids)
27
+
28
+ prediction = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
29
+
30
+ return [{"generated_text": prediction}]