CineAI commited on
Commit
ecf0dad
1 Parent(s): 5cabd9c

Update audio_processing/A2T.py

Browse files
Files changed (1) hide show
  1. audio_processing/A2T.py +16 -8
audio_processing/A2T.py CHANGED
@@ -14,7 +14,7 @@ class A2T:
14
  self.mic = mic
15
 
16
  def __generate_text(self, inputs, task: Optional[str] = None) -> str:
17
- if inputs is None:
18
  raise Exception("Inputs is None")
19
 
20
  transcribed_text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
@@ -22,11 +22,19 @@ class A2T:
22
 
23
  @staticmethod
24
  def __preprocess(raw: bytes) -> np.ndarray:
25
- print(f"Raw type : {type(raw)}")
26
- chunk = io.BytesIO(raw)
27
- audio, sample_rate = librosa.load(chunk, sr=16000)
28
- print(f"Sample rate : {sample_rate}")
29
- return audio
 
 
 
 
 
 
 
 
30
 
31
  def predict(self) -> str:
32
  try:
@@ -40,7 +48,7 @@ class A2T:
40
  if isinstance(audio, np.ndarray):
41
  return self.__generate_text(inputs=audio, task=TASK)
42
  else:
43
- raise Exception("Audio is not np array")
44
 
45
  except Exception as e:
46
- return f"Oops some kinda error : {e}"
 
14
  self.mic = mic
15
 
16
  def __generate_text(self, inputs, task: Optional[str] = None) -> str:
17
+ if inputs is None:
18
  raise Exception("Inputs is None")
19
 
20
  transcribed_text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
 
22
 
23
  @staticmethod
24
  def __preprocess(raw: bytes) -> np.ndarray:
25
+ print(f"Raw type: {type(raw)}")
26
+
27
+ if not isinstance(raw, bytes):
28
+ raise ValueError("Expected raw audio data as bytes")
29
+
30
+ try:
31
+ chunk = io.BytesIO(raw)
32
+ print(f"Chunk type: {type(chunk)}")
33
+ audio, sample_rate = librosa.load(chunk, sr=16000)
34
+ print(f"Sample rate : {sample_rate}")
35
+ return audio
36
+ except Exception as e:
37
+ print(f"Error loading audio: {e}")
38
 
39
  def predict(self) -> str:
40
  try:
 
48
  if isinstance(audio, np.ndarray):
49
  return self.__generate_text(inputs=audio, task=TASK)
50
  else:
51
+ raise ValueError("Audio is not np array")
52
 
53
  except Exception as e:
54
+ print(f"Oops some kinda error : {e}")