From 51c5696bb9a2bec4ddbd606a0fe60b35366a0e5b Mon Sep 17 00:00:00 2001 From: josc146 Date: Wed, 14 Jun 2023 22:21:17 +0800 Subject: [PATCH] improve python dependencies installation --- backend-golang/rwkv.go | 44 +++++++------------ backend-golang/utils.go | 14 ++++-- backend-python/requirements.txt | Bin 180 -> 230 bytes backend-python/requirements_versions.txt | Bin 312 -> 362 bytes backend-python/requirements_without_cyac.txt | Bin 168 -> 218 bytes main.go | 2 + 6 files changed, 30 insertions(+), 30 deletions(-) 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 5ab0abc57f853ff06e680ba3321e39807bc736de..b3239db4f5dec15ce7d9a829926be87139afb5fc 100644 GIT binary patch delta 69 zcmdnO_>9r^|GyH3e1;;1WQGg|UIs1(1h0%Clc5-h^BMBcWD*%l8B#zp6S-Fc0BWib AQ~&?~ delta 19 acmaFHxP_7X|GyH3e1;;1WQL51wkrWl)(1QQ diff --git a/backend-python/requirements_versions.txt b/backend-python/requirements_versions.txt index 79a0167796e3695c8bf307c63f9c65363251ee8d..76e6989257c5c6cbdec1f70908ea8d3fbba5b884 100644 GIT binary patch delta 70 zcmdnN^oq&$|GyH3e1;;1WQGg|UIs1(1h0%Clc5-h^BMBcWD*%l8B#zp8@Y8E0ev?R A0{{R3 delta 20 bcmaFGw1bKJ|GyH3e1;;1WQL55wz`Y}R96P; diff --git a/backend-python/requirements_without_cyac.txt b/backend-python/requirements_without_cyac.txt index 30857abeac802137112736e1e4e4000c34cb6d44..bad09f4fd4d492ea4f12a32cebb4f179784da154 100644 GIT binary patch delta 69 zcmZ3%c#F~Y|GyH3e1;;1WQGg|UIs1(1h0%Clc5-h^BMBcWD*%l8B#zp6S?OC0AxN8 AFaQ7m delta 19 acmcb`xPp=U|GyH3e1;;1WQL51wsQeX8V48v 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