philschmid commited on
Commit
2e65025
·
1 Parent(s): 8dc2154

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +33 -0
handler.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from transformers import pipeline
3
+ import mii
4
+
5
+ class EndpointHandler():
6
+ def __init__(self, path=""):
7
+ self.deploy_name = "bert"
8
+ mii_config = {"dtype": "fp16"}
9
+ mii.deploy(task='text-classification',
10
+ model=path,
11
+ deployment_name=deploy_name,
12
+ mii_config=mii_config)
13
+ # create handler for server
14
+ self.pipeline = mii.mii_query_handle(self.deploy_name)
15
+
16
+
17
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
18
+ """
19
+ data args:
20
+ inputs (:obj: `str`)
21
+ date (:obj: `str`)
22
+ Return:
23
+ A :obj:`list` | `dict`: will be serialized and returned
24
+ """
25
+ # get inputs
26
+ inputs = data.pop("inputs",data)
27
+ date = data.pop("date", None)
28
+ # check if date exists and if it is a holiday
29
+ if date is not None and date in self.holidays:
30
+ return [{"label": "happy", "score": 1}]
31
+ # run normal prediction
32
+ prediction = self.pipeline(inputs)
33
+ return prediction