This commit is contained in:
josc146
2023-05-17 21:20:41 +08:00
parent 11813454de
commit df8eef5f64
14 changed files with 438 additions and 121 deletions

View File

@@ -5,6 +5,8 @@ import {Button} from '@fluentui/react-components';
import {observer} from 'mobx-react-lite';
import {exit, readRoot, switchModel, updateConfig} from '../apis';
import {toast} from 'react-toastify';
import manifest from '../../../manifest.json';
import {getStrategy} from '../utils';
const mainButtonText = {
[ModelStatus.Offline]: 'Run',
@@ -39,11 +41,12 @@ const onClickMainButton = async () => {
frequency_penalty: modelConfig.apiParameters.frequencyPenalty
});
switchModel({
model: `models\\${modelConfig.modelParameters.modelName}`,
strategy: commonStore.getStrategy(modelConfig)
model: `${manifest.localModelDir}/${modelConfig.modelParameters.modelName}`,
strategy: getStrategy(modelConfig)
}).then((r) => {
if (r.ok) {
commonStore.setModelStatus(ModelStatus.Working);
toast('Startup Completed', {type: 'success'});
} else if (r.status === 304) {
toast('Loading Model', {type: 'info'});
} else {

View File

@@ -13,6 +13,9 @@ import {Page} from '../components/Page';
import {useNavigate} from 'react-router';
import {RunButton} from '../components/RunButton';
import {updateConfig} from '../apis';
import {ConvertModel} from '../../wailsjs/go/backend_golang/App';
import manifest from '../../../manifest.json';
import {getStrategy, refreshLocalModels} from '../utils';
export const Configs: FC = observer(() => {
const [selectedIndex, setSelectedIndex] = React.useState(commonStore.currentModelConfigIndex);
@@ -173,7 +176,18 @@ export const Configs: FC = observer(() => {
}}/>
</div>
}/>
<ToolTipButton text="Convert" desc="Convert model with these configs"/>
<ToolTipButton text="Convert" desc="Convert model with these configs" onClick={() => {
const modelPath = `${manifest.localModelDir}/${selectedConfig.modelParameters.modelName}`;
const strategy = getStrategy(selectedConfig);
const newModelPath = modelPath + '-' + strategy.replace(/[> *+]/g, '-');
toast('Start Converting', {autoClose: 1000, type: 'info'});
ConvertModel(modelPath, strategy, newModelPath).then(() => {
toast(`Convert Success - ${newModelPath}`, {type: 'success'});
refreshLocalModels({models: commonStore.modelSourceList});
}).catch(e => {
toast(`Convert Failed - ${e}`, {type: 'error'});
});
}}/>
<Labeled label="Device" content={
<Dropdown style={{minWidth: 0}} className="grow" value={selectedConfig.modelParameters.device}
selectedOptions={[selectedConfig.modelParameters.device]}

View File

@@ -21,6 +21,7 @@ import {DownloadFile, OpenFileFolder} from '../../wailsjs/go/backend_golang/App'
import manifest from '../../../manifest.json';
import {toast} from 'react-toastify';
import {Page} from '../components/Page';
import {refreshModels} from '../utils';
const columns: TableColumnDefinition<ModelSourceItem>[] = [
createTableColumn<ModelSourceItem>({
@@ -106,13 +107,13 @@ const columns: TableColumnDefinition<ModelSourceItem>[] = [
{
item.isLocal &&
<ToolTipButton desc="Open Folder" icon={<Folder20Regular/>} onClick={() => {
OpenFileFolder(`.\\${manifest.localModelPath}\\${item.name}`);
OpenFileFolder(`./${manifest.localModelDir}/${item.name}`);
}}/>
}
{item.downloadUrl && !item.isLocal &&
<ToolTipButton desc="Download" icon={<ArrowDownload20Regular/>} onClick={() => {
toast(`Downloading ${item.name}`);
DownloadFile(`./${manifest.localModelPath}/${item.name}`, item.downloadUrl!);
DownloadFile(`./${manifest.localModelDir}/${item.name}`, item.downloadUrl!);
}}/>}
{item.url && <ToolTipButton desc="Open Url" icon={<Open20Regular/>} onClick={() => {
BrowserOpenURL(item.url!);
@@ -131,7 +132,9 @@ export const Models: FC = observer(() => {
<div className="flex flex-col gap-1">
<div className="flex justify-between">
<Text weight="medium">Model Source Manifest List</Text>
<ToolTipButton desc="Refresh" icon={<ArrowClockwise20Regular/>}/>
<ToolTipButton desc="Refresh" icon={<ArrowClockwise20Regular/>} onClick={() => {
refreshModels(false);
}}/>
</div>
<Text size={100}>
Provide JSON file URLs for the models manifest. Separate URLs with semicolons. The "models"
@@ -146,6 +149,7 @@ export const Models: FC = observer(() => {
items={commonStore.modelSourceList}
columns={columns}
sortable={true}
defaultSortState={{sortColumn: 'actions', sortDirection: 'ascending'}}
style={{display: 'flex'}}
className="flex-col w-full"
>

View File

@@ -1,18 +1,14 @@
import commonStore, {defaultModelConfigs, ModelSourceItem} from './stores/commonStore';
import {ListDirFiles, ReadJson, SaveJson} from '../wailsjs/go/backend_golang/App';
import manifest from '../../manifest.json';
import commonStore, {defaultModelConfigs} from './stores/commonStore';
import {ReadJson} from '../wailsjs/go/backend_golang/App';
import {LocalConfig, refreshModels} from './utils';
export async function startup() {
initCache();
await initConfig();
}
type Cache = {
models: ModelSourceItem[]
}
async function initConfig() {
await ReadJson('config.json').then((configData) => {
await ReadJson('config.json').then((configData: LocalConfig) => {
if (configData.modelSourceManifestList)
commonStore.setModelSourceManifestList(configData.modelSourceManifestList);
if (configData.modelConfigs && Array.isArray(configData.modelConfigs))
@@ -27,80 +23,5 @@ async function initConfig() {
}
async function initCache() {
let cache: Cache = {models: []};
await ReadJson('cache.json').then((cacheData: Cache) => {
cache = cacheData;
}).catch(
async () => {
cache = {models: manifest.models};
await SaveJson('cache.json', cache).catch(() => {
});
}
);
// built-in
commonStore.setModelSourceList(cache.models);
await ListDirFiles(manifest.localModelPath).then((data) => {
cache.models.push(...data.flatMap(d => {
if (!d.isDir && d.name.endsWith('.pth'))
return [{
name: d.name,
size: d.size,
lastUpdated: d.modTime,
isLocal: true
}];
return [];
}));
}).catch(() => {
});
for (let i = 0; i < cache.models.length; i++) {
if (!cache.models[i].lastUpdatedMs)
cache.models[i].lastUpdatedMs = Date.parse(cache.models[i].lastUpdated);
for (let j = i + 1; j < cache.models.length; j++) {
if (!cache.models[j].lastUpdatedMs)
cache.models[j].lastUpdatedMs = Date.parse(cache.models[j].lastUpdated);
if (cache.models[i].name === cache.models[j].name) {
if (cache.models[i].size === cache.models[j].size) {
if (cache.models[i].lastUpdatedMs! < cache.models[j].lastUpdatedMs!) {
cache.models[i] = Object.assign({}, cache.models[i], cache.models[j]);
} else {
cache.models[i] = Object.assign({}, cache.models[j], cache.models[i]);
}
} // else is bad local file
cache.models.splice(j, 1);
j--;
}
}
}
// local files
commonStore.setModelSourceList(cache.models);
await SaveJson('cache.json', cache).catch(() => {
});
const manifestUrls = commonStore.modelSourceManifestList.split(/[,;\n]/);
const requests = manifestUrls.filter(url => url.endsWith('.json')).map(
url => fetch(url, {cache: 'no-cache'}).then(r => r.json()));
await Promise.allSettled(requests)
.then((data: PromiseSettledResult<Cache>[]) => {
cache.models.push(...data.flatMap(d => {
if (d.status === 'fulfilled')
return d.value.models;
return [];
}));
})
.catch(() => {
});
cache.models = cache.models.filter((model, index, self) => {
return model.name.endsWith('.pth')
&& index === self.findIndex(
m => m.name === model.name || (m.SHA256 === model.SHA256 && m.size === model.size));
});
// remote files
commonStore.setModelSourceList(cache.models);
await SaveJson('cache.json', cache).catch(() => {
});
await refreshModels(true);
}

View File

@@ -1,5 +1,5 @@
import {makeAutoObservable} from 'mobx';
import {SaveJson} from '../../wailsjs/go/backend_golang/App';
import {saveConfigs} from '../utils';
export enum ModelStatus {
Offline,
@@ -83,28 +83,6 @@ class CommonStore {
modelSourceManifestList: string = 'https://cdn.jsdelivr.net/gh/josstorer/RWKV-Runner/manifest.json;';
modelSourceList: ModelSourceItem[] = [];
async saveConfigs() {
await SaveJson('config.json', {
modelSourceManifestList: this.modelSourceManifestList,
currentModelConfigIndex: this.currentModelConfigIndex,
modelConfigs: this.modelConfigs
});
}
getStrategy(modelConfig: ModelConfig | undefined = undefined) {
let params: ModelParameters;
if (modelConfig) params = modelConfig.modelParameters;
else params = this.getCurrentModelConfig().modelParameters;
let strategy = '';
strategy += (params.device === 'CPU' ? 'cpu' : 'cuda') + ' ';
strategy += (params.precision === 'fp16' ? 'fp16' : params.precision === 'int8' ? 'fp16i8' : 'fp32');
if (params.storedLayers < params.maxStoredLayers)
strategy += ` *${params.storedLayers}+`;
if (params.enableHighPrecisionForLastLayer)
strategy += ' -> cpu fp32 *1';
return strategy;
}
getCurrentModelConfig = () => {
return this.modelConfigs[this.currentModelConfigIndex];
};
@@ -116,19 +94,19 @@ class CommonStore {
setCurrentConfigIndex = (index: number, saveConfig: boolean = true) => {
this.currentModelConfigIndex = index;
if (saveConfig)
this.saveConfigs();
saveConfigs();
};
setModelConfig = (index: number, config: ModelConfig, saveConfig: boolean = true) => {
this.modelConfigs[index] = config;
if (saveConfig)
this.saveConfigs();
saveConfigs();
};
setModelConfigs = (configs: ModelConfig[], saveConfig: boolean = true) => {
this.modelConfigs = configs;
if (saveConfig)
this.saveConfigs();
saveConfigs();
};
createModelConfig = (config: ModelConfig = defaultModelConfigs[0], saveConfig: boolean = true) => {
@@ -136,7 +114,7 @@ class CommonStore {
config.name = new Date().toLocaleString();
this.modelConfigs.push(config);
if (saveConfig)
this.saveConfigs();
saveConfigs();
};
deleteModelConfig = (index: number, saveConfig: boolean = true) => {
@@ -151,7 +129,7 @@ class CommonStore {
this.setCurrentConfigIndex(this.modelConfigs.length - 1);
}
if (saveConfig)
this.saveConfigs();
saveConfigs();
};
setModelSourceManifestList = (value: string) => {

135
frontend/src/utils/index.ts Normal file
View File

@@ -0,0 +1,135 @@
import {ListDirFiles, ReadJson, SaveJson} from '../../wailsjs/go/backend_golang/App';
import manifest from '../../../manifest.json';
import commonStore, {ModelConfig, ModelParameters, ModelSourceItem} from '../stores/commonStore';
export type Cache = {
models: ModelSourceItem[]
}
export type LocalConfig = {
modelSourceManifestList: string
currentModelConfigIndex: number
modelConfigs: ModelConfig[]
}
export async function refreshBuiltInModels(readCache: boolean = false) {
let cache: Cache = {models: []};
if (readCache)
await ReadJson('cache.json').then((cacheData: Cache) => {
cache = cacheData;
}).catch(
async () => {
cache = {models: manifest.models};
}
);
else cache = {models: manifest.models};
commonStore.setModelSourceList(cache.models);
await saveCache().catch(() => {
});
return cache;
}
export async function refreshLocalModels(cache: Cache) {
cache.models = cache.models.filter(m => !m.isLocal);
await ListDirFiles(manifest.localModelDir).then((data) => {
cache.models.push(...data.flatMap(d => {
if (!d.isDir && d.name.endsWith('.pth'))
return [{
name: d.name,
size: d.size,
lastUpdated: d.modTime,
isLocal: true
}];
return [];
}));
}).catch(() => {
});
for (let i = 0; i < cache.models.length; i++) {
if (!cache.models[i].lastUpdatedMs)
cache.models[i].lastUpdatedMs = Date.parse(cache.models[i].lastUpdated);
for (let j = i + 1; j < cache.models.length; j++) {
if (!cache.models[j].lastUpdatedMs)
cache.models[j].lastUpdatedMs = Date.parse(cache.models[j].lastUpdated);
if (cache.models[i].name === cache.models[j].name) {
if (cache.models[i].size === cache.models[j].size) {
if (cache.models[i].lastUpdatedMs! < cache.models[j].lastUpdatedMs!) {
cache.models[i] = Object.assign({}, cache.models[i], cache.models[j]);
} else {
cache.models[i] = Object.assign({}, cache.models[j], cache.models[i]);
}
} // else is bad local file
cache.models.splice(j, 1);
j--;
}
}
}
commonStore.setModelSourceList(cache.models);
await saveCache().catch(() => {
});
}
export async function refreshRemoteModels(cache: Cache) {
const manifestUrls = commonStore.modelSourceManifestList.split(/[,;\n]/);
const requests = manifestUrls.filter(url => url.endsWith('.json')).map(
url => fetch(url, {cache: 'no-cache'}).then(r => r.json()));
await Promise.allSettled(requests)
.then((data: PromiseSettledResult<Cache>[]) => {
cache.models.push(...data.flatMap(d => {
if (d.status === 'fulfilled')
return d.value.models;
return [];
}));
})
.catch(() => {
});
cache.models = cache.models.filter((model, index, self) => {
return model.name.endsWith('.pth')
&& index === self.findIndex(
m => m.name === model.name || (m.SHA256 === model.SHA256 && m.size === model.size));
});
commonStore.setModelSourceList(cache.models);
await saveCache().catch(() => {
});
}
export const refreshModels = async (readCache: boolean = false) => {
const cache = await refreshBuiltInModels(readCache);
await refreshLocalModels(cache);
await refreshRemoteModels(cache);
};
export const getStrategy = (modelConfig: ModelConfig | undefined = undefined) => {
let params: ModelParameters;
if (modelConfig) params = modelConfig.modelParameters;
else params = commonStore.getCurrentModelConfig().modelParameters;
let strategy = '';
strategy += (params.device === 'CPU' ? 'cpu' : 'cuda') + ' ';
strategy += (params.precision === 'fp16' ? 'fp16' : params.precision === 'int8' ? 'fp16i8' : 'fp32');
if (params.storedLayers < params.maxStoredLayers)
strategy += ` *${params.storedLayers}+`;
if (params.enableHighPrecisionForLastLayer)
strategy += ' -> cpu fp32 *1';
return strategy;
};
export const saveConfigs = async () => {
const data: LocalConfig = {
modelSourceManifestList: commonStore.modelSourceManifestList,
currentModelConfigIndex: commonStore.currentModelConfigIndex,
modelConfigs: commonStore.modelConfigs
};
return SaveJson('config.json', data);
};
export const saveCache = async () => {
const data: Cache = {
models: commonStore.modelSourceList
};
return SaveJson('cache.json', data);
};