- add custom endpoint handler
Browse files- handler.py +36 -0
handler.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
|
3 |
+
import torch as torch
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
|
7 |
+
class EndpointHandler():
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
def __init__(self, path=""):
|
12 |
+
device = 0 if torch.cuda.is_available() else "cpu"
|
13 |
+
self.pipe = pipeline(
|
14 |
+
task="automatic-speech-recognition",
|
15 |
+
model="openai/whisper-large",
|
16 |
+
chunk_length_s=30,
|
17 |
+
device=device,
|
18 |
+
)
|
19 |
+
self.pipe.model.config.forced_decoder_ids = self.pipe.tokenizer.get_decoder_prompt_ids(language="nl", task="transcribe")
|
20 |
+
|
21 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
22 |
+
"""
|
23 |
+
data args:
|
24 |
+
inputs (:obj: `str`)
|
25 |
+
date (:obj: `str`)
|
26 |
+
Return:
|
27 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
28 |
+
"""
|
29 |
+
#print request
|
30 |
+
print("request")
|
31 |
+
print(data)
|
32 |
+
# get inputs
|
33 |
+
inputs = data.pop("inputs", data)
|
34 |
+
|
35 |
+
text = self.pipe(inputs)["text"]
|
36 |
+
return text
|