AIDSC commited on
Commit
192aae2
1 Parent(s): f5db02d

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +42 -0
handler.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ import torch
3
+ from accelerate import Accelerator
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM
5
+ import numpy as np
6
+
7
+
8
+ def softmax(x):
9
+ z = x - max(x)
10
+ numerator = np.exp(z)
11
+ denominator = np.sum(numerator)
12
+ softmax = numerator/denominator
13
+ return softmax
14
+
15
+ class EndpointHandler():
16
+ def __init__(self, path=""):
17
+ self.accelerator = Accelerator()
18
+ self.device = self.accelerator.device
19
+ self.model = AutoModelForCausalLM.from_pretrained(path, trust_remote_code=True, device_map="auto")
20
+ self.model = self.accelerator.prepare(self.model)
21
+ self.tokenizer = AutoTokenizer.from_pretrained(path)
22
+ self.options_tokens = [self.tokenizer.encode(choice)[-1] for choice in ["A", "B", "C", "D"]]
23
+
24
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
25
+ """
26
+ data args:
27
+ inputs (:obj: `str` | `PIL.Image` | `np.array`)
28
+ kwargss
29
+ Return:
30
+ A :obj:`list` | `dict`: will be serialized and returned
31
+ """
32
+ with torch.no_grad():
33
+ prompt = data.pop("prompt")
34
+ inputs = self.tokenizer(prompt, return_tensors="pt").to(self.device)
35
+ input_size = inputs['input_ids'].size(1)
36
+ input_ids = inputs["input_ids"].to(self.device)
37
+ outputs = self.model(**inputs)
38
+ last_token_logits = outputs.logits[:, -1, :]
39
+ options_tokens_logits = last_token_logits[:, self.options_tokens].detach().cpu().numpy()
40
+ conf = softmax(options_tokens_logits[0])
41
+ pred = np.argmax(options_tokens_logits[0])
42
+ return [{"pred": pred, "conf":conf}]