From b6282556612b9250adf516a9166aee556eddc353 Mon Sep 17 00:00:00 2001 From: butaixianran Date: Mon, 22 Jul 2024 22:06:45 +0800 Subject: [PATCH] v1.11.0 --- README.md | 3 + scripts/ch_lib/civitai.py | 96 ++++++++++++++++++-------- scripts/ch_lib/js_action_civitai.py | 2 +- scripts/ch_lib/model_action_civitai.py | 6 +- scripts/ch_lib/setting.py | 11 ++- scripts/ch_lib/util.py | 2 +- scripts/civitai_helper.py | 9 ++- 7 files changed, 91 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 6949484f..53da9a05 100644 --- a/README.md +++ b/README.md @@ -245,6 +245,9 @@ Since v1.5.5, we've already optimized the SHA256 function to the top. So the onl # Change Log +## v1.11.0 +* Added a new checkbox option "When checking model's new version, checking new version existing under all model folders". When this value is False, it gonna only checking that model's current folder. + ## v1.10.4 * Fix a bug about model path parsing when using custom model folder diff --git a/scripts/ch_lib/civitai.py b/scripts/ch_lib/civitai.py index 6fd2a2a4..d3d67d75 100644 --- a/scripts/ch_lib/civitai.py +++ b/scripts/ch_lib/civitai.py @@ -423,10 +423,11 @@ def get_preview_image_by_model_path(model_path:str, max_size_preview, skip_nsfw_ # search local model by version id in 1 folder, no subfolder # return - model_info -def search_local_model_info_by_version_id(folder:str, version_id:int) -> dict: +def search_local_model_info_by_version_id(folder:str, version_id:int, walk:bool=False) -> dict: util.printD("Searching local model by version id") util.printD("folder: " + folder) util.printD("version_id: " + str(version_id)) + util.printD("walk: " + str(walk)) if not folder: util.printD("folder is none") @@ -441,34 +442,70 @@ def search_local_model_info_by_version_id(folder:str, version_id:int) -> dict: return # search civitai model info file - for filename in os.listdir(folder): - # check ext - base, ext = os.path.splitext(filename) - if ext == model.info_ext: - # find info file - if len(base) < 9: - # not a civitai info file - continue - - if base[-8:] == suffix: - # find a civitai info file - path = os.path.join(folder, filename) - model_info = model.load_model_info(path) - if not model_info: - continue + # walk from top + if walk: + util.printD(f"Searching model version id {version_id} by walking in: {folder}") + for root, dirs, files in os.walk(folder, followlinks=True): + for filename in files: + # check ext + base, ext = os.path.splitext(filename) + if ext == model.info_ext: + # find info file + if len(base) < 9: + # not a civitai info file + continue - if "id" not in model_info.keys(): - continue + if base[-8:] == suffix: + # find a civitai info file + path = os.path.join(root, filename) + model_info = model.load_model_info(path) + if not model_info: + continue + + if "id" not in model_info.keys(): + continue + + id = model_info["id"] + if not id: + continue - id = model_info["id"] - if not id: + # util.printD(f"Compare version id, src: {id}, target:{version_id}") + if str(id) == str(version_id): + # find the one + util.printD(f"Found model: {path}") + return model_info + + # only check current path + else: + util.printD(f"Searching model version id {version_id} under: {folder}") + for filename in os.listdir(folder): + # check ext + base, ext = os.path.splitext(filename) + if ext == model.info_ext: + # find info file + if len(base) < 9: + # not a civitai info file continue - # util.printD(f"Compare version id, src: {id}, target:{version_id}") - if str(id) == str(version_id): - # find the one - return model_info - + if base[-8:] == suffix: + # find a civitai info file + path = os.path.join(folder, filename) + model_info = model.load_model_info(path) + if not model_info: + continue + + if "id" not in model_info.keys(): + continue + + id = model_info["id"] + if not id: + continue + + # util.printD(f"Compare version id, src: {id}, target:{version_id}") + if str(id) == str(version_id): + # find the one + return model_info + return @@ -596,7 +633,7 @@ def check_model_new_version_by_path(model_path:str, delay:float=1) -> tuple: # check model's new version # parameter: delay - float, how many seconds to delay between each request to civitai # return: new_versions - a list for all new versions, each one is (model_path, model_id, model_name, new_verion_id, new_version_name, description, download_url, img_url) -def check_models_new_version_by_model_types(model_types:list, delay:float=1) -> list: +def check_models_new_version_by_model_types(model_types:list, delay:float=1, check_new_ver_exist_in_all_folder:bool=False) -> list: util.printD("Checking models' new version") if not model_types: @@ -654,7 +691,12 @@ def check_models_new_version_by_model_types(model_types:list, delay:float=1) -> continue # search this new version id to check if this model is already downloaded - target_model_info = search_local_model_info_by_version_id(root, current_version_id) + if check_new_ver_exist_in_all_folder: + # walk from top folder for this model type + target_model_info = search_local_model_info_by_version_id(model_folder, current_version_id, check_new_ver_exist_in_all_folder) + else: + # only check current folder + target_model_info = search_local_model_info_by_version_id(root, current_version_id, check_new_ver_exist_in_all_folder) if target_model_info: util.printD("New version is already existed") continue diff --git a/scripts/ch_lib/js_action_civitai.py b/scripts/ch_lib/js_action_civitai.py index 0caa40c0..532651dd 100644 --- a/scripts/ch_lib/js_action_civitai.py +++ b/scripts/ch_lib/js_action_civitai.py @@ -252,7 +252,7 @@ def dl_model_new_version(msg, max_size_preview, skip_nsfw_preview): # no need to check when downloading new version, since checking new version is already checked # check if this model is already existed - # r = civitai.search_local_model_info_by_version_id(model_folder, version_id) + # r = civitai.search_local_model_info_by_version_id(model_folder, version_id, False) # if r: # output = "This model version is already existed" # util.printD(output) diff --git a/scripts/ch_lib/model_action_civitai.py b/scripts/ch_lib/model_action_civitai.py index f09f0752..a63acd41 100644 --- a/scripts/ch_lib/model_action_civitai.py +++ b/scripts/ch_lib/model_action_civitai.py @@ -147,8 +147,8 @@ def get_model_info_by_input(model_type, model_name, model_url_or_id, max_size_pr # check models' new version and output to UI as markdown doc -def check_models_new_version_to_md(model_types:list) -> str: - new_versions = civitai.check_models_new_version_by_model_types(model_types, 1) +def check_models_new_version_to_md(model_types:list, check_new_ver_exist_in_all_folder:bool) -> str: + new_versions = civitai.check_models_new_version_by_model_types(model_types, 1, check_new_ver_exist_in_all_folder) count = 0 output = "" @@ -454,7 +454,7 @@ def dl_model_by_input(model_info:dict, model_type:str, subfolder_str:str, versio # check if this model is already existed - r = civitai.search_local_model_info_by_version_id(model_folder, version_id) + r = civitai.search_local_model_info_by_version_id(model_folder, version_id, False) if r: output = "This model version is already existed" util.printD(output) diff --git a/scripts/ch_lib/setting.py b/scripts/ch_lib/setting.py index d22d0ba7..7175ab28 100644 --- a/scripts/ch_lib/setting.py +++ b/scripts/ch_lib/setting.py @@ -12,7 +12,8 @@ data = { "model":{ "max_size_preview": True, - "skip_nsfw_preview": False + "skip_nsfw_preview": False, + "check_new_ver_exist_in_all_folder": False }, "general":{ "open_url_with_js": True, @@ -75,16 +76,20 @@ def load(): if "proxy" not in data["general"].keys(): data["general"]["proxy"] = "" + if "check_new_ver_exist_in_all_folder" not in data["model"].keys(): + data["general"]["check_new_ver_exist_in_all_folder"] = False + return # save setting from parameter -def save_from_input(max_size_preview, skip_nsfw_preview, open_url_with_js, proxy): +def save_from_input(max_size_preview, skip_nsfw_preview, open_url_with_js, proxy, check_new_ver_exist_in_all_folder): global data data = { "model":{ "max_size_preview": max_size_preview, - "skip_nsfw_preview": skip_nsfw_preview + "skip_nsfw_preview": skip_nsfw_preview, + "check_new_ver_exist_in_all_folder": check_new_ver_exist_in_all_folder }, "general":{ "open_url_with_js": open_url_with_js, diff --git a/scripts/ch_lib/util.py b/scripts/ch_lib/util.py index 094d9efb..c56cb9d4 100644 --- a/scripts/ch_lib/util.py +++ b/scripts/ch_lib/util.py @@ -6,7 +6,7 @@ import shutil -version = "1.10.4" +version = "1.11.0" def_headers = {'User-Agent': 'Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148', "Authorization": ""} diff --git a/scripts/civitai_helper.py b/scripts/civitai_helper.py index e20abd5c..342023ca 100644 --- a/scripts/civitai_helper.py +++ b/scripts/civitai_helper.py @@ -43,6 +43,7 @@ def on_ui_settings(): shared.opts.add_option("ch_max_size_preview", shared.OptionInfo(True, "Download Max Size Preview", gr.Checkbox, {"interactive": True}, section=ch_section)) shared.opts.add_option("ch_skip_nsfw_preview", shared.OptionInfo(False, "Skip NSFW Preview Images", gr.Checkbox, {"interactive": True}, section=ch_section)) shared.opts.add_option("ch_open_url_with_js", shared.OptionInfo(True, "Open Url At Client Side", gr.Checkbox, {"interactive": True}, section=ch_section)) + shared.opts.add_option("ch_check_new_ver_exist_in_all_folder", shared.OptionInfo(True, "When checking new model version, check new version existing in all model folders", gr.Checkbox, {"interactive": True}, section=ch_section)) shared.opts.add_option("ch_proxy", shared.OptionInfo("", "Civitai Helper Proxy", gr.Textbox, {"interactive": True, "lines":1, "info":"format: socks5h://127.0.0.1:port"}, section=ch_section)) shared.opts.add_option("ch_civiai_api_key", shared.OptionInfo("", "Civitai API Key", gr.Textbox, {"interactive": True, "lines":1, "info":"check doc:https://github.com/zixaphir/Stable-Diffusion-Webui-Civitai-Helper/tree/master#api-key"}, section=ch_section)) @@ -68,6 +69,7 @@ def on_ui_tabs(): max_size_preview = shared.opts.data.get("ch_max_size_preview", True) skip_nsfw_preview = shared.opts.data.get("ch_skip_nsfw_preview", False) open_url_with_js = shared.opts.data.get("ch_open_url_with_js", True) + check_new_ver_exist_in_all_folder = shared.opts.data.get("ch_check_new_ver_exist_in_all_folder", False) proxy = shared.opts.data.get("ch_proxy", "") civitai_api_key = shared.opts.data.get("ch_civiai_api_key", "") @@ -75,6 +77,7 @@ def on_ui_tabs(): util.printD("max_size_preview: " + str(max_size_preview)) util.printD("skip_nsfw_preview: " + str(skip_nsfw_preview)) util.printD("open_url_with_js: " + str(open_url_with_js)) + util.printD("check_new_ver_exist_in_all_folder: " + str(check_new_ver_exist_in_all_folder)) util.printD("proxy: " + str(proxy)) # set civitai_api_key @@ -104,8 +107,8 @@ def get_model_info_by_input(model_type_drop, model_name_drop, model_url_or_id_tx def dl_model_by_input(dl_model_info, dl_model_type_txtbox, dl_subfolder_drop, dl_version_drop, dl_all_ckb): return model_action_civitai.dl_model_by_input(dl_model_info, dl_model_type_txtbox, dl_subfolder_drop, dl_version_drop, dl_all_ckb, max_size_preview, skip_nsfw_preview) - def check_models_new_version_to_md(dl_model_info, dl_model_type_txtbox, dl_subfolder_drop, dl_version_drop, dl_all_ckb): - return model_action_civitai.check_models_new_version_to_md(dl_model_info, dl_model_type_txtbox, dl_subfolder_drop, dl_version_drop, dl_all_ckb, max_size_preview, skip_nsfw_preview) + def check_models_new_version_to_md(model_types): + return model_action_civitai.check_models_new_version_to_md(model_types, check_new_ver_exist_in_all_folder) def open_model_url(js_msg_txtbox): return js_action_civitai.open_model_url(js_msg_txtbox, open_url_with_js) @@ -229,7 +232,7 @@ def get_model_info_by_url(url): dl_civitai_model_by_id_btn.click(dl_model_by_input, inputs=[dl_model_info, dl_model_type_txtbox, dl_subfolder_drop, dl_version_drop, dl_all_ckb], outputs=dl_log_md) # Check models' new version - check_models_new_version_btn.click(model_action_civitai.check_models_new_version_to_md, inputs=model_types_ckbg, outputs=check_models_new_version_log_md) + check_models_new_version_btn.click(check_models_new_version_to_md, inputs=model_types_ckbg, outputs=check_models_new_version_log_md) # js action js_open_url_btn.click(open_model_url, inputs=[js_msg_txtbox], outputs=py_msg_txtbox)