Create ffmpeg_handler.py
Browse files- ffmpeg_handler.py +40 -0
ffmpeg_handler.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, Any, List
|
2 |
+
from transformers import WhisperForConditionalGeneration, AutoProcessor, WhisperTokenizer, WhisperProcessor, pipeline, WhisperFeatureExtractor
|
3 |
+
import torch
|
4 |
+
from transformers.pipelines.audio_utils import ffmpeg_read
|
5 |
+
#import io
|
6 |
+
|
7 |
+
|
8 |
+
#device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
9 |
+
|
10 |
+
|
11 |
+
class EndpointHandler:
|
12 |
+
def __init__(self, path=""):
|
13 |
+
#tokenizer = WhisperTokenizer.from_pretrained('openai/whisper-large', language="korean", task='transcribe')
|
14 |
+
#model = WhisperForConditionalGeneration.from_pretrained(path)
|
15 |
+
#self.tokenizer = WhisperTokenizer.from_pretrained(path)
|
16 |
+
#self.processor = WhisperProcessor.from_pretrained(path, language="korean", task='transcribe')
|
17 |
+
#processor = AutoProcessor.from_pretrained(path)
|
18 |
+
#self.pipe = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.feature_extractor, feature_extractor=processor.feature_extractor)
|
19 |
+
#feature_extractor = WhisperFeatureExtractor.from_pretrained('openai/whisper-large')
|
20 |
+
self.pipe = pipeline(task='automatic-speech-recognition', model=path, device=)
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
# Move model to device
|
25 |
+
# self.model.to(device)
|
26 |
+
|
27 |
+
def __call__(self, data: Any) -> List[Dict[str, str]]:
|
28 |
+
print('==========NEW PROCESS=========')
|
29 |
+
|
30 |
+
inputs = data.pop("inputs", data)
|
31 |
+
audio_nparray = ffmpeg_read(inputs, 16000)
|
32 |
+
audio_tensor= torch.from_numpy(audio_nparray)
|
33 |
+
|
34 |
+
|
35 |
+
transcribe = pipeline(task="automatic-speech-recognition", model="vasista22/whisper-kannada-tiny")
|
36 |
+
transcribe.model.config.forced_decoder_ids = transcribe.tokenizer.get_decoder_prompt_ids(language="ko", task="transcribe")
|
37 |
+
result = transcribe(audio_tensor)
|
38 |
+
|
39 |
+
|
40 |
+
return result
|