From 75d5291589b5d7dd451102f614a7eed64c3f9bda Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Fri, 25 Aug 2023 18:56:34 +0800 Subject: [PATCH] try fetch whsiper API --- src/chatbox.tsx | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/chatbox.tsx b/src/chatbox.tsx index f97b0de..314cbd0 100644 --- a/src/chatbox.tsx +++ b/src/chatbox.tsx @@ -571,30 +571,36 @@ export default function ChatBOX(props: { }); reader.onloadend = async () => { - const base64data = reader.result; + try { + const base64data = reader.result; - // post to openai whisper api - const formData = new FormData(); - // append file - formData.append("file", blob, "audio.ogx"); - formData.append("model", "whisper-1"); - formData.append("response_format", "text"); - formData.append("prompt", prompt); + // post to openai whisper api + const formData = new FormData(); + // append file + formData.append("file", blob, "audio.ogx"); + formData.append("model", "whisper-1"); + formData.append("response_format", "text"); + formData.append("prompt", prompt); - const response = await fetch(chatStore.whisper_api, { - method: "POST", - headers: { - Authorization: `Bearer ${ - chatStore.whisper_api || chatStore.apiKey - }`, - }, - body: formData, - }); + const response = await fetch(chatStore.whisper_api, { + method: "POST", + headers: { + Authorization: `Bearer ${ + chatStore.whisper_api || chatStore.apiKey + }`, + }, + body: formData, + }); - const { text } = await response.json(); + const { text } = await response.json(); - setInputMsg(inputMsg ? inputMsg + " " + text : text); - setIsRecording("Mic"); + setInputMsg(inputMsg ? inputMsg + " " + text : text); + } catch (error) { + alert(error); + console.log(error); + } finally { + setIsRecording("Mic"); + } }; }); } catch (error) {