From a6f39e0fb70d5453b8b7cb11d63d0073a2ab2d3a Mon Sep 17 00:00:00 2001 From: youweiyu Date: Sun, 26 Mar 2023 18:46:16 -0700 Subject: [PATCH 1/6] fix: update python-socketio[client] version in requirements --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 8635dbf..689bba7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -python-socketio[client]==5.7.2 \ No newline at end of file +python-socketio[client]>=5.7.2 \ No newline at end of file From d6c811bc0af548d72993fc838baf97d63df20805 Mon Sep 17 00:00:00 2001 From: youweiyu Date: Sun, 26 Mar 2023 18:48:57 -0700 Subject: [PATCH 2/6] feat: modify install process and log display --- civitai/lib.py | 1 + install.py | 47 +++++++++++++++++++++++------------------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/civitai/lib.py b/civitai/lib.py index 275fdcf..342446e 100644 --- a/civitai/lib.py +++ b/civitai/lib.py @@ -27,6 +27,7 @@ #region Utils def log(message): """Log a message to the console.""" + if not shared.opts.data.get('civitai_link_logging', True): return print(f'Civitai: {message}') def download_file(url, dest, on_progress=None): diff --git a/install.py b/install.py index 9ef04f2..0b66d62 100644 --- a/install.py +++ b/install.py @@ -4,7 +4,8 @@ import git -from launch import run +import launch +from modules import shared req_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "requirements.txt") @@ -36,8 +37,8 @@ def check_versions(): # Loop through reqs and check if installed for req in reqs_dict: available = is_package_installed(req, reqs_dict[req]) - if available: print(f"[+] {req} version {reqs_dict[req]} installed.") - else : print(f"[!] {req} version {reqs_dict[req]} NOT installed.") + if available: print(f"[+]Civitai Link requirement: {req} version {reqs_dict[req]} installed.") + else : print(f"[!]Civitai Link requirement: {req} version {reqs_dict[req]} NOT installed.") base_dir = os.path.dirname(os.path.realpath(__file__)) revision = "" @@ -51,29 +52,27 @@ def check_versions(): except: pass -print("") -print("#######################################################################################################") -print("Initializing Civitai Link") -print("If submitting an issue on github, please provide the below text for debugging purposes:") -print("") -print(f"Python revision: {sys.version}") -print(f"Civitai Link revision: {revision}") -print(f"SD-WebUI revision: {app_revision}") -print("") -civitai_skip_install = os.environ.get('CIVITAI_SKIP_INSTALL', False) +if shared.opts.data.get('civitai_link_logging', True): + print("") + print("#######################################################################################################") + print("Initializing Civitai Link") + print("If submitting an issue on github, please provide the below text for debugging purposes:") + print("") + print(f"Python revision: {sys.version}") + print(f"Civitai Link revision: {revision}") + print(f"SD-WebUI revision: {app_revision}") + print("") -try: - requirements_file = os.environ.get('REQS_FILE', "requirements_versions.txt") - if requirements_file == req_file: - civitai_skip_install = True -except: - pass +civitai_skip_install = os.environ.get('CIVITAI_SKIP_INSTALL', False) if not civitai_skip_install: - name = "Civitai Link" - run(f'"{sys.executable}" -m pip install -r "{req_file}"', f"Checking {name} requirements...", - f"Couldn't install {name} requirements.") + with open(req_file) as file: + for lib in file: + lib = lib.strip() + if not launch.is_installed(lib): + launch.run_pip(f"install {lib}", f"Civitai Link requirement: {lib}") check_versions() -print("") -print("#######################################################################################################") +if shared.opts.data.get('civitai_link_logging', True): + print("") + print("#######################################################################################################") From df8f33c228320042f039b5d6c1090c512f76f3c0 Mon Sep 17 00:00:00 2001 From: youweiyu Date: Sun, 26 Mar 2023 18:50:09 -0700 Subject: [PATCH 3/6] feat: add manual load and set default startup load to off --- scripts/previews.py | 18 ++++++++++++++---- scripts/settings.py | 17 ++++++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/scripts/previews.py b/scripts/previews.py index 0c23c45..7212932 100644 --- a/scripts/previews.py +++ b/scripts/previews.py @@ -1,4 +1,5 @@ from typing import List +from datetime import datetime import gradio as gr import threading @@ -7,9 +8,10 @@ from modules import script_callbacks, shared previewable_types = ['LORA', 'Hypernetwork', 'TextualInversion', 'Checkpoint'] +previews_update_info = ["Press Refresh button on the right to manully load previews"] + def load_previews(): - download_missing_previews = shared.opts.data.get('civitai_download_previews', True) - if not download_missing_previews: return + previews_update_info[0] = datetime.now().strftime("%Y/%m/%d/ %H:%M:%S") nsfw_previews = shared.opts.data.get('civitai_nsfw_previews', True) civitai.log(f"Check resources for missing preview images") @@ -29,10 +31,12 @@ def load_previews(): results.extend(civitai.get_all_by_hash(batch)) except: civitai.log("Failed to fetch preview images from Civitai") + previews_update_info[0] += " - Failed to fetch preview images from Civitai" return if len(results) == 0: - civitai.log("No preview images found on Civitai") + civitai.log("No new preview image found on Civitai") + previews_update_info[0] += " - No new preview image found on Civitai" return civitai.log(f"Found {len(results)} hash matches") @@ -54,10 +58,16 @@ def load_previews(): updated += 1 civitai.log(f"Updated {updated} preview images") + previews_update_info[0] += f" - Found and updated {updated} of {len(missing_previews)} missing preview images" +def load_previews_on_startup(): + download_missing_previews = shared.opts.data.get('civitai_on_startup_download_previews', False) + if not download_missing_previews: return + load_previews() + # Automatically pull model with corresponding hash from Civitai def start_load_previews(demo: gr.Blocks, app): - thread = threading.Thread(target=load_previews) + thread = threading.Thread(target=load_previews_on_startup) thread.start() script_callbacks.on_app_started(start_load_previews) diff --git a/scripts/settings.py b/scripts/settings.py index df5192d..19e705a 100644 --- a/scripts/settings.py +++ b/scripts/settings.py @@ -1,16 +1,27 @@ +import sys +import os +import gradio as gr + +cwd = os.getcwd() +utils_dir = os.path.join(cwd, 'extensions', 'sd_civitai_extension', 'scripts') +sys.path.extend([utils_dir]) + from civitai.link import on_civitai_link_key_changed +from previews import load_previews,previews_update_info + from modules import shared, script_callbacks + def on_ui_settings(): section = ('civitai_link', "Civitai") shared.opts.add_option("civitai_link_key", shared.OptionInfo("", "Your Civitai Link Key", section=section, onchange=on_civitai_link_key_changed)) shared.opts.add_option("civitai_link_logging", shared.OptionInfo(True, "Show Civitai Link events in the console", section=section)) shared.opts.add_option("civitai_api_key", shared.OptionInfo("", "Your Civitai API Key", section=section)) - shared.opts.add_option("civitai_download_previews", shared.OptionInfo(True, "Download missing preview images on startup", section=section)) + shared.opts.add_option("civitai_on_startup_download_previews", shared.OptionInfo(False, "Download missing preview images on startup", section=section)) shared.opts.add_option("civitai_nsfw_previews", shared.OptionInfo(False, "Download NSFW (adult) preview images", section=section)) shared.opts.add_option("civitai_download_missing_models", shared.OptionInfo(True, "Download missing models upon reading generation parameters from prompt", section=section)) shared.opts.add_option("civitai_hashify_resources", shared.OptionInfo(True, "Include resource hashes in image metadata (for resource auto-detection on Civitai)", section=section)) shared.opts.add_option("civitai_folder_lora", shared.OptionInfo("", "LoRA directory (if not default)", section=section)) + shared.opts.add_option("civitai_manual_download_previews", shared.OptionInfo(previews_update_info[0], "Last manually load time", gr.Dropdown, lambda: {"choices": previews_update_info},refresh=load_previews,section=section)) - -script_callbacks.on_ui_settings(on_ui_settings) \ No newline at end of file +script_callbacks.on_ui_settings(on_ui_settings) From 7fbb4581b22cb3740eacd5dbad0b2c4466aa2e5f Mon Sep 17 00:00:00 2001 From: youweiyu Date: Sun, 26 Mar 2023 18:58:16 -0700 Subject: [PATCH 4/6] style: fix typo --- scripts/previews.py | 2 +- scripts/settings.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/previews.py b/scripts/previews.py index 7212932..d347729 100644 --- a/scripts/previews.py +++ b/scripts/previews.py @@ -8,7 +8,7 @@ from modules import script_callbacks, shared previewable_types = ['LORA', 'Hypernetwork', 'TextualInversion', 'Checkpoint'] -previews_update_info = ["Press Refresh button on the right to manully load previews"] +previews_update_info = ["Press Refresh button on the right to manually load previews"] def load_previews(): previews_update_info[0] = datetime.now().strftime("%Y/%m/%d/ %H:%M:%S") diff --git a/scripts/settings.py b/scripts/settings.py index 19e705a..8a6a8c2 100644 --- a/scripts/settings.py +++ b/scripts/settings.py @@ -22,6 +22,6 @@ def on_ui_settings(): shared.opts.add_option("civitai_download_missing_models", shared.OptionInfo(True, "Download missing models upon reading generation parameters from prompt", section=section)) shared.opts.add_option("civitai_hashify_resources", shared.OptionInfo(True, "Include resource hashes in image metadata (for resource auto-detection on Civitai)", section=section)) shared.opts.add_option("civitai_folder_lora", shared.OptionInfo("", "LoRA directory (if not default)", section=section)) - shared.opts.add_option("civitai_manual_download_previews", shared.OptionInfo(previews_update_info[0], "Last manually load time", gr.Dropdown, lambda: {"choices": previews_update_info},refresh=load_previews,section=section)) + shared.opts.add_option("civitai_manually_download_previews", shared.OptionInfo(previews_update_info[0], "Last manually load time", gr.Dropdown, lambda: {"choices": previews_update_info},refresh=load_previews,section=section)) script_callbacks.on_ui_settings(on_ui_settings) From a9245d581aa6cb70663a6b88b931c9488a49f728 Mon Sep 17 00:00:00 2001 From: youweiyu Date: Sun, 26 Mar 2023 19:23:22 -0700 Subject: [PATCH 5/6] feat: add logging check for tqdm --- civitai/lib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/civitai/lib.py b/civitai/lib.py index 342446e..bc3f9ee 100644 --- a/civitai/lib.py +++ b/civitai/lib.py @@ -46,7 +46,7 @@ def download_file(url, dest, on_progress=None): try: current = 0 - with tqdm(total=total, unit='B', unit_scale=True, unit_divisor=1024) as bar: + with tqdm(total=total, unit='B', unit_scale=True, unit_divisor=1024, disable=not shared.opts.data.get('civitai_link_logging', True)) as bar: for data in response.iter_content(chunk_size=download_chunk_size): current += len(data) pos = f.write(data) From 99aee80711a4562756ebaf36d3547baff0841ed8 Mon Sep 17 00:00:00 2001 From: youweiyu Date: Sun, 26 Mar 2023 19:35:28 -0700 Subject: [PATCH 6/6] feat: remove unnecessary check_versions and reorder install.py --- install.py | 56 ++++++++++++++++++------------------------------------ 1 file changed, 18 insertions(+), 38 deletions(-) diff --git a/install.py b/install.py index 0b66d62..04c723b 100644 --- a/install.py +++ b/install.py @@ -24,35 +24,27 @@ def is_package_installed(package_name, version): return True return False -def check_versions(): - global req_file - reqs = open(req_file, 'r') - lines = reqs.readlines() - reqs_dict = {} - for line in lines: - splits = line.split("==") - if len(splits) == 2: - key = splits[0] - reqs_dict[key] = splits[1].replace("\n", "").strip() - # Loop through reqs and check if installed - for req in reqs_dict: - available = is_package_installed(req, reqs_dict[req]) - if available: print(f"[+]Civitai Link requirement: {req} version {reqs_dict[req]} installed.") - else : print(f"[!]Civitai Link requirement: {req} version {reqs_dict[req]} NOT installed.") - -base_dir = os.path.dirname(os.path.realpath(__file__)) -revision = "" -app_revision = "" +civitai_skip_install = os.environ.get('CIVITAI_SKIP_INSTALL', False) -try: - repo = git.Repo(base_dir) - revision = repo.rev_parse("HEAD") - app_repo = git.Repo(os.path.join(base_dir, "..", "..")) - app_revision = app_repo.rev_parse("HEAD") -except: - pass +if not civitai_skip_install: + with open(req_file) as file: + for lib in file: + lib = lib.strip() + if not launch.is_installed(lib): + launch.run_pip(f"install {lib}", f"Civitai Link requirement: {lib}") if shared.opts.data.get('civitai_link_logging', True): + base_dir = os.path.dirname(os.path.realpath(__file__)) + revision = "" + app_revision = "" + + try: + repo = git.Repo(base_dir) + revision = repo.rev_parse("HEAD") + app_repo = git.Repo(os.path.join(base_dir, "..", "..")) + app_revision = app_repo.rev_parse("HEAD") + except: + pass print("") print("#######################################################################################################") print("Initializing Civitai Link") @@ -62,17 +54,5 @@ def check_versions(): print(f"Civitai Link revision: {revision}") print(f"SD-WebUI revision: {app_revision}") print("") - -civitai_skip_install = os.environ.get('CIVITAI_SKIP_INSTALL', False) - -if not civitai_skip_install: - with open(req_file) as file: - for lib in file: - lib = lib.strip() - if not launch.is_installed(lib): - launch.run_pip(f"install {lib}", f"Civitai Link requirement: {lib}") - -check_versions() -if shared.opts.data.get('civitai_link_logging', True): print("") print("#######################################################################################################")