slplab commited on
Commit
139c456
1 Parent(s): f0f40a8

Rename handler.py to good_handler.py

Browse files
Files changed (2) hide show
  1. good_handler.py +24 -0
  2. handler.py +0 -72
good_handler.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, Any, List
2
+ from transformers import pipeline
3
+ import torch
4
+
5
+ #### USE of PIPELINE
6
+
7
+ class EndpointHandler:
8
+ def __init__(self, path=""):
9
+
10
+ self.pipe = pipeline(task='automatic-speech-recognition', model=path)
11
+
12
+
13
+
14
+ # Move model to device
15
+ # self.model.to(device)
16
+
17
+ def __call__(self, data: Any) -> List[Dict[str, str]]:
18
+ print('==========NEW PROCESS=========')
19
+ transcribe = self.pipe
20
+ transcribe.model.config.forced_decoder_ids = transcribe.tokenizer.get_decoder_prompt_ids(language="ko", task="transcribe")
21
+ result = transcribe(data['inputs'])
22
+
23
+
24
+ return result
handler.py DELETED
@@ -1,72 +0,0 @@
1
- from typing import Dict, Any, List
2
- from transformers import WhisperForConditionalGeneration, AutoProcessor, WhisperTokenizer, WhisperProcessor, pipeline, WhisperFeatureExtractor
3
- import torch
4
- #import io
5
-
6
-
7
- #device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
-
9
- #### USE of PIPELINE
10
-
11
- class EndpointHandler:
12
- def __init__(self, path=""):
13
- #tokenizer = WhisperTokenizer.from_pretrained('openai/whisper-large', language="korean", task='transcribe')
14
- #model = WhisperForConditionalGeneration.from_pretrained(path)
15
- #self.tokenizer = WhisperTokenizer.from_pretrained(path)
16
- #self.processor = WhisperProcessor.from_pretrained(path, language="korean", task='transcribe')
17
- #processor = AutoProcessor.from_pretrained(path)
18
- #self.pipe = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.feature_extractor, feature_extractor=processor.feature_extractor)
19
- #feature_extractor = WhisperFeatureExtractor.from_pretrained('openai/whisper-large')
20
- self.pipe = pipeline(task='automatic-speech-recognition', model=path)
21
-
22
-
23
-
24
- # Move model to device
25
- # self.model.to(device)
26
-
27
- def __call__(self, data: Any) -> List[Dict[str, str]]:
28
- print('==========NEW PROCESS=========')
29
- transcribe = self.pipe
30
- transcribe.model.config.forced_decoder_ids = transcribe.tokenizer.get_decoder_prompt_ids(language="ko", task="transcribe")
31
- result = transcribe(data['inputs'])
32
-
33
-
34
- #print(f"{data}")
35
- #inputs = data.pop("inputs", data)
36
- #print(f'1. inputs: {inputs}')
37
-
38
-
39
- #inputs, _ = sf.read(io.BytesIO(data['inputs']))
40
- #inputs, _ = sf.read(data['inputs'])
41
- #print(f'2. inputs: {inputs}')
42
-
43
- # input_features = self.feature_extractor(inputs, sampling_rate=16000).input_features[0]
44
- # #print(f'3. input_features: {input_features}')
45
- # input_features_tensor = torch.tensor(input_features).unsqueeze(0)
46
- # input_ids = self.model.generate(input_features_tensor)
47
- # #(f'4. input_ids: {input_ids}')
48
-
49
- # transcription = self.tokenizer.batch_decode(input_ids, skip_special_tokens=True)[0]
50
-
51
- # #inputs, _ = torchaudio.load(inputs, normalize=True)
52
- # #input_features = self.processor.feature_extractor(inputs, sampling_rate=16000).input_features[0]
53
-
54
- #input_ids = self.processor.tokenizer(input_features, return_tensors="pt").input_ids
55
- #generated_ids = self.model.generate(input_ids)
56
-
57
- # #transcription = self.pipe(inputs, generate_kwargs = {"task":"transcribe", "language":"<|ko|>"})
58
- # #transcription = self.pipe(inputs)
59
- # #print(input)
60
- # inputs = self.processor(inputs, retun_tensors="pt")
61
- # #input_features = {key: value.to(device) for key, value in input_features.items()}
62
- # input_features = inputs.input_features
63
-
64
- # generated_ids = self.model.generate(input_features)
65
- # #generated_ids = self.model.generate(inputs=input_features)
66
- # #self.model.generate = partial(self.model.generate, language="korean", task="transcribe")
67
- # #generated_ids = self.model.generate(inputs = input_features)
68
-
69
- #transcription = self.processor.tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
70
- #transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
71
-
72
- return result