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