Skip to content

Commit 6a467fc

Browse files
perf(stt-whisper): close the audio file handle after calling the OpenAI transcription API (#8528)
* 在调用 OpenAI API 后关闭文件句柄再删除临时文件。 核心问题:whisper_api_source.py 第 121 行用 open(audio_url, "rb") 打开文件后,文件句柄没有被关闭,导致 Windows 上报 "另一个程序正在使用此文件" 的错误,temp wav 文件无法删除。 修复方案:在调用 OpenAI API 后关闭文件句柄再删除临时文件。 * fix(whisper_api): use context manager for audio file handling to ensure proper closure --------- Co-authored-by: Soulter <905617992@qq.com>
1 parent d912e14 commit 6a467fc

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

astrbot/core/provider/sources/whisper_api_source.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,11 @@ async def get_text(self, audio_url: str) -> str:
116116

117117
audio_url = output_path
118118

119-
result = await self.client.audio.transcriptions.create(
120-
model=self.model_name,
121-
file=("audio.wav", open(audio_url, "rb")),
122-
)
119+
with open(audio_url, "rb") as audio_file:
120+
result = await self.client.audio.transcriptions.create(
121+
model=self.model_name,
122+
file=("audio.wav", audio_file),
123+
)
123124

124125
# remove temp file
125126
if output_path and os.path.exists(output_path):

0 commit comments

Comments
 (0)