Skip to content

Commit

Permalink
improvements, see comments: cogentapps#58
Browse files Browse the repository at this point in the history
  • Loading branch information
tluyben committed Mar 19, 2023
1 parent e3f4dfe commit 39e175b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
41 changes: 22 additions & 19 deletions app/src/components/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default function MessageInput(props: MessageInputProps) {
}
}, [context, message, dispatch]);

const onSpeechStart = () => {
const onSpeechStart = useCallback(() => {

if (!recording) {
setRecording(true);
Expand Down Expand Up @@ -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,
});

const json = await response.json()

if (json.text) {
dispatch(setMessage(json.text));
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()

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<HTMLTextAreaElement>) => {
Expand Down Expand Up @@ -192,14 +195,14 @@ export default function MessageInput(props: MessageInputProps) {
</>)}
{!context.generating && (
<>
<ActionIcon size="xl"
onClick={onSubmit}>
<i className="fa fa-paper-plane" style={{ fontSize: '90%' }} />
</ActionIcon>
<ActionIcon size="xl"
onClick={onSpeechStart}>
<i className="fa fa-microphone" style={{ fontSize: '90%', color: recording ? 'red' : 'inherit' }} />
</ActionIcon>
<ActionIcon size="xl"
onClick={onSubmit}>
<i className="fa fa-paper-plane" style={{ fontSize: '90%' }} />
</ActionIcon>
</>
)}
</div>
Expand Down
12 changes: 8 additions & 4 deletions app/src/components/settings/user.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -30,9 +30,13 @@ export default function UserOptionsTab(props: any) {
<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." />
</a>
</p>
<p>
<input type="checkbox" id="use-openai-whisper-api" checked={useOpenAIWhisper!} onChange={onUseOpenAIWhisperChange} /> Use the OpenAI Whisper API for speech recognition.
</p>

<Checkbox
style={{ marginTop: '1rem' }}
id="use-openai-whisper-api" checked={useOpenAIWhisper!} onChange={onUseOpenAIWhisperChange}
label="Use the OpenAI Whisper API for speech recognition."
/>

<p>
<FormattedMessage defaultMessage="Your API key is stored only on this device and never transmitted to anyone except OpenAI." />
</p>
Expand Down
4 changes: 2 additions & 2 deletions app/src/store/api-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
};

Expand Down

0 comments on commit 39e175b

Please sign in to comment.