fix: cpu usage issue

This commit is contained in:
makaveli10
2024-02-28 13:55:37 +05:30
parent ec1349360a
commit 18b6168807
2 changed files with 14 additions and 4 deletions
+10 -3
View File
@@ -408,6 +408,11 @@ class ServeClientBase(object):
if self.frames_np is not None and self.frames_np.shape[0] > 45*self.RATE: if self.frames_np is not None and self.frames_np.shape[0] > 45*self.RATE:
self.frames_offset += 30.0 self.frames_offset += 30.0
self.frames_np = self.frames_np[int(30*self.RATE):] self.frames_np = self.frames_np[int(30*self.RATE):]
# check timestamp offset(should be >= self.frame_offset)
# this basically means that there is no speech as timestamp offset hasnt updated
# and is less than frame_offset
if self.timestamp_offset < self.frames_offset:
self.timestamp_offset = self.frames_offset
if self.frames_np is None: if self.frames_np is None:
self.frames_np = frame_np.copy() self.frames_np = frame_np.copy()
else: else:
@@ -796,7 +801,8 @@ class ServeClientFasterWhisper(ServeClientBase):
task=self.task, task=self.task,
vad_filter=self.use_vad, vad_filter=self.use_vad,
vad_parameters=self.vad_parameters if self.use_vad else None) vad_parameters=self.vad_parameters if self.use_vad else None)
if self.language is None:
if self.language is None and info is not None:
self.set_language(info) self.set_language(info)
return result return result
@@ -881,7 +887,9 @@ class ServeClientFasterWhisper(ServeClientBase):
input_sample = input_bytes.copy() input_sample = input_bytes.copy()
result = self.transcribe_audio(input_sample) result = self.transcribe_audio(input_sample)
if self.language is None: if result is None or self.language is None:
self.timestamp_offset += duration
time.sleep(0.25) # wait for voice activity, result is None when no voice activity
continue continue
self.handle_transcription_output(result, duration) self.handle_transcription_output(result, duration)
@@ -932,7 +940,6 @@ class ServeClientFasterWhisper(ServeClientBase):
""" """
offset = None offset = None
self.current_out = '' self.current_out = ''
last_segment = None
# process complete segments # process complete segments
if len(segments) > 1: if len(segments) > 1:
for i, s in enumerate(segments[:-1]): for i, s in enumerate(segments[:-1]):
+4 -1
View File
@@ -180,7 +180,7 @@ class WhisperModel:
return config return config
def transcribe( def transcribe( # noqa: C901
self, self,
audio: Union[str, BinaryIO, np.ndarray], audio: Union[str, BinaryIO, np.ndarray],
language: Optional[str] = None, language: Optional[str] = None,
@@ -315,6 +315,9 @@ class WhisperModel:
else: else:
speech_chunks = None speech_chunks = None
if audio.shape[0] == 0:
return None, None
features = self.feature_extractor(audio) features = self.feature_extractor(audio)
encoder_output = None encoder_output = None