Skip to content

Commit 389736d

Browse files
committed
improvements, see comments: cogentapps/chat-with-gpt#58
1 parent a8d6d1d commit 389736d

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

app/src/components/input.tsx

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export default function MessageInput(props: MessageInputProps) {
104104
}
105105
}, [context, message, dispatch]);
106106

107-
const onSpeechStart = () => {
107+
const onSpeechStart = useCallback(() => {
108108

109109
if (!recording) {
110110
setRecording(true);
@@ -141,27 +141,30 @@ export default function MessageInput(props: MessageInputProps) {
141141
data.append('file', file);
142142
data.append('model', 'whisper-1')
143143

144-
const response = await fetch("https://api.openai.com/v1/audio/transcriptions", {
145-
method: "POST",
146-
headers: {
147-
'Authorization': `Bearer ${openAIApiKey}`,
148-
},
149-
body: data,
150-
});
151-
152-
const json = await response.json()
153-
154-
if (json.text) {
155-
dispatch(setMessage(json.text));
144+
try {
145+
const response = await fetch("https://api.openai.com/v1/audio/transcriptions", {
146+
method: "POST",
147+
headers: {
148+
'Authorization': `Bearer ${openAIApiKey}`,
149+
},
150+
body: data,
151+
});
152+
153+
const json = await response.json()
154+
155+
if (json.text) {
156+
dispatch(setMessage(json.text));
157+
}
158+
} catch (e) {
159+
console.log(e)
156160
}
157161

158162
}).catch((e: any) => console.error(e));
159163
} else {
160164
speechRecognition.stop();
161-
162165
}
163166
}
164-
}
167+
}, [recording, message, dispatch]);
165168

166169

167170
const onKeyDown = useCallback((e: React.KeyboardEvent<HTMLTextAreaElement>) => {
@@ -192,14 +195,14 @@ export default function MessageInput(props: MessageInputProps) {
192195
</>)}
193196
{!context.generating && (
194197
<>
195-
<ActionIcon size="xl"
196-
onClick={onSubmit}>
197-
<i className="fa fa-paper-plane" style={{ fontSize: '90%' }} />
198-
</ActionIcon>
199198
<ActionIcon size="xl"
200199
onClick={onSpeechStart}>
201200
<i className="fa fa-microphone" style={{ fontSize: '90%', color: recording ? 'red' : 'inherit' }} />
202201
</ActionIcon>
202+
<ActionIcon size="xl"
203+
onClick={onSubmit}>
204+
<i className="fa fa-paper-plane" style={{ fontSize: '90%' }} />
205+
</ActionIcon>
203206
</>
204207
)}
205208
</div>

app/src/components/settings/user.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import SettingsTab from "./tab";
22
import SettingsOption from "./option";
3-
import { TextInput } from "@mantine/core";
3+
import { Checkbox, TextInput } from "@mantine/core";
44
import { useCallback, useMemo } from "react";
55
import { useAppDispatch, useAppSelector } from "../../store";
66
import { selectOpenAIApiKey, setOpenAIApiKeyFromEvent, selectUseOpenAIWhisper, setUseOpenAIWhisperFromEvent } from "../../store/api-keys";
@@ -30,9 +30,13 @@ export default function UserOptionsTab(props: any) {
3030
<FormattedMessage defaultMessage="Find your API key here." description="Label for the link that takes the user to the page on the OpenAI website where they can find their API key." />
3131
</a>
3232
</p>
33-
<p>
34-
<input type="checkbox" id="use-openai-whisper-api" checked={useOpenAIWhisper!} onChange={onUseOpenAIWhisperChange} /> Use the OpenAI Whisper API for speech recognition.
35-
</p>
33+
34+
<Checkbox
35+
style={{ marginTop: '1rem' }}
36+
id="use-openai-whisper-api" checked={useOpenAIWhisper!} onChange={onUseOpenAIWhisperChange}
37+
label="Use the OpenAI Whisper API for speech recognition."
38+
/>
39+
3640
<p>
3741
<FormattedMessage defaultMessage="Your API key is stored only on this device and never transmitted to anyone except OpenAI." />
3842
</p>

app/src/store/api-keys.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import type { RootState } from '.';
33

44
const initialState: {
55
openAIApiKey?: string | null | undefined;
6-
useOpenAIWhisper?: boolean | null | undefined;
6+
useOpenAIWhisper: boolean;
77
elevenLabsApiKey?: string | null | undefined;
88

99
} = {
1010
openAIApiKey: localStorage.getItem('openai-api-key'),
11-
useOpenAIWhisper: localStorage.getItem('use-openai-whisper') === 'true',
11+
useOpenAIWhisper: false,
1212
elevenLabsApiKey: localStorage.getItem('elevenlabs-api-key'),
1313
};
1414

0 commit comments

Comments
 (0)