2022-09-13 07:48:18 -06:00
# this scripts installs necessary requirements and launches main program in webui.py
import subprocess
import os
import sys
import importlib . util
2022-10-08 07:13:26 -06:00
import platform
2022-11-27 08:48:08 -07:00
import json
2023-05-11 14:00:53 -06:00
from functools import lru_cache
2023-03-11 22:58:58 -07:00
2023-03-25 07:05:12 -06:00
from modules import cmd_args
from modules . paths_internal import script_path , extensions_dir
2023-03-11 22:58:58 -07:00
2023-03-25 07:05:12 -06:00
args , _ = cmd_args . parser . parse_known_args ( )
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 " )
2023-03-25 03:21:18 -06:00
if ' GRADIO_ANALYTICS_ENABLED ' not in os . environ :
os . environ [ ' 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
{ " Alternatively, use a binary release of WebUI: https://github.com/AUTOMATIC1111/stable-diffusion-webui/releases " if is_windows else " " }
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-05-14 11:30:02 -06:00
return subprocess . check_output ( [ git , " 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-05-14 11:30:02 -06:00
return subprocess . check_output ( [ git , " describe " , " --tags " ] , shell = False , encoding = ' utf8 ' ) . strip ( )
2023-05-08 06:23:49 -06:00
except Exception :
2023-05-11 14:00:53 -06:00
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 :
spec = importlib . util . find_spec ( package )
except ModuleNotFoundError :
return False
return spec is not None
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
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 10:22:19 -07:00
current_hash = run ( f ' " { git } " -C " { dir } " rev-parse HEAD ' , None , f " Couldn ' t determine { name } ' s hash: { commithash } " ) . strip ( )
2022-10-09 06:22:51 -06:00
if current_hash == commithash :
return
2023-01-24 10:22:19 -07: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 06:22:51 -06:00
return
2022-09-13 07:48:18 -06:00
2022-10-09 06:22:51 -06: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 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 ( )
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 08:48:08 -07: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 ' , [ ] ) )
2023-03-27 15:23:20 -06:00
disable_all_extensions = settings . get ( ' disable_all_extensions ' , ' none ' )
if disable_all_extensions != ' none ' :
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
2022-11-27 08:48:08 -07:00
for dirname_extension in list_extensions ( settings_file ) :
2023-03-25 07:05:12 -06:00
run_extension_installer ( os . path . join ( extensions_dir , dirname_extension ) )
2022-11-01 05:19:24 -06:00
2022-10-09 06:22:51 -06:00
2022-12-03 02:35:17 -07:00
def prepare_environment ( ) :
2023-05-11 01:35:22 -06:00
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 } " )
2022-10-09 05:44:13 -06:00
requirements_file = os . environ . get ( ' REQS_FILE ' , " requirements_versions.txt " )
2022-10-06 03:08:06 -06:00
2023-04-04 15:27:27 -06:00
xformers_package = os . environ . get ( ' XFORMERS_PACKAGE ' , ' xformers==0.0.17 ' )
2023-05-11 01:48:48 -06:00
gfpgan_package = os . environ . get ( ' GFPGAN_PACKAGE ' , " https://github.com/TencentARC/GFPGAN/archive/8d2447a2d918f8eba5a4a01463fd48e45126a379.zip " )
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
2022-11-26 06:10:46 -07:00
stable_diffusion_repo = os . environ . get ( ' STABLE_DIFFUSION_REPO ' , " https://github.com/Stability-AI/stablediffusion.git " )
2022-11-12 20:41:22 -07:00
taming_transformers_repo = os . environ . get ( ' TAMING_TRANSFORMERS_REPO ' , " https://github.com/CompVis/taming-transformers.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 ' )
2022-11-12 20:41:22 -07:00
codeformer_repo = os . environ . get ( ' CODEFORMER_REPO ' , ' https://github.com/sczhou/CodeFormer.git ' )
2022-10-16 01:13:18 -06:00
blip_repo = os . environ . get ( ' BLIP_REPO ' , ' https://github.com/salesforce/BLIP.git ' )
2022-10-09 05:44:13 -06:00
2023-03-24 20:48:16 -06:00
stable_diffusion_commit_hash = os . environ . get ( ' STABLE_DIFFUSION_COMMIT_HASH ' , " cf1d67a6fd5ea1aa600c4df58e5b47da45f6bdbf " )
2022-10-09 05:44:13 -06:00
taming_transformers_commit_hash = os . environ . get ( ' TAMING_TRANSFORMERS_COMMIT_HASH ' , " 24268930bf1dce879235a7fddd0b2355b84d7ea6 " )
2022-11-26 06:10:46 -07:00
k_diffusion_commit_hash = os . environ . get ( ' K_DIFFUSION_COMMIT_HASH ' , " 5b3af030dd83e0297272d861c19477735d0317ec " )
2022-10-09 05:44:13 -06:00
codeformer_commit_hash = os . environ . get ( ' CODEFORMER_COMMIT_HASH ' , " c5b4593074ba6214284d6acd5f1719b6c5d739af " )
blip_commit_hash = os . environ . get ( ' BLIP_COMMIT_HASH ' , " 48211a1594f1321b00f14c9f7a5b4813144b2fb9 " )
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-01-05 01:57:01 -07:00
commit = commit_hash ( )
2023-05-08 06:23:49 -06:00
tag = git_tag ( )
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 )
2022-09-13 07:48:18 -06:00
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 '
)
2022-09-13 07:48:18 -06:00
2022-10-09 05:44:13 -06:00
if not is_installed ( " gfpgan " ) :
2022-10-14 20:02:18 -06:00
run_pip ( f " install { gfpgan_package } " , " gfpgan " )
2022-10-02 09:21:56 -06:00
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 " )
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-03-25 07:05:12 -06:00
if ( not is_installed ( " xformers " ) or args . reinstall_xformers ) and args . xformers :
2022-10-09 05:44:13 -06:00
if platform . system ( ) == " Windows " :
2022-10-19 12:16:22 -06:00
if platform . python_version ( ) . startswith ( " 3.10 " ) :
2023-04-29 03:36:50 -06:00
run_pip ( f " install -U -I --no-deps { xformers_package } " , " xformers " , live = True )
2022-10-19 12:16:22 -06: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 05:44:13 -06:00
elif platform . system ( ) == " Linux " :
2023-01-30 08:48:10 -07:00
run_pip ( f " install { xformers_package } " , " xformers " )
2022-10-08 10:02:56 -06:00
2023-05-16 11:15:30 -06:00
if not is_installed ( " ngrok " ) and args . ngrok :
run_pip ( " install ngrok " , " 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
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 )
2022-10-16 01:13:18 -06: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 07:48:18 -06:00
2022-10-09 05:44:13 -06:00
if not is_installed ( " lpips " ) :
2023-03-12 03:11:51 -06:00
run_pip ( f " install -r \" { os . path . join ( repo_dir ( ' CodeFormer ' ) , ' requirements.txt ' ) } \" " , " requirements for CodeFormer " )
2022-09-13 07:48:18 -06:00
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-03-30 08:57:54 -06:00
run_pip ( f " install -r \" { requirements_file } \" " , " requirements " )
2022-10-09 05:44:13 -06:00
2022-11-27 08:48:08 -07:00
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-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-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
2023-03-25 07:05:12 -06:00
if args . tests and not args . no_tests :
exitcode = tests ( args . tests )
2022-11-14 04:36:07 -07:00
exit ( exitcode )
2022-10-29 23:22:44 -06:00
2022-11-14 03:39:22 -07: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 " )
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 " )
2023-03-25 07:05:12 -06:00
if " --no-tests " not in sys . argv :
sys . argv . append ( " --no-tests " )
2022-10-29 23:28:36 -06:00
2022-11-14 03:39:22 -07:00
print ( f " Launching Web UI in another process for testing with arguments: { ' ' . join ( sys . argv [ 1 : ] ) } " )
2022-10-29 23:22:44 -06:00
2023-01-10 02:46:35 -07:00
os . environ [ ' COMMANDLINE_ARGS ' ] = " "
2023-03-10 06:38:35 -07:00
with open ( os . path . join ( script_path , ' test/stdout.txt ' ) , " w " , encoding = " utf8 " ) as stdout , open ( os . path . join ( script_path , ' test/stderr.txt ' ) , " w " , encoding = " utf8 " ) as stderr :
2022-11-14 03:39:22 -07:00
proc = subprocess . Popen ( [ sys . executable , * sys . argv ] , stdout = stdout , stderr = stderr )
2022-10-29 23:22:44 -06:00
import test . server_poll
2022-11-14 04:36:07 -07:00
exitcode = test . server_poll . run_tests ( proc , test_dir )
2022-10-29 23:22:44 -06:00
print ( f " Stopping Web UI process with id { proc . pid } " )
proc . kill ( )
2022-11-14 04:36:07 -07:00
return exitcode
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 ( ) :
print ( f " Launching { ' API server ' if ' --nowebui ' in sys . argv else ' Web UI ' } with arguments: { ' ' . 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 ( )
2022-09-13 07:48:18 -06:00
2022-10-09 05:44:13 -06:00
2022-09-20 05:56:07 -06:00
if __name__ == " __main__ " :
2022-12-03 02:35:17 -07:00
prepare_environment ( )
2022-11-02 23:08:11 -06:00
start ( )