diff --git a/javascript/civitai_helper.js b/javascript/civitai_helper.js index a96ae8d..ed5ac3e 100644 --- a/javascript/civitai_helper.js +++ b/javascript/civitai_helper.js @@ -559,6 +559,10 @@ onUiLoaded(() => { additional_node = card.querySelector(".actions .additional"); //get ul node, which is the parent of all buttons ul_node = card.querySelector(".actions .additional ul"); + if (ul_node==null) { + ul_node = document.createElement("ul"); + additional_node.appendChild(ul_node); + } // replace preview text button replace_preview_btn = card.querySelector(".actions .additional a"); @@ -594,6 +598,11 @@ onUiLoaded(() => { }); } + if (replace_preview_btn==null) { + replace_preview_btn = document.createElement("a"); + additional_node.appendChild(replace_preview_btn); + } + // check thumb mode if (is_thumb_mode) { additional_node.style.display = null; diff --git a/scripts/ch_lib/civitai.py b/scripts/ch_lib/civitai.py index 7ec09c3..9e579fe 100644 --- a/scripts/ch_lib/civitai.py +++ b/scripts/ch_lib/civitai.py @@ -231,15 +231,14 @@ def load_model_info_by_search_term(model_type, search_term): break; if not found: - util.printD(f"Can not find model info file: {model_info_filepath}") + + if not os.path.isfile(model_info_filepath): + util.printD(f"Can not find model info file: {model_info_filepath}") return return model.load_model_info(model_info_filepath) - - - # get model file names by model type # parameter: model_type - string # parameter: filter - dict, which kind of model you need @@ -276,6 +275,31 @@ def get_model_names_by_type_and_filter(model_type:str, filter:dict) -> list: if ext in model.exts: # find a model + for root, dirs, files in os.walk(model_folder, followlinks=True): + for filename in files: + item = os.path.join(root, filename) + # check extension + base, ext = os.path.splitext(item) + if ext in model.exts: + # find a model + + # check filter + if no_info_only: + # check model info file + info_file = base + suffix + model.info_ext + if os.path.isfile(info_file): + continue + + if empty_info_only: + # check model info file + info_file = base + suffix + model.info_ext + if os.path.isfile(info_file): + if model_info := model.load_model_info(info_file): + if "id" in model_info.keys(): + # find a non-empty model info file + continue + + # check filter if no_info_only: # check model info file @@ -545,8 +569,6 @@ def check_model_new_version_by_path(model_path:str, delay:float=1) -> tuple: return (model_path, model_id, model_name, current_version_id, new_version_name, description, downloadUrl, img_url) - - # 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) @@ -613,9 +635,4 @@ def check_models_new_version_by_model_types(model_types:list, delay:float=1) -> new_versions.append(r) - - return new_versions - - - diff --git a/scripts/ch_lib/downloader.py b/scripts/ch_lib/downloader.py index 2b14ebc..28e5caa 100644 --- a/scripts/ch_lib/downloader.py +++ b/scripts/ch_lib/downloader.py @@ -12,7 +12,7 @@ requests.packages.urllib3.disable_warnings() # output is downloaded file path def dl(url, folder, filename, filepath): - util.printD("Start downloading from: " + url) + util.printD(f"Start downloading from: {url}") # get file_path file_path = "" if filepath: @@ -22,11 +22,11 @@ def dl(url, folder, filename, filepath): if not folder: util.printD("folder is none") return - + if not os.path.isdir(folder): - util.printD("folder does not exist: "+folder) + util.printD(f"folder does not exist: {folder}") return - + if filename: file_path = os.path.join(folder, filename) @@ -40,25 +40,25 @@ def dl(url, folder, filename, filepath): # if file_path is empty, need to get file name from download url's header if not file_path: filename = "" - if "Content-Disposition" in rh.headers.keys(): + if "Content-Disposition" in rh.headers: cd = rh.headers["Content-Disposition"] # Extract the filename from the header # content of a CD: "attachment;filename=FileName.txt" # in case "" is in CD filename's start and end, need to strip them out filename = cd.split("=")[1].strip('"') if not filename: - util.printD("Fail to get file name from Content-Disposition: " + cd) + util.printD(f"Fail to get file name from Content-Disposition: {cd}") return - + if not filename: util.printD("Can not get file name from download url's header") return - + # with folder and filename, now we have the full file path file_path = os.path.join(folder, filename) - util.printD("Target file path: " + file_path) + util.printD(f"Target file path: {file_path}") base, ext = os.path.splitext(file_path) # check if file is already exist @@ -67,7 +67,7 @@ def dl(url, folder, filename, filepath): while os.path.isfile(file_path): util.printD("Target file already exist.") # re-name - new_base = base + "_" + str(count) + new_base = f"{base}_{str(count)}" file_path = new_base + ext count += 1 diff --git a/scripts/ch_lib/js_action_civitai.py b/scripts/ch_lib/js_action_civitai.py index 0c81853..a9afd42 100644 --- a/scripts/ch_lib/js_action_civitai.py +++ b/scripts/ch_lib/js_action_civitai.py @@ -23,7 +23,7 @@ def open_model_url(msg, open_url_with_js): if not result: util.printD("Parsing js ms failed") return - + model_type = result["model_type"] search_term = result["search_term"] @@ -50,7 +50,7 @@ def open_model_url(msg, open_url_with_js): } if not open_url_with_js: - util.printD("Open Url: " + url) + util.printD(f"Open Url: {url}") # open url webbrowser.open_new_tab(url) else: @@ -73,7 +73,7 @@ def add_trigger_words(msg): if not result: util.printD("Parsing js ms failed") return - + model_type = result["model_type"] search_term = result["search_term"] prompt = result["prompt"] @@ -83,29 +83,29 @@ def add_trigger_words(msg): if not model_info: util.printD(f"Failed to get model info for {model_type} {search_term}") return [prompt, prompt] - + if "trainedWords" not in model_info.keys(): util.printD(f"Failed to get trainedWords from info file for {model_type} {search_term}") return [prompt, prompt] - + trainedWords = model_info["trainedWords"] if not trainedWords: util.printD(f"No trainedWords from info file for {model_type} {search_term}") return [prompt, prompt] - + if len(trainedWords) == 0: util.printD(f"trainedWords from info file for {model_type} {search_term} is empty") return [prompt, prompt] - + # get ful trigger words trigger_words = "" for word in trainedWords: trigger_words = trigger_words + word + ", " - new_prompt = prompt + " " + trigger_words - util.printD("trigger_words: " + trigger_words) - util.printD("prompt: " + prompt) - util.printD("new_prompt: " + new_prompt) + new_prompt = f"{prompt} {trigger_words}" + util.printD(f"trigger_words: {trigger_words}") + util.printD(f"prompt: {prompt}") + util.printD(f"new_prompt: {new_prompt}") util.printD("End add_trigger_words") @@ -188,14 +188,14 @@ def dl_model_new_version(msg, max_size_preview, skip_nsfw_preview): output = "Parsing js ms failed" util.printD(output) return output - + model_path = result["model_path"] version_id = result["version_id"] download_url = result["download_url"] - util.printD("model_path: " + model_path) - util.printD("version_id: " + str(version_id)) - util.printD("download_url: " + download_url) + util.printD(f"model_path: {model_path}") + util.printD(f"version_id: {str(version_id)}") + util.printD(f"download_url: {download_url}") # check data if not model_path: @@ -207,14 +207,14 @@ def dl_model_new_version(msg, max_size_preview, skip_nsfw_preview): output = "version_id is empty" util.printD(output) return output - + if not download_url: output = "download_url is empty" util.printD(output) return output if not os.path.isfile(model_path): - output = "model_path is not a file: "+ model_path + output = f"model_path is not a file: {model_path}" util.printD(output) return output @@ -232,14 +232,14 @@ def dl_model_new_version(msg, max_size_preview, skip_nsfw_preview): # download file new_model_path = downloader.dl(download_url, model_folder, None, None) if not new_model_path: - output = "Download failed, check console log for detail. Download url: " + download_url + output = f"Download failed, check console log for detail. Download url: {download_url}" util.printD(output) return output # get version info version_info = civitai.get_version_info_by_version_id(version_id) if not version_info: - output = "Model downloaded, but failed to get version info, check console log for detail. Model saved to: " + new_model_path + output = f"Model downloaded, but failed to get version info, check console log for detail. Model saved to: {new_model_path}" util.printD(output) return output @@ -250,7 +250,7 @@ def dl_model_new_version(msg, max_size_preview, skip_nsfw_preview): # then, get preview image civitai.get_preview_image_by_model_path(new_model_path, max_size_preview, skip_nsfw_preview) - - output = "Done. Model downloaded to: " + new_model_path + + output = f"Done. Model downloaded to: {new_model_path}" util.printD(output) return output diff --git a/scripts/ch_lib/model.py b/scripts/ch_lib/model.py index a4d280a..8520ff1 100644 --- a/scripts/ch_lib/model.py +++ b/scripts/ch_lib/model.py @@ -122,7 +122,6 @@ def get_model_path_by_type_and_name(model_type:str, model_name:str) -> str: else: model_folders = [folders[model_type]] - # model could be in subfolder, need to walk. model_root = "" model_path = "" @@ -136,6 +135,3 @@ def get_model_path_by_type_and_name(model_type:str, model_name:str) -> str: return (model_root, model_path) return - - - diff --git a/scripts/ch_lib/model_action_civitai.py b/scripts/ch_lib/model_action_civitai.py index f09f075..640ca35 100644 --- a/scripts/ch_lib/model_action_civitai.py +++ b/scripts/ch_lib/model_action_civitai.py @@ -19,14 +19,14 @@ def scan_model(scan_model_types, max_size_preview, skip_nsfw_preview): output = "Model Types is None, can not scan." util.printD(output) return output - + model_types = [] # check type if it is a string if type(scan_model_types) == str: model_types.append(scan_model_types) else: model_types = scan_model_types - + model_count = 0 image_count = 0 # scan_log = "" @@ -34,7 +34,7 @@ def scan_model(scan_model_types, max_size_preview, skip_nsfw_preview): if model_type not in model_types: continue - util.printD("Scanning path: " + model_folder) + util.printD(f"Scanning path: {model_folder}") for root, dirs, files in os.walk(model_folder, followlinks=True): for filename in files: # check ext @@ -45,7 +45,7 @@ def scan_model(scan_model_types, max_size_preview, skip_nsfw_preview): if len(base) > 4: if base[-4:] == model.vae_suffix: # find .vae - util.printD("This is a vae file: " + filename) + util.printD(f"This is a vae file: {filename}") continue # find a model @@ -53,15 +53,15 @@ def scan_model(scan_model_types, max_size_preview, skip_nsfw_preview): info_file = base + civitai.suffix + model.info_ext # check info file if not os.path.isfile(info_file): - util.printD("Creating model info for: " + filename) + util.printD(f"Creating model info for: {filename}") # get model's sha256 hash = util.gen_file_sha256(item) if not hash: - output = "failed generating SHA256 for model:" + filename + output = f"failed generating SHA256 for model:{filename}" util.printD(output) return output - + # use this sha256 to get model info from civitai model_info = civitai.get_model_info_by_hash(hash) # delay 1 second for ti @@ -72,7 +72,7 @@ def scan_model(scan_model_types, max_size_preview, skip_nsfw_preview): if model_info is None: output = "Connect to Civitai API service failed. Wait a while and try again" util.printD(output) - return output+", check console log for detail" + return f"{output}, check console log for detail" # write model info to file model.write_model_info(info_file, model_info) @@ -102,7 +102,7 @@ def get_model_info_by_input(model_type, model_name, model_url_or_id, max_size_pr # parse model id model_id = civitai.get_model_id_from_url(model_url_or_id) if not model_id: - output = "failed to parse model id from url: " + model_url_or_id + output = f"failed to parse model id from url: {model_url_or_id}" util.printD(output) return output @@ -113,13 +113,13 @@ def get_model_info_by_input(model_type, model_name, model_url_or_id, max_size_pr output = "failed to get model file path" util.printD(output) return output - + model_root, model_path = result if not model_path: output = "model path is empty" util.printD(output) return output - + # get info file path base, ext = os.path.splitext(model_path) info_file = base + civitai.suffix + model.info_ext @@ -129,19 +129,19 @@ def get_model_info_by_input(model_type, model_name, model_url_or_id, max_size_pr model_info = civitai.get_version_info_by_model_id(model_id) if not model_info: - output = "failed to get model info from url: " + model_url_or_id + output = f"failed to get model info from url: {model_url_or_id}" util.printD(output) return output - + # write model info to file model.write_model_info(info_file, model_info) - util.printD("Saved model info to: "+ info_file) + util.printD(f"Saved model info to: {info_file}") # check preview image civitai.get_preview_image_by_model_path(model_path, max_size_preview, skip_nsfw_preview) - output = "Model Info saved to: " + info_file + output = f"Model Info saved to: {info_file}" return output @@ -166,28 +166,28 @@ def check_models_new_version_to_md(model_types:list) -> str: url = civitai.url_dict["modelPage"]+str(model_id) part = f'
'+ description + '
{description}