-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
projects_functions.py
222 lines (186 loc) · 10.4 KB
/
projects_functions.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import subprocess
import os
from util.CONSTANTS import *
import requests
import util.installation_status as installation_status
from util.json_tools_projects import get_pref_project_data
from util.path_handler import full_path
def convert_to_backslashes(file_path):
return file_path.replace("/", "\\")
def get_project_path(project_data):
if pref_project_path(project_data):
return convert_to_backslashes(pref_project_path(project_data))
else:
# return os.path.abspath(project_data['repo_name'])
return os.path.join(full_path, project_data['repo_name'])
def get_venv_path(project_data):
return f"{os.path.join(get_project_path(project_data), 'venv')}"
def get_entry_point(project_data, key):
return os.path.join(get_project_path(project_data), project_data['entry_point'][key])
def get_pref_project_path(project_name, project_pref_path):
project_path = os.path.join(project_pref_path,project_name)
return project_path
def pref_project_path(project_data):
project_id = project_data['id']
if get_pref_project_data(project_id):
pref_project_data =get_pref_project_data(project_id)
if pref_project_data['isSet']:
return pref_project_data['path']
else:
return False
def clone_repo(project_data):
project_name = project_data['repo_name']
git_clone_url = project_data['git_clone_url']
project_path = get_project_path(project_data)
subprocess.run(["git", "clone", git_clone_url, project_path])
print(f"{project_name} cloned into {project_path}")
def create_virtual_environment(project_data, args=[]):
project_name = project_data['repo_name']
venv_path = get_venv_path(project_data)
print(f"Creating virtual environment for {project_name} at {venv_path} ")
subprocess.run(["python", "-m", "venv", venv_path], shell=True)
print(f"Virtual environment created for {project_name} at {venv_path}")
def update(project_data,args=[]):
project_path = get_project_path(project_data)
print(f"Updating {project_data['repo_name']} at {project_path}")
subprocess.run(["git", "pull"], cwd=project_path)
def uninstall(project_data,args=[]):
project_path = get_project_path(project_data)
print(f"Uninstalling {project_data['repo_name']} deleting {project_path} folder.")
subprocess.run(["rd", "/s", "/q", project_path], shell=True)
print(f"{project_data['repo_name']} uninstalled {project_path} folder deleted.")
def launch(project_data, args=[]):
project_path = get_project_path(project_data)
venv_path = get_venv_path(project_data)
launch_path = get_entry_point(project_data, 'launch')
print(f"launching {project_data['repo_name']} at {project_path}")
installation_status_venv = installation_status.check_project_venv(project_data)
installation_status_project = installation_status.check_project(project_data)
if not installation_status_venv:
print(f"Virtual environment (venv) not found for {project_data['repo_name']} at {project_path}. Will create a new one.")
install(project_data,args=[])
if installation_status_project:
command_len = len(launch_path.split())
cmd_launch = project_data['entry_point']['launch']
activate_script = f"{venv_path}/Scripts/activate.bat"
cmd_command = f'cmd /K ""{activate_script}" && "{venv_path}/Scripts/python" {cmd_launch} {" ".join(args)}"'
cmd_command_no_py = f'cmd /K ""{activate_script}" && {cmd_launch} {" ".join(args)}"'
if launch_path.endswith(".py") and command_len == 1:
subprocess.run([f"{venv_path}/Scripts/python", launch_path, *args], cwd=project_path)
elif launch_path.endswith(".py") and command_len > 1:
subprocess.run(cmd_command, cwd=project_path, shell=True)
elif launch_path.endswith(".bat"):
subprocess.run([launch_path, *args], cwd=project_path)
else:
subprocess.run(cmd_command_no_py, cwd=project_path, shell=True)
else:
print(f"Failed to launch {project_data['repo_name']} venv not installed.")
def install(project_data,args=[]):
project_path = get_project_path(project_data)
print(f"installing {project_data['repo_name']} with at {project_path}")
clone_repo(project_data)
create_virtual_environment(project_data)
venv_path = get_venv_path(project_data)
install_path = get_entry_point(project_data, 'install')
command_len = len(install_path.split())
cmd_launch = project_data['entry_point']['install']
activate_script = f"{venv_path}/Scripts/activate.bat"
cmd_command = f'cmd /K ""{activate_script}" && "{venv_path}/Scripts/python" {cmd_launch} {" ".join(args)}"'
cmd_command_no_py = f'cmd /K ""{activate_script}" && {cmd_launch} {" ".join(args)}"'
if project_data['install_requirements']:
install_requirements(project_data)
if project_data['install_instructions_available']:
install_instructions(project_data)
if install_path.endswith(".py") and command_len == 1:
subprocess.run([f"{venv_path}/Scripts/python", install_path, *args], cwd=project_path)
elif install_path.endswith(".py") and command_len > 1:
subprocess.run(cmd_command, cwd=project_path, shell=True)
elif install_path.endswith(".bat"):
subprocess.run([install_path, *args], cwd=project_path)
else:
subprocess.run(cmd_command_no_py, cwd=project_path, shell=True)
def delete_virtual_environment(project_data,args=[]):
venv_path = get_venv_path(project_data)
print(f"Deleting virtual environment for {project_data['repo_name']} at {venv_path}")
subprocess.run(["rd", "/s", "/q", venv_path], shell=True)
print(f"virtual environment deleted for {project_data['repo_name']}")
def install_cuda(project_data):
project_path = get_project_path(project_data)
venv_path = get_venv_path(project_data)
print(f"installing cuda")
cmd = f"{venv_path}/Scripts/python -m pip install torch==1.13.1 torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117"
subprocess.run(cmd, shell=True, cwd=project_path)
print(f"cuda installed")
def install_requirements(project_data):
project_path = get_project_path(project_data)
venv_path = get_venv_path(project_data)
print(f"Installing requirements for {project_data['repo_name']} at {venv_path}")
if project_data['install_cuda']:
install_cuda(project_data)
subprocess.run([f"{venv_path}/Scripts/python","-m", "pip","install","-r","requirements.txt"], cwd=project_path)
print(f"{project_data['repo_name']} requirements installed at {venv_path}")
def install_instructions(project_data):
project_path = get_project_path(project_data)
venv_path = get_venv_path(project_data)
print(f"Executing install instructions for {project_data['repo_name']}")
for command in project_data["install_instructions"]:
subprocess.run([f"{venv_path}/Scripts/python"] + command.split(), cwd=project_path)
print(f"{project_data['repo_name']} Instructed installation completed")
def install_webui_extension(project_data,args=[]):
print(f"installing {project_data['repo_name']}")
subprocess.run(["git","clone",project_data["git_clone_url"],f"{full_path}\{project_data['webui_path']}\extensions\{project_data['repo_name']}"])
# print(["git","clone",project_data["git_clone_url"],f"{full_path}\{project_data['webui_path']}\extensions\{project_data['repo_name']}"])
print(f"{project_data['repo_name']} installed")
download_models(project_data, skip_existing=True)
def update_webui_extension(project_data,args=[]):
print(f"updating {project_data['repo_name']}")
subprocess.run(["git","pull"], cwd=f"{project_data['webui_path']}\extensions\{project_data['repo_name']}")
def uninstall_webui_extension(project_data,args=[]):
print(f"uninstalling {project_data['repo_name']}")
subprocess.run(["rd", "/s", "/q", f"{project_data['webui_path']}\extensions\{project_data['repo_name']}"], shell=True)
print(f"{project_data['repo_name']} uninstalled")
def download_models(project_data, skip_existing=True):
# base_url = "https://huggingface.co/datasets/disty/seait_ControlNet-modules-safetensors/resolve/main/"
base_url = "https://huggingface.co/datasets/disty/seait_ControlNet1-1-modules-safetensors/resolve/main/"
download_folder = os.path.join(full_path,project_data['webui_path'], 'models', 'ControlNet')
# print(download_folder)
os.makedirs(download_folder, exist_ok=True)
for model in CN_MODELS_11:
file_path = os.path.join(download_folder, model)
if skip_existing and os.path.exists(file_path):
print(f"{model} already exists. Skipping download.")
continue
url = base_url + model
response = requests.get(url)
if response.status_code == 200:
model_size_mb = int(response.headers.get("Content-Length", 0)) / (1024 * 1024)
print(f"Downloading {model} ({model_size_mb:.2f} MB)")
with open(file_path, "wb") as f:
f.write(response.content)
print(f"{model} downloaded.")
else:
print(f"Error: Failed to download {model}. Status code: {response.status_code}")
print("All models downloaded.")
def download_comfyui_models(project_data):
if project_data["id"] == 2:
# print(f"downloading models for {project_data['repo_name']}")
project_path = get_project_path(project_data)
checkpoints_path = os.path.join(f"{project_path}/{project_data['checkpoints_path']}")
# subprocess.run(["git","clone",project_data["models_path"],f"{project_data['webui_path']}\models\ControlNet"])
# download_file(project_data["download_models_path"], project_data['checkpoints_path'])
# print(checkpoints_path)
# download_file(project_data['download_models_path'],f"C:/repos/seai/ComfyUI/models/checkpoints/{project_data['download_models_path']}")
print(f"{project_data['repo_name']} models downloaded")
methods = {
"clone": clone_repo,
"update": update,
"delete_venv": delete_virtual_environment,
"create_venv": create_virtual_environment,
"uninstall": uninstall,
"launch": launch,
"install": install,
"install_webui_extension": install_webui_extension,
"uninstall_webui_extension": uninstall_webui_extension,
"update_webui_extension": update_webui_extension,
"download_models": download_models,
}