2022-09-13 07:48:18 -06:00
# this scripts installs necessary requirements and launches main program in webui.py
2023-08-09 11:49:33 -06:00
import logging
2023-07-17 11:10:24 -06:00
import re
2022-09-13 07:48:18 -06:00
import subprocess
import os
2023-08-10 08:18:10 -06:00
import shutil
2022-09-13 07:48:18 -06:00
import sys
import importlib . util
2023-12-03 19:35:35 -07:00
import importlib . metadata
2022-10-08 07:13:26 -06:00
import platform
2022-11-27 08:48:08 -07:00
import json
2024-07-07 23:51:06 -06:00
import shlex
2023-05-11 14:00:53 -06:00
from functools import lru_cache
2023-03-11 22:58:58 -07:00
2023-05-31 10:56:37 -06:00
from modules import cmd_args , errors
2023-03-25 07:05:12 -06:00
from modules . paths_internal import script_path , extensions_dir
2023-07-22 04:49:29 -06:00
from modules . timer import startup_timer
2023-08-09 11:49:33 -06:00
from modules import logging_config
2023-03-11 22:58:58 -07:00
2023-03-25 07:05:12 -06:00
args , _ = cmd_args . parser . parse_known_args ( )
2023-08-09 11:49:33 -06:00
logging_config . setup_logging ( args . loglevel )
2022-09-13 07:48:18 -06:00
2022-10-09 06:22:51 -06:00
python = sys . executable
git = os . environ . get ( ' GIT ' , " git " )
2022-10-15 01:28:20 -06:00
index_url = os . environ . get ( ' INDEX_URL ' , " " )
2023-03-25 07:05:12 -06:00
dir_repos = " repositories "
2023-01-05 01:57:01 -07:00
2023-05-11 01:30:29 -06:00
# Whether to default to printing command output
default_command_live = ( os . environ . get ( ' WEBUI_LAUNCH_LIVE_OUTPUT ' ) == " 1 " )
2024-01-04 10:13:36 -07:00
os . environ . setdefault ( ' GRADIO_ANALYTICS_ENABLED ' , ' False ' )
2023-01-05 01:57:01 -07:00
2023-03-25 07:05:12 -06:00
2023-01-25 10:07:48 -07:00
def check_python_version ( ) :
2023-01-28 00:48:08 -07:00
is_windows = platform . system ( ) == " Windows "
major = sys . version_info . major
minor = sys . version_info . minor
micro = sys . version_info . micro
if is_windows :
2023-01-28 00:21:31 -07:00
supported_minors = [ 10 ]
else :
supported_minors = [ 7 , 8 , 9 , 10 , 11 ]
2023-01-28 00:48:08 -07:00
if not ( major == 3 and minor in supported_minors ) :
2023-01-28 00:21:31 -07:00
import modules . errors
modules . errors . print_error_explanation ( f """
2023-01-28 00:48:08 -07:00
INCOMPATIBLE PYTHON VERSION
This program is tested with 3.10 .6 Python , but you have { major } . { minor } . { micro } .
2023-01-27 05:35:24 -07:00
If you encounter an error with " RuntimeError: Couldn ' t install torch. " message ,
or any other error regarding unsuccessful package ( library ) installation ,
please downgrade ( or upgrade ) to the latest version of 3.10 Python
and delete current Python and " venv " folder in WebUI ' s directory.
2023-03-29 15:00:20 -06:00
You can download 3.10 Python from here : https : / / www . python . org / downloads / release / python - 3106 /
2023-01-28 00:48:08 -07:00
2024-02-28 23:04:46 -07:00
{ " Alternatively, use a binary release of WebUI: https://github.com/AUTOMATIC1111/stable-diffusion-webui/releases/tag/v1.0.0-pre " if is_windows else " " }
2023-01-28 00:48:08 -07:00
Use - - skip - python - version - check to suppress this warning .
2023-01-28 00:21:31 -07:00
""" )
2023-01-25 10:07:48 -07:00
2023-05-11 14:00:53 -06:00
@lru_cache ( )
2023-01-05 01:57:01 -07:00
def commit_hash ( ) :
try :
2023-09-12 01:50:56 -06:00
return subprocess . check_output ( [ git , " -C " , script_path , " rev-parse " , " HEAD " ] , shell = False , encoding = ' utf8 ' ) . strip ( )
2023-01-05 01:57:01 -07:00
except Exception :
2023-05-11 14:00:53 -06:00
return " <none> "
2022-10-09 06:22:51 -06:00
2022-09-21 01:25:45 -06:00
2023-05-11 14:00:53 -06:00
@lru_cache ( )
2023-05-08 06:23:49 -06:00
def git_tag ( ) :
try :
2023-09-12 01:50:56 -06:00
return subprocess . check_output ( [ git , " -C " , script_path , " describe " , " --tags " ] , shell = False , encoding = ' utf8 ' ) . strip ( )
2023-05-08 06:23:49 -06:00
except Exception :
2023-06-02 22:07:07 -06:00
try :
2023-07-15 00:33:21 -06:00
2024-05-04 08:42:37 -06:00
changelog_md = os . path . join ( script_path , " CHANGELOG.md " )
2023-07-15 00:33:21 -06:00
with open ( changelog_md , " r " , encoding = " utf-8 " ) as file :
line = next ( ( line . strip ( ) for line in file if line . strip ( ) ) , " <none> " )
line = line . replace ( " ## " , " " )
return line
2023-06-02 22:07:07 -06:00
except Exception :
return " <none> "
2023-05-08 06:23:49 -06:00
2023-05-11 01:34:19 -06:00
def run ( command , desc = None , errdesc = None , custom_env = None , live : bool = default_command_live ) - > str :
2022-09-13 07:48:18 -06:00
if desc is not None :
print ( desc )
2023-05-11 01:34:19 -06:00
run_kwargs = {
" args " : command ,
" shell " : True ,
" env " : os . environ if custom_env is None else custom_env ,
" encoding " : ' utf8 ' ,
" errors " : ' ignore ' ,
}
2023-01-24 11:13:05 -07:00
2023-05-11 01:34:19 -06:00
if not live :
run_kwargs [ " stdout " ] = run_kwargs [ " stderr " ] = subprocess . PIPE
2023-01-24 11:13:05 -07:00
2023-05-11 01:34:19 -06:00
result = subprocess . run ( * * run_kwargs )
2022-09-13 07:48:18 -06:00
if result . returncode != 0 :
2023-05-11 01:34:19 -06:00
error_bits = [
f " { errdesc or ' Error running command ' } . " ,
f " Command: { command } " ,
f " Error code: { result . returncode } " ,
]
if result . stdout :
error_bits . append ( f " stdout: { result . stdout } " )
if result . stderr :
error_bits . append ( f " stderr: { result . stderr } " )
raise RuntimeError ( " \n " . join ( error_bits ) )
2022-09-13 07:48:18 -06:00
2023-05-11 01:34:19 -06:00
return ( result . stdout or " " )
2022-09-13 07:48:18 -06:00
def is_installed ( package ) :
try :
2023-12-03 21:26:12 -07:00
dist = importlib . metadata . distribution ( package )
2023-12-03 19:35:35 -07:00
except importlib . metadata . PackageNotFoundError :
2023-12-03 21:26:12 -07:00
try :
2023-12-03 19:35:35 -07:00
spec = importlib . util . find_spec ( package )
except ModuleNotFoundError :
2023-12-03 21:26:12 -07:00
return False
2022-09-13 07:48:18 -06:00
2023-12-03 19:35:35 -07:00
return spec is not None
2022-09-13 07:48:18 -06:00
2023-12-03 19:35:35 -07:00
return dist is not None
2022-09-13 07:48:18 -06:00
2022-10-09 06:22:51 -06:00
def repo_dir ( name ) :
2023-03-10 06:38:35 -07:00
return os . path . join ( script_path , dir_repos , name )
2022-10-09 06:22:51 -06:00
2023-05-11 01:30:29 -06:00
def run_pip ( command , desc = None , live = default_command_live ) :
2023-04-29 08:07:21 -06:00
if args . skip_install :
2023-01-16 10:13:23 -07:00
return
2022-10-15 01:28:20 -06:00
index_url_line = f ' --index-url { index_url } ' if index_url != ' ' else ' '
2023-04-29 08:07:21 -06:00
return run ( f ' " { python } " -m pip { command } --prefer-binary { index_url_line } ' , desc = f " Installing { desc } " , errdesc = f " Couldn ' t install { desc } " , live = live )
2022-10-09 06:22:51 -06:00
2023-05-11 14:29:34 -06:00
def check_run_python ( code : str ) - > bool :
2023-05-14 11:30:02 -06:00
result = subprocess . run ( [ python , " -c " , code ] , capture_output = True , shell = False )
2023-05-11 14:29:34 -06:00
return result . returncode == 0
2022-10-09 06:22:51 -06:00
2023-07-31 21:26:59 -06:00
def git_fix_workspace ( dir , name ) :
2023-07-31 21:20:59 -06:00
run ( f ' " { git } " -C " { dir } " fetch --refetch --no-auto-gc ' , f " Fetching all contents for { name } " , f " Couldn ' t fetch { name } " , live = True )
run ( f ' " { git } " -C " { dir } " gc --aggressive --prune=now ' , f " Pruning { name } " , f " Couldn ' t prune { name } " , live = True )
return
2023-08-06 23:38:18 -06:00
def run_git ( dir , name , command , desc = None , errdesc = None , custom_env = None , live : bool = default_command_live , autofix = True ) :
try :
return run ( f ' " { git } " -C " { dir } " { command } ' , desc = desc , errdesc = errdesc , custom_env = custom_env , live = live )
except RuntimeError :
2023-08-10 08:18:10 -06:00
if not autofix :
raise
2023-08-06 23:38:18 -06:00
print ( f " { errdesc } , attempting autofix... " )
git_fix_workspace ( dir , name )
return run ( f ' " { git } " -C " { dir } " { command } ' , desc = desc , errdesc = errdesc , custom_env = custom_env , live = live )
2022-10-09 06:22:51 -06:00
def git_clone ( url , dir , name , commithash = None ) :
# TODO clone into temporary dir and move if successful
if os . path . exists ( dir ) :
if commithash is None :
return
2023-08-06 23:38:18 -06:00
current_hash = run_git ( dir , name , ' rev-parse HEAD ' , None , f " Couldn ' t determine { name } ' s hash: { commithash } " , live = False ) . strip ( )
if current_hash == commithash :
return
2022-10-09 06:22:51 -06:00
2023-07-19 23:44:45 -06:00
if run_git ( dir , name , ' config --get remote.origin.url ' , None , f " Couldn ' t determine { name } ' s origin URL " , live = False ) . strip ( ) != url :
run_git ( dir , name , f ' remote set-url origin " { url } " ' , None , f " Failed to set { name } ' s origin URL " , live = False )
2023-08-13 07:51:21 -06:00
run_git ( dir , name , ' fetch ' , f " Fetching updates for { name } ... " , f " Couldn ' t fetch { name } " , autofix = False )
2023-07-31 21:20:59 -06:00
2023-08-13 11:15:20 -06:00
run_git ( dir , name , f ' checkout { commithash } ' , f " Checking out commit for { name } with hash: { commithash } ... " , f " Couldn ' t checkout commit { commithash } for { name } " , live = True )
2023-07-31 21:20:59 -06:00
2022-10-09 06:22:51 -06:00
return
2022-09-13 07:48:18 -06:00
2023-08-10 08:18:10 -06:00
try :
2024-02-15 05:26:14 -07:00
run ( f ' " { git } " clone --config core.filemode=false " { url } " " { dir } " ' , f " Cloning { name } into { dir } ... " , f " Couldn ' t clone { name } " , live = True )
2023-08-10 08:18:10 -06:00
except RuntimeError :
shutil . rmtree ( dir , ignore_errors = True )
raise
2022-10-09 06:22:51 -06:00
if commithash is not None :
2023-01-24 10:22:19 -07:00
run ( f ' " { git } " -C " { dir } " checkout { commithash } ' , None , " Couldn ' t checkout {name} ' s hash: {commithash} " )
2022-10-09 06:22:51 -06:00
2023-03-11 02:21:53 -07:00
2023-03-09 07:25:18 -07:00
def git_pull_recursive ( dir ) :
for subdir , _ , _ in os . walk ( dir ) :
if os . path . exists ( os . path . join ( subdir , ' .git ' ) ) :
try :
2023-03-11 02:21:53 -07:00
output = subprocess . check_output ( [ git , ' -C ' , subdir , ' pull ' , ' --autostash ' ] )
2023-03-09 07:25:18 -07:00
print ( f " Pulled changes for repository in ' { subdir } ' : \n { output . decode ( ' utf-8 ' ) . strip ( ) } \n " )
except subprocess . CalledProcessError as e :
print ( f " Couldn ' t perform ' git pull ' on repository in ' { subdir } ' : \n { e . output . decode ( ' utf-8 ' ) . strip ( ) } \n " )
2023-03-11 02:21:53 -07:00
2022-10-16 10:04:09 -06:00
def version_check ( commit ) :
try :
import requests
commits = requests . get ( ' https://api.github.com/repos/AUTOMATIC1111/stable-diffusion-webui/branches/master ' ) . json ( )
if commit != " <none> " and commits [ ' commit ' ] [ ' sha ' ] != commit :
print ( " -------------------------------------------------------- " )
print ( " | You are not up to date with the most recent release. | " )
print ( " | Consider running `git pull` to update. | " )
print ( " -------------------------------------------------------- " )
elif commits [ ' commit ' ] [ ' sha ' ] == commit :
print ( " You are up to date with the most recent release. " )
else :
print ( " Not a git clone, can ' t perform version check. " )
except Exception as e :
2022-11-01 05:19:24 -06:00
print ( " version check failed " , e )
2022-11-12 01:11:47 -07:00
def run_extension_installer ( extension_dir ) :
path_installer = os . path . join ( extension_dir , " install.py " )
if not os . path . isfile ( path_installer ) :
return
try :
env = os . environ . copy ( )
2024-05-04 08:42:37 -06:00
env [ ' PYTHONPATH ' ] = f " { script_path } { os . pathsep } { env . get ( ' PYTHONPATH ' , ' ' ) } "
2022-11-12 01:11:47 -07:00
2023-08-29 22:27:45 -06:00
stdout = run ( f ' " { python } " " { path_installer } " ' , errdesc = f " Error running install.py for extension { extension_dir } " , custom_env = env ) . strip ( )
2023-08-29 00:00:34 -06:00
if stdout :
print ( stdout )
2022-11-12 01:11:47 -07:00
except Exception as e :
2023-05-31 10:56:37 -06:00
errors . report ( str ( e ) )
2022-11-12 01:11:47 -07:00
2022-11-27 08:48:08 -07:00
def list_extensions ( settings_file ) :
settings = { }
try :
2024-01-03 15:16:58 -07:00
with open ( settings_file , " r " , encoding = " utf8 " ) as file :
settings = json . load ( file )
except FileNotFoundError :
pass
2023-05-28 23:54:13 -06:00
except Exception :
2024-01-03 14:21:53 -07:00
errors . report ( f ' \n Could not load settings \n The config file " { settings_file } " is likely corrupted \n It has been moved to the " tmp/config.json " \n Reverting config to default \n \n ' ' ' , exc_info = True )
os . replace ( settings_file , os . path . join ( script_path , " tmp " , " config.json " ) )
2022-11-27 08:48:08 -07:00
disabled_extensions = set ( settings . get ( ' disabled_extensions ' , [ ] ) )
2023-03-27 15:23:20 -06:00
disable_all_extensions = settings . get ( ' disable_all_extensions ' , ' none ' )
2023-08-16 09:24:24 -06:00
if disable_all_extensions != ' none ' or args . disable_extra_extensions or args . disable_all_extensions or not os . path . isdir ( extensions_dir ) :
2023-03-27 15:23:20 -06:00
return [ ]
2022-11-27 08:48:08 -07:00
2023-03-25 07:05:12 -06:00
return [ x for x in os . listdir ( extensions_dir ) if x not in disabled_extensions ]
2022-11-27 08:48:08 -07:00
def run_extensions_installers ( settings_file ) :
2023-03-25 07:05:12 -06:00
if not os . path . isdir ( extensions_dir ) :
2022-11-01 05:19:24 -06:00
return
2023-07-22 04:49:29 -06:00
with startup_timer . subcategory ( " run extensions installers " ) :
2023-08-09 11:49:33 -06:00
for dirname_extension in list_extensions ( settings_file ) :
logging . debug ( f " Installing { dirname_extension } " )
2023-08-09 08:08:36 -06:00
2023-07-22 08:08:45 -06:00
path = os . path . join ( extensions_dir , dirname_extension )
if os . path . isdir ( path ) :
run_extension_installer ( path )
startup_timer . record ( dirname_extension )
2022-11-01 05:19:24 -06:00
2022-10-09 06:22:51 -06:00
2023-07-17 11:10:24 -06:00
re_requirement = re . compile ( r " \ s*([-_a-zA-Z0-9]+) \ s*(?:== \ s*([-+_.a-zA-Z0-9]+))? \ s* " )
2023-07-21 07:49:33 -06:00
def requirements_met ( requirements_file ) :
2023-07-17 11:10:24 -06:00
"""
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
2022-12-03 02:35:17 -07:00
def prepare_environment ( ) :
2023-11-26 02:07:33 -07:00
torch_index_url = os . environ . get ( ' TORCH_INDEX_URL ' , " https://download.pytorch.org/whl/cu121 " )
2023-12-16 00:15:08 -07:00
torch_command = os . environ . get ( ' TORCH_COMMAND ' , f " pip install torch==2.1.2 torchvision==0.16.2 --extra-index-url { torch_index_url } " )
2023-12-02 02:11:11 -07:00
if args . use_ipex :
if platform . system ( ) == " Windows " :
# The "Nuullll/intel-extension-for-pytorch" wheels were built from IPEX source for Intel Arc GPU: https://github.com/intel/intel-extension-for-pytorch/tree/xpu-main
# This is NOT an Intel official release so please use it at your own risk!!
# See https://github.com/Nuullll/intel-extension-for-pytorch/releases/tag/v2.0.110%2Bxpu-master%2Bdll-bundle for details.
#
# Strengths (over official IPEX 2.0.110 windows release):
# - AOT build (for Arc GPU only) to eliminate JIT compilation overhead: https://github.com/intel/intel-extension-for-pytorch/issues/399
# - Bundles minimal oneAPI 2023.2 dependencies into the python wheels, so users don't need to install oneAPI for the whole system.
# - Provides a compatible torchvision wheel: https://github.com/intel/intel-extension-for-pytorch/issues/465
# Limitation:
# - Only works for python 3.10
url_prefix = " https://github.com/Nuullll/intel-extension-for-pytorch/releases/download/v2.0.110 % 2Bxpu-master % 2Bdll-bundle "
torch_command = os . environ . get ( ' TORCH_COMMAND ' , f " pip install { url_prefix } /torch-2.0.0a0+gite9ebda2-cp310-cp310-win_amd64.whl { url_prefix } /torchvision-0.15.2a0+fa99a53-cp310-cp310-win_amd64.whl { url_prefix } /intel_extension_for_pytorch-2.0.110+gitc6ea20b-cp310-cp310-win_amd64.whl " )
else :
# Using official IPEX release for linux since it's already an AOT build.
# However, users still have to install oneAPI toolkit and activate oneAPI environment manually.
# See https://intel.github.io/intel-extension-for-pytorch/index.html#installation for details.
torch_index_url = os . environ . get ( ' TORCH_INDEX_URL ' , " https://pytorch-extension.intel.com/release-whl/stable/xpu/us/ " )
torch_command = os . environ . get ( ' TORCH_COMMAND ' , f " pip install torch==2.0.0a0 intel-extension-for-pytorch==2.0.110+gitba7f6c1 --extra-index-url { torch_index_url } " )
2022-10-09 05:44:13 -06:00
requirements_file = os . environ . get ( ' REQS_FILE ' , " requirements_versions.txt " )
2024-01-30 19:46:53 -07:00
requirements_file_for_npu = os . environ . get ( ' REQS_FILE_FOR_NPU ' , " requirements_npu.txt " )
2022-10-06 03:08:06 -06:00
2023-12-16 00:15:08 -07:00
xformers_package = os . environ . get ( ' XFORMERS_PACKAGE ' , ' xformers==0.0.23.post1 ' )
2023-05-11 01:48:48 -06:00
clip_package = os . environ . get ( ' CLIP_PACKAGE ' , " https://github.com/openai/CLIP/archive/d50d76daa670286dd6cacf3bcd80b5e4823fc8e1.zip " )
openclip_package = os . environ . get ( ' OPENCLIP_PACKAGE ' , " https://github.com/mlfoundations/open_clip/archive/bb6e834e9c70d9c27d0dc3ecedeebeaeb1ffad6b.zip " )
2022-10-16 01:13:18 -06:00
2024-01-06 23:21:21 -07:00
assets_repo = os . environ . get ( ' ASSETS_REPO ' , " https://github.com/AUTOMATIC1111/stable-diffusion-webui-assets.git " )
2022-11-26 06:10:46 -07:00
stable_diffusion_repo = os . environ . get ( ' STABLE_DIFFUSION_REPO ' , " https://github.com/Stability-AI/stablediffusion.git " )
2023-07-11 12:16:43 -06:00
stable_diffusion_xl_repo = os . environ . get ( ' STABLE_DIFFUSION_XL_REPO ' , " https://github.com/Stability-AI/generative-models.git " )
2022-10-16 01:13:18 -06:00
k_diffusion_repo = os . environ . get ( ' K_DIFFUSION_REPO ' , ' https://github.com/crowsonkb/k-diffusion.git ' )
blip_repo = os . environ . get ( ' BLIP_REPO ' , ' https://github.com/salesforce/BLIP.git ' )
2022-10-09 05:44:13 -06:00
2024-01-06 23:21:21 -07:00
assets_commit_hash = os . environ . get ( ' ASSETS_COMMIT_HASH ' , " 6f7db241d2f8ba7457bac5ca9753331f0c266917 " )
2023-03-24 20:48:16 -06:00
stable_diffusion_commit_hash = os . environ . get ( ' STABLE_DIFFUSION_COMMIT_HASH ' , " cf1d67a6fd5ea1aa600c4df58e5b47da45f6bdbf " )
2023-07-26 21:05:16 -06:00
stable_diffusion_xl_commit_hash = os . environ . get ( ' STABLE_DIFFUSION_XL_COMMIT_HASH ' , " 45c443b316737a4ab6e40413d7794a7f5657c19f " )
2023-08-13 05:57:29 -06:00
k_diffusion_commit_hash = os . environ . get ( ' K_DIFFUSION_COMMIT_HASH ' , " ab527a9a6d347f364e3d185ba6d714e22d80cb3c " )
2022-10-09 05:44:13 -06:00
blip_commit_hash = os . environ . get ( ' BLIP_COMMIT_HASH ' , " 48211a1594f1321b00f14c9f7a5b4813144b2fb9 " )
2023-06-03 00:59:56 -06:00
try :
2023-08-13 10:03:49 -06:00
# the existence of this file is a signal to webui.sh/bat that webui needs to be restarted when it stops execution
2023-06-03 00:59:56 -06:00
os . remove ( os . path . join ( script_path , " tmp " , " restart " ) )
2023-07-21 07:49:33 -06:00
os . environ . setdefault ( ' SD_WEBUI_RESTARTING ' , ' 1 ' )
2023-06-03 00:59:56 -06:00
except OSError :
pass
2023-03-25 07:05:12 -06:00
if not args . skip_python_version_check :
2023-01-28 00:48:08 -07:00
check_python_version ( )
2023-07-22 04:49:29 -06:00
startup_timer . record ( " checks " )
2023-01-05 01:57:01 -07:00
commit = commit_hash ( )
2023-05-08 06:23:49 -06:00
tag = git_tag ( )
2023-07-22 04:49:29 -06:00
startup_timer . record ( " git version info " )
2022-09-21 06:03:15 -06:00
2022-10-09 05:44:13 -06:00
print ( f " Python { sys . version } " )
2023-05-08 06:23:49 -06:00
print ( f " Version: { tag } " )
2022-10-09 05:44:13 -06:00
print ( f " Commit hash: { commit } " )
2023-01-24 11:13:05 -07:00
2023-03-25 07:05:12 -06:00
if args . reinstall_torch or not is_installed ( " torch " ) or not is_installed ( " torchvision " ) :
2023-01-24 11:13:05 -07:00
run ( f ' " { python } " -m { torch_command } ' , " Installing torch and torchvision " , " Couldn ' t install torch " , live = True )
2023-07-22 04:49:29 -06:00
startup_timer . record ( " install torch " )
2022-09-13 07:48:18 -06:00
2023-12-02 02:11:11 -07:00
if args . use_ipex :
args . skip_torch_cuda_test = True
2023-05-11 14:29:34 -06:00
if not args . skip_torch_cuda_test and not check_run_python ( " import torch; assert torch.cuda.is_available() " ) :
raise RuntimeError (
' Torch is not able to use GPU; '
' add --skip-torch-cuda-test to COMMANDLINE_ARGS variable to disable this check '
)
2023-07-22 04:49:29 -06:00
startup_timer . record ( " torch GPU test " )
2022-10-09 05:44:13 -06:00
if not is_installed ( " clip " ) :
2022-10-14 20:02:18 -06:00
run_pip ( f " install { clip_package } " , " clip " )
2023-07-22 04:49:29 -06:00
startup_timer . record ( " install clip " )
2022-10-08 05:42:34 -06:00
2022-11-26 06:10:46 -07:00
if not is_installed ( " open_clip " ) :
run_pip ( f " install { openclip_package } " , " open_clip " )
2023-07-22 04:49:29 -06:00
startup_timer . record ( " install open_clip " )
2022-11-26 06:10:46 -07:00
2023-03-25 07:05:12 -06:00
if ( not is_installed ( " xformers " ) or args . reinstall_xformers ) and args . xformers :
2023-08-28 23:32:48 -06:00
run_pip ( f " install -U -I --no-deps { xformers_package } " , " xformers " )
2023-07-22 04:49:29 -06:00
startup_timer . record ( " install xformers " )
2023-05-16 11:15:30 -06:00
if not is_installed ( " ngrok " ) and args . ngrok :
run_pip ( " install ngrok " , " ngrok " )
2023-07-22 04:49:29 -06:00
startup_timer . record ( " install ngrok " )
2022-10-11 03:38:03 -06:00
2023-03-10 06:38:35 -07:00
os . makedirs ( os . path . join ( script_path , dir_repos ) , exist_ok = True )
2022-09-30 02:42:40 -06:00
2024-01-06 23:21:21 -07:00
git_clone ( assets_repo , repo_dir ( ' stable-diffusion-webui-assets ' ) , " assets " , assets_commit_hash )
2022-11-26 06:10:46 -07:00
git_clone ( stable_diffusion_repo , repo_dir ( ' stable-diffusion-stability-ai ' ) , " Stable Diffusion " , stable_diffusion_commit_hash )
2023-07-11 12:16:43 -06:00
git_clone ( stable_diffusion_xl_repo , repo_dir ( ' generative-models ' ) , " Stable Diffusion XL " , stable_diffusion_xl_commit_hash )
2022-10-16 01:13:18 -06:00
git_clone ( k_diffusion_repo , repo_dir ( ' k-diffusion ' ) , " K-diffusion " , k_diffusion_commit_hash )
git_clone ( blip_repo , repo_dir ( ' BLIP ' ) , " BLIP " , blip_commit_hash )
2022-09-13 07:48:18 -06:00
2023-07-22 04:49:29 -06:00
startup_timer . record ( " clone repositores " )
2023-03-12 03:11:51 -06:00
if not os . path . isfile ( requirements_file ) :
requirements_file = os . path . join ( script_path , requirements_file )
2023-07-17 11:10:24 -06:00
2023-07-21 07:49:33 -06:00
if not requirements_met ( requirements_file ) :
2023-07-17 11:10:24 -06:00
run_pip ( f " install -r \" { requirements_file } \" " , " requirements " )
2023-07-22 04:49:29 -06:00
startup_timer . record ( " install requirements " )
2022-10-09 05:44:13 -06:00
2024-01-30 19:46:53 -07:00
if not os . path . isfile ( requirements_file_for_npu ) :
requirements_file_for_npu = os . path . join ( script_path , requirements_file_for_npu )
if " torch_npu " in torch_command and not requirements_met ( requirements_file_for_npu ) :
run_pip ( f " install -r \" { requirements_file_for_npu } \" " , " requirements_for_npu " )
startup_timer . record ( " install requirements_for_npu " )
2023-08-28 23:58:01 -06:00
if not args . skip_install :
run_extensions_installers ( settings_file = args . ui_settings_file )
2022-11-01 05:19:24 -06:00
2023-03-25 07:05:12 -06:00
if args . update_check :
2022-10-16 10:04:09 -06:00
version_check ( commit )
2023-07-22 04:49:29 -06:00
startup_timer . record ( " check version " )
2023-03-09 07:25:18 -07:00
2023-03-25 07:05:12 -06:00
if args . update_all_extensions :
git_pull_recursive ( extensions_dir )
2023-07-22 04:49:29 -06:00
startup_timer . record ( " update extensions " )
2023-05-11 09:28:15 -06:00
2022-10-18 06:18:02 -06:00
if " --exit " in sys . argv :
2022-10-09 05:44:13 -06:00
print ( " Exiting because of --exit argument " )
exit ( 0 )
2022-09-13 07:48:18 -06:00
2022-10-29 23:22:44 -06:00
2023-07-12 14:52:43 -06:00
2023-05-11 12:57:43 -06:00
def configure_for_tests ( ) :
2022-11-14 03:39:22 -07:00
if " --api " not in sys . argv :
sys . argv . append ( " --api " )
if " --ckpt " not in sys . argv :
sys . argv . append ( " --ckpt " )
2023-03-10 06:38:35 -07:00
sys . argv . append ( os . path . join ( script_path , " test/test_files/empty.pt " ) )
2022-11-14 08:40:15 -07:00
if " --skip-torch-cuda-test " not in sys . argv :
sys . argv . append ( " --skip-torch-cuda-test " )
2023-01-17 01:04:56 -07:00
if " --disable-nan-check " not in sys . argv :
sys . argv . append ( " --disable-nan-check " )
2022-10-29 23:22:44 -06:00
2023-01-10 02:46:35 -07:00
os . environ [ ' COMMANDLINE_ARGS ' ] = " "
2022-10-29 23:22:44 -06:00
2022-09-13 07:48:18 -06:00
2022-11-02 23:08:11 -06:00
def start ( ) :
2024-07-07 23:51:06 -06:00
print ( f " Launching { ' API server ' if ' --nowebui ' in sys . argv else ' Web UI ' } with arguments: { shlex . join ( sys . argv [ 1 : ] ) } " )
2022-09-13 07:48:18 -06:00
import webui
2022-11-02 23:08:11 -06:00
if ' --nowebui ' in sys . argv :
webui . api_only ( )
else :
webui . webui ( )
2023-08-30 10:48:47 -06:00
def dump_sysinfo ( ) :
from modules import sysinfo
import datetime
text = sysinfo . get ( )
2023-11-19 09:38:31 -07:00
filename = f " sysinfo- { datetime . datetime . utcnow ( ) . strftime ( ' % Y- % m- %d - % H- % M ' ) } .json "
2023-08-30 10:48:47 -06:00
with open ( filename , " w " , encoding = " utf8 " ) as file :
file . write ( text )
return filename