File size: 4,036 Bytes
922184b
 
 
 
 
 
 
 
 
 
 
 
 
 
109cd63
922184b
 
 
 
 
2f68faf
922184b
 
 
 
 
 
 
 
 
 
 
 
44deeae
 
922184b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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(path, 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('HELLO')
        #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)
        print(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