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