This commit is contained in:
josc146 2023-05-20 15:15:49 +08:00
parent 358705625a
commit 4a5fceb014
4 changed files with 88 additions and 64 deletions

View File

@ -88,5 +88,7 @@
"Downloads": "下载", "Downloads": "下载",
"Pause": "暂停", "Pause": "暂停",
"Continue": "继续", "Continue": "继续",
"Check": "查看" "Check": "查看",
"Model file not found": "模型文件不存在",
"Can not find download url": "找不到下载地址"
} }

View File

@ -1,16 +1,16 @@
import React, {FC, MouseEventHandler, ReactElement} from 'react'; import React, {FC, MouseEventHandler, ReactElement} from 'react';
import commonStore, {ModelStatus} from '../stores/commonStore'; import commonStore, {ModelStatus} from '../stores/commonStore';
import {StartServer} from '../../wailsjs/go/backend_golang/App'; import {AddToDownloadList, FileExists, StartServer} from '../../wailsjs/go/backend_golang/App';
import {Button} from '@fluentui/react-components'; import {Button} from '@fluentui/react-components';
import {observer} from 'mobx-react-lite'; import {observer} from 'mobx-react-lite';
import {exit, readRoot, switchModel, updateConfig} from '../apis'; import {exit, readRoot, switchModel, updateConfig} from '../apis';
import {toast} from 'react-toastify'; import {toast} from 'react-toastify';
import manifest from '../../../manifest.json'; import manifest from '../../../manifest.json';
import {getStrategy} from '../utils'; import {getStrategy, toastWithButton} from '../utils';
import {useTranslation} from 'react-i18next'; import {useTranslation} from 'react-i18next';
import {t} from 'i18next';
import {ToolTipButton} from './ToolTipButton'; import {ToolTipButton} from './ToolTipButton';
import {Play16Regular, Stop16Regular} from '@fluentui/react-icons'; import {Play16Regular, Stop16Regular} from '@fluentui/react-icons';
import {useNavigate} from 'react-router';
const mainButtonText = { const mainButtonText = {
[ModelStatus.Offline]: 'Run', [ModelStatus.Offline]: 'Run',
@ -26,69 +26,90 @@ const iconModeButtonIcon: { [modelStatus: number]: ReactElement } = {
[ModelStatus.Working]: <Stop16Regular/> [ModelStatus.Working]: <Stop16Regular/>
}; };
const onClickMainButton = async () => {
const modelConfig = commonStore.getCurrentModelConfig();
const port = modelConfig.apiParameters.apiPort;
if (commonStore.modelStatus === ModelStatus.Offline) {
commonStore.setModelStatus(ModelStatus.Starting);
await exit(1000).catch(() => {
});
StartServer(port);
let timeoutCount = 6;
let loading = false;
const intervalId = setInterval(() => {
readRoot()
.then(r => {
if (r.ok && !loading) {
clearInterval(intervalId);
commonStore.setModelStatus(ModelStatus.Loading);
loading = true;
toast(t('Loading Model'), {type: 'info'});
updateConfig({
max_tokens: modelConfig.apiParameters.maxResponseToken,
temperature: modelConfig.apiParameters.temperature,
top_p: modelConfig.apiParameters.topP,
presence_penalty: modelConfig.apiParameters.presencePenalty,
frequency_penalty: modelConfig.apiParameters.frequencyPenalty
});
switchModel({
model: `${manifest.localModelDir}/${modelConfig.modelParameters.modelName}`,
strategy: getStrategy(modelConfig)
}).then((r) => {
if (r.ok) {
commonStore.setModelStatus(ModelStatus.Working);
toast(t('Startup Completed'), {type: 'success'});
} else if (r.status === 304) {
toast(t('Loading Model'), {type: 'info'});
} else {
commonStore.setModelStatus(ModelStatus.Offline);
toast(t('Failed to switch model'), {type: 'error'});
}
});
}
}).catch(() => {
if (timeoutCount <= 0) {
clearInterval(intervalId);
commonStore.setModelStatus(ModelStatus.Offline);
}
});
timeoutCount--;
}, 1000);
} else {
commonStore.setModelStatus(ModelStatus.Offline);
exit();
}
};
export const RunButton: FC<{ onClickRun?: MouseEventHandler, iconMode?: boolean }> export const RunButton: FC<{ onClickRun?: MouseEventHandler, iconMode?: boolean }>
= observer(({ = observer(({
onClickRun, onClickRun,
iconMode iconMode
}) => { }) => {
const {t} = useTranslation(); const {t} = useTranslation();
const navigate = useNavigate();
const onClickMainButton = async () => {
const modelConfig = commonStore.getCurrentModelConfig();
const modelName = modelConfig.modelParameters.modelName;
const modelPath = `./${manifest.localModelDir}/${modelName}`;
if (!await FileExists(modelPath)) {
toastWithButton(t('Model file not found'), t('Download'), () => {
const downloadUrl = commonStore.modelSourceList.find(item => item.name === modelName)?.downloadUrl;
if (downloadUrl) {
toastWithButton(`${t('Downloading')} ${modelName}`, t('Check'), () => {
navigate({pathname: '/downloads'});
},
{autoClose: 3000});
AddToDownloadList(modelPath, downloadUrl);
} else {
toast(t('Can not find download url'), {type: 'error'});
}
});
return;
}
const port = modelConfig.apiParameters.apiPort;
if (commonStore.modelStatus === ModelStatus.Offline) {
commonStore.setModelStatus(ModelStatus.Starting);
await exit(1000).catch(() => {
});
StartServer(port);
let timeoutCount = 6;
let loading = false;
const intervalId = setInterval(() => {
readRoot()
.then(r => {
if (r.ok && !loading) {
clearInterval(intervalId);
commonStore.setModelStatus(ModelStatus.Loading);
loading = true;
toast(t('Loading Model'), {type: 'info'});
updateConfig({
max_tokens: modelConfig.apiParameters.maxResponseToken,
temperature: modelConfig.apiParameters.temperature,
top_p: modelConfig.apiParameters.topP,
presence_penalty: modelConfig.apiParameters.presencePenalty,
frequency_penalty: modelConfig.apiParameters.frequencyPenalty
});
switchModel({
model: `${manifest.localModelDir}/${modelConfig.modelParameters.modelName}`,
strategy: getStrategy(modelConfig)
}).then((r) => {
if (r.ok) {
commonStore.setModelStatus(ModelStatus.Working);
toast(t('Startup Completed'), {type: 'success'});
} else if (r.status === 304) {
toast(t('Loading Model'), {type: 'info'});
} else {
commonStore.setModelStatus(ModelStatus.Offline);
toast(t('Failed to switch model'), {type: 'error'});
}
});
}
}).catch(() => {
if (timeoutCount <= 0) {
clearInterval(intervalId);
commonStore.setModelStatus(ModelStatus.Offline);
}
});
timeoutCount--;
}, 1000);
} else {
commonStore.setModelStatus(ModelStatus.Offline);
exit();
}
};
const onClick = async (e: any) => { const onClick = async (e: any) => {
if (commonStore.modelStatus === ModelStatus.Offline) if (commonStore.modelStatus === ModelStatus.Offline)

View File

@ -138,7 +138,7 @@ const columns: TableColumnDefinition<ModelSourceItem>[] = [
toastWithButton(`${t('Downloading')} ${item.name}`, t('Check'), () => { toastWithButton(`${t('Downloading')} ${item.name}`, t('Check'), () => {
navigate({pathname: '/downloads'}); navigate({pathname: '/downloads'});
}, },
{autoClose: 3000, position: 'top-center'}); {autoClose: 3000});
AddToDownloadList(`./${manifest.localModelDir}/${item.name}`, item.downloadUrl!); AddToDownloadList(`./${manifest.localModelDir}/${item.name}`, item.downloadUrl!);
}}/>} }}/>}
{item.url && <ToolTipButton desc={t('Open Url')} icon={<Open20Regular/>} onClick={() => { {item.url && <ToolTipButton desc={t('Open Url')} icon={<Open20Regular/>} onClick={() => {

View File

@ -225,6 +225,9 @@ export async function checkUpdate() {
}); });
}); });
}, 500); }, 500);
}, {
autoClose: false,
position: 'bottom-left'
}); });
} }
} else { } else {
@ -248,8 +251,6 @@ export function toastWithButton(text: string, buttonText: string, onClickButton:
<Button appearance="primary" onClick={onClickButton}>{buttonText}</Button> <Button appearance="primary" onClick={onClickButton}>{buttonText}</Button>
</div>, </div>,
{ {
autoClose: false,
position: 'bottom-left',
type: 'info', type: 'info',
...options ...options
}); });