diff --git a/backend-golang/rwkv.go b/backend-golang/rwkv.go index 395ac2c..cd68e82 100644 --- a/backend-golang/rwkv.go +++ b/backend-golang/rwkv.go @@ -2,9 +2,11 @@ package backend_golang import ( "errors" + "os" "os/exec" "runtime" "strconv" + "strings" ) func (a *App) StartServer(python string, port int, host string) (string, error) { @@ -48,6 +50,9 @@ func (a *App) InstallPyDep(python string, cnMirror bool) (string, error) { var err error if python == "" { python, err = GetPython() + if runtime.GOOS == "windows" { + python = `"` + python + `"` + } } if err != nil { return "", err @@ -55,39 +60,24 @@ func (a *App) InstallPyDep(python string, cnMirror bool) (string, error) { if runtime.GOOS == "windows" { ChangeFileLine("./py310/python310._pth", 3, "Lib\\site-packages") - } - - if runtime.GOOS == "windows" { - if cnMirror { - _, err = Cmd(python, "./backend-python/get-pip.py", "-i", "https://pypi.tuna.tsinghua.edu.cn/simple") - } else { - _, err = Cmd(python, "./backend-python/get-pip.py") + installScript := python + " ./backend-python/get-pip.py -i https://pypi.tuna.tsinghua.edu.cn/simple\n" + + python + " -m pip install torch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 --index-url https://download.pytorch.org/whl/cu117\n" + + python + " -m pip install -r ./backend-python/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple\n" + + "exit" + if !cnMirror { + installScript = strings.Replace(installScript, " -i https://pypi.tuna.tsinghua.edu.cn/simple", "", -1) + installScript = strings.Replace(installScript, "requirements.txt", "requirements_versions.txt", -1) } + err = os.WriteFile("./install-py-dep.bat", []byte(installScript), 0644) if err != nil { return "", err } + return Cmd("install-py-dep.bat") } - 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") + if cnMirror { + return Cmd(python, "-m", "pip", "install", "-r", "./backend-python/requirements_without_cyac.txt", "-i", "https://pypi.tuna.tsinghua.edu.cn/simple") } else { - _, err = Cmd(python, "-m", "pip", "install", "torch", "torchvision", "torchaudio") - } - if err != nil { - return "", err - } - - if runtime.GOOS == "windows" { - if cnMirror { - return Cmd(python, "-m", "pip", "install", "-r", "./backend-python/requirements.txt", "-i", "https://pypi.tuna.tsinghua.edu.cn/simple") - } else { - return Cmd(python, "-m", "pip", "install", "-r", "./backend-python/requirements_versions.txt") - } - } else { - if cnMirror { - return Cmd(python, "-m", "pip", "install", "-r", "./backend-python/requirements_without_cyac.txt", "-i", "https://pypi.tuna.tsinghua.edu.cn/simple") - } else { - return Cmd(python, "-m", "pip", "install", "-r", "./backend-python/requirements_without_cyac.txt") - } + return Cmd(python, "-m", "pip", "install", "-r", "./backend-python/requirements_without_cyac.txt") } } diff --git a/backend-golang/utils.go b/backend-golang/utils.go index ddea470..d14c288 100644 --- a/backend-golang/utils.go +++ b/backend-golang/utils.go @@ -17,7 +17,7 @@ import ( func Cmd(args ...string) (string, error) { switch platform := runtime.GOOS; platform { case "windows": - if err := os.WriteFile("./cmd-helper.bat", []byte("start /wait %*"), 0644); err != nil { + if err := os.WriteFile("./cmd-helper.bat", []byte("start %*"), 0644); err != nil { return "", err } cmdHelper, err := filepath.Abs("./cmd-helper") @@ -115,11 +115,19 @@ func GetPython() (string, error) { if err != nil { return "", errors.New("failed to unzip python") } else { - return "py310/python.exe", nil + python, err := filepath.Abs("py310/python.exe") + if err != nil { + return "", err + } + return python, nil } } } else { - return "py310/python.exe", nil + python, err := filepath.Abs("py310/python.exe") + if err != nil { + return "", err + } + return python, nil } case "darwin": return "python3", nil diff --git a/backend-python/requirements.txt b/backend-python/requirements.txt index 5ab0abc..b3239db 100644 Binary files a/backend-python/requirements.txt and b/backend-python/requirements.txt differ diff --git a/backend-python/requirements_versions.txt b/backend-python/requirements_versions.txt index 79a0167..76e6989 100644 Binary files a/backend-python/requirements_versions.txt and b/backend-python/requirements_versions.txt differ diff --git a/backend-python/requirements_without_cyac.txt b/backend-python/requirements_without_cyac.txt index 30857ab..bad09f4 100644 Binary files a/backend-python/requirements_without_cyac.txt and b/backend-python/requirements_without_cyac.txt differ diff --git a/main.go b/main.go index f885f42..f9b2a90 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "embed" + "os" "runtime/debug" "strings" @@ -29,6 +30,7 @@ func main() { backend.CopyEmbed(cyac) backend.CopyEmbed(cyacInfo) backend.CopyEmbed(py) + os.Mkdir("models", os.ModePerm) } // Create an instance of the app structure