CineAI commited on
Commit
003e418
·
verified ·
1 Parent(s): 1c10618

Create A2T.py

Browse files
Files changed (1) hide show
  1. audio_processing/A2T.py +43 -0
audio_processing/A2T.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import librosa
3
+ import io
4
+
5
+ from .init import pipe
6
+
7
+ TASK = "transcribe"
8
+ BATCH_SIZE = 8
9
+
10
+ class A2T:
11
+ def __init__(self, mic):
12
+ self.mic = mic
13
+
14
+ def __generate_text(self, inputs, task: str = None):
15
+ if inputs is None:
16
+ raise Exception("Inputs is None")
17
+
18
+ transcribed_text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
19
+ return transcribed_text
20
+
21
+ def __preprocces(self, raw: bytes):
22
+ print(f"Raw type : {type(raw)}")
23
+ chunk = io.BytesIO(raw)
24
+ audio, sample_rate = librosa.load(chunk, sr=16000)
25
+ print(f"Sample rate : {sample_rate}")
26
+ return audio
27
+
28
+ def predict(self):
29
+ try:
30
+ if self.mic is not None:
31
+ raw = self.mic
32
+ audio = self.__preprocces(raw=raw)
33
+ print(f"audio type : {type(audio)} \n shape : {audio.shape} \n audio max value : {np.max(audio)}")
34
+ else:
35
+ raise Exception("please provide audio")
36
+
37
+ if isinstance(audio , np.ndarray):
38
+ return self.__generate_text(inputs=audio, task=TASK)
39
+ else:
40
+ raise Exception("Audio is not np array")
41
+
42
+ except Exception as e:
43
+ return f"Oops some kinda error : {e}"