2023-01-08 08:09:04 +00:00
|
|
|
"""
|
|
|
|
InvokeAI installer script
|
|
|
|
"""
|
|
|
|
|
2023-01-09 05:13:01 +00:00
|
|
|
import os
|
|
|
|
import platform
|
2023-01-08 08:09:04 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import venv
|
|
|
|
from pathlib import Path
|
2023-01-09 08:09:56 +00:00
|
|
|
from tempfile import TemporaryDirectory, TemporaryFile
|
2023-01-13 09:11:23 +00:00
|
|
|
from typing import Union
|
2023-01-08 08:09:04 +00:00
|
|
|
|
|
|
|
SUPPORTED_PYTHON = ">=3.9.0,<3.11"
|
2023-01-10 21:55:57 +00:00
|
|
|
INSTALLER_REQS = ["rich", "semver", "requests", "plumbum", "prompt-toolkit"]
|
2023-01-09 05:13:01 +00:00
|
|
|
|
|
|
|
OS = platform.uname().system
|
|
|
|
ARCH = platform.uname().machine
|
|
|
|
VERSION = "latest"
|
2023-01-08 08:09:04 +00:00
|
|
|
|
|
|
|
### Feature flags
|
2023-01-09 05:13:01 +00:00
|
|
|
# Install the virtualenv into the runtime dir
|
2023-01-09 08:09:56 +00:00
|
|
|
FF_VENV_IN_RUNTIME = True
|
2023-01-08 08:09:04 +00:00
|
|
|
|
|
|
|
# Install the wheel from pypi
|
2023-01-09 08:09:56 +00:00
|
|
|
FF_USE_WHEEL = False
|
|
|
|
|
2023-01-09 18:30:34 +00:00
|
|
|
INVOKE_AI_SRC = f"https://github.com/invoke-ai/InvokeAI/archive/refs/tags/${VERSION}.zip"
|
2023-01-08 08:09:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Installer:
|
|
|
|
"""
|
|
|
|
Deploys an InvokeAI installation into a given path
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.reqs = INSTALLER_REQS
|
|
|
|
self.preflight()
|
2023-01-09 08:09:56 +00:00
|
|
|
if os.getenv("VIRTUAL_ENV") is None:
|
|
|
|
# Only bootstrap if not already in a venv
|
|
|
|
self.bootstrap()
|
2023-01-08 08:09:04 +00:00
|
|
|
|
|
|
|
def preflight(self) -> None:
|
|
|
|
"""
|
|
|
|
Preflight checks
|
|
|
|
"""
|
|
|
|
|
|
|
|
# TODO
|
|
|
|
# verify python version
|
|
|
|
# on macOS verify XCode tools are present
|
|
|
|
# verify libmesa, libglx on linux
|
|
|
|
# check that the system arch is not i386 (?)
|
|
|
|
# check that the system has a GPU, and the type of GPU
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2023-01-09 05:13:01 +00:00
|
|
|
def mktemp_venv(self) -> TemporaryDirectory:
|
2023-01-08 08:09:04 +00:00
|
|
|
"""
|
|
|
|
Creates a temporary virtual environment for the installer itself
|
|
|
|
|
|
|
|
:return: path to the created virtual environment directory
|
|
|
|
:rtype: TemporaryDirectory
|
|
|
|
"""
|
|
|
|
|
2023-01-14 06:50:11 +00:00
|
|
|
# Cleaning up temporary directories on Windows results in a race condition
|
|
|
|
# and a stack trace.
|
|
|
|
# `ignore_cleanup_errors` was only added in Python 3.10
|
|
|
|
# users of Python 3.9 will see a gnarly stack trace on installer exit
|
|
|
|
if OS == "Windows" and int(platform.python_version_tuple()[1])>=10:
|
|
|
|
venv_dir = TemporaryDirectory(prefix="invokeai-installer-", ignore_cleanup_errors=True)
|
|
|
|
else:
|
|
|
|
venv_dir = TemporaryDirectory(prefix="invokeai-installer-")
|
|
|
|
|
|
|
|
|
2023-01-08 08:09:04 +00:00
|
|
|
venv.create(venv_dir.name, with_pip=True)
|
|
|
|
self.venv_dir = venv_dir
|
2023-01-09 18:30:34 +00:00
|
|
|
add_venv_site(Path(venv_dir.name))
|
|
|
|
|
2023-01-08 08:09:04 +00:00
|
|
|
return venv_dir
|
|
|
|
|
|
|
|
def bootstrap(self, verbose: bool = False) -> TemporaryDirectory:
|
|
|
|
"""
|
|
|
|
Bootstrap the installer venv with packages required at install time
|
|
|
|
|
|
|
|
:return: path to the virtual environment directory that was bootstrapped
|
|
|
|
:rtype: TemporaryDirectory
|
|
|
|
"""
|
|
|
|
|
2023-01-09 05:13:01 +00:00
|
|
|
print("Initializing the installer. This may take a minute - please wait...")
|
2023-01-08 08:09:04 +00:00
|
|
|
|
2023-01-09 05:13:01 +00:00
|
|
|
venv_dir = self.mktemp_venv()
|
2023-01-09 18:30:34 +00:00
|
|
|
pip = get_venv_pip(Path(venv_dir.name))
|
2023-01-08 08:09:04 +00:00
|
|
|
|
|
|
|
cmd = [pip, "install", "--require-virtualenv"]
|
|
|
|
cmd.extend(self.reqs)
|
|
|
|
|
|
|
|
try:
|
|
|
|
res = subprocess.check_output(cmd).decode()
|
|
|
|
if verbose:
|
|
|
|
print(res)
|
|
|
|
return venv_dir
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
print(e)
|
|
|
|
|
2023-01-09 08:09:56 +00:00
|
|
|
def app_venv(self, path: str = None):
|
|
|
|
"""
|
|
|
|
Create a virtualenv for the InvokeAI installation
|
|
|
|
"""
|
|
|
|
|
|
|
|
# explicit venv location
|
|
|
|
# currently unused in normal operation
|
|
|
|
# useful for testing or special cases
|
|
|
|
if path is not None:
|
|
|
|
venv_dir = Path(path)
|
|
|
|
|
|
|
|
# experimental / testing
|
|
|
|
#
|
|
|
|
elif not FF_VENV_IN_RUNTIME:
|
|
|
|
if OS == "Windows":
|
|
|
|
venv_dir_parent = os.getenv("APPDATA", "~/AppData/Roaming")
|
|
|
|
elif OS == "Darwin":
|
|
|
|
# there is no environment variable on macOS to find this
|
|
|
|
# TODO: confirm this is working as expected
|
|
|
|
venv_dir_parent = "~/Library/Application Support"
|
|
|
|
elif OS == "Linux":
|
|
|
|
venv_dir_parent = os.getenv("XDG_DATA_DIR", "~/.local/share")
|
|
|
|
venv_dir = Path(venv_dir_parent).expanduser().resolve() / f"InvokeAI/{VERSION}/venv"
|
|
|
|
|
|
|
|
# stable / current
|
|
|
|
else:
|
|
|
|
venv_dir = self.dest / ".venv"
|
|
|
|
|
|
|
|
venv.create(venv_dir, with_pip=True)
|
|
|
|
return venv_dir
|
|
|
|
|
|
|
|
def get_payload():
|
|
|
|
"""
|
|
|
|
Obtain the InvokeAI installation payload
|
|
|
|
"""
|
|
|
|
|
2023-01-10 21:55:57 +00:00
|
|
|
pass
|
|
|
|
|
2023-01-08 08:09:04 +00:00
|
|
|
def install(self, path: str = "~/invokeai", version: str = "latest") -> None:
|
|
|
|
"""
|
|
|
|
Install the InvokeAI application into the given runtime path
|
|
|
|
|
|
|
|
:param path: Destination path for the installation
|
|
|
|
:type path: str
|
|
|
|
:param version: InvokeAI version to install
|
|
|
|
:type version: str
|
|
|
|
"""
|
|
|
|
|
2023-01-13 09:11:23 +00:00
|
|
|
import messages
|
2023-01-08 08:09:04 +00:00
|
|
|
|
2023-01-13 09:11:23 +00:00
|
|
|
messages.welcome()
|
2023-01-09 08:09:56 +00:00
|
|
|
|
2023-01-13 09:11:23 +00:00
|
|
|
self.dest = messages.dest_path(path)
|
2023-01-08 08:09:04 +00:00
|
|
|
|
2023-01-09 08:09:56 +00:00
|
|
|
self.venv = self.app_venv()
|
|
|
|
|
2023-01-09 18:30:34 +00:00
|
|
|
self.instance = InvokeAiInstance(runtime=self.dest, venv=self.venv)
|
2023-01-09 08:09:56 +00:00
|
|
|
|
2023-01-13 09:11:23 +00:00
|
|
|
self.instance.deploy(extra_index_url=get_torch_source())
|
2023-01-09 08:09:56 +00:00
|
|
|
|
2023-01-10 03:19:38 +00:00
|
|
|
self.instance.configure()
|
|
|
|
|
2023-01-08 08:09:04 +00:00
|
|
|
|
2023-01-09 08:09:56 +00:00
|
|
|
class InvokeAiInstance:
|
2023-01-08 08:09:04 +00:00
|
|
|
"""
|
2023-01-09 18:30:34 +00:00
|
|
|
Manages an installed instance of InvokeAI, comprising a virtual environment and a runtime directory.
|
|
|
|
The virtual environment *may* reside within the runtime directory.
|
|
|
|
A single runtime directory *may* be shared by multiple virtual environments, though this isn't currently tested or supported.
|
2023-01-08 08:09:04 +00:00
|
|
|
"""
|
|
|
|
|
2023-01-09 08:09:56 +00:00
|
|
|
def __init__(self, runtime: Path, venv: Path) -> None:
|
2023-01-09 18:30:34 +00:00
|
|
|
|
2023-01-09 08:09:56 +00:00
|
|
|
self.runtime = runtime
|
|
|
|
self.venv = venv
|
2023-01-09 18:30:34 +00:00
|
|
|
self.pip = get_venv_pip(venv)
|
|
|
|
|
|
|
|
add_venv_site(venv)
|
2023-01-09 08:09:56 +00:00
|
|
|
os.environ["INVOKEAI_ROOT"] = str(self.runtime.expanduser().resolve())
|
|
|
|
os.environ["VIRTUAL_ENV"] = str(self.venv.expanduser().resolve())
|
|
|
|
|
|
|
|
def get(self) -> tuple[Path, Path]:
|
|
|
|
"""
|
|
|
|
Get the location of the virtualenv directory for this installation
|
|
|
|
|
|
|
|
:return: Paths of the runtime and the venv directory
|
|
|
|
:rtype: tuple[Path, Path]
|
|
|
|
"""
|
|
|
|
|
|
|
|
return (self.runtime, self.venv)
|
|
|
|
|
2023-01-13 09:11:23 +00:00
|
|
|
def deploy(self, extra_index_url=None):
|
|
|
|
"""
|
|
|
|
Install packages with pip
|
|
|
|
|
|
|
|
:param extra_index_url: the "--extra-index-url ..." line for pip to look in extra indexes.
|
|
|
|
:type extra_index_url: str
|
|
|
|
"""
|
2023-01-09 08:09:56 +00:00
|
|
|
|
|
|
|
### this is all very rough for now as a PoC
|
|
|
|
### source installer basically
|
2023-01-13 09:11:23 +00:00
|
|
|
### TODO: need to pull the source from Github like the current installer does
|
|
|
|
### until we continuously build wheels
|
2023-01-09 08:09:56 +00:00
|
|
|
|
2023-01-13 09:11:23 +00:00
|
|
|
import messages
|
2023-01-10 03:19:38 +00:00
|
|
|
from plumbum import local, FG
|
|
|
|
|
2023-01-13 09:11:23 +00:00
|
|
|
# pre-installing Torch because this is the most reliable way to ensure
|
|
|
|
# the correct version gets installed.
|
|
|
|
# this works with either source or wheel install and has
|
|
|
|
# negligible impact on installation times.
|
|
|
|
messages.simple_banner("Installing PyTorch :fire:")
|
|
|
|
self.install_torch(extra_index_url)
|
|
|
|
|
|
|
|
messages.simple_banner("Installing InvokeAI base dependencies :rocket:")
|
|
|
|
extra_index_url_arg = "--extra-index-url" if extra_index_url is not None else None
|
|
|
|
|
2023-01-10 03:19:38 +00:00
|
|
|
pip = local[self.pip]
|
|
|
|
|
|
|
|
(
|
|
|
|
pip[
|
2023-01-09 18:30:34 +00:00
|
|
|
"install",
|
|
|
|
"--require-virtualenv",
|
|
|
|
"-r",
|
|
|
|
(Path(__file__).parents[1] / "environments-and-requirements/requirements-base.txt")
|
|
|
|
.expanduser()
|
|
|
|
.resolve(),
|
2023-01-13 09:11:23 +00:00
|
|
|
extra_index_url_arg,
|
|
|
|
extra_index_url,
|
2023-01-10 03:19:38 +00:00
|
|
|
]
|
|
|
|
& FG
|
2023-01-09 18:30:34 +00:00
|
|
|
)
|
|
|
|
|
2023-01-13 09:11:23 +00:00
|
|
|
messages.simple_banner("Installing the InvokeAI Application :art:")
|
|
|
|
(
|
|
|
|
pip[
|
|
|
|
"install",
|
|
|
|
"--require-virtualenv",
|
|
|
|
Path(__file__).parents[1].expanduser().resolve(),
|
|
|
|
extra_index_url_arg,
|
|
|
|
extra_index_url,
|
|
|
|
]
|
|
|
|
& FG
|
|
|
|
)
|
|
|
|
|
|
|
|
def install_torch(self, extra_index_url=None):
|
|
|
|
"""
|
|
|
|
Install PyTorch
|
|
|
|
"""
|
|
|
|
|
|
|
|
from plumbum import local, FG
|
|
|
|
|
|
|
|
extra_index_url_arg = "--extra-index-url" if extra_index_url is not None else None
|
|
|
|
|
|
|
|
pip = local[self.pip]
|
|
|
|
|
|
|
|
(
|
|
|
|
pip[
|
|
|
|
"install",
|
|
|
|
"--require-virtualenv",
|
|
|
|
"torch",
|
|
|
|
"torchvision",
|
|
|
|
extra_index_url_arg,
|
|
|
|
extra_index_url,
|
|
|
|
]
|
|
|
|
& FG
|
|
|
|
)
|
2023-01-09 18:30:34 +00:00
|
|
|
|
|
|
|
def configure(self):
|
|
|
|
"""
|
|
|
|
Configure the InvokeAI runtime directory
|
|
|
|
"""
|
|
|
|
|
2023-01-13 09:09:48 +00:00
|
|
|
from messages import introduction
|
|
|
|
|
|
|
|
introduction()
|
|
|
|
|
2023-01-12 05:56:47 +00:00
|
|
|
from ldm.invoke.config import configure_invokeai
|
2023-01-10 03:19:38 +00:00
|
|
|
|
2023-01-12 05:56:47 +00:00
|
|
|
configure_invokeai.main()
|
2023-01-10 03:19:38 +00:00
|
|
|
|
2023-01-09 18:30:34 +00:00
|
|
|
def update(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
pass
|
2023-01-09 08:09:56 +00:00
|
|
|
|
|
|
|
|
2023-01-09 18:30:34 +00:00
|
|
|
### Utility functions ###
|
2023-01-09 08:09:56 +00:00
|
|
|
|
|
|
|
|
2023-01-09 18:30:34 +00:00
|
|
|
def get_venv_pip(venv_path: Path) -> str:
|
|
|
|
"""
|
|
|
|
Given a path to a virtual environment, get the absolute path to the `pip` executable
|
|
|
|
in a cross-platform fashion. Does not validate that the pip executable
|
|
|
|
actually exists in the virtualenv.
|
|
|
|
|
|
|
|
:param venv_path: Path to the virtual environment
|
|
|
|
:type venv_path: Path
|
|
|
|
:return: Absolute path to the pip executable
|
|
|
|
:rtype: str
|
|
|
|
"""
|
2023-01-09 08:09:56 +00:00
|
|
|
|
2023-01-09 18:30:34 +00:00
|
|
|
pip = "Scripts\pip.exe" if OS == "Windows" else "bin/pip"
|
|
|
|
return str(venv_path.absolute() / pip)
|
2023-01-09 08:09:56 +00:00
|
|
|
|
|
|
|
|
2023-01-09 18:30:34 +00:00
|
|
|
def add_venv_site(venv_path: Path) -> None:
|
|
|
|
"""
|
|
|
|
Given a path to a virtual environment, add the python site-packages directory from this venv
|
|
|
|
into the sys.path, in a cross-platform fashion, such that packages from this venv
|
|
|
|
may be imported in the current process.
|
|
|
|
|
|
|
|
:param venv_path: Path to the virtual environment
|
|
|
|
:type venv_path: Path
|
|
|
|
"""
|
|
|
|
|
|
|
|
lib = "Lib" if OS == "Windows" else f"lib/python{sys.version_info.major}.{sys.version_info.minor}"
|
|
|
|
sys.path.append(str(Path(venv_path, lib, "site-packages").absolute()))
|
2023-01-13 09:11:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_torch_source() -> Union[str, None]:
|
|
|
|
"""
|
|
|
|
Determine the extra index URL for pip to use for torch installation.
|
|
|
|
This depends on the OS and the graphics accelerator in use.
|
|
|
|
This is only applicable to Windows and Linux, since PyTorch does not
|
|
|
|
offer accelerated builds for macOS.
|
|
|
|
|
|
|
|
Prefer CUDA if the user wasn't sure of their GPU, as it will fallback to CPU if possible.
|
|
|
|
|
|
|
|
A NoneType return means just go to PyPi.
|
|
|
|
|
|
|
|
:return: The list of arguments to pip pointing at the PyTorch wheel source, if available
|
|
|
|
:rtype: list
|
|
|
|
"""
|
|
|
|
|
|
|
|
from messages import graphical_accelerator
|
|
|
|
|
|
|
|
device = graphical_accelerator()
|
|
|
|
|
|
|
|
url = None
|
|
|
|
if OS == "Linux":
|
|
|
|
if device in ["cuda", "idk"]:
|
|
|
|
url = "https://download.pytorch.org/whl/cu117"
|
|
|
|
elif device == "rocm":
|
|
|
|
url = "https://download.pytorch.org/whl/rocm5.2"
|
|
|
|
else:
|
|
|
|
url = "https://download.pytorch.org/whl/cpu"
|
|
|
|
|
|
|
|
elif OS == "Windows":
|
|
|
|
if device in ["cuda", "idk"]:
|
|
|
|
url = "https://download.pytorch.org/whl/cu117"
|
|
|
|
|
|
|
|
# ignoring macOS because its wheels come from PyPi anyway (cpu only)
|
|
|
|
|
|
|
|
return url
|