from typing import Dict, Any, List | |
#from transformers import WhisperForCTC, WhisperTokenizer | |
from transformers import WhisperForConditionalGeneration, AutoProcessor, WhisperTokenizer, WhisperProcessor, pipeline, WhisperFeatureExtractor | |
import torch | |
#from functools import partial | |
#import torchaudio | |
import soundfile as sf | |
import io | |
# Check for GPU | |
#device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
class EndpointHandler: | |
def __init__(self, path=""): | |
self.tokenizer = WhisperTokenizer.from_pretrained('openai/whisper-large', language="korean", task='transcribe') | |
self.model = WhisperForConditionalGeneration.from_pretrained(path) | |
#self.tokenizer = WhisperTokenizer.from_pretrained(path) | |
#self.processor = WhisperProcessor.from_pretrained(path, language="korean", task='transcribe') | |
#self.processor = AutoProcessor.from_pretrained(path) | |
#self.pipe = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.feature_extractor, feature_extractor=processor.feature_extractor) | |
self.feature_extractor = WhisperFeatureExtractor.from_pretrained('openai/whisper-large') | |
# Move model to device | |
# self.model.to(device) | |
def __call__(self, data: Any) -> List[Dict[str, str]]: | |
print('==========NEW PROCESS=========') | |
#print(f"{data}") | |
#inputs = data.pop("inputs", data) | |
#print(f'1. inputs: {inputs}') | |
inputs, _ = sf.read(io.BytesIO(data['inputs'])) | |
#inputs, _ = sf.read(data['inputs']) | |
#print(f'2. inputs: {inputs}') | |
input_features = self.feature_extractor(inputs, sampling_rate=16000).input_features[0] | |
#print(f'3. input_features: {input_features}') | |
input_features_tensor = torch.tensor(input_features).unsqueeze(0) | |
input_ids = self.model.generate(input_features_tensor) | |
#(f'4. input_ids: {input_ids}') | |
transcription = self.tokenizer.batch_decode(input_ids, skip_special_tokens=True)[0] | |
#inputs, _ = torchaudio.load(inputs, normalize=True) | |
#input_features = self.processor.feature_extractor(inputs, sampling_rate=16000).input_features[0] | |
#input_ids = self.processor.tokenizer(input_features, return_tensors="pt").input_ids | |
#generated_ids = self.model.generate(input_ids) | |
# #transcription = self.pipe(inputs, generate_kwargs = {"task":"transcribe", "language":"<|ko|>"}) | |
# #transcription = self.pipe(inputs) | |
# #print(input) | |
# inputs = self.processor(inputs, retun_tensors="pt") | |
# #input_features = {key: value.to(device) for key, value in input_features.items()} | |
# input_features = inputs.input_features | |
# generated_ids = self.model.generate(input_features) | |
# #generated_ids = self.model.generate(inputs=input_features) | |
# #self.model.generate = partial(self.model.generate, language="korean", task="transcribe") | |
# #generated_ids = self.model.generate(inputs = input_features) | |
#transcription = self.processor.tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] | |
#transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] | |
return transcription | |
#original __call__ | |
# def __call__(self, data: Any) -> List[Dict[str, str]]: | |
# inputs = data.pop("inputs", data) | |
# # Preprocess the input audio | |
# input_features = self.tokenizer(inputs, return_tensors="pt", padding="longest") | |
# input_features = {key: value.to(device) for key, value in input_features.items()} | |
# # Perform automatic speech recognition | |
# with torch.no_grad(): | |
# logits = self.model(**input_features).logits | |
# predicted_ids = torch.argmax(logits, dim=-1) | |
# transcription = self.tokenizer.batch_decode(predicted_ids)[0] | |
# response = [{"task": "transcribe", "language": "korean", "transcription": transcription}] | |
# return response |