roemmele commited on
Commit
3c54df5
·
1 Parent(s): 6559271

Upload handler.py

Browse files
Files changed (1) hide show
  1. handler.py +20 -0
handler.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ from typing import Dict, List, Any
3
+
4
+
5
+ class EndpointHandler():
6
+ def __init__(self, path=""):
7
+ self.model = AutoModelForCausalLM.from_pretrained(path,
8
+ trust_remote_code=True)
9
+ self.tokenizer = AutoTokenizer.from_pretrained(path)
10
+
11
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
12
+ input_text = data.pop("inputs", data)
13
+ inputs = self.tokenizer(input_text,
14
+ return_tensors="pt")
15
+ input_ids = inputs.input_ids
16
+ attention_mask = inputs.attention_mask
17
+ score = self.model(input_ids=input_ids,
18
+ attention_mask=attention_mask,
19
+ labels=input_ids).loss.item()
20
+ return score