AIDSC commited on
Commit
45b837c
1 Parent(s): 45e3df8

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +43 -0
handler.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ import torch
3
+ from accelerate import Accelerator
4
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
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 = AutoModelForSeq2SeqLM.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)[0] 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
+ start_token = self.tokenizer('<pad>', return_tensors="pt").to(self.device)
38
+ outputs = self.model(**inputs, decoder_input_ids=start_token['input_ids'])
39
+ last_token_logits = outputs.logits[:, -1, :]
40
+ options_tokens_logits = last_token_logits[:, self.options_tokens].detach().cpu().numpy()
41
+ conf = softmax(options_tokens_logits[0])
42
+ pred = np.argmax(options_tokens_logits[0])
43
+ return [{"pred": pred, "conf":conf}]