-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
128 lines (93 loc) · 3.41 KB
/
run.py
File metadata and controls
128 lines (93 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
llm-qlient • Qt desktop client for interacting with local LLMs
This file is a part of the llm-qlient
project and distributed under MIT license.
Repository: https://github.com/kadir014/llm-qlient
Issues: https://github.com/kadir014/llm-qlient/issues
"""
# Easy auto-installation & runner script
#
# For the first run:
# 1. Installs uv
# 2. Setups environment
# 3. Installs llama-cpp-python (JamePeng's fork)
# 4. Runs the app
#
# For the subsequent runs, the installation will not occur unless opted.
import os
import sys
import subprocess
import platform
import argparse
PYTHON = sys.executable
if not PYTHON:
PYTHON = "python"
args: argparse.Namespace
def normalize(string: str) -> str:
return string.strip().lower()
def log(msg: str) -> None:
print(f"\n[RUNNER] {msg}\n")
def run(cmd: str) -> int:
return subprocess.run(cmd, shell=True).returncode
def install_llama_cpp_python() -> None:
# llama-cpp-python supported backends for JamePeng's fork
# https://github.com/JamePeng/llama-cpp-python?tab=readme-ov-file#supported-backends
print(
"\n"
"[RUNNER] Do you want GPU acceleration? (for the llama-cpp-python build)\n"
" 0: None, build for CPU (default)\n"
" 1: Nvidia CUDA\n"
" 2: AMD ROCm"
)
gpu_accel = normalize(input("(Leave blank for default)"))
if gpu_accel in {"1", "one", "cuda", "nvidia"}:
os.environ["CMAKE_ARGS"] = "-DGGML_CUDA=on"
log("Nvidia CUDA support is chosen")
elif gpu_accel in {"2", "two", "rocm", "amd"}:
os.environ["CMAKE_ARGS"] = "-DGGML_HIP=ON -DGPU_TARGETS=gfx1030"
log("AMD ROCm support is chosen")
else:
os.environ["CMAKE_ARGS"] = "-DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS"
log("CPU support is chosen")
force_arg = "--force-reinstall" if args.force_reinstall else ""
dist = "llama-cpp-python @ git+https://github.com/JamePeng/llama-cpp-python.git"
run(f"uv pip install --upgrade {force_arg} \"{dist}\"")
def is_installed(module: str) -> bool:
"""
Check if the given module is installed in current uv environment.
Parameters
----------
module
Name of the module to check
"""
out = subprocess.check_output("uv pip list").decode("utf-8")
return normalize(module) in normalize(out)
def main() -> None:
""" Installer & runner entry point. """
log(f"Python: {platform.python_version()} - {platform.python_compiler()}")
run(f"\"{PYTHON}\" -m pip install --upgrade uv")
run("uv venv --no-clear")
if not args.no_install and (not is_installed("llama-cpp-python") or args.force_reinstall):
log("llama-cpp-python is not found, installing.")
install_llama_cpp_python()
else:
log("llama-cpp-python is found, skipping installing.")
run("uv run main --debug")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="run.py",
description="Easy auto installation & runner script"
)
parser.add_argument(
"--force-reinstall",
help="Force reinstall llama-cpp-python even if it exists.",
action="store_true"
)
parser.add_argument(
"--no-install",
help="Don't install llama-cpp-python even if it's not found. "
"This overwrites any installation related conditions.",
action="store_true"
)
args = parser.parse_args()
main()