Skip to content

Commit

Permalink
skip installing packages with pip if theyare already installed
Browse files Browse the repository at this point in the history
record time it took to launch
  • Loading branch information
AUTOMATIC1111 committed Jul 17, 2023
1 parent 699108b commit a99d570
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
46 changes: 45 additions & 1 deletion modules/launch_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# this scripts installs necessary requirements and launches main program in webui.py
import re
import subprocess
import os
import sys
Expand All @@ -9,6 +10,9 @@

from modules import cmd_args, errors
from modules.paths_internal import script_path, extensions_dir
from modules import timer

timer.startup_timer.record("start")

args, _ = cmd_args.parser.parse_known_args()

Expand Down Expand Up @@ -226,6 +230,44 @@ def run_extensions_installers(settings_file):
run_extension_installer(os.path.join(extensions_dir, dirname_extension))


re_requirement = re.compile(r"\s*([-_a-zA-Z0-9]+)\s*(?:==\s*([-+_.a-zA-Z0-9]+))?\s*")


def requrements_met(requirements_file):
"""
Does a simple parse of a requirements.txt file to determine if all rerqirements in it
are already installed. Returns True if so, False if not installed or parsing fails.
"""

import importlib.metadata
import packaging.version

with open(requirements_file, "r", encoding="utf8") as file:
for line in file:
if line.strip() == "":
continue

m = re.match(re_requirement, line)
if m is None:
return False

package = m.group(1).strip()
version_required = (m.group(2) or "").strip()

if version_required == "":
continue

try:
version_installed = importlib.metadata.version(package)
except Exception:
return False

if packaging.version.parse(version_required) != packaging.version.parse(version_installed):
return False

return True


def prepare_environment():
torch_index_url = os.environ.get('TORCH_INDEX_URL', "https://download.pytorch.org/whl/cu118")
torch_command = os.environ.get('TORCH_COMMAND', f"pip install torch==2.0.1 torchvision==0.15.2 --extra-index-url {torch_index_url}")
Expand Down Expand Up @@ -311,7 +353,9 @@ def prepare_environment():

if not os.path.isfile(requirements_file):
requirements_file = os.path.join(script_path, requirements_file)
run_pip(f"install -r \"{requirements_file}\"", "requirements")

if not requrements_met(requirements_file):
run_pip(f"install -r \"{requirements_file}\"", "requirements")

run_extensions_installers(settings_file=args.ui_settings_file)

Expand Down
4 changes: 2 additions & 2 deletions requirements_versions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ einops==0.4.1
fastapi==0.94.0
gfpgan==1.3.8
gradio==3.32.0
httpcore<=0.15
httpcore==0.15
inflection==0.5.1
jsonmerge==1.8.0
kornia==0.6.7
Expand All @@ -17,7 +17,7 @@ numpy==1.23.5
omegaconf==2.2.3
open-clip-torch==2.20.0
piexif==1.1.3
psutil~=5.9.5
psutil==5.9.5
pytorch_lightning==1.9.4
realesrgan==0.3.0
resize-right==0.0.2
Expand Down
9 changes: 5 additions & 4 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,22 @@
logging.getLogger("torch.distributed.nn").setLevel(logging.ERROR) # sshh...
logging.getLogger("xformers").addFilter(lambda record: 'A matching Triton is not available' not in record.getMessage())

from modules import paths, timer, import_hook, errors, devices # noqa: F401

from modules import timer
startup_timer = timer.startup_timer
startup_timer.record("launcher")

import torch
import pytorch_lightning # noqa: F401 # pytorch_lightning should be imported after torch, but it re-enables warnings on import so import once to disable them
warnings.filterwarnings(action="ignore", category=DeprecationWarning, module="pytorch_lightning")
warnings.filterwarnings(action="ignore", category=UserWarning, module="torchvision")


startup_timer.record("import torch")

import gradio # noqa: F401
startup_timer.record("import gradio")

from modules import paths, timer, import_hook, errors, devices # noqa: F401
startup_timer.record("setup paths")

import ldm.modules.encoders.modules # noqa: F401
startup_timer.record("import ldm")

Expand Down

0 comments on commit a99d570

Please sign in to comment.