slplab commited on
Commit
e68ccbb
1 Parent(s): 26143fa

Create pipe_handler.py

Browse files
Files changed (1) hide show
  1. pipe_handler.py +69 -0
pipe_handler.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, Any, List
2
+ from transformers import WhisperForConditionalGeneration, AutoProcessor, WhisperTokenizer, WhisperProcessor, pipeline, WhisperFeatureExtractor
3
+ import torch
4
+ import soundfile as sf
5
+ import io
6
+
7
+
8
+ class EndpointHandler:
9
+ def __init__(self, path=""):
10
+ tokenizer = WhisperTokenizer.from_pretrained('openai/whisper-large', language="korean", task='transcribe')
11
+ model = WhisperForConditionalGeneration.from_pretrained(path)
12
+ #self.tokenizer = WhisperTokenizer.from_pretrained(path)
13
+ #self.processor = WhisperProcessor.from_pretrained(path, language="korean", task='transcribe')
14
+ processor = AutoProcessor.from_pretrained(path)
15
+ #self.pipe = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.feature_extractor, feature_extractor=processor.feature_extractor)
16
+ feature_extractor = WhisperFeatureExtractor.from_pretrained('openai/whisper-large')
17
+ self.pipe = pipeline(task='automatic-speech-recognition', model=path)
18
+
19
+
20
+
21
+ # Move model to device
22
+ # self.model.to(device)
23
+
24
+ def __call__(self, data: Any) -> List[Dict[str, str]]:
25
+ print('==========NEW PROCESS=========')
26
+ transcription = pipeline(task="automatic-speech-recognition", model="vasista22/whisper-kannada-tiny", chunk_length_s=30, device=device)
27
+ transcription.model.config.forced_decoder_ids = transcribe.tokenizer.get_decoder_prompt_ids(language="ko", task="transcribe")
28
+ result = transcription(data['inputs'])
29
+
30
+
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
+ # #(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 result