-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall.py
79 lines (69 loc) · 3.39 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
79
import launch
from importlib import metadata
from pathlib import Path
from typing import Optional
from packaging.version import parse
repo_root = Path(__file__).parent
main_req_file = repo_root / "requirements.txt"
def get_installed_version(package: str) -> Optional[str]:
try:
return metadata.version(package)
except Exception:
return None
def extract_base_package(package_string: str) -> str:
base_package = package_string.split("@git")[0]
return base_package
def install_requirements(req_file):
with open(req_file) as file:
for package in file:
try:
package = package.strip()
if "==" in package:
package_name, package_version = package.split("==")
installed_version = get_installed_version(package_name)
if installed_version != package_version:
launch.run_pip(
f'install -U "{package}"',
f"sd-webui-birefnet requirement: changing {package_name} version from {installed_version} to {package_version}",
)
elif ">=" in package:
package_name, package_version = package.split(">=")
installed_version = get_installed_version(package_name)
if not installed_version or parse(
installed_version
) < parse(package_version):
launch.run_pip(
f'install -U "{package}"',
f"sd-webui-birefnet requirement: changing {package_name} version from {installed_version} to {package_version}",
)
elif "<=" in package:
package_name, package_version = package.split("<=")
installed_version = get_installed_version(package_name)
if not installed_version or parse(
installed_version
) > parse(package_version):
launch.run_pip(
f'install "{package_name}=={package_version}"',
f"sd-webui-birefnet requirement: changing {package_name} version from {installed_version} to {package_version}",
)
elif "<" in package:
package_name, package_version = package.split("<")
installed_version = get_installed_version(package_name)
if not installed_version or parse(
installed_version
) >= parse(package_version):
launch.run_pip(
f'install "{package_name}=={package_version}"',
f"sd-webui-birefnet requirement: changing {package_name} version from {installed_version} to {package_version}",
)
elif not launch.is_installed(extract_base_package(package)):
launch.run_pip(
f'install "{package}"',
f"sd-webui-birefnet requirement: {package}",
)
except Exception as e:
print(e)
print(
f"Warning: Failed to install {package}, some preprocessors may not work."
)
install_requirements(main_req_file)