arthur-stackadoc-com commited on
Commit
83b1cb8
1 Parent(s): ada0c23

added handler

Browse files
Files changed (2) hide show
  1. call_handler.py +14 -0
  2. handler.py +24 -0
call_handler.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from handler import EndpointHandler
2
+
3
+ # init handler
4
+ my_handler = EndpointHandler(path=".")
5
+
6
+ # prepare sample payload
7
+ non_holiday_payload = {"inputs": "I am quite excited how this will turn out", }
8
+ holiday_payload = {"inputs": "Today is a though day"}
9
+
10
+ # test the handler
11
+ non_holiday_pred=my_handler(non_holiday_payload)
12
+
13
+ # show results
14
+ print("holiday_payload", holiday_payload)
handler.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from transformers import ClapModel, ClapProcessor
3
+ import gc
4
+
5
+ class EndpointHandler:
6
+ def __init__(self, path=""):
7
+ model_name = "laion/larger_clap_general"
8
+ self.model = ClapModel.from_pretrained(model_name)
9
+ self.processor = ClapProcessor.from_pretrained(model_name)
10
+
11
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
12
+ """
13
+ data args:
14
+ inputs (:obj: `str`)
15
+ date (:obj: `str`)
16
+ Return:
17
+ A :obj:`list` | `dict`: will be serialized and returned
18
+ """
19
+ print(type(data))
20
+ query = data['inputs']
21
+ text_inputs = self.processor(text=query, return_tensors="pt")
22
+ text_embed = self.model.get_text_features(**text_inputs)[0]
23
+ gc.collect()
24
+ return text_embed