ashercn97 commited on
Commit
2e67299
·
verified ·
1 Parent(s): 9c92e03

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +29 -0
handler.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from transformers import pipeline
3
+ import holidays
4
+
5
+
6
+ class EndpointHandler:
7
+ def __init__(self, path=""):
8
+ self.pipeline = pipeline("text-generation", model=path)
9
+
10
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
11
+ """
12
+ data args:
13
+ inputs (:obj: `str`)
14
+ date (:obj: `str`)
15
+ Return:
16
+ A :obj:`list` | `dict`: will be serialized and returned
17
+ """
18
+ # get inputs
19
+ template = """
20
+ INSTRUCTION
21
+ {}
22
+ RESPONSE
23
+ {}
24
+ """
25
+ instruction = data.pop("instruction", data)
26
+ inputs = template.format(instruction, "")
27
+ # run normal prediction
28
+ prediction = self.pipeline(inputs)
29
+ return prediction