Rename handler to handler.py
Browse files- handler +0 -88
- handler.py +20 -0
handler
DELETED
|
@@ -1,88 +0,0 @@
|
|
| 1 |
-
from typing import Dict, Any, List
|
| 2 |
-
#from transformers import WhisperForCTC, WhisperTokenizer
|
| 3 |
-
from transformers import WhisperForConditionalGeneration, AutoProcessor, WhisperTokenizer, WhisperProcessor, pipeline, WhisperFeatureExtractor
|
| 4 |
-
import torch
|
| 5 |
-
#from functools import partial
|
| 6 |
-
#import torchaudio
|
| 7 |
-
import soundfile as sf
|
| 8 |
-
import io
|
| 9 |
-
|
| 10 |
-
# Check for GPU
|
| 11 |
-
#device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
-
|
| 13 |
-
class EndpointHandler:
|
| 14 |
-
def __init__(self, path=""):
|
| 15 |
-
# Load the model and tokenizer
|
| 16 |
-
# #self.model = WhisperForCTC.from_pretrained(path)
|
| 17 |
-
self.tokenizer = WhisperTokenizer.from_pretrained(path)
|
| 18 |
-
self.model = WhisperForConditionalGeneration.from_pretrained(path)
|
| 19 |
-
#self.tokenizer = WhisperTokenizer.from_pretrained(path)
|
| 20 |
-
self.processor = WhisperProcessor.from_pretrained(path, language="korean", task='transcribe')
|
| 21 |
-
#self.processor = AutoProcessor.from_pretrained(path)
|
| 22 |
-
#self.pipe = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.feature_extractor, feature_extractor=processor.feature_extractor)
|
| 23 |
-
self.feature_extractor = WhisperFeatureExtractor.from_pretrained(path)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Move model to device
|
| 27 |
-
# self.model.to(device)
|
| 28 |
-
|
| 29 |
-
def __call__(self, data: Any) -> List[Dict[str, str]]:
|
| 30 |
-
print('HELLO')
|
| 31 |
-
#print(f"{data}")
|
| 32 |
-
#inputs = data.pop("inputs", data)
|
| 33 |
-
#print(f'1. inputs: {inputs}')
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# inputs, _ = sf.read(io.BytesIO(data['inputs']))
|
| 37 |
-
inputs, _ = sf.read(data['inputs'])
|
| 38 |
-
print(f'2. inputs: {inputs}')
|
| 39 |
-
|
| 40 |
-
input_features = self.feature_extractor(inputs, sampling_rate=16000).input_features[0]
|
| 41 |
-
print(f'3. input_features: {input_features}')
|
| 42 |
-
input_features_tensor = torch.tensor(input_features).unsqueeze(0)
|
| 43 |
-
input_ids = self.model.generate(input_features_tensor)
|
| 44 |
-
print(f'4. input_ids: {input_ids}')
|
| 45 |
-
|
| 46 |
-
transcription = self.tokenizer.batch_decode(input_ids, skip_special_tokens=True)[0]
|
| 47 |
-
|
| 48 |
-
#inputs, _ = torchaudio.load(inputs, normalize=True)
|
| 49 |
-
#input_features = self.processor.feature_extractor(inputs, sampling_rate=16000).input_features[0]
|
| 50 |
-
|
| 51 |
-
#input_ids = self.processor.tokenizer(input_features, return_tensors="pt").input_ids
|
| 52 |
-
#generated_ids = self.model.generate(input_ids)
|
| 53 |
-
|
| 54 |
-
# #transcription = self.pipe(inputs, generate_kwargs = {"task":"transcribe", "language":"<|ko|>"})
|
| 55 |
-
# #transcription = self.pipe(inputs)
|
| 56 |
-
# #print(input)
|
| 57 |
-
# inputs = self.processor(inputs, retun_tensors="pt")
|
| 58 |
-
# #input_features = {key: value.to(device) for key, value in input_features.items()}
|
| 59 |
-
# input_features = inputs.input_features
|
| 60 |
-
|
| 61 |
-
# generated_ids = self.model.generate(input_features)
|
| 62 |
-
# #generated_ids = self.model.generate(inputs=input_features)
|
| 63 |
-
# #self.model.generate = partial(self.model.generate, language="korean", task="transcribe")
|
| 64 |
-
# #generated_ids = self.model.generate(inputs = input_features)
|
| 65 |
-
|
| 66 |
-
#transcription = self.processor.tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 67 |
-
#transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 68 |
-
|
| 69 |
-
return transcription
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
#original __call__
|
| 74 |
-
# def __call__(self, data: Any) -> List[Dict[str, str]]:
|
| 75 |
-
# inputs = data.pop("inputs", data)
|
| 76 |
-
|
| 77 |
-
# # Preprocess the input audio
|
| 78 |
-
# input_features = self.tokenizer(inputs, return_tensors="pt", padding="longest")
|
| 79 |
-
# input_features = {key: value.to(device) for key, value in input_features.items()}
|
| 80 |
-
|
| 81 |
-
# # Perform automatic speech recognition
|
| 82 |
-
# with torch.no_grad():
|
| 83 |
-
# logits = self.model(**input_features).logits
|
| 84 |
-
# predicted_ids = torch.argmax(logits, dim=-1)
|
| 85 |
-
# transcription = self.tokenizer.batch_decode(predicted_ids)[0]
|
| 86 |
-
|
| 87 |
-
# response = [{"task": "transcribe", "language": "korean", "transcription": transcription}]
|
| 88 |
-
# return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handler.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, Any, List
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
#### USE of PIPELINE
|
| 6 |
+
|
| 7 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 8 |
+
|
| 9 |
+
class EndpointHandler:
|
| 10 |
+
def __init__(self, path=""):
|
| 11 |
+
self.pipe = pipeline(task='automatic-speech-recognition', model=path, device=device)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def __call__(self, data: Any) -> List[Dict[str, str]]:
|
| 15 |
+
inputs = data.pop("inputs", data)
|
| 16 |
+
transcribe = self.pipe
|
| 17 |
+
transcribe.model.config.forced_decoder_ids = transcribe.tokenizer.get_decoder_prompt_ids(language="ko", task="transcribe")
|
| 18 |
+
result = transcribe(inputs)
|
| 19 |
+
|
| 20 |
+
return result
|