2022-09-13 16:48:18 +03:00
# this scripts installs necessary requirements and launches main program in webui.py
import subprocess
import os
import sys
import importlib . util
import shlex
2022-10-08 16:13:26 +03:00
import platform
2022-11-27 18:48:08 +03:00
import argparse
import json
2022-09-13 16:48:18 +03:00
2022-10-09 15:22:51 +03:00
dir_repos = " repositories "
2022-11-01 14:19:24 +03:00
dir_extensions = " extensions "
2022-10-09 15:22:51 +03:00
python = sys . executable
git = os . environ . get ( ' GIT ' , " git " )
2022-10-15 10:28:20 +03:00
index_url = os . environ . get ( ' INDEX_URL ' , " " )
2023-01-05 11:57:01 +03:00
stored_commit_hash = None
2023-01-16 20:13:23 +03:00
skip_install = False
2023-01-05 11:57:01 +03:00
2023-01-25 20:07:48 +03:00
def check_python_version ( ) :
version = sys . version_info
version_range = None
if os . name == " nt " :
version_range = range ( 7 , 11 )
else :
version_range = range ( 7 , 12 )
2023-01-25 20:33:35 +03:00
try :
2023-01-25 20:38:28 +03:00
assert version . major == 3 and version . minor in version_range , " Unsupported Python version, please use Python 3.10.x instead. You can download latest release as of 25th January (3.10.9) from here: https://www.python.org/downloads/release/python-3109/. Please, make sure to first delete current version of Python first. "
2023-01-25 20:33:35 +03:00
except AssertionError as e :
print ( e )
2023-01-25 20:38:28 +03:00
sys . exit ( - 1 )
2023-01-25 20:07:48 +03:00
2023-01-05 11:57:01 +03:00
def commit_hash ( ) :
global stored_commit_hash
if stored_commit_hash is not None :
return stored_commit_hash
try :
stored_commit_hash = run ( f " { git } rev-parse HEAD " ) . strip ( )
except Exception :
stored_commit_hash = " <none> "
return stored_commit_hash
2022-10-09 15:22:51 +03:00
2022-09-21 10:25:45 +03:00
def extract_arg ( args , name ) :
return [ x for x in args if x != name ] , name in args
2022-11-14 13:39:22 +03:00
def extract_opt ( args , name ) :
opt = None
is_present = False
if name in args :
is_present = True
idx = args . index ( name )
del args [ idx ]
if idx < len ( args ) and args [ idx ] [ 0 ] != " - " :
opt = args [ idx ]
del args [ idx ]
return args , is_present , opt
2023-01-24 21:13:05 +03:00
def run ( command , desc = None , errdesc = None , custom_env = None , live = False ) :
2022-09-13 16:48:18 +03:00
if desc is not None :
print ( desc )
2023-01-24 21:13:05 +03:00
if live :
result = subprocess . run ( command , shell = True , env = os . environ if custom_env is None else custom_env )
if result . returncode != 0 :
raise RuntimeError ( f """ { errdesc or ' Error running command ' } .
Command : { command }
Error code : { result . returncode } """ )
return " "
2022-11-01 14:48:53 +03:00
result = subprocess . run ( command , stdout = subprocess . PIPE , stderr = subprocess . PIPE , shell = True , env = os . environ if custom_env is None else custom_env )
2022-09-13 16:48:18 +03:00
if result . returncode != 0 :
message = f """ { errdesc or ' Error running command ' } .
Command : { command }
Error code : { result . returncode }
stdout : { result . stdout . decode ( encoding = " utf8 " , errors = " ignore " ) if len ( result . stdout ) > 0 else ' <empty> ' }
stderr : { result . stderr . decode ( encoding = " utf8 " , errors = " ignore " ) if len ( result . stderr ) > 0 else ' <empty> ' }
"""
raise RuntimeError ( message )
return result . stdout . decode ( encoding = " utf8 " , errors = " ignore " )
def check_run ( command ) :
result = subprocess . run ( command , stdout = subprocess . PIPE , stderr = subprocess . PIPE , shell = True )
return result . returncode == 0
def is_installed ( package ) :
try :
spec = importlib . util . find_spec ( package )
except ModuleNotFoundError :
return False
return spec is not None
2022-10-09 15:22:51 +03:00
def repo_dir ( name ) :
return os . path . join ( dir_repos , name )
def run_python ( code , desc = None , errdesc = None ) :
return run ( f ' " { python } " -c " { code } " ' , desc , errdesc )
def run_pip ( args , desc = None ) :
2023-01-16 20:13:23 +03:00
if skip_install :
return
2022-10-15 10:28:20 +03:00
index_url_line = f ' --index-url { index_url } ' if index_url != ' ' else ' '
return run ( f ' " { python } " -m pip { args } --prefer-binary { index_url_line } ' , desc = f " Installing { desc } " , errdesc = f " Couldn ' t install { desc } " )
2022-10-09 15:22:51 +03:00
def check_run_python ( code ) :
return check_run ( f ' " { python } " -c " { code } " ' )
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-01-24 20:22:19 +03:00
current_hash = run ( f ' " { git } " -C " { dir } " rev-parse HEAD ' , None , f " Couldn ' t determine { name } ' s hash: { commithash } " ) . strip ( )
2022-10-09 15:22:51 +03:00
if current_hash == commithash :
return
2023-01-24 20:22:19 +03:00
run ( f ' " { git } " -C " { dir } " fetch ' , f " Fetching updates for { name } ... " , f " Couldn ' t fetch { name } " )
run ( f ' " { git } " -C " { dir } " checkout { commithash } ' , f " Checking out commit for { name } with hash: { commithash } ... " , f " Couldn ' t checkout commit { commithash } for { name } " )
2022-10-09 15:22:51 +03:00
return
2022-09-13 16:48:18 +03:00
2022-10-09 15:22:51 +03:00
run ( f ' " { git } " clone " { url } " " { dir } " ' , f " Cloning { name } into { dir } ... " , f " Couldn ' t clone { name } " )
if commithash is not None :
2023-01-24 20:22:19 +03:00
run ( f ' " { git } " -C " { dir } " checkout { commithash } ' , None , " Couldn ' t checkout {name} ' s hash: {commithash} " )
2022-10-09 15:22:51 +03:00
2022-10-16 19:04:09 +03: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 14:19:24 +03:00
print ( " version check failed " , e )
2022-11-12 11:11:47 +03: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 ( )
env [ ' PYTHONPATH ' ] = os . path . abspath ( " . " )
print ( run ( f ' " { python } " " { path_installer } " ' , errdesc = f " Error running install.py for extension { extension_dir } " , custom_env = env ) )
except Exception as e :
print ( e , file = sys . stderr )
2022-11-27 18:48:08 +03:00
def list_extensions ( settings_file ) :
settings = { }
try :
if os . path . isfile ( settings_file ) :
with open ( settings_file , " r " , encoding = " utf8 " ) as file :
settings = json . load ( file )
except Exception as e :
print ( e , file = sys . stderr )
disabled_extensions = set ( settings . get ( ' disabled_extensions ' , [ ] ) )
return [ x for x in os . listdir ( dir_extensions ) if x not in disabled_extensions ]
def run_extensions_installers ( settings_file ) :
2022-11-01 14:19:24 +03:00
if not os . path . isdir ( dir_extensions ) :
return
2022-11-27 18:48:08 +03:00
for dirname_extension in list_extensions ( settings_file ) :
2022-11-12 11:11:47 +03:00
run_extension_installer ( os . path . join ( dir_extensions , dirname_extension ) )
2022-11-01 14:19:24 +03:00
2022-10-09 15:22:51 +03:00
2022-12-03 12:35:17 +03:00
def prepare_environment ( ) :
2023-01-16 20:13:23 +03:00
global skip_install
2022-12-23 01:28:10 +03:00
torch_command = os . environ . get ( ' TORCH_COMMAND ' , " pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 --extra-index-url https://download.pytorch.org/whl/cu117 " )
2022-10-09 14:44:13 +03:00
requirements_file = os . environ . get ( ' REQS_FILE ' , " requirements_versions.txt " )
commandline_args = os . environ . get ( ' COMMANDLINE_ARGS ' , " " )
2022-10-06 12:08:06 +03:00
2022-10-09 14:44:13 +03:00
gfpgan_package = os . environ . get ( ' GFPGAN_PACKAGE ' , " git+https://github.com/TencentARC/GFPGAN.git@8d2447a2d918f8eba5a4a01463fd48e45126a379 " )
clip_package = os . environ . get ( ' CLIP_PACKAGE ' , " git+https://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1 " )
2022-11-26 16:10:46 +03:00
openclip_package = os . environ . get ( ' OPENCLIP_PACKAGE ' , " git+https://github.com/mlfoundations/open_clip.git@bb6e834e9c70d9c27d0dc3ecedeebeaeb1ffad6b " )
2022-10-16 10:13:18 +03:00
2022-11-26 16:10:46 +03:00
stable_diffusion_repo = os . environ . get ( ' STABLE_DIFFUSION_REPO ' , " https://github.com/Stability-AI/stablediffusion.git " )
2022-11-13 06:41:22 +03:00
taming_transformers_repo = os . environ . get ( ' TAMING_TRANSFORMERS_REPO ' , " https://github.com/CompVis/taming-transformers.git " )
2022-10-16 10:13:18 +03:00
k_diffusion_repo = os . environ . get ( ' K_DIFFUSION_REPO ' , ' https://github.com/crowsonkb/k-diffusion.git ' )
2022-11-13 06:41:22 +03:00
codeformer_repo = os . environ . get ( ' CODEFORMER_REPO ' , ' https://github.com/sczhou/CodeFormer.git ' )
2022-10-16 10:13:18 +03:00
blip_repo = os . environ . get ( ' BLIP_REPO ' , ' https://github.com/salesforce/BLIP.git ' )
2022-10-09 14:44:13 +03:00
2022-11-26 16:10:46 +03:00
stable_diffusion_commit_hash = os . environ . get ( ' STABLE_DIFFUSION_COMMIT_HASH ' , " 47b6b607fdd31875c9279cd2f4f16b92e4ea958e " )
2022-10-09 14:44:13 +03:00
taming_transformers_commit_hash = os . environ . get ( ' TAMING_TRANSFORMERS_COMMIT_HASH ' , " 24268930bf1dce879235a7fddd0b2355b84d7ea6 " )
2022-11-26 16:10:46 +03:00
k_diffusion_commit_hash = os . environ . get ( ' K_DIFFUSION_COMMIT_HASH ' , " 5b3af030dd83e0297272d861c19477735d0317ec " )
2022-10-09 14:44:13 +03:00
codeformer_commit_hash = os . environ . get ( ' CODEFORMER_COMMIT_HASH ' , " c5b4593074ba6214284d6acd5f1719b6c5d739af " )
blip_commit_hash = os . environ . get ( ' BLIP_COMMIT_HASH ' , " 48211a1594f1321b00f14c9f7a5b4813144b2fb9 " )
2022-10-18 15:18:02 +03:00
sys . argv + = shlex . split ( commandline_args )
2022-10-09 14:44:13 +03:00
2022-11-27 18:48:08 +03:00
parser = argparse . ArgumentParser ( )
parser . add_argument ( " --ui-settings-file " , type = str , help = " filename to use for ui settings " , default = ' config.json ' )
args , _ = parser . parse_known_args ( sys . argv )
2022-12-01 12:04:14 +03:00
sys . argv , _ = extract_arg ( sys . argv , ' -f ' )
2022-10-18 15:18:02 +03:00
sys . argv , skip_torch_cuda_test = extract_arg ( sys . argv , ' --skip-torch-cuda-test ' )
sys . argv , reinstall_xformers = extract_arg ( sys . argv , ' --reinstall-xformers ' )
2023-01-23 17:17:31 +03:00
sys . argv , reinstall_torch = extract_arg ( sys . argv , ' --reinstall-torch ' )
2022-10-18 15:18:02 +03:00
sys . argv , update_check = extract_arg ( sys . argv , ' --update-check ' )
2022-11-14 13:39:22 +03:00
sys . argv , run_tests , test_dir = extract_opt ( sys . argv , ' --tests ' )
2023-01-16 20:13:23 +03:00
sys . argv , skip_install = extract_arg ( sys . argv , ' --skip-install ' )
2022-10-18 15:18:02 +03:00
xformers = ' --xformers ' in sys . argv
ngrok = ' --ngrok ' in sys . argv
2022-10-09 14:44:13 +03:00
2023-01-05 11:57:01 +03:00
commit = commit_hash ( )
2022-09-21 15:03:15 +03:00
2022-10-09 14:44:13 +03:00
print ( f " Python { sys . version } " )
print ( f " Commit hash: { commit } " )
2023-01-24 21:13:05 +03:00
2023-01-23 17:17:31 +03:00
if reinstall_torch or not is_installed ( " torch " ) or not is_installed ( " torchvision " ) :
2023-01-24 21:13:05 +03:00
run ( f ' " { python } " -m { torch_command } ' , " Installing torch and torchvision " , " Couldn ' t install torch " , live = True )
2022-09-13 16:48:18 +03:00
2022-12-03 10:28:53 +03:00
if not skip_torch_cuda_test :
2022-10-09 14:44:13 +03:00
run_python ( " import torch; assert torch.cuda.is_available(), ' Torch is not able to use GPU; add --skip-torch-cuda-test to COMMANDLINE_ARGS variable to disable this check ' " )
2022-09-13 16:48:18 +03:00
2022-10-09 14:44:13 +03:00
if not is_installed ( " gfpgan " ) :
2022-10-15 05:02:18 +03:00
run_pip ( f " install { gfpgan_package } " , " gfpgan " )
2022-10-02 18:21:56 +03:00
2022-10-09 14:44:13 +03:00
if not is_installed ( " clip " ) :
2022-10-15 05:02:18 +03:00
run_pip ( f " install { clip_package } " , " clip " )
2022-10-08 14:42:34 +03:00
2022-11-26 16:10:46 +03:00
if not is_installed ( " open_clip " ) :
run_pip ( f " install { openclip_package } " , " open_clip " )
2022-10-19 21:16:22 +03:00
if ( not is_installed ( " xformers " ) or reinstall_xformers ) and xformers :
2022-10-09 14:44:13 +03:00
if platform . system ( ) == " Windows " :
2022-10-19 21:16:22 +03:00
if platform . python_version ( ) . startswith ( " 3.10 " ) :
2022-12-23 01:28:10 +03:00
run_pip ( f " install -U -I --no-deps xformers==0.0.16rc425 " , " xformers " )
2022-10-19 21:16:22 +03:00
else :
print ( " Installation of xformers is not supported in this version of Python. " )
print ( " You can also check this and build manually: https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Xformers#building-xformers-on-windows-by-duckness " )
if not is_installed ( " xformers " ) :
exit ( 0 )
2022-10-09 14:44:13 +03:00
elif platform . system ( ) == " Linux " :
2023-01-24 06:21:07 +03:00
run_pip ( " install xformers==0.0.16rc425 " , " xformers " )
2022-10-08 19:02:56 +03:00
2022-10-11 12:38:03 +03:00
if not is_installed ( " pyngrok " ) and ngrok :
2022-10-15 05:02:18 +03:00
run_pip ( " install pyngrok " , " ngrok " )
2022-10-11 12:38:03 +03:00
2022-10-09 14:44:13 +03:00
os . makedirs ( dir_repos , exist_ok = True )
2022-09-30 11:42:40 +03:00
2022-11-26 16:10:46 +03:00
git_clone ( stable_diffusion_repo , repo_dir ( ' stable-diffusion-stability-ai ' ) , " Stable Diffusion " , stable_diffusion_commit_hash )
2022-10-16 10:13:18 +03:00
git_clone ( taming_transformers_repo , repo_dir ( ' taming-transformers ' ) , " Taming Transformers " , taming_transformers_commit_hash )
git_clone ( k_diffusion_repo , repo_dir ( ' k-diffusion ' ) , " K-diffusion " , k_diffusion_commit_hash )
git_clone ( codeformer_repo , repo_dir ( ' CodeFormer ' ) , " CodeFormer " , codeformer_commit_hash )
git_clone ( blip_repo , repo_dir ( ' BLIP ' ) , " BLIP " , blip_commit_hash )
2022-09-13 16:48:18 +03:00
2022-10-09 14:44:13 +03:00
if not is_installed ( " lpips " ) :
2022-10-15 05:02:18 +03:00
run_pip ( f " install -r { os . path . join ( repo_dir ( ' CodeFormer ' ) , ' requirements.txt ' ) } " , " requirements for CodeFormer " )
2022-09-13 16:48:18 +03:00
2022-10-15 05:02:18 +03:00
run_pip ( f " install -r { requirements_file } " , " requirements for Web UI " )
2022-10-09 14:44:13 +03:00
2022-11-27 18:48:08 +03:00
run_extensions_installers ( settings_file = args . ui_settings_file )
2022-11-01 14:19:24 +03:00
2022-10-18 15:18:02 +03:00
if update_check :
2022-10-16 19:04:09 +03:00
version_check ( commit )
2022-10-16 12:54:09 +03:00
2022-10-18 15:18:02 +03:00
if " --exit " in sys . argv :
2022-10-09 14:44:13 +03:00
print ( " Exiting because of --exit argument " )
exit ( 0 )
2022-09-13 16:48:18 +03:00
2022-10-30 08:22:44 +03:00
if run_tests :
2022-11-14 14:36:07 +03:00
exitcode = tests ( test_dir )
exit ( exitcode )
2022-10-30 08:22:44 +03:00
2022-11-14 13:39:22 +03:00
def tests ( test_dir ) :
if " --api " not in sys . argv :
sys . argv . append ( " --api " )
if " --ckpt " not in sys . argv :
sys . argv . append ( " --ckpt " )
sys . argv . append ( " ./test/test_files/empty.pt " )
2022-11-14 18:40:15 +03:00
if " --skip-torch-cuda-test " not in sys . argv :
sys . argv . append ( " --skip-torch-cuda-test " )
2023-01-17 11:04:56 +03:00
if " --disable-nan-check " not in sys . argv :
sys . argv . append ( " --disable-nan-check " )
2022-10-30 08:28:36 +03:00
2022-11-14 13:39:22 +03:00
print ( f " Launching Web UI in another process for testing with arguments: { ' ' . join ( sys . argv [ 1 : ] ) } " )
2022-10-30 08:22:44 +03:00
2023-01-10 12:46:35 +03:00
os . environ [ ' COMMANDLINE_ARGS ' ] = " "
2022-10-30 08:22:44 +03:00
with open ( ' test/stdout.txt ' , " w " , encoding = " utf8 " ) as stdout , open ( ' test/stderr.txt ' , " w " , encoding = " utf8 " ) as stderr :
2022-11-14 13:39:22 +03:00
proc = subprocess . Popen ( [ sys . executable , * sys . argv ] , stdout = stdout , stderr = stderr )
2022-10-30 08:22:44 +03:00
import test . server_poll
2022-11-14 14:36:07 +03:00
exitcode = test . server_poll . run_tests ( proc , test_dir )
2022-10-30 08:22:44 +03:00
print ( f " Stopping Web UI process with id { proc . pid } " )
proc . kill ( )
2022-11-14 14:36:07 +03:00
return exitcode
2022-10-30 08:22:44 +03:00
2022-09-13 16:48:18 +03:00
2022-11-03 08:08:11 +03:00
def start ( ) :
print ( f " Launching { ' API server ' if ' --nowebui ' in sys . argv else ' Web UI ' } with arguments: { ' ' . join ( sys . argv [ 1 : ] ) } " )
2022-09-13 16:48:18 +03:00
import webui
2022-11-03 08:08:11 +03:00
if ' --nowebui ' in sys . argv :
webui . api_only ( )
else :
webui . webui ( )
2022-09-13 16:48:18 +03:00
2022-10-09 14:44:13 +03:00
2022-09-20 14:56:07 +03:00
if __name__ == " __main__ " :
2023-01-25 20:07:48 +03:00
check_python_version ( )
2022-12-03 12:35:17 +03:00
prepare_environment ( )
2022-11-03 08:08:11 +03:00
start ( )