diff --git a/openai_stt/stt.py b/openai_stt/stt.py index 52ff0d24f49ddd143077afcec793dddc62f72754..66622dc01c8b1e2d16d5723ae23866c3358b031e 100644 --- a/openai_stt/stt.py +++ b/openai_stt/stt.py @@ -6,6 +6,7 @@ import aiohttp import logging import os import tempfile +import aiofiles import voluptuous as vol from homeassistant.components.stt import ( AudioBitRates, @@ -105,9 +106,11 @@ class OpenAISTTProvider(Provider): 'Authorization': f'Bearer {self._api_key}', } - file_to_send = open(temp_file_path, 'rb') + async with aiofiles.open(temp_file_path, mode='rb') as file: + audio_data = await file.read() + form = aiohttp.FormData() - form.add_field('file', file_to_send, filename='audio.wav', content_type='audio/wav') + form.add_field('file', audio_data, filename='audio.wav', content_type='audio/wav') form.add_field('language', metadata.language) form.add_field('model', 'whisper-1') @@ -124,7 +127,5 @@ class OpenAISTTProvider(Provider): _LOGGER.warning(e) return SpeechResult("", SpeechResultState.ERROR) finally: - if 'file_to_send' in locals(): - file_to_send.close() if temp_file_path: os.remove(temp_file_path)