danbigeffect commited on
Commit
bde033d
·
verified ·
1 Parent(s): 8b9204d

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +25 -0
handler.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModel, AutoFeatureExtractor
2
+ import torch
3
+
4
+ class EndpointHandler:
5
+ def __init__(self, model_dir):
6
+ # This path (model_dir) is where HF automatically places the files
7
+ self.model = AutoModel.from_pretrained(model_dir, trust_remote_code=True)
8
+ self.feature_extractor = AutoFeatureExtractor.from_pretrained(model_dir, trust_remote_code=True)
9
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10
+ self.model.to(self.device)
11
+ self.model.eval()
12
+
13
+ def __call__(self, data):
14
+ # 'data' is the dictionary from the POST request body
15
+ inputs = data.get("inputs")
16
+
17
+ # Preprocess
18
+ processed = self.feature_extractor(inputs, return_tensors="pt", sampling_rate=16000).to(self.device)
19
+
20
+ # Inference
21
+ with torch.no_grad():
22
+ outputs = self.model(**processed)
23
+
24
+ # Return result
25
+ return outputs.last_hidden_state.mean(dim=1).cpu().numpy().tolist()