CineAI commited on
Commit
4abedda
1 Parent(s): cc17ed6

Update audio2text/a2t.py

Browse files
Files changed (1) hide show
  1. audio2text/a2t.py +55 -39
audio2text/a2t.py CHANGED
@@ -1,53 +1,69 @@
1
- import librosa
2
  import numpy as np
3
- from .init import pipe
4
 
5
- TASK = "transcribe"
6
- BATCH_SIZE = 8
7
- LIMIT = 60
8
- SAMPLING_RATE = 16000
9
 
10
- class A2T:
11
- def __init__(self, mic):
12
- self.mic = mic
13
 
14
- def __transcribe(self, inputs, task: str = None):
15
- if inputs is None:
16
- print("Inputs 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: np.ndarray, sampling_rate: int):
22
 
23
- chunk = raw.astype(np.float32, order='C') / 32768.0
24
 
25
- print(f"Chunk : {chunk} max chunk : {np.max(chunk)}")
26
 
27
- if len(chunk.shape) > 1:
28
- chunk = librosa.to_mono(chunk.T)
29
 
30
- chunk = chunk[:SAMPLING_RATE*LIMIT]
31
 
32
- return chunk
33
 
34
- def predict(self):
35
- try:
36
- if self.mic is not None:
37
- raw = self.mic.get_array_of_samples()
38
- chunk = np.array(raw, dtype=np.int16)
39
- sampling_rate = self.mic.frame_rate
40
- audio = self.__preprocces(raw=chunk, sampling_rate=sampling_rate)
41
- print(f"audio : {audio} \n shape : {audio.shape} \n max : {np.max(audio)} \n shape of chunk : {chunk.shape} \n sampling rate : {sampling_rate} \n max chunk : {np.max(chunk)} \n chunk : {chunk}")
42
- else:
43
- raise Exception("please provide audio")
44
-
45
- if isinstance(audio , np.ndarray):
46
- inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
47
- return self.__transcribe(inputs=inputs, task=TASK)
48
- else:
49
- raise Exception("Audio is not np array")
50
 
51
- except Exception as e:
52
- return f"Oops some kinda error : {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
 
 
1
  import numpy as np
2
+ import speech_recognition as sr
3
 
4
+ # TASK = "transcribe"
5
+ # BATCH_SIZE = 8
6
+ # LIMIT = 60
7
+ # SAMPLING_RATE = 16000
8
 
9
+ # class A2T:
10
+ # def __init__(self, mic):
11
+ # self.mic = mic
12
 
13
+ # def __transcribe(self, inputs, task: str = None):
14
+ # if inputs is None:
15
+ # print("Inputs None")
16
 
17
+ # transcribed_text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
18
+ # return transcribed_text
19
 
20
+ # def __preprocces(self, raw: np.ndarray, sampling_rate: int):
21
 
22
+ # chunk = raw.astype(np.float32, order='C') / 32768.0
23
 
24
+ # print(f"Chunk : {chunk} max chunk : {np.max(chunk)}")
25
 
26
+ # if len(chunk.shape) > 1:
27
+ # chunk = librosa.to_mono(chunk.T)
28
 
29
+ # chunk = chunk[:SAMPLING_RATE*LIMIT]
30
 
31
+ # return chunk
32
 
33
+ # def predict(self):
34
+ # try:
35
+ # if self.mic is not None:
36
+ # raw = self.mic.get_array_of_samples()
37
+ # chunk = np.array(raw, dtype=np.int16)
38
+ # sampling_rate = self.mic.frame_rate
39
+ # audio = self.__preprocces(raw=chunk, sampling_rate=sampling_rate)
40
+ # print(f"audio : {audio} \n shape : {audio.shape} \n max : {np.max(audio)} \n shape of chunk : {chunk.shape} \n sampling rate : {sampling_rate} \n max chunk : {np.max(chunk)} \n chunk : {chunk}")
41
+ # else:
42
+ # raise Exception("please provide audio")
43
+
44
+ # if isinstance(audio , np.ndarray):
45
+ # inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
46
+ # return self.__transcribe(inputs=inputs, task=TASK)
47
+ # else:
48
+ # raise Exception("Audio is not np array")
49
 
50
+ # except Exception as e:
51
+ # return f"Oops some kinda error : {e}"
52
+
53
+
54
+ class A2T:
55
+ def get_text(self):
56
+ # obtain audio from the microphone
57
+ r = sr.Recognizer()
58
+ with sr.Microphone() as source:
59
+ print("Say something!")
60
+ audio = r.listen(source)
61
+
62
+ # recognize speech using Sphinx
63
+ try:
64
+ return r.recognize_sphinx(audio)
65
+ except sr.UnknownValueError:
66
+ raise Exception("Sphinx could not understand audio")
67
+ except sr.RequestError as e:
68
+ raise Exception("Sphinx error; {0}".format(e))
69