diff --git a/openai_stt/stt.py b/openai_stt/stt.py index 66622dc01c8b1e2d16d5723ae23866c3358b031e..f760e7913552577c9dc177a2509efe94513fd53c 100644 --- a/openai_stt/stt.py +++ b/openai_stt/stt.py @@ -4,9 +4,7 @@ Support for Whisper API STT. from typing import AsyncIterable import aiohttp import logging -import os -import tempfile -import aiofiles +import io import voluptuous as vol from homeassistant.components.stt import ( AudioBitRates, @@ -93,24 +91,19 @@ class OpenAISTTProvider(Provider): return SpeechResult("", SpeechResultState.ERROR) try: - with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_file: - with wave.open(temp_file, 'wb') as wav_file: - wav_file.setnchannels(metadata.channel) - wav_file.setsampwidth(2) # 2 bytes per sample - wav_file.setframerate(metadata.sample_rate) - wav_file.writeframes(data) - temp_file_path = temp_file.name - + byte_io = io.BytesIO() + with wave.open(byte_io, 'wb') as wav_file: + wav_file.setnchannels(metadata.channel) + wav_file.setsampwidth(2) # 2 bytes per sample + wav_file.setframerate(metadata.sample_rate) + wav_file.writeframes(data) headers = { 'Authorization': f'Bearer {self._api_key}', } - async with aiofiles.open(temp_file_path, mode='rb') as file: - audio_data = await file.read() - form = aiohttp.FormData() - form.add_field('file', audio_data, filename='audio.wav', content_type='audio/wav') + form.add_field('file', byte_io.getvalue(), filename='audio.wav', content_type='audio/wav') form.add_field('language', metadata.language) form.add_field('model', 'whisper-1') @@ -126,6 +119,3 @@ class OpenAISTTProvider(Provider): except Exception as e: _LOGGER.warning(e) return SpeechResult("", SpeechResultState.ERROR) - finally: - if temp_file_path: - os.remove(temp_file_path)