From b494ee2f1c3d3bd9135194fc772470a9357fb2cf Mon Sep 17 00:00:00 2001 From: Lecter Date: Sun, 14 Apr 2024 14:33:17 +0800 Subject: [PATCH] fix openai voice_to_text whisper --- voice/openai/openai_voice.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/voice/openai/openai_voice.py b/voice/openai/openai_voice.py index 767353e..506d8b5 100644 --- a/voice/openai/openai_voice.py +++ b/voice/openai/openai_voice.py @@ -21,8 +21,21 @@ class OpenaiVoice(Voice): logger.debug("[Openai] voice file name={}".format(voice_file)) try: file = open(voice_file, "rb") - result = openai.Audio.transcribe("whisper-1", file) - text = result["text"] + api_base = conf().get("open_ai_api_base") or "https://api.openai.com/v1" + url = f'{api_base}/audio/transcriptions' + headers = { + 'Authorization': 'Bearer ' + conf().get("open_ai_api_key"), + # 'Content-Type': 'multipart/form-data' # 加了会报错,不知道什么原因 + } + files = { + "file": file, + } + data = { + "model": "whisper-1", + } + response = requests.post(url, headers=headers, files=files, data=data) + response_data = response.json() + text = response_data['text'] reply = Reply(ReplyType.TEXT, text) logger.info("[Openai] voiceToText text={} voice file name={}".format(text, voice_file)) except Exception as e: