From b63370928dc2462a4b80f9b539fa27ea3b9dab53 Mon Sep 17 00:00:00 2001 From: josc146 Date: Thu, 1 Jun 2023 16:54:21 +0800 Subject: [PATCH] macOS --- .gitignore | 1 + Makefile | 1 + backend-golang/app.go | 13 ++++++- backend-golang/download.go | 4 +- backend-golang/file.go | 34 ++++++++++------- backend-golang/rwkv.go | 10 +++-- backend-golang/utils.go | 41 +++++++++++++++++---- backend-python/dep_check.py | 1 - backend-python/routes/config.py | 1 - frontend/src/_locales/zh-hans/main.json | 3 +- frontend/src/pages/Configs.tsx | 5 +++ frontend/src/pages/Downloads.tsx | 2 +- frontend/wailsjs/go/backend_golang/App.d.ts | 0 frontend/wailsjs/go/backend_golang/App.js | 0 frontend/wailsjs/go/models.ts | 0 15 files changed, 85 insertions(+), 31 deletions(-) mode change 100644 => 100755 frontend/wailsjs/go/backend_golang/App.d.ts mode change 100644 => 100755 frontend/wailsjs/go/backend_golang/App.js mode change 100644 => 100755 frontend/wailsjs/go/models.ts diff --git a/.gitignore b/.gitignore index f5373d4..1515b8e 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ __pycache__ /backend-python/wkv_cuda *.exe *.old +.DS_Store diff --git a/Makefile b/Makefile index 0575d63..f6d5fb7 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,7 @@ build-windows: build-macos: @echo ---- build for macos + wails build -ldflags "-s -w" dev: wails dev diff --git a/backend-golang/app.go b/backend-golang/app.go index 69d2323..78639f9 100644 --- a/backend-golang/app.go +++ b/backend-golang/app.go @@ -5,6 +5,7 @@ import ( "net/http" "os" "os/exec" + "path/filepath" "runtime" "github.com/minio/selfupdate" @@ -13,7 +14,9 @@ import ( // App struct type App struct { - ctx context.Context + ctx context.Context + exDir string + cmdPrefix string } // NewApp creates a new App application struct @@ -25,6 +28,14 @@ func NewApp() *App { // so we can call the runtime methods func (a *App) OnStartup(ctx context.Context) { a.ctx = ctx + ex, _ := os.Executable() + a.exDir = filepath.Dir(ex) + a.cmdPrefix = "" + + if runtime.GOOS == "darwin" { + a.exDir += "/../../../" + a.cmdPrefix = "cd " + a.exDir + " && " + } a.downloadLoop() } diff --git a/backend-golang/download.go b/backend-golang/download.go index 9a856cf..032126b 100644 --- a/backend-golang/download.go +++ b/backend-golang/download.go @@ -9,7 +9,7 @@ import ( ) func (a *App) DownloadFile(path string, url string) error { - _, err := grab.Get(path, url) + _, err := grab.Get(a.exDir+path, url) if err != nil { return err } @@ -81,7 +81,7 @@ func (a *App) AddToDownloadList(path string, url string) { downloadList = append(downloadList, DownloadStatus{ resp: nil, Name: filepath.Base(path), - Path: path, + Path: a.exDir + path, Url: url, Downloading: true, }) diff --git a/backend-golang/file.go b/backend-golang/file.go index e34b71f..48eb759 100644 --- a/backend-golang/file.go +++ b/backend-golang/file.go @@ -2,7 +2,7 @@ package backend_golang import ( "encoding/json" - "fmt" + "errors" "io" "os" "os/exec" @@ -18,14 +18,14 @@ func (a *App) SaveJson(fileName string, jsonData any) error { return err } - if err := os.WriteFile(fileName, text, 0644); err != nil { + if err := os.WriteFile(a.exDir+fileName, text, 0644); err != nil { return err } return nil } func (a *App) ReadJson(fileName string) (any, error) { - file, err := os.ReadFile(fileName) + file, err := os.ReadFile(a.exDir + fileName) if err != nil { return nil, err } @@ -40,7 +40,7 @@ func (a *App) ReadJson(fileName string) (any, error) { } func (a *App) FileExists(fileName string) bool { - _, err := os.Stat(fileName) + _, err := os.Stat(a.exDir + fileName) return err == nil } @@ -52,7 +52,7 @@ type FileInfo struct { } func (a *App) ReadFileInfo(fileName string) (FileInfo, error) { - info, err := os.Stat(fileName) + info, err := os.Stat(a.exDir + fileName) if err != nil { return FileInfo{}, err } @@ -65,7 +65,7 @@ func (a *App) ReadFileInfo(fileName string) (FileInfo, error) { } func (a *App) ListDirFiles(dirPath string) ([]FileInfo, error) { - files, err := os.ReadDir(dirPath) + files, err := os.ReadDir(a.exDir + dirPath) if err != nil { return nil, err } @@ -87,7 +87,7 @@ func (a *App) ListDirFiles(dirPath string) ([]FileInfo, error) { } func (a *App) DeleteFile(path string) error { - err := os.Remove(path) + err := os.Remove(a.exDir + path) if err != nil { return err } @@ -95,18 +95,18 @@ func (a *App) DeleteFile(path string) error { } func (a *App) CopyFile(src string, dst string) error { - sourceFile, err := os.Open(src) + sourceFile, err := os.Open(a.exDir + src) if err != nil { return err } defer sourceFile.Close() - err = os.MkdirAll(dst[:strings.LastIndex(dst, "/")], 0755) + err = os.MkdirAll(a.exDir+dst[:strings.LastIndex(dst, "/")], 0755) if err != nil { return err } - destFile, err := os.Create(dst) + destFile, err := os.Create(a.exDir + dst) if err != nil { return err } @@ -120,7 +120,7 @@ func (a *App) CopyFile(src string, dst string) error { } func (a *App) OpenFileFolder(path string) error { - absPath, err := filepath.Abs(path) + absPath, err := filepath.Abs(a.exDir + path) if err != nil { return err } @@ -131,10 +131,16 @@ func (a *App) OpenFileFolder(path string) error { if err != nil { return err } + return nil case "darwin": - fmt.Println("Running on macOS") + cmd := exec.Command("open", "-R", absPath) + err := cmd.Run() + if err != nil { + return err + } + return nil case "linux": - fmt.Println("Running on Linux") + println("unsupported OS") } - return nil + return errors.New("unsupported OS") } diff --git a/backend-golang/rwkv.go b/backend-golang/rwkv.go index 1e8cdac..d20e018 100644 --- a/backend-golang/rwkv.go +++ b/backend-golang/rwkv.go @@ -26,7 +26,7 @@ func (a *App) ConvertModel(python string, modelPath string, strategy string, out if err != nil { return "", err } - return Cmd(python, "./backend-python/convert_model.py", "--in", modelPath, "--out", outPath, "--strategy", strategy) + return Cmd(python, "./backend-python/convert_model.py", "--in", modelPath, "--out", outPath, "--strategy", `"`+strategy+`"`) } func (a *App) DepCheck(python string) error { @@ -37,7 +37,7 @@ func (a *App) DepCheck(python string) error { if err != nil { return err } - out, err := exec.Command(python, "./backend-python/dep_check.py").CombinedOutput() + out, err := exec.Command(python, a.exDir+"./backend-python/dep_check.py").CombinedOutput() if err != nil { return errors.New("DepCheck Error: " + string(out)) } @@ -63,7 +63,11 @@ func (a *App) InstallPyDep(python string, cnMirror bool) (string, error) { if err != nil { return "", err } - _, err = Cmd(python, "-m", "pip", "install", "torch==1.13.1", "torchvision==0.14.1", "torchaudio==0.13.1", "--index-url", "https://download.pytorch.org/whl/cu117") + if runtime.GOOS == "windows" { + _, err = Cmd(python, "-m", "pip", "install", "torch==1.13.1", "torchvision==0.14.1", "torchaudio==0.13.1", "--index-url", "https://download.pytorch.org/whl/cu117") + } else { + _, err = Cmd(python, "-m", "pip", "install", "torch", "torchvision", "torchaudio") + } if err != nil { return "", err } diff --git a/backend-golang/utils.go b/backend-golang/utils.go index b9e5af0..3bfaf82 100644 --- a/backend-golang/utils.go +++ b/backend-golang/utils.go @@ -15,12 +15,8 @@ import ( ) func Cmd(args ...string) (string, error) { - path, err := filepath.Abs(args[0]) - if err != nil { - return "", err - } - args[0] = path - if runtime.GOOS == "windows" { + switch platform := runtime.GOOS; platform { + case "windows": _, err := os.Stat("cmd-helper.bat") if err != nil { if err := os.WriteFile("./cmd-helper.bat", []byte("start %*"), 0644); err != nil { @@ -31,13 +27,33 @@ func Cmd(args ...string) (string, error) { if err != nil { return "", err } + + path, err := filepath.Abs(args[0]) + if err != nil { + return "", err + } + args[0] = path + cmd := exec.Command(cmdHelper, args...) out, err := cmd.CombinedOutput() if err != nil { return "", err } return string(out), nil - } else { + case "darwin": + ex, err := os.Executable() + if err != nil { + return "", err + } + exDir := filepath.Dir(ex) + "/../../../" + cmd := exec.Command("osascript", "-e", `tell application "Terminal" to do script "`+"cd "+exDir+" && "+strings.Join(args, " ")+`"`) + err = cmd.Start() + if err != nil { + return "", err + } + cmd.Wait() + return "", nil + case "linux": cmd := exec.Command(args[0], args[1:]...) err := cmd.Start() if err != nil { @@ -46,9 +62,19 @@ func Cmd(args ...string) (string, error) { cmd.Wait() return "", nil } + return "", errors.New("unsupported OS") } func CopyEmbed(efs embed.FS) error { + prefix := "" + if runtime.GOOS == "darwin" { + ex, err := os.Executable() + if err != nil { + return err + } + prefix = filepath.Dir(ex) + "/../../../" + } + err := fs.WalkDir(efs, ".", func(path string, d fs.DirEntry, err error) error { if d.IsDir() { return nil @@ -61,6 +87,7 @@ func CopyEmbed(efs embed.FS) error { return err } + path = prefix + path err = os.MkdirAll(path[:strings.LastIndex(path, "/")], 0755) if err != nil { return err diff --git a/backend-python/dep_check.py b/backend-python/dep_check.py index e9a17cf..87dde75 100644 --- a/backend-python/dep_check.py +++ b/backend-python/dep_check.py @@ -1,4 +1,3 @@ -import cyac import GPUtil import torch import rwkv diff --git a/backend-python/routes/config.py b/backend-python/routes/config.py index 52e9cfe..a28c4a9 100644 --- a/backend-python/routes/config.py +++ b/backend-python/routes/config.py @@ -2,7 +2,6 @@ import pathlib from fastapi import APIRouter, HTTPException, Response, status as Status from pydantic import BaseModel -from langchain.llms import RWKV from utils.rwkv import * from utils.torch import * import global_var diff --git a/frontend/src/_locales/zh-hans/main.json b/frontend/src/_locales/zh-hans/main.json index 902d22e..5334833 100644 --- a/frontend/src/_locales/zh-hans/main.json +++ b/frontend/src/_locales/zh-hans/main.json @@ -132,5 +132,6 @@ "Are you sure you want to reset all configs? This will obtain the latest preset configs, but will override your custom configs and cannot be undone.": "你确定要重置所有配置吗?这会获取最新的预设配置,但会覆盖你的自定义配置,并且无法撤销", "Advanced": "高级", "Custom Python Path": "自定义Python路径", - "Custom Models Path": "自定义模型路径" + "Custom Models Path": "自定义模型路径", + "MacOS is not supported yet, please convert manually.": "暂不支持MacOS, 请手动转换" } \ No newline at end of file diff --git a/frontend/src/pages/Configs.tsx b/frontend/src/pages/Configs.tsx index bb9f033..4526c96 100644 --- a/frontend/src/pages/Configs.tsx +++ b/frontend/src/pages/Configs.tsx @@ -835,6 +835,11 @@ export const Configs: FC = observer(() => { } /> { + if(commonStore.platform=="darwin"){ + toast(t("MacOS is not supported yet, please convert manually."), { type: 'info' }) + return + } + const modelPath = `${commonStore.settings.customModelsPath}/${selectedConfig.modelParameters.modelName}`; if (await FileExists(modelPath)) { const strategy = getStrategy(selectedConfig); diff --git a/frontend/src/pages/Downloads.tsx b/frontend/src/pages/Downloads.tsx index 1813cb5..c585fd5 100644 --- a/frontend/src/pages/Downloads.tsx +++ b/frontend/src/pages/Downloads.tsx @@ -62,7 +62,7 @@ export const Downloads: FC = observer(() => { ContinueDownload(status.url); }} />} } onClick={() => { - OpenFileFolder(status.path); + OpenFileFolder(`${commonStore.settings.customModelsPath}/${status.name}`); }} /> diff --git a/frontend/wailsjs/go/backend_golang/App.d.ts b/frontend/wailsjs/go/backend_golang/App.d.ts old mode 100644 new mode 100755 diff --git a/frontend/wailsjs/go/backend_golang/App.js b/frontend/wailsjs/go/backend_golang/App.js old mode 100644 new mode 100755 diff --git a/frontend/wailsjs/go/models.ts b/frontend/wailsjs/go/models.ts old mode 100644 new mode 100755