improve python dependencies installation

This commit is contained in:
josc146 2023-06-14 22:21:17 +08:00
parent 64f0610ed7
commit 51c5696bb9
6 changed files with 30 additions and 30 deletions

View File

@ -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")
}
}

View File

@ -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

Binary file not shown.

View File

@ -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