forked from Gourieff/comfyui-reactor-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
78 lines (68 loc) · 2.76 KB
/
install.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
import subprocess
import os, sys, shutil
import pkg_resources
from tqdm import tqdm
import urllib.request
from packaging import version as pv
try:
from folder_paths import models_dir
except:
from pathlib import Path
models_dir = os.path.join(Path(__file__).parents[2], "models")
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
req_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "requirements.txt")
model_url = "https://github.com/facefusion/facefusion-assets/releases/download/models/inswapper_128.onnx"
model_name = os.path.basename(model_url)
models_dir_path = os.path.join(models_dir, "insightface")
model_path = os.path.join(models_dir_path, model_name)
models_dir_old = os.path.abspath("models/insightface")
model_path_old = os.path.join(models_dir_old, model_name)
def run_pip(*args):
subprocess.run([sys.executable, "-m", "pip", "install", *args])
def is_installed (
package: str, version: str | None = None, strict: bool = True
):
has_package = None
try:
has_package = pkg_resources.get_distribution(package)
if has_package is not None:
installed_version = has_package.version
if (installed_version != version and strict == True) or (pv.parse(installed_version) < pv.parse(version) and strict == False):
return False
else:
return True
else:
return False
except Exception as e:
print(f"Error: {e}")
return False
def download(url, path):
request = urllib.request.urlopen(url)
total = int(request.headers.get('Content-Length', 0))
with tqdm(total=total, desc='Downloading', unit='B', unit_scale=True, unit_divisor=1024) as progress:
urllib.request.urlretrieve(url, path, reporthook=lambda count, block_size, total_size: progress.update(block_size))
if not os.path.exists(models_dir_path):
os.makedirs(models_dir_path)
if os.path.exists(models_dir_old):
shutil.move(model_path_old, model_path)
if not os.path.exists(model_path):
download(model_url, model_path)
print("Installing...")
with open(req_file) as file:
strict = True
for package in file:
package_version = None
try:
package = package.strip()
if "==" in package:
package_version = package.split('==')[1]
elif ">=" in package:
package_version = package.split('>=')[1]
strict = False
if not is_installed(package,package_version,strict):
run_pip(package)
except Exception as e:
print(e)
print(f"Warning: Failed to install {package}, ReActor will not work.")
raise e
print("Ok")