From 8abdb197a7b716a577c2273c75271d89a6a94a8e Mon Sep 17 00:00:00 2001 From: SevaSk Date: Tue, 30 May 2023 19:04:28 -0400 Subject: [PATCH] fixed potential temp disk memory leak --- AudioTranscriber.py | 22 ++++++++++++---------- TranscriberModels.py | 4 +--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/AudioTranscriber.py b/AudioTranscriber.py index a1e7686..a29dc69 100644 --- a/AudioTranscriber.py +++ b/AudioTranscriber.py @@ -45,8 +45,14 @@ class AudioTranscriber: who_spoke, data, time_spoken = audio_queue.get() self.update_last_sample_and_phrase_status(who_spoke, data, time_spoken) source_info = self.audio_sources[who_spoke] - temp_file = source_info["process_data_func"](source_info["last_sample"]) - text = self.audio_model.get_transcription(temp_file) + + text = '' + temp_file = NamedTemporaryFile(delete=False, suffix=".wav") + source_info["process_data_func"](source_info["last_sample"], temp_file.name) + text = self.audio_model.get_transcription(temp_file.name) + + temp_file.close() + os.unlink(temp_file.name) if text != '' and text.lower() != 'you': self.update_transcript(who_spoke, text, time_spoken) @@ -63,23 +69,19 @@ class AudioTranscriber: source_info["last_sample"] += data source_info["last_spoken"] = time_spoken - def process_mic_data(self, data): - temp_file = NamedTemporaryFile().name + def process_mic_data(self, data, temp_file_name): audio_data = sr.AudioData(data, self.audio_sources["You"]["sample_rate"], self.audio_sources["You"]["sample_width"]) wav_data = io.BytesIO(audio_data.get_wav_data()) - with open(temp_file, 'w+b') as f: + with open(temp_file_name, 'w+b') as f: f.write(wav_data.read()) - return temp_file - def process_speaker_data(self, data): - temp_file = NamedTemporaryFile().name - with wave.open(temp_file, 'wb') as wf: + def process_speaker_data(self, data, temp_file_name): + with wave.open(temp_file_name, 'wb') as wf: wf.setnchannels(self.audio_sources["Speaker"]["channels"]) p = pyaudio.PyAudio() wf.setsampwidth(p.get_sample_size(pyaudio.paInt16)) wf.setframerate(self.audio_sources["Speaker"]["sample_rate"]) wf.writeframes(data) - return temp_file def update_transcript(self, who_spoke, text, time_spoken): source_info = self.audio_sources[who_spoke] diff --git a/TranscriberModels.py b/TranscriberModels.py index 843d18b..60a3dd8 100644 --- a/TranscriberModels.py +++ b/TranscriberModels.py @@ -24,9 +24,7 @@ class WhisperTranscriber: class APIWhisperTranscriber: def get_transcription(self, wav_file_path): - new_file_path = wav_file_path + '.wav' - os.rename(wav_file_path, new_file_path) - audio_file= open(new_file_path, "rb") + audio_file= open(wav_file_path, "rb") try: result = openai.Audio.translate("whisper-1", audio_file) except Exception as e: