added custom handler
Browse files- handler.py +20 -0
handler.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, List, Any
|
2 |
+
from transformers import pipeline
|
3 |
+
import torch
|
4 |
+
|
5 |
+
class EndpointHandler():
|
6 |
+
def __init__(self, path=""):
|
7 |
+
device = 0 if torch.cuda.is_available() else "cpu"
|
8 |
+
self.pipe = pipeline(
|
9 |
+
task="automatic-speech-recognition",
|
10 |
+
model=path,
|
11 |
+
chunk_length_s=30,
|
12 |
+
device=device,
|
13 |
+
)
|
14 |
+
|
15 |
+
self.pipe.model.config.forced_decoder_ids = self.pipe.tokenizer.get_decoder_prompt_ids(language="turkish", task="transcribe")
|
16 |
+
|
17 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
18 |
+
inputs = data.pop("inputs",data)
|
19 |
+
prediction = self.pipe(inputs, return_timestamps=True, generate_kwargs={"language": "turkish", "task": "transcribe"})
|
20 |
+
return prediction
|