Skip to content

Commit

Permalink
feat: password protect (#61)
Browse files Browse the repository at this point in the history
feat: password protect (#61)
  • Loading branch information
hahahumble committed Apr 19, 2023
2 parents 9567b8b + 457953f commit cbb9b3f
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 11 deletions.
1 change: 1 addition & 0 deletions .env
@@ -1,3 +1,4 @@
VITE_ACCESS_CODE=REPLACE_WITH_YOUR_OWN
VITE_OPENAI_API_KEY=REPLACE_WITH_YOUR_OWN
VITE_OPENAI_HOST=REPLACE_WITH_YOUR_OWN
VITE_OPENAI_MODEL=REPLACE_WITH_YOUR_OWN
Expand Down
10 changes: 10 additions & 0 deletions src/components/AzureSpeechToText.tsx
@@ -1,5 +1,6 @@
import React, { useState } from 'react';
import * as sdk from 'microsoft-cognitiveservices-speech-sdk';
import { existEnvironmentVariable, getEnvironmentVariable } from '../helpers/utils';

interface AzureSpeechToTextProps {
subscriptionKey: string;
Expand All @@ -10,6 +11,7 @@ interface AzureSpeechToTextProps {
setIsListening: (update: ((prevIsListening: boolean) => boolean) | boolean) => void;
setWaiting: (update: ((prevWaiting: boolean) => boolean) | boolean) => void;
notify: any;
accessCode: string;
}

const AzureSpeechToText: React.FC<AzureSpeechToTextProps> = ({
Expand All @@ -21,6 +23,7 @@ const AzureSpeechToText: React.FC<AzureSpeechToTextProps> = ({
setTranscript,
setWaiting,
notify,
accessCode,
}) => {
const [recognizer, setRecognizer] = useState<sdk.SpeechRecognizer | null>(null);

Expand All @@ -37,6 +40,13 @@ const AzureSpeechToText: React.FC<AzureSpeechToTextProps> = ({
}, [isListening]);

const startSpeechRecognition = () => {
if (accessCode !== getEnvironmentVariable('ACCESS_CODE')) {
notify.invalidAccessCodeNotify();
setIsListening(false);
setWaiting(false);
return;
}

setWaiting(true);

if (subscriptionKey === '' || region === '') {
Expand Down
18 changes: 18 additions & 0 deletions src/components/Content.tsx
Expand Up @@ -109,6 +109,13 @@ const Content: React.FC<ContentProps> = ({ notify }) => {
if (disableSpeaker) {
return;
}
if (existEnvironmentVariable('ACCESS_CODE')) {
const accessCode = getEnvironmentVariable('ACCESS_CODE');
if (accessCode !== key.accessCode) {
notify.invalidAccessCodeNotify();
return;
}
}
stopSpeechSynthesis();
setStatus('speaking');
setFinished(false);
Expand Down Expand Up @@ -219,6 +226,16 @@ const Content: React.FC<ContentProps> = ({ notify }) => {
const openaiApiModel = existEnvironmentVariable('OPENAI_MODEL')
? getEnvironmentVariable('OPENAI_MODEL')
: key.openaiModel;

if (existEnvironmentVariable('ACCESS_CODE')) {
const accessCode = getEnvironmentVariable('ACCESS_CODE');
if (accessCode !== key.accessCode) {
notify.invalidAccessCodeNotify();
setStatus('idle');
return;
}
}

sendRequest(
conversationsToSent as any,
openaiApiKey,
Expand Down Expand Up @@ -465,6 +482,7 @@ const Content: React.FC<ContentProps> = ({ notify }) => {
setTranscript={setTranscript}
setWaiting={setWaiting}
notify={notify}
accessCode={existEnvironmentVariable('ACCESS_CODE') ? key.accessCode : ''}
/>
)}
<div className="overflow-y-scroll h-full" ref={conversationRef}>
Expand Down
12 changes: 6 additions & 6 deletions src/components/ConversationPanel.tsx
Expand Up @@ -64,7 +64,7 @@ function ConversationPanel({
className="group relative rounded-lg hover:bg-gray-200 p-2 flex flex-row space-x-3 transition-colors duration-100"
>
<ChatIcon role={conversation.role} />
<div
<div
className="py-1 text-gray-800 markdown-content"
onMouseDown={handleMouseDown}
onMouseUp={handleMouseUp}
Expand All @@ -74,10 +74,10 @@ function ConversationPanel({
{marked(conversation.content ?? '')}
</div>
<div
className={`absolute right-2 top-2 group-hover:opacity-100 opacity-0 transition-colors duration-100 flex-row space-x-0.5 ${
isHidden ? 'hidden' : 'flex'
}`}
>
className={`absolute right-2 top-2 group-hover:opacity-100 opacity-0 transition-colors duration-100 flex-row space-x-0.5 ${
isHidden ? 'hidden' : 'flex'
}`}
>
<TippyButton
onClick={() => {
generateSpeech(conversation.content);
Expand Down Expand Up @@ -109,4 +109,4 @@ function ConversationPanel({
);
}

export default ConversationPanel;
export default ConversationPanel;
6 changes: 6 additions & 0 deletions src/components/Notification.tsx
Expand Up @@ -24,6 +24,12 @@ export const resetNotify = () => {
});
};

export const invalidAccessCodeNotify = () => {
toast.error(i18next.t('notification.invalid-access-code') as string, {
style: notificationStyle,
});
};

// OpenAI
export const invalidOpenAiKeyNotify = () => {
toast.error(i18next.t('notification.invalid-openai-key') as string, {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Settings/AboutSection.tsx
Expand Up @@ -51,7 +51,7 @@ const AboutSection: React.FC<AboutSectionProps> = ({}) => {
<SettingDivider />
<SettingTitle text={i18n.t('setting.about.version') as string} />
<SettingGroup>
<div className={'flex space-y-2 text-left ml-0.5 text-gray-700'}>Version: v0.4.0</div>
<div className={'flex space-y-2 text-left ml-0.5 text-gray-700'}>Version: v0.4.1</div>
</SettingGroup>
</div>
);
Expand Down
14 changes: 14 additions & 0 deletions src/components/Settings/ChatSection.tsx
Expand Up @@ -69,6 +69,20 @@ const ChatSection: React.FC<ChatSectionProps> = ({}) => {
/>
</>
)}
{existEnvironmentVariable('ACCESS_CODE') ? (
<SettingInput
text={i18n.t('setting.chat.access-code') as string}
helpText={i18n.t('setting.chat.access-code-tooltip') as string}
id={'openai-host'}
type={'text'}
value={key.accessCode}
onChange={e => setKey({ ...key, accessCode: e })}
placeholder={i18n.t('setting.chat.code') as string}
className={''}
/>
) : (
<></>
)}
</SettingGroup>

<SettingDivider />
Expand Down
6 changes: 5 additions & 1 deletion src/locales/en.json
Expand Up @@ -54,6 +54,9 @@
"setting": {
"chat": {
"title": "Chat",
"access-code": "Access Code",
"access-code-tooltip": "Enter the access code to use the application",
"code": "Code",
"api-key": "API Key",
"default-host-address": "chat.openai.com",
"openai": "OpenAI",
Expand Down Expand Up @@ -153,6 +156,7 @@
"azure-synthesis-error": "There was an error with Azure speech synthesis",
"azure-recognition-error": "There was an error with Azure speech recognition",
"polly-synthesis-error": "There was an error with Amazon Polly speech synthesis",
"cannot-be-empty": "This field cannot be empty"
"cannot-be-empty": "This field cannot be empty",
"invalid-access-code": "Invalid access code"
}
}
6 changes: 5 additions & 1 deletion src/locales/es.json
Expand Up @@ -54,6 +54,9 @@
"setting": {
"chat": {
"title": "Chat",
"access-code": "Access Code",
"access-code-tooltip": "Enter the access code to use the application",
"code": "Code",
"api-key": "Código de API",
"default-host-address": "chat.openai.com",
"openai": "OpenAI",
Expand Down Expand Up @@ -153,6 +156,7 @@
"azure-synthesis-error": "Se ha producido un error con la síntesis de voz de Azure",
"azure-recognition-error": "Se ha producido un error en el reconocimiento de voz de Azure",
"polly-synthesis-error": "Se ha producido un error con la síntesis de voz de Amazon Polly",
"cannot-be-empty": "This field cannot be empty"
"cannot-be-empty": "This field cannot be empty",
"invalid-access-code": "Invalid access code"
}
}
6 changes: 5 additions & 1 deletion src/locales/zh-CN.json
Expand Up @@ -54,6 +54,9 @@
"setting": {
"chat": {
"title": "对话",
"access-code": "访问密码",
"access-code-tooltip": "输入访问密码以使用应用",
"code": "Code",
"api-key": "API Key",
"default-host-address": "chat.openai.com",
"openai": "OpenAI",
Expand Down Expand Up @@ -153,6 +156,7 @@
"azure-synthesis-error": "Azure 语音合成错误",
"azure-recognition-error": "Azure 语音识别错误",
"polly-synthesis-error": "Amazon Polly 语音合成错误",
"cannot-be-empty": "不能为空"
"cannot-be-empty": "不能为空",
"invalid-access-code": "无效的访问密码"
}
}
2 changes: 1 addition & 1 deletion src/pages/Home.tsx
Expand Up @@ -5,7 +5,6 @@ import * as Notify from '../components/Notification';
import { browserName, isMobile } from 'react-device-detect';
import { useGlobalStore, useSessionStore } from '../store/module';
import { v4 as uuidv4 } from 'uuid';
import { current } from '@reduxjs/toolkit';
import { useTranslation } from 'react-i18next';

function Home() {
Expand All @@ -32,6 +31,7 @@ function Home() {
awsErrorNotify: Notify.awsErrorNotify,
emptyAzureKeyNotify: Notify.emptyAzureKeyNotify,
cannotBeEmptyNotify: Notify.cannotBeEmptyNotify,
invalidAccessCodeNotify: Notify.invalidAccessCodeNotify,
};

if (isMobile || browserName !== 'Chrome') {
Expand Down
1 change: 1 addition & 0 deletions src/store/module/global.ts
Expand Up @@ -22,6 +22,7 @@ const defaultGlobalState = {
locale: navigator.language.split(/[-_]/)[0] || '',
appearance: 'system',
key: {
accessCode: '',
openaiApiKey: '',
openaiModel: '',
openaiHost: '',
Expand Down
1 change: 1 addition & 0 deletions src/store/reducer/global.ts
Expand Up @@ -6,6 +6,7 @@ export const globalSlice = createSlice({
locale: navigator.language.split(/[-_]/)[0] || '',
appearance: 'system',
key: {
accessCode: '',
openaiApiKey: '',
openaiModel: '',
openaiHost: '',
Expand Down

1 comment on commit cbb9b3f

@vercel
Copy link

@vercel vercel bot commented on cbb9b3f Apr 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

speechgpt – ./

speechgpt-git-main-hahahumble.vercel.app
speechgpt-hahahumble.vercel.app
speechgpt-alpha.vercel.app

Please sign in to comment.