Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ __pycache__
/SwinIR/*
/repositories
/venv
/venv310
/tmp
/model.ckpt
/models/**/*
Expand Down Expand Up @@ -37,3 +38,5 @@ notification.mp3
/node_modules
/package-lock.json
/.coverage*
/output.log
/webui-user.sh
4 changes: 2 additions & 2 deletions extensions-builtin/Lora/scripts/lora_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def before_ui():
script_callbacks.on_infotext_pasted(networks.infotext_pasted)


shared.options_templates.update(shared.options_section(('extra_networks', "Extra Networks"), {
shared.options_templates.update(shared.options_section((None, "Extra Networks"), {
"sd_lora": shared.OptionInfo("None", "Add network to prompt", gr.Dropdown, lambda: {"choices": ["None", *networks.available_networks]}, refresh=networks.list_available_networks),
"lora_preferred_name": shared.OptionInfo("Alias from file", "When adding to prompt, refer to Lora by", gr.Radio, {"choices": ["Alias from file", "Filename"]}),
"lora_add_hashes_to_infotext": shared.OptionInfo(True, "Add Lora hashes to infotext"),
Expand All @@ -42,7 +42,7 @@ def before_ui():
}))


shared.options_templates.update(shared.options_section(('compatibility', "Compatibility"), {
shared.options_templates.update(shared.options_section((None, "Compatibility"), {
"lora_functional": shared.OptionInfo(False, "Lora/Networks: use old method that takes longer when you have multiple Loras active and produces same results as kohya-ss/sd-webui-additional-networks extension"),
}))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gradio as gr
from modules import shared

shared.options_templates.update(shared.options_section(('canvas_hotkey', "Canvas Hotkeys"), {
shared.options_templates.update(shared.options_section((None, "Canvas Hotkeys"), {
"canvas_hotkey_zoom": shared.OptionInfo("Alt", "Zoom canvas", gr.Radio, {"choices": ["Shift","Ctrl", "Alt"]}).info("If you choose 'Shift' you cannot scroll horizontally, 'Alt' can cause a little trouble in firefox"),
"canvas_hotkey_adjust": shared.OptionInfo("Ctrl", "Adjust brush size", gr.Radio, {"choices": ["Shift","Ctrl", "Alt"]}).info("If you choose 'Shift' you cannot scroll horizontally, 'Alt' can cause a little trouble in firefox"),
"canvas_hotkey_move": shared.OptionInfo("F", "Moving the canvas").info("To work correctly in firefox, turn off 'Automatically search the page text when typing' in the browser settings"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def before_process(self, p, *args):
p.override_settings[name] = value


shared.options_templates.update(shared.options_section(('ui', "User interface"), {
shared.options_templates.update(shared.options_section((None, "User interface"), {
"extra_options_txt2img": shared.OptionInfo([], "Options in main UI - txt2img", ui_components.DropdownMulti, lambda: {"choices": list(shared.opts.data_labels.keys())}).js("info", "settingsHintsShowQuicksettings").info("setting entries that also appear in txt2img interfaces").needs_reload_ui(),
"extra_options_img2img": shared.OptionInfo([], "Options in main UI - img2img", ui_components.DropdownMulti, lambda: {"choices": list(shared.opts.data_labels.keys())}).js("info", "settingsHintsShowQuicksettings").info("setting entries that also appear in img2img interfaces").needs_reload_ui(),
"extra_options_cols": shared.OptionInfo(1, "Options in main UI - number of columns", gr.Number, {"precision": 0}).needs_reload_ui(),
Expand Down
2 changes: 2 additions & 0 deletions modules/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,9 @@ def unloadapi(self):
return {}

def reloadapi(self):
print("start reload api")
reload_model_weights()
print("end reload api")

return {}

Expand Down
8 changes: 8 additions & 0 deletions modules/hashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def calculate_remote_sha256(filename):

return hash_sha256.hexdigest()

# def calculate_remote_sha256(filename):
# blksize = 1024 * 1024

# buf = read_remote_model(filename, start = 0, size=blksize)
# hash_object = hashlib.sha256(buf)

# return hash_object.hexdigest()

def sha256_from_cache(filename, title, use_addnet_hash=False, remote_model = False):
hashes = cache("hashes-addnet") if use_addnet_hash else cache("hashes")
ondisk_mtime = os.path.getmtime(filename) if not remote_model else get_remote_model_mmtime(filename)
Expand Down
2 changes: 1 addition & 1 deletion modules/initialize_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def configure_opts_onchange():
from modules.call_queue import wrap_queued_call

shared.opts.onchange("sd_model_checkpoint", wrap_queued_call(lambda: sd_models.reload_model_weights()), call=False)
shared.opts.onchange("load_remote_ckpt", wrap_queued_call(lambda: (sd_models.list_models(),sd_models.load_model())), call=False)
# shared.opts.onchange("load_remote_ckpt", wrap_queued_call(lambda: (sd_models.list_models(),sd_models.load_model())), call=False)
shared.opts.onchange("sd_vae", wrap_queued_call(lambda: sd_vae.reload_vae_weights()), call=False)
shared.opts.onchange("sd_vae_overrides_per_model_preferences", wrap_queued_call(lambda: sd_vae.reload_vae_weights()), call=False)
shared.opts.onchange("temp_dir", ui_tempdir.on_tmpdir_changed)
Expand Down
6 changes: 5 additions & 1 deletion modules/launch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,11 @@ def prepare_environment():
if args.reinstall_torch or not is_installed("torch") or not is_installed("torchvision"):
run(f'"{python}" -m {torch_command}', "Installing torch and torchvision", "Couldn't install torch", live=True)
startup_timer.record("install torch")


if not is_installed("cython"):
run_pip("install cython", "cython")
startup_timer.record("install cython")

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; '
Expand Down
53 changes: 30 additions & 23 deletions modules/sd_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,26 @@ def read_metadata():
self.metadata = cache.cached_data_for_file('safetensors-metadata', "checkpoint/" + name + ('[remote]' if self.remote_model else '' ), filename, read_metadata, remote_model)
except Exception as e:
errors.display(e, f"reading metadata for {filename}")

print("CheckpointInfo start")
self.name = name
self.name_for_extra = os.path.splitext(os.path.basename(filename))[0]
self.model_name = os.path.splitext(name.replace("/", "_").replace("\\", "_"))[0]
self.hash = model_hash(filename) if not remote_model else remote_model_hash(filename)

self.sha256 = hashes.sha256_from_cache(self.filename, f"checkpoint/{name + ('[remote]' if self.remote_model else '' )}", remote_model=remote_model)
self.shorthash = self.sha256[0:10] if self.sha256 else None

print("sha256: %s" % self.sha256)
print("shorthash: %s" % self.shorthash)

self.title = name + ('[remote]' if self.remote_model else '' )+ ('' if self.shorthash is None else f'[{self.shorthash}]')
self.short_title = self.name_for_extra + ('[remote]' if self.remote_model else '') + ('' if self.shorthash is None else f'[{self.shorthash}]')
print("title: %s" % self.title)
print("short_title: %s" % self.short_title)

self.ids = [self.hash, self.model_name, self.title, name, self.name_for_extra, f'{name} [{self.hash}]']
if self.shorthash:
self.ids += [self.shorthash, self.sha256, f'{self.name} [{self.shorthash}]', f'{self.name_for_extra} [{self.shorthash}]']
print("CheckpointInfo end")

def register(self):
checkpoints_list[self.title] = self
Expand Down Expand Up @@ -144,32 +149,34 @@ def list_models():
checkpoints_list.clear()
checkpoint_aliases.clear()

if not shared.opts.load_remote_ckpt:
cmd_ckpt = shared.cmd_opts.ckpt
if shared.cmd_opts.no_download_sd_model or cmd_ckpt != shared.sd_model_file or os.path.exists(cmd_ckpt):
model_url = None
else:
model_url = "https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors"
# if not shared.opts.load_remote_ckpt:
# cmd_ckpt = shared.cmd_opts.ckpt
# if shared.cmd_opts.no_download_sd_model or cmd_ckpt != shared.sd_model_file or os.path.exists(cmd_ckpt):
# model_url = None
# else:
# model_url = "https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors"

model_list = modelloader.load_models(model_path=model_path, model_url=model_url, command_path=shared.cmd_opts.ckpt_dir, ext_filter=[".ckpt", ".safetensors"], download_name="v1-5-pruned-emaonly.safetensors", ext_blacklist=[".vae.ckpt", ".vae.safetensors"])
# model_list = modelloader.load_models(model_path=model_path, model_url=model_url, command_path=shared.cmd_opts.ckpt_dir, ext_filter=[".ckpt", ".safetensors"], download_name="v1-5-pruned-emaonly.safetensors", ext_blacklist=[".vae.ckpt", ".vae.safetensors"])

if os.path.exists(cmd_ckpt):
checkpoint_info = CheckpointInfo(cmd_ckpt)
checkpoint_info.register()
# if os.path.exists(cmd_ckpt):
# checkpoint_info = CheckpointInfo(cmd_ckpt)
# checkpoint_info.register()

shared.opts.data['sd_model_checkpoint'] = checkpoint_info.title
elif cmd_ckpt is not None and cmd_ckpt != shared.default_sd_model_file:
print(f"Checkpoint in --ckpt argument not found (Possible it was moved to {model_path}: {cmd_ckpt}", file=sys.stderr)
# shared.opts.data['sd_model_checkpoint'] = checkpoint_info.title
# elif cmd_ckpt is not None and cmd_ckpt != shared.default_sd_model_file:
# print(f"Checkpoint in --ckpt argument not found (Possible it was moved to {model_path}: {cmd_ckpt}", file=sys.stderr)

for filename in model_list:
checkpoint_info = CheckpointInfo(filename)
checkpoint_info.register()
# for filename in model_list:
# checkpoint_info = CheckpointInfo(filename)
# checkpoint_info.register()

else:
remote_models = list_remote_models(ext_filter=[".ckpt", ".safetensors"])
for filename in remote_models:
checkpoint_info = CheckpointInfo(filename, remote_model=True)
checkpoint_info.register()
# else:
remote_models = list_remote_models(ext_filter=[".ckpt", ".safetensors"])
for filename in remote_models:
checkpoint_info = CheckpointInfo(filename, remote_model=True)
checkpoint_info.register()
print ("list_model: %s " % filename)

re_strip_checksum = re.compile(r"\s*\[[^]]+]\s*$")


Expand Down
29 changes: 26 additions & 3 deletions modules/sd_remote_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
import threading
from io import BytesIO
from modules import shared
from osstorchconnector import OssCheckpoint
import torch


def __check_bucket_opts():
if os.environ.get('BUCKET_NAME') and os.environ.get('BUCKET_ENDPOINT'):
return True
print("Bucket opts not specified.")
return False

def __bucket__():
auth = oss2.Auth(os.environ.get('ACCESS_KEY_ID'), os.environ.get('ACCESS_KEY_SECRET'))
return oss2.Bucket(auth, shared.opts.bucket_endpoint, shared.opts.bucket_name, enable_crc=False)
return oss2.Bucket(auth, os.environ.get('BUCKET_ENDPOINT'), os.environ.get('BUCKET_NAME'), enable_crc=False)

def __get_object_size(object_name):
simplifiedmeta = __bucket__().get_object_meta(object_name)
Expand All @@ -19,8 +25,10 @@ def get_remote_model_mmtime(model_name):
return __bucket__().head_object(model_name).last_modified

def list_remote_models(ext_filter):
dir = shared.opts.bucket_model_ckpt_dir if shared.opts.bucket_model_ckpt_dir.endswith('/') else shared.opts.bucket_model_ckpt_dir + '/'
if not __check_bucket_opts():
return []
output = []
dir = os.environ.get('BUCKET_MODEL_DIR') if os.environ.get('BUCKET_MODEL_DIR').endswith('/') else os.environ.get('BUCKET_MODEL_DIR') + '/'
for obj in oss2.ObjectIteratorV2(__bucket__(), prefix = dir, delimiter = '/', start_after=dir, fetch_owner=False):
if obj.is_prefix():
print('directory: ', obj.key)
Expand Down Expand Up @@ -69,6 +77,21 @@ def read_remote_model(checkpoint_file, start=0, size=-1):
buffer.seek(0)
return buffer



def load_remote_model_ckpt(checkpoint_file, map_location) -> bytes:
if not __check_bucket_opts():
return bytes()

checkpoint = OssCheckpoint(endpoint=os.environ.get('BUCKET_ENDPOINT'))
CHECKPOINT_URI = "oss://%s/%s" % (os.environ.get('BUCKET_NAME'), checkpoint_file)
print("load %s state.." % CHECKPOINT_URI)
state_dict = None
with checkpoint.reader(CHECKPOINT_URI) as reader:
state_dict = torch.load(reader, map_location = map_location, weights_only = True)
print("type:", type(state_dict))
return state_dict

def __range_get(object_name, buffer, offset, start, end, read_chunk_size):
chunk_size = int(read_chunk_size)
with __bucket__().get_object(object_name, byte_range=(start, end))as object_stream:
Expand Down
Loading