This commit is contained in:
josc146 2023-05-13 20:15:18 +08:00
parent ffec039feb
commit 08e024a998
16 changed files with 328 additions and 119 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ __pycache__
package.json.md5 package.json.md5
cache.json cache.json
stats.html stats.html
models

View File

@ -1,18 +0,0 @@
package backend_golang
import (
"encoding/json"
"os"
)
func (a *App) SaveJson(fileName string, jsonData interface{}) string {
text, err := json.MarshalIndent(jsonData, "", " ")
if err != nil {
return err.Error()
}
if err := os.WriteFile(fileName, text, 0644); err != nil {
return err.Error()
}
return ""
}

63
backend-golang/file.go Normal file
View File

@ -0,0 +1,63 @@
package backend_golang
import (
"encoding/json"
"os"
"github.com/cavaliergopher/grab/v3"
)
func (a *App) SaveJson(fileName string, jsonData any) error {
text, err := json.MarshalIndent(jsonData, "", " ")
if err != nil {
return err
}
if err := os.WriteFile(fileName, text, 0644); err != nil {
return err
}
return nil
}
func (a *App) ReadJson(fileName string) (any, error) {
file, err := os.ReadFile(fileName)
if err != nil {
return nil, err
}
var data any
err = json.Unmarshal(file, &data)
if err != nil {
return nil, err
}
return data, nil
}
func (a *App) FileExists(fileName string) (bool, error) {
_, err := os.Stat(fileName)
if err == nil {
return true, nil
}
return false, err
}
func (a *App) FileInfo(fileName string) (any, error) {
info, err := os.Stat(fileName)
if err != nil {
return nil, err
}
return map[string]any{
"name": info.Name(),
"size": info.Size(),
"isDir": info.IsDir(),
}, nil
}
func (a *App) DownloadFile(path string, url string) error {
_, err := grab.Get(path, url)
if err != nil {
return err
}
return nil
}

View File

@ -4,12 +4,12 @@ import (
"os/exec" "os/exec"
) )
func (a *App) StartServer(strategy string, modelPath string) string { func (a *App) StartServer(strategy string, modelPath string) (string, error) {
//cmd := exec.Command(`explorer`, `/select,`, `e:\RWKV-4-Raven-7B-v10-Eng49%25-Chn50%25-Other1%25-20230420-ctx4096.pth`) //cmd := exec.Command(`explorer`, `/select,`, `e:\RWKV-4-Raven-7B-v10-Eng49%25-Chn50%25-Other1%25-20230420-ctx4096.pth`)
cmd := exec.Command("cmd-helper", "python", "./backend-python/main.py", strategy, modelPath) cmd := exec.Command("cmd-helper", "python", "./backend-python/main.py", strategy, modelPath)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return err.Error() return "", err
} }
return string(out) return string(out), nil
} }

View File

@ -1,10 +1,17 @@
import React, {FC, ReactElement} from 'react'; import React, {FC, MouseEventHandler, ReactElement} from 'react';
import {Button, Tooltip} from '@fluentui/react-components'; import {Button, Tooltip} from '@fluentui/react-components';
export const ToolTipButton: FC<{ text?: string, desc: string, icon?: ReactElement }> = ({text, desc, icon}) => { export const ToolTipButton: FC<{
text?: string, desc: string, icon?: ReactElement, onClick?: MouseEventHandler
}> = ({
text,
desc,
icon,
onClick
}) => {
return ( return (
<Tooltip content={desc} showDelay={0} hideDelay={0} relationship="label"> <Tooltip content={desc} showDelay={0} hideDelay={0} relationship="label">
<Button icon={icon}>{text}</Button> <Button icon={icon} onClick={onClick}>{text}</Button>
</Tooltip> </Tooltip>
); );
}; };

View File

@ -1,15 +1,18 @@
import React from 'react' import React from 'react';
import {createRoot} from 'react-dom/client' import {createRoot} from 'react-dom/client';
import './style.css' import './style.css';
import App from './App' import App from './App';
import {HashRouter} from 'react-router-dom'; import {HashRouter} from 'react-router-dom';
import {startup} from './startup';
const container = document.getElementById('root') startup().then(() => {
const container = document.getElementById('root');
const root = createRoot(container!) const root = createRoot(container!);
root.render( root.render(
<HashRouter> <HashRouter>
<App/> <App/>
</HashRouter> </HashRouter>
) );
});

View File

@ -1,6 +1,6 @@
import {Button, CompoundButton, Dropdown, Link, Option, Text} from '@fluentui/react-components'; import {Button, CompoundButton, Dropdown, Link, Option, Text} from '@fluentui/react-components';
import React, {FC, ReactElement} from 'react'; import React, {FC, ReactElement} from 'react';
import Banner from '../assets/images/banner.jpg'; import banner from '../assets/images/banner.jpg';
import { import {
Chat20Regular, Chat20Regular,
DataUsageSettings20Regular, DataUsageSettings20Regular,
@ -64,7 +64,7 @@ export const Home: FC = observer(() => {
const onClickMainButton = async () => { const onClickMainButton = async () => {
if (commonStore.modelStatus === ModelStatus.Offline) { if (commonStore.modelStatus === ModelStatus.Offline) {
commonStore.updateModelStatus(ModelStatus.Starting); commonStore.setModelStatus(ModelStatus.Starting);
StartServer('cuda fp16i8', 'E:\\RWKV-4-Raven-3B-v10-Eng49%-Chn50%-Other1%-20230419-ctx4096.pth'); StartServer('cuda fp16i8', 'E:\\RWKV-4-Raven-3B-v10-Eng49%-Chn50%-Other1%-20230419-ctx4096.pth');
let timeoutCount = 5; let timeoutCount = 5;
@ -74,7 +74,7 @@ export const Home: FC = observer(() => {
.then(r => { .then(r => {
if (r.ok && !loading) { if (r.ok && !loading) {
clearInterval(intervalId); clearInterval(intervalId);
commonStore.updateModelStatus(ModelStatus.Loading); commonStore.setModelStatus(ModelStatus.Loading);
loading = true; loading = true;
fetch('http://127.0.0.1:8000/update-config', { fetch('http://127.0.0.1:8000/update-config', {
method: 'POST', method: 'POST',
@ -84,27 +84,27 @@ export const Home: FC = observer(() => {
body: JSON.stringify({}) body: JSON.stringify({})
}).then(async (r) => { }).then(async (r) => {
if (r.ok) if (r.ok)
commonStore.updateModelStatus(ModelStatus.Working); commonStore.setModelStatus(ModelStatus.Working);
}); });
} }
}).catch(() => { }).catch(() => {
if (timeoutCount <= 0) { if (timeoutCount <= 0) {
clearInterval(intervalId); clearInterval(intervalId);
commonStore.updateModelStatus(ModelStatus.Offline); commonStore.setModelStatus(ModelStatus.Offline);
} }
}); });
timeoutCount--; timeoutCount--;
}, 1000); }, 1000);
} else { } else {
commonStore.updateModelStatus(ModelStatus.Offline); commonStore.setModelStatus(ModelStatus.Offline);
fetch('http://127.0.0.1:8000/exit', {method: 'POST'}); fetch('http://127.0.0.1:8000/exit', {method: 'POST'});
} }
}; };
return ( return (
<div className="flex flex-col justify-between h-full"> <div className="flex flex-col justify-between h-full">
<img className="rounded-xl select-none hidden sm:block" src={Banner}/> <img className="rounded-xl select-none hidden sm:block" src={banner}/>
<div className="flex flex-col gap-2"> <div className="flex flex-col gap-2">
<Text size={600} weight="medium">Introduction</Text> <Text size={600} weight="medium">Introduction</Text>
<div className="h-40 overflow-y-auto p-1"> <div className="h-40 overflow-y-auto p-1">

View File

@ -1,4 +1,4 @@
import React, {FC, useEffect} from 'react'; import React, {FC} from 'react';
import { import {
createTableColumn, createTableColumn,
DataGrid, DataGrid,
@ -12,40 +12,18 @@ import {
Text, Text,
Textarea Textarea
} from '@fluentui/react-components'; } from '@fluentui/react-components';
import {EditRegular} from '@fluentui/react-icons/lib/fonts';
import {ToolTipButton} from '../components/ToolTipButton'; import {ToolTipButton} from '../components/ToolTipButton';
import {ArrowClockwise20Regular} from '@fluentui/react-icons'; import {ArrowClockwise20Regular, ArrowDownload20Regular, Open20Regular} from '@fluentui/react-icons';
import {observer} from 'mobx-react-lite';
import commonStore, {ModelSourceItem} from '../stores/commonStore';
import {BrowserOpenURL} from '../../wailsjs/runtime';
import {DownloadFile} from '../../wailsjs/go/backend_golang/App';
type Operation = { const columns: TableColumnDefinition<ModelSourceItem>[] = [
icon: JSX.Element; createTableColumn<ModelSourceItem>({
desc: string
}
type Item = {
filename: string;
desc: string;
size: number;
lastUpdated: number;
actions: Operation[];
isLocal: boolean;
};
const items: Item[] = [
{
filename: 'RWKV-4-Raven-14B-v11x-Eng99%-Other1%-20230501-ctx8192.pth',
desc: 'Mainly English language corpus',
size: 28297309490,
lastUpdated: 1,
actions: [{icon: <EditRegular/>, desc: 'Edit'}],
isLocal: false
}
];
const columns: TableColumnDefinition<Item>[] = [
createTableColumn<Item>({
columnId: 'file', columnId: 'file',
compare: (a, b) => { compare: (a, b) => {
return a.filename.localeCompare(b.filename); return a.name.localeCompare(b.name);
}, },
renderHeaderCell: () => { renderHeaderCell: () => {
return 'File'; return 'File';
@ -53,15 +31,15 @@ const columns: TableColumnDefinition<Item>[] = [
renderCell: (item) => { renderCell: (item) => {
return ( return (
<TableCellLayout className="break-all"> <TableCellLayout className="break-all">
{item.filename} {item.name}
</TableCellLayout> </TableCellLayout>
); );
} }
}), }),
createTableColumn<Item>({ createTableColumn<ModelSourceItem>({
columnId: 'desc', columnId: 'desc',
compare: (a, b) => { compare: (a, b) => {
return a.desc.localeCompare(b.desc); return a.desc['en'].localeCompare(b.desc['en']);
}, },
renderHeaderCell: () => { renderHeaderCell: () => {
return 'Desc'; return 'Desc';
@ -69,12 +47,12 @@ const columns: TableColumnDefinition<Item>[] = [
renderCell: (item) => { renderCell: (item) => {
return ( return (
<TableCellLayout> <TableCellLayout>
{item.desc} {item.desc['en']}
</TableCellLayout> </TableCellLayout>
); );
} }
}), }),
createTableColumn<Item>({ createTableColumn<ModelSourceItem>({
columnId: 'size', columnId: 'size',
compare: (a, b) => { compare: (a, b) => {
return a.size - b.size; return a.size - b.size;
@ -90,10 +68,14 @@ const columns: TableColumnDefinition<Item>[] = [
); );
} }
}), }),
createTableColumn<Item>({ createTableColumn<ModelSourceItem>({
columnId: 'lastUpdated', columnId: 'lastUpdated',
compare: (a, b) => { compare: (a, b) => {
return a.lastUpdated - b.lastUpdated; if (!a.lastUpdatedMs)
a.lastUpdatedMs = Date.parse(a.lastUpdated);
if (!b.lastUpdatedMs)
b.lastUpdatedMs = Date.parse(b.lastUpdated);
return a.lastUpdatedMs - b.lastUpdatedMs;
}, },
renderHeaderCell: () => { renderHeaderCell: () => {
return 'Last updated'; return 'Last updated';
@ -103,10 +85,10 @@ const columns: TableColumnDefinition<Item>[] = [
return new Date(item.lastUpdated).toLocaleString(); return new Date(item.lastUpdated).toLocaleString();
} }
}), }),
createTableColumn<Item>({ createTableColumn<ModelSourceItem>({
columnId: 'actions', columnId: 'actions',
compare: (a, b) => { compare: (a, b) => {
return a.isLocal === b.isLocal ? 0 : a.isLocal ? -1 : 1; return a.isDownloading ? 0 : a.isLocal ? 1 : 2;
}, },
renderHeaderCell: () => { renderHeaderCell: () => {
return 'Actions'; return 'Actions';
@ -114,54 +96,63 @@ const columns: TableColumnDefinition<Item>[] = [
renderCell: (item) => { renderCell: (item) => {
return ( return (
<TableCellLayout> <TableCellLayout>
<div className="flex gap-1">
<ToolTipButton desc="Download" icon={<ArrowDownload20Regular/>} onClick={() => {
DownloadFile(`./models/${item.name}`, item.downloadUrl);
}}/>
<ToolTipButton desc="Open Url" icon={<Open20Regular/>} onClick={() => {
BrowserOpenURL(item.url);
}}/>
</div>
</TableCellLayout> </TableCellLayout>
); );
} }
}) })
]; ];
export const Models: FC = () => { export const Models: FC = observer(() => {
useEffect(() => {
fetch('https://cdn.jsdelivr.net/gh/josstorer/RWKV-Runner/manifest.json')
.then(
res => res.json().then(console.log)
);
}, []);
return ( return (
<div className="flex flex-col box-border gap-5 p-2"> <div className="flex flex-col gap-2 p-2 h-full">
<Text size={600}>In Development</Text> <Text size={600}>Models</Text>
<div className="flex flex-col gap-1"> <div className="flex flex-col gap-1">
<div className="flex justify-between"> <div className="flex justify-between">
<Text weight="medium">Model Source Url List</Text> <Text weight="medium">Model Source Manifest List</Text>
<ToolTipButton desc="Refresh" icon={<ArrowClockwise20Regular/>}/> <ToolTipButton desc="Refresh" icon={<ArrowClockwise20Regular/>}/>
</div> </div>
<Text size={100}>description</Text> <Text size={100}>Provide JSON file URLs for the models manifest. Separate URLs with semicolons. The "models"
field in JSON files will be parsed into the following table.</Text>
<Textarea size="large" resize="vertical" <Textarea size="large" resize="vertical"
defaultValue="https://cdn.jsdelivr.net/gh/josstorer/RWKV-Runner/manifest.json;"/> defaultValue={commonStore.modelSourceManifestList}
onChange={(e, data) => commonStore.setModelSourceManifestList(data.value)}/>
</div> </div>
<DataGrid <div className="flex grow overflow-hidden">
items={items} <DataGrid
columns={columns} items={commonStore.modelSourceList}
sortable={true} columns={columns}
> sortable={true}
<DataGridHeader> style={{display: 'flex'}}
<DataGridRow> className="flex-col"
{({renderHeaderCell}) => ( >
<DataGridHeaderCell>{renderHeaderCell()}</DataGridHeaderCell> <DataGridHeader>
)} <DataGridRow>
</DataGridRow> {({renderHeaderCell}) => (
</DataGridHeader> <DataGridHeaderCell>{renderHeaderCell()}</DataGridHeaderCell>
<DataGridBody<Item>>
{({item, rowId}) => (
<DataGridRow<Item> key={rowId}>
{({renderCell}) => (
<DataGridCell>{renderCell(item)}</DataGridCell>
)} )}
</DataGridRow> </DataGridRow>
)} </DataGridHeader>
</DataGridBody> <div className="overflow-y-auto overflow-x-hidden">
</DataGrid> <DataGridBody<ModelSourceItem>>
{({item, rowId}) => (
<DataGridRow<ModelSourceItem> key={rowId}>
{({renderCell}) => (
<DataGridCell>{renderCell(item)}</DataGridCell>
)}
</DataGridRow>
)}
</DataGridBody>
</div>
</DataGrid>
</div>
</div> </div>
); );
}; });

46
frontend/src/startup.ts Normal file
View File

@ -0,0 +1,46 @@
import commonStore, {ModelSourceItem} from './stores/commonStore';
import {ReadJson, SaveJson} from '../wailsjs/go/backend_golang/App';
import manifest from '../../manifest.json';
export async function startup() {
initConfig();
}
type Cache = {
models: ModelSourceItem[]
}
async function initConfig() {
let cache: Cache = {models: []};
await ReadJson('cache.json').then((cacheData: Cache) => {
cache = cacheData;
}).catch(
() => {
cache = {models: manifest.models};
SaveJson('cache.json', cache).catch(() => {
});
}
);
commonStore.setModelSourceList(cache.models);
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.SHA256 === model.SHA256 && m.size === model.size);
});
commonStore.setModelSourceList(cache.models);
SaveJson('cache.json', cache).catch(() => {
});
}

View File

@ -7,15 +7,101 @@ export enum ModelStatus {
Working, Working,
} }
export type ModelSourceItem = {
name: string;
desc: { [lang: string]: string; };
size: number;
lastUpdated: string;
SHA256: string;
url: string;
downloadUrl: string;
isLocal?: boolean;
isDownloading?: boolean;
lastUpdatedMs?: number;
};
export type ApiParameters = {
apiPort: number
maxResponseToken: number;
temperature: number;
topP: number;
presencePenalty: number;
countPenalty: number;
}
export type ModelParameters = {
modelName: string;
device: string;
precision: string;
streamedLayers: number;
enableHighPrecisionForLastLayer: boolean;
}
export type ModelConfig = {
configName: string;
apiParameters: ApiParameters
modelParameters: ModelParameters
}
const defaultModelConfigs: ModelConfig[] = [
{
configName: 'Default',
apiParameters: {
apiPort: 8000,
maxResponseToken: 1000,
temperature: 1,
topP: 1,
presencePenalty: 0,
countPenalty: 0
},
modelParameters: {
modelName: '124M',
device: 'CPU',
precision: 'fp32',
streamedLayers: 1,
enableHighPrecisionForLastLayer: false
}
}
];
class CommonStore { class CommonStore {
constructor() { constructor() {
makeAutoObservable(this); makeAutoObservable(this);
} }
modelStatus: ModelStatus = ModelStatus.Offline; modelStatus: ModelStatus = ModelStatus.Offline;
updateModelStatus = (status: ModelStatus) => { currentModelConfigIndex: number = 0;
modelConfigs: ModelConfig[] = defaultModelConfigs;
modelSourceManifestList: string = 'https://cdn.jsdelivr.net/gh/josstorer/RWKV-Runner/manifest.json;';
modelSourceList: ModelSourceItem[] = [];
setModelStatus = (status: ModelStatus) => {
this.modelStatus = status; this.modelStatus = status;
}; };
setCurrentConfigIndex = (index: number) => {
this.currentModelConfigIndex = index;
};
setModelConfig = (index: number, config: ModelConfig) => {
this.modelConfigs[index] = config;
};
createModelConfig = (config: ModelConfig = defaultModelConfigs[0]) => {
this.modelConfigs.push(config);
};
deleteModelConfig = (index: number) => {
this.modelConfigs.splice(index, 1);
};
setModelSourceManifestList = (value: string) => {
this.modelSourceManifestList = value;
};
setModelSourceList = (value: ModelSourceItem[]) => {
this.modelSourceList = value;
};
} }
export default new CommonStore(); export default new CommonStore();

View File

@ -5,6 +5,7 @@
body { body {
margin: 0; margin: 0;
overflow: hidden; overflow: hidden;
height: 100%;
} }
* { * {

View File

@ -1,6 +1,14 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT // This file is automatically generated. DO NOT EDIT
export function SaveJson(arg1:string,arg2:any):Promise<string>; export function DownloadFile(arg1:string,arg2:string):Promise<void>;
export function FileExists(arg1:string):Promise<boolean>;
export function FileInfo(arg1:string):Promise<any>;
export function ReadJson(arg1:string):Promise<any>;
export function SaveJson(arg1:string,arg2:any):Promise<void>;
export function StartServer(arg1:string,arg2:string):Promise<string>; export function StartServer(arg1:string,arg2:string):Promise<string>;

View File

@ -2,6 +2,22 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL // Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT // This file is automatically generated. DO NOT EDIT
export function DownloadFile(arg1, arg2) {
return window['go']['backend_golang']['App']['DownloadFile'](arg1, arg2);
}
export function FileExists(arg1) {
return window['go']['backend_golang']['App']['FileExists'](arg1);
}
export function FileInfo(arg1) {
return window['go']['backend_golang']['App']['FileInfo'](arg1);
}
export function ReadJson(arg1) {
return window['go']['backend_golang']['App']['ReadJson'](arg1);
}
export function SaveJson(arg1, arg2) { export function SaveJson(arg1, arg2) {
return window['go']['backend_golang']['App']['SaveJson'](arg1, arg2); return window['go']['backend_golang']['App']['SaveJson'](arg1, arg2);
} }

5
go.mod
View File

@ -2,7 +2,10 @@ module rwkv-runner
go 1.18 go 1.18
require github.com/wailsapp/wails/v2 v2.4.1 require (
github.com/cavaliergopher/grab/v3 v3.0.1
github.com/wailsapp/wails/v2 v2.4.1
)
require ( require (
github.com/bep/debounce v1.2.1 // indirect github.com/bep/debounce v1.2.1 // indirect

2
go.sum
View File

@ -1,5 +1,7 @@
github.com/bep/debounce v1.2.1 h1:v67fRdBA9UQu2NhLFXrSg0Brw7CexQekrBwDMM8bzeY= github.com/bep/debounce v1.2.1 h1:v67fRdBA9UQu2NhLFXrSg0Brw7CexQekrBwDMM8bzeY=
github.com/bep/debounce v1.2.1/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0= github.com/bep/debounce v1.2.1/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0=
github.com/cavaliergopher/grab/v3 v3.0.1 h1:4z7TkBfmPjmLAAmkkAZNX/6QJ1nNFdv3SdIHXju0Fr4=
github.com/cavaliergopher/grab/v3 v3.0.1/go.mod h1:1U/KNnD+Ft6JJiYoYBAimKH2XrYptb8Kl3DFGmsjpq4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

View File

@ -28,7 +28,7 @@ func main() {
Assets: assets, Assets: assets,
}, },
OnStartup: app.OnStartup, OnStartup: app.OnStartup,
Bind: []interface{}{ Bind: []any{
app, app,
}, },
}) })