CineAI commited on
Commit
a677076
1 Parent(s): 3e4be91

41727469634d6f6e6b65793a31392e30332e32343a31373230

Browse files
Files changed (1) hide show
  1. audio2text/a2t.py +41 -3
audio2text/a2t.py CHANGED
@@ -1,8 +1,46 @@
1
  import librosa
2
  import torch
3
- from transformers import WhisperProcessor, WhisperForConditionalGeneration
 
 
4
 
5
  class A2T:
6
- def __init__(self, ):
7
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
 
1
  import librosa
2
  import torch
3
+ from __init__ import processor, model
4
+
5
+ LIMIT = 90 # limit 90 seconds
6
 
7
  class A2T:
8
+ def __init__(self, mic):
9
+ self.mic = mic
10
+
11
+ def __preproccess(self, audio, frame_rate):
12
+ try:
13
+ audio = audio / 32678.0
14
+
15
+ if len(audio.shape) > 1:
16
+ audio = librosa.to_mono(audio.T)
17
+
18
+ if frame_rate != 16_000:
19
+ audio = librosa.resample(audio, orig_sr=frame_rate, target_sr=16000)
20
+
21
+ audio = audio[:16_000*LIMIT]
22
+
23
+ audio = torch.tensor(audio)
24
+ return audio
25
+ except Exception as e:
26
+ print("Error", e)
27
+ return None
28
+
29
+ def predict(self):
30
+ if this.mic is not None:
31
+ audio = self.mic
32
+ frame_rate = audio.frame_rate
33
+ else:
34
+ return "please provide audio"
35
+
36
+ try:
37
+ forced_decoder_ids = processor.get_decoder_prompt_ids(language="english", task="transcribe")
38
+ audio = self.__preproccess(audio=audio, frame_rate=frame_rate)
39
+ inputs = processor(audio=audio, sampling_rate=16000, return_tensors="pt")
40
+ predicted_ids = model.generate(**inputs, max_length=400, forced_decoder_ids=forced_decoder_ids)
41
+ transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
42
+ return transcription[0]
43
+ except Exception as e:
44
+ print("Error", e)
45
+ return "Oops some kinda error"
46