From 39e175bef69edbd2e19e4b19ed75f41f97ad3438 Mon Sep 17 00:00:00 2001
From: Tycho Luyben
Date: Sun, 19 Mar 2023 09:33:52 +0000
Subject: [PATCH] improvements, see comments:
https://github.com/cogentapps/chat-with-gpt/pull/58
---
app/src/components/input.tsx | 37 +++++++++++++++-------------
app/src/components/settings/user.tsx | 12 ++++++---
app/src/store/api-keys.ts | 4 +--
3 files changed, 30 insertions(+), 23 deletions(-)
diff --git a/app/src/components/input.tsx b/app/src/components/input.tsx
index 0a7352c..2e440b6 100644
--- a/app/src/components/input.tsx
+++ b/app/src/components/input.tsx
@@ -104,7 +104,7 @@ export default function MessageInput(props: MessageInputProps) {
}
}, [context, message, dispatch]);
- const onSpeechStart = () => {
+ const onSpeechStart = useCallback(() => {
if (!recording) {
setRecording(true);
@@ -141,27 +141,30 @@ export default function MessageInput(props: MessageInputProps) {
data.append('file', file);
data.append('model', 'whisper-1')
- const response = await fetch("https://api.openai.com/v1/audio/transcriptions", {
- method: "POST",
- headers: {
- 'Authorization': `Bearer ${openAIApiKey}`,
- },
- body: data,
- });
+ try {
+ const response = await fetch("https://api.openai.com/v1/audio/transcriptions", {
+ method: "POST",
+ headers: {
+ 'Authorization': `Bearer ${openAIApiKey}`,
+ },
+ body: data,
+ });
- const json = await response.json()
+ const json = await response.json()
- if (json.text) {
- dispatch(setMessage(json.text));
+ if (json.text) {
+ dispatch(setMessage(json.text));
+ }
+ } catch (e) {
+ console.log(e)
}
}).catch((e: any) => console.error(e));
} else {
speechRecognition.stop();
-
}
}
- }
+ }, [recording, message, dispatch]);
const onKeyDown = useCallback((e: React.KeyboardEvent) => {
@@ -192,14 +195,14 @@ export default function MessageInput(props: MessageInputProps) {
>)}
{!context.generating && (
<>
-
-
-
+
+
+
>
)}
diff --git a/app/src/components/settings/user.tsx b/app/src/components/settings/user.tsx
index fc2370e..6b6d130 100644
--- a/app/src/components/settings/user.tsx
+++ b/app/src/components/settings/user.tsx
@@ -1,6 +1,6 @@
import SettingsTab from "./tab";
import SettingsOption from "./option";
-import { TextInput } from "@mantine/core";
+import { Checkbox, TextInput } from "@mantine/core";
import { useCallback, useMemo } from "react";
import { useAppDispatch, useAppSelector } from "../../store";
import { selectOpenAIApiKey, setOpenAIApiKeyFromEvent, selectUseOpenAIWhisper, setUseOpenAIWhisperFromEvent } from "../../store/api-keys";
@@ -30,9 +30,13 @@ export default function UserOptionsTab(props: any) {
-
- Use the OpenAI Whisper API for speech recognition.
-
+
+
+
diff --git a/app/src/store/api-keys.ts b/app/src/store/api-keys.ts
index 3a53cf7..eced56c 100644
--- a/app/src/store/api-keys.ts
+++ b/app/src/store/api-keys.ts
@@ -3,12 +3,12 @@ import type { RootState } from '.';
const initialState: {
openAIApiKey?: string | null | undefined;
- useOpenAIWhisper?: boolean | null | undefined;
+ useOpenAIWhisper: boolean;
elevenLabsApiKey?: string | null | undefined;
} = {
openAIApiKey: localStorage.getItem('openai-api-key'),
- useOpenAIWhisper: localStorage.getItem('use-openai-whisper') === 'true',
+ useOpenAIWhisper: false,
elevenLabsApiKey: localStorage.getItem('elevenlabs-api-key'),
};