RWKV-Runner/backend-golang/rwkv.go

89 lines
2.4 KiB
Go
Raw Normal View History

2023-05-06 23:39:23 +08:00
package backend_golang
import (
2023-05-20 23:34:33 +08:00
"errors"
"os"
2023-05-06 23:39:23 +08:00
"os/exec"
2023-06-15 21:57:54 +08:00
"path/filepath"
2023-05-27 14:40:59 +08:00
"runtime"
2023-05-17 11:39:00 +08:00
"strconv"
"strings"
2023-05-06 23:39:23 +08:00
)
2023-05-31 15:45:26 +08:00
func (a *App) StartServer(python string, port int, host string) (string, error) {
var err error
if python == "" {
python, err = GetPython()
}
2023-05-17 11:39:00 +08:00
if err != nil {
return "", err
}
2023-05-24 22:03:30 +08:00
return Cmd(python, "./backend-python/main.py", strconv.Itoa(port), host)
2023-05-20 23:34:33 +08:00
}
2023-05-31 15:45:26 +08:00
func (a *App) ConvertModel(python string, modelPath string, strategy string, outPath string) (string, error) {
var err error
if python == "" {
python, err = GetPython()
}
2023-05-06 23:39:23 +08:00
if err != nil {
2023-05-13 20:15:18 +08:00
return "", err
2023-05-06 23:39:23 +08:00
}
2023-06-02 22:20:57 +08:00
return Cmd(python, "./backend-python/convert_model.py", "--in", modelPath, "--out", outPath, "--strategy", strategy)
2023-05-06 23:39:23 +08:00
}
2023-05-17 21:20:41 +08:00
2023-05-31 15:45:26 +08:00
func (a *App) DepCheck(python string) error {
var err error
if python == "" {
python, err = GetPython()
}
2023-05-20 23:34:33 +08:00
if err != nil {
return err
}
2023-06-01 16:54:21 +08:00
out, err := exec.Command(python, a.exDir+"./backend-python/dep_check.py").CombinedOutput()
2023-05-20 23:34:33 +08:00
if err != nil {
return errors.New("DepCheck Error: " + string(out))
}
return nil
2023-05-17 23:27:52 +08:00
}
2023-05-31 15:45:26 +08:00
func (a *App) InstallPyDep(python string, cnMirror bool) (string, error) {
var err error
if python == "" {
python, err = GetPython()
if runtime.GOOS == "windows" {
2023-06-15 21:57:54 +08:00
python, err = filepath.Abs(python)
if err != nil {
return "", err
}
python = `"` + python + `"`
}
2023-05-31 15:45:26 +08:00
}
2023-05-20 23:34:33 +08:00
if err != nil {
return "", err
}
2023-05-27 14:40:59 +08:00
if runtime.GOOS == "windows" {
ChangeFileLine("./py310/python310._pth", 3, "Lib\\site-packages")
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")
2023-05-20 23:34:33 +08:00
}
if cnMirror {
return Cmd(python, "-m", "pip", "install", "-r", "./backend-python/requirements_without_cyac.txt", "-i", "https://pypi.tuna.tsinghua.edu.cn/simple")
2023-05-21 10:49:45 +08:00
} else {
return Cmd(python, "-m", "pip", "install", "-r", "./backend-python/requirements_without_cyac.txt")
2023-05-21 10:49:45 +08:00
}
2023-05-17 21:20:41 +08:00
}