From 302ba65cc4e69693d2a099d4b15bcac27fd6916a Mon Sep 17 00:00:00 2001 From: butaixianran Date: Mon, 4 Mar 2024 22:25:10 +0800 Subject: [PATCH] handle path cases for linux --- README.md | 5 ++++- javascript/civitai_helper.js | 40 +++++++++++++++++++++++++----------- scripts/ch_lib/civitai.py | 34 ++++++++++++++++++++++++++++-- scripts/ch_lib/model.py | 23 +++++++++++++++++++-- scripts/ch_lib/util.py | 2 +- 5 files changed, 86 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 18a788e..a7bc731 100644 --- a/README.md +++ b/README.md @@ -228,7 +228,10 @@ Since v1.5.5, we've already optimized the SHA256 function to the top. So the onl -# Change Log +# Change Log3 +## v1.10.1 +* Handle model path cases for linux + ## v1.10.0 * Support SD webui v1.8.x * With SD webui v1.8.x, this extension's refresh function is added to Official Extra Network's refresh button. There is no need another green fresh button anymore. diff --git a/javascript/civitai_helper.js b/javascript/civitai_helper.js index f51b5d2..3887843 100644 --- a/javascript/civitai_helper.js +++ b/javascript/civitai_helper.js @@ -101,6 +101,8 @@ const get_new_ch_py_msg = (max_count=5) => new Promise((resolve, reject) => { }) + + function getActiveTabType() { const currentTab = get_uiCurrentTabContent(); switch (currentTab.id) { @@ -381,12 +383,28 @@ async function remove_card(event, model_type, search_term){ let active_tab = getActiveTabType(); console.log("get active tab id: " + active_tab); if (active_tab){ - let refresh_btn_id = active_tab + "_extra_refresh"; - let refresh_btn = gradioApp().getElementById(refresh_btn_id); + let refresh_btn_id = ""; + let refresh_btn = null; + + //check sd version + let sd_version = ch_sd_version(); + console.log(`sd version is: ${sd_version}`); + if (sd_version >= "1.8.0") { + let js_model_type = convertModelTypeFromPyToJS(model_type); + if (!js_model_type){return;} + + refresh_btn_id = active_tab + "_" + js_model_type + "_extra_refresh"; + refresh_btn = gradioApp().getElementById(refresh_btn_id); + } else { + refresh_btn_id = active_tab + "_extra_refresh"; + refresh_btn = gradioApp().getElementById(refresh_btn_id); + } + if (refresh_btn){ console.log("click button: "+refresh_btn_id); refresh_btn.click(); } + } } @@ -1030,12 +1048,10 @@ onUiLoaded(() => { //from sd v1.8, need to replace all single '\' into '\\' search_term = search_term.replaceAll("\\", "\\\\"); - //from sd v1.8, search_term is changed, also added a `data-sort-path` to card node with model's full path - //so we can use this new path and give up search_term - console.log("card path: " + card.dataset.sortPath); - card_path = card.dataset.sortPath.replaceAll("\\", "\\\\"); - - + //`data-sort-path` convert all path to lowercase, which can not be used to find model on linux. + //so this is not used and fall back to use search_term + //console.log("card path: " + card.dataset.sortPath); + //card_path = card.dataset.sortPath.replaceAll("\\", "\\\\"); console.log("adding buttons"); // then we need to add 3 buttons to each ul node: @@ -1045,7 +1061,7 @@ onUiLoaded(() => { open_url_node.className = "card-button"; open_url_node.title = "Open this model's civitai url"; - open_url_node.setAttribute("onclick","open_model_url_with_path(event, '"+model_type+"', '"+card_path+"')"); + open_url_node.setAttribute("onclick","open_model_url(event, '"+model_type+"', '"+search_term+"')"); let add_trigger_words_node = document.createElement("a"); add_trigger_words_node.href = "#"; @@ -1053,7 +1069,7 @@ onUiLoaded(() => { add_trigger_words_node.className = "card-button"; add_trigger_words_node.title = "Add trigger words to prompt"; - add_trigger_words_node.setAttribute("onclick","add_trigger_words_with_path(event, '"+model_type+"', '"+card_path+"')"); + add_trigger_words_node.setAttribute("onclick","add_trigger_words(event, '"+model_type+"', '"+search_term+"')"); let use_preview_prompt_node = document.createElement("a"); use_preview_prompt_node.href = "#"; @@ -1061,7 +1077,7 @@ onUiLoaded(() => { use_preview_prompt_node.className = "card-button"; use_preview_prompt_node.title = "Use prompt from preview image"; - use_preview_prompt_node.setAttribute("onclick","use_preview_prompt_with_path(event, '"+model_type+"', '"+card_path+"')"); + use_preview_prompt_node.setAttribute("onclick","use_preview_prompt(event, '"+model_type+"', '"+search_term+"')"); let remove_card_node = document.createElement("a"); remove_card_node.href = "#"; @@ -1069,7 +1085,7 @@ onUiLoaded(() => { remove_card_node.className = "card-button"; remove_card_node.title = "Remove this model"; - remove_card_node.setAttribute("onclick","remove_card_with_path(event, '"+model_type+"', '"+card_path+"')"); + remove_card_node.setAttribute("onclick","remove_card(event, '"+model_type+"', '"+search_term+"')"); //add to card button_row.appendChild(open_url_node); diff --git a/scripts/ch_lib/civitai.py b/scripts/ch_lib/civitai.py index d77183a..6992f52 100644 --- a/scripts/ch_lib/civitai.py +++ b/scripts/ch_lib/civitai.py @@ -201,11 +201,41 @@ def load_model_info_by_search_term(model_type, search_term): util.printD("unknow model type: " + model_type) return + # with sd webui < 1.8.0 # search_term = subfolderpath + model name + ext. And it always start with a / even there is no sub folder + # with sd webui >= 1.8.0 + # search_term = model type + subfolderpath + model name + ext. And it always start with a / even there is no sub folder + # this model type is based on sd webui's model folder name. ti is embeddings, ckp is Stable-diffusion, and so on base, ext = os.path.splitext(search_term) model_info_base = base - if base[:1] == "/": - model_info_base = base[1:] + + if model_info_base[:1] == "/": + model_info_base = model_info_base[1:] + + + model_folder_name = ""; + if model_type == "ti": + model_folder_name = "embeddings" + elif model_type == "hyper": + model_folder_name = "hypernetworks" + elif model_type == "ckp": + model_folder_name = "Stable-diffusion" + else: + model_folder_name = "Lora" + + # check if model folder is already in search_term + if model_info_base.startswith(model_folder_name): + # this is sd webui v1.8.0+'s search_term + # need to remove this model_folder_name+"/" or "\\" from model_info_base + model_info_base = model_info_base[len(model_folder_name):] + + # util.printD("cut model_info_base: " + model_info_base) + + if model_info_base.startswith("/") or model_info_base.startswith("\\"): + model_info_base = model_info_base[1:] + + # util.printD("final model_info_base: " + model_info_base) + model_folder = model.folders[model_type] model_info_filename = model_info_base + suffix + model.info_ext diff --git a/scripts/ch_lib/model.py b/scripts/ch_lib/model.py index 6507afe..15cd3c4 100644 --- a/scripts/ch_lib/model.py +++ b/scripts/ch_lib/model.py @@ -149,12 +149,31 @@ def get_model_path_by_search_term(model_type:str, search_term:str): model_sub_path = model_sub_path.strip() - if model_sub_path[:1] == "/": model_sub_path = model_sub_path[1:] + model_folder_name = ""; + if model_type == "ti": + model_folder_name = "embeddings" + elif model_type == "hyper": + model_folder_name = "hypernetworks" + elif model_type == "ckp": + model_folder_name = "Stable-diffusion" + else: + model_folder_name = "Lora" + + # check if model folder is already in search_term + if model_sub_path.startswith(model_folder_name): + # this is sd webui v1.8.0+'s search_term + # need to remove this model_folder_name+"/"or""\\" from model_sub_path + model_sub_path = model_sub_path[len(model_folder_name):] + + if model_sub_path.startswith("/") or model_sub_path.startswith("\\"): + model_sub_path = model_sub_path[1:] + if model_type == "hyper": - model_sub_path = model_sub_path+".pt" + if not model_sub_path.endswith(".pt"): + model_sub_path = model_sub_path+".pt" model_folder = folders[model_type] diff --git a/scripts/ch_lib/util.py b/scripts/ch_lib/util.py index f5ad657..5fc86c5 100644 --- a/scripts/ch_lib/util.py +++ b/scripts/ch_lib/util.py @@ -6,7 +6,7 @@ import requests import shutil -version = "1.10.0" +version = "1.10.1" 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": ""}