Young Kim commited on
Commit
36a8565
1 Parent(s): e95227d

Add handler.py

Browse files
Files changed (1) hide show
  1. handler.py +26 -0
handler.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from typing import  Dict, List, Any
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
4
+
5
+ # check for GPU
6
+ device = 0 if torch.cuda.is_available() else -1
7
+
8
+ class EndpointHandler():
9
+     def __init__(self, path=""):
10
+         # load the model
11
+         tokenizer = AutoTokenizer.from_pretrained(path)
12
+         model = AutoModelForSeq2SeqLM.from_pretrained(path ,low_cpu_mem_usage=True)
13
+         # create inference pipeline
14
+         self.pipeline = pipeline("text2text-generation", model=model, tokenizer=tokenizer,device=device)
15
+
16
+     def __call__(self, data: Any) -> List[List[Dict[str, float]]]:
17
+         inputs = data.pop("inputs", data)
18
+         parameters = data.pop("parameters", None)
19
+
20
+         # pass inputs with all kwargs in data
21
+         if parameters is not None:
22
+             prediction = self.pipeline(inputs, **parameters)
23
+         else:
24
+             prediction = self.pipeline(inputs)
25
+         # postprocess the prediction
26
+         return prediction