Delete handler.py
Browse files- handler.py +0 -46
handler.py
DELETED
@@ -1,46 +0,0 @@
|
|
1 |
-
from typing import Dict
|
2 |
-
from transformers.pipelines.audio_utils import ffmpeg_read
|
3 |
-
import whisper
|
4 |
-
import torch
|
5 |
-
|
6 |
-
SAMPLE_RATE = 16000
|
7 |
-
|
8 |
-
MODEL_NAME = "openai/whisper-large" #this always needs to stay in line 8 :D sorry for the hackiness
|
9 |
-
lang = "dk"
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
class EndpointHandler():
|
15 |
-
def __init__(self, path=""):
|
16 |
-
pipe = pipeline(
|
17 |
-
task="automatic-speech-recognition",
|
18 |
-
model=MODEL_NAME,
|
19 |
-
chunk_length_s=30,
|
20 |
-
device=device,
|
21 |
-
)
|
22 |
-
|
23 |
-
# load the model
|
24 |
-
#self.model = whisper.load_model("large")
|
25 |
-
self.model.config.forced_decoder_ids = pipe.tokenizer.get_decoder_prompt_ids(language=lang, task="transcribe")
|
26 |
-
|
27 |
-
|
28 |
-
def __call__(self, data: Dict[str, bytes]) -> Dict[str, str]:
|
29 |
-
"""
|
30 |
-
Args:
|
31 |
-
data (:obj:):
|
32 |
-
includes the deserialized audio file as bytes
|
33 |
-
Return:
|
34 |
-
A :obj:`dict`:. base64 encoded image
|
35 |
-
"""
|
36 |
-
# process input
|
37 |
-
inputs = data.pop("inputs", data)
|
38 |
-
audio_nparray = ffmpeg_read(inputs, SAMPLE_RATE)
|
39 |
-
audio_tensor= torch.from_numpy(audio_nparray)
|
40 |
-
|
41 |
-
# run inference pipeline
|
42 |
-
result = self.model.transcribe(audio_nparray)
|
43 |
-
|
44 |
-
|
45 |
-
# postprocess the prediction
|
46 |
-
return {"tekst": result["text"]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|