Skip to content

Commit

Permalink
fix: 🐛 install logic
Browse files Browse the repository at this point in the history
  • Loading branch information
melMass committed Jul 22, 2023
1 parent dad3966 commit 7e301e2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 59 deletions.
129 changes: 73 additions & 56 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@

here = Path(__file__).parent
wheels_directory = here / "wheels"
executable = sys.executable

# - detect mode
mode = None
if os.environ.get("COLAB_GPU"):
mode = "colab"
elif "python_embeded" in executable:
mode = "embeded"
elif ".venv" in executable:
mode = "venv"


if mode == None:
mode = "unknown"

# region ansi
# ANSI escape sequences for text styling
ANSI_FORMATS = {
Expand Down Expand Up @@ -109,6 +124,7 @@ def print_formatted(text, *formats, color=None, background=None, **kwargs):
print_formatted("Installing tqdm...", "italic", color="yellow")
subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "tqdm"])
from tqdm import tqdm
import importlib


pip_map = {
Expand Down Expand Up @@ -154,78 +170,81 @@ def download_file(url, file_name):
progress_bar.update(len(chunk))


from typing import Union, Literal


def detect_mode() -> (
Union[Literal["colab"], Literal["embeded"] | Literal["venv"], None]
):
if os.environ.get("COLAB_GPU"):
return "colab"
executable = sys.executable
if "python_embeded" in executable:
return "embeded"
if ".venv" in executable:
return "venv"


# Install dependencies from requirements.txt
def install_dependencies(dry=False):
with open("requirements.txt", "r") as requirements_file:
def get_requirements(path):
with open(path, "r") as requirements_file:
requirements_txt = requirements_file.read()

try:
parsed_requirements = requirements.parse(requirements_txt)
except AttributeError:
print_formatted(
"Failed to parse requirements.txt. Please make sure the file is correctly formatted.",
f"Failed to parse {path}. Please make sure the file is correctly formatted.",
"bold",
color="red",
)

return

print_formatted(
"Installing dependencies from requirements.txt...", "italic", color="yellow"
)
return parsed_requirements

for requirement in parsed_requirements:
dependency = requirement.name.strip()
import_name = pip_map.get(dependency, dependency)

pip_name = dependency
if specs := requirement.specs:
pip_name += "".join(specs[0])
def import_or_install(requirement, dry=False):
dependency = requirement.name.strip()
import_name = pip_map.get(dependency, dependency)

try:
import_module(import_name)
pip_name = dependency
if specs := requirement.specs:
pip_name += "".join(specs[0])

try:
import_module(import_name)
print_formatted(
f"Package {pip_name} already installed (import name: '{import_name}').",
"bold",
color="green",
)
except ImportError:
if dry:
print_formatted(
f"Package {pip_name} already installed (import name: '{import_name}').",
"bold",
color="green",
f"Dry-run: Package {pip_name} would be installed (import name: '{import_name}').",
color="yellow",
)
except ImportError:
if dry:
else:
try:
subprocess.check_call(
[sys.executable, "-m", "pip", "install", pip_name]
)
print_formatted(
f"Dry-run: Package {pip_name} would be installed (import name: '{import_name}').",
color="yellow",
f"Package {pip_name} installed successfully using pip package name (import name: '{import_name}')",
"bold",
color="green",
)
except subprocess.CalledProcessError as e:
print_formatted(
f"Failed to install package {pip_name} using pip package name (import name: '{import_name}'). Error: {str(e)}",
"bold",
color="red",
)
else:
try:
subprocess.check_call(
[sys.executable, "-m", "pip", "install", pip_name]
)
print_formatted(
f"Package {pip_name} installed successfully using pip package name (import name: '{import_name}')",
"bold",
color="green",
)
except subprocess.CalledProcessError as e:
print_formatted(
f"Failed to install package {pip_name} using pip package name (import name: '{import_name}'). Error: {str(e)}",
"bold",
color="red",
)


# Install dependencies from requirements.txt
def install_dependencies(dry=False):
parsed_requirements = get_requirements(here / "requirements.txt")
if not parsed_requirements:
return
print_formatted(
"Installing dependencies from requirements.txt...", "italic", color="yellow"
)

for requirement in parsed_requirements:
import_or_install(requirement, dry=dry)

if mode == "venv":
parsed_requirements = get_requirements(here / "requirements-wheels.txt")
if not parsed_requirements:
return
for requirement in parsed_requirements:
import_or_install(requirement, dry=dry)


if __name__ == "__main__":
Expand All @@ -252,11 +271,9 @@ def install_dependencies(dry=False):
args = parser.parse_args()

# Install dependencies from requirements.txt
if args.requirements:
if args.requirements or mode == "venv":
install_dependencies(dry=args.dry)

mode = detect_mode()

if not args.wheels and mode not in ["colab", "embeded"]:
print_formatted(
"Skipping wheel installation. Use --wheels to install wheel dependencies. (only needed for Comfy embed)",
Expand Down
3 changes: 0 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ onnxruntime-gpu==1.15.1
imageio===2.28.1
qrcode[pil]
numpy==1.23.5
insightface==0.7.3
mmcv==2.0.0
mmdet==3.0.0
rembg==2.0.37
facexlib==0.3.0
basicsr==1.4.2
Expand Down

0 comments on commit 7e301e2

Please sign in to comment.