From cdf092f3aca4f05545d0184fab8981142ebf1592 Mon Sep 17 00:00:00 2001 From: DominikDoom Date: Mon, 7 Aug 2023 15:17:49 +0200 Subject: [PATCH] Fix lora keyword lookup for deep subfolders --- javascript/ext_loras.js | 3 ++- javascript/ext_lycos.js | 3 ++- javascript/tagAutocomplete.js | 5 +++-- scripts/tag_autocomplete_helper.py | 26 ++++++++------------------ 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/javascript/ext_loras.js b/javascript/ext_loras.js index b5b9458..2e99eef 100644 --- a/javascript/ext_loras.js +++ b/javascript/ext_loras.js @@ -41,7 +41,8 @@ async function load() { async function sanitize(tagType, text) { if (tagType === ResultType.lora) { let multiplier = TAC_CFG.extraNetworksDefaultMultiplier; - let info = await fetchAPI(`tacapi/v1/lora-info/${text}`) + let finalComponent = text.lastIndexOf("/") > -1 ? text.substring(text.lastIndexOf("/") + 1) : text; + let info = await fetchAPI(`tacapi/v1/lora-info/${finalComponent}`) if (info && info["preferred weight"]) { multiplier = info["preferred weight"]; } diff --git a/javascript/ext_lycos.js b/javascript/ext_lycos.js index b053ecb..a7f9cf8 100644 --- a/javascript/ext_lycos.js +++ b/javascript/ext_lycos.js @@ -41,7 +41,8 @@ async function load() { async function sanitize(tagType, text) { if (tagType === ResultType.lyco) { let multiplier = TAC_CFG.extraNetworksDefaultMultiplier; - let info = await fetchAPI(`tacapi/v1/lyco-info/${text}`) + let finalComponent = text.lastIndexOf("/") > -1 ? text.substring(text.lastIndexOf("/") + 1) : text; + let info = await fetchAPI(`tacapi/v1/lyco-info/${finalComponent}`) if (info && info["preferred weight"]) { multiplier = info["preferred weight"]; } diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index f78c87d..071a986 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -1,4 +1,4 @@ -const styleColors = { +const styleColors = { "--results-bg": ["#0b0f19", "#ffffff"], "--results-border-color": ["#4b5563", "#e5e7eb"], "--results-border-width": ["1px", "1.5px"], @@ -453,7 +453,8 @@ async function insertTextAtCursor(textArea, result, tagword, tabCompletedWithout let keywords = null; // Check built-in activation words first if (tagType === ResultType.lora || tagType === ResultType.lyco) { - let info = await fetchAPI(`tacapi/v1/lora-info/${result.text}`) + let finalComponent = result.text.lastIndexOf("/") > -1 ? result.text.substring(result.text.lastIndexOf("/") + 1) : result.text; + let info = await fetchAPI(`tacapi/v1/lora-info/${finalComponent}`) if (info && info["activation text"]) { keywords = info["activation text"]; } diff --git a/scripts/tag_autocomplete_helper.py b/scripts/tag_autocomplete_helper.py index b9e7f69..0868f1a 100644 --- a/scripts/tag_autocomplete_helper.py +++ b/scripts/tag_autocomplete_helper.py @@ -446,34 +446,24 @@ def on_ui_settings(): script_callbacks.on_ui_settings(on_ui_settings) def api_tac(_: gr.Blocks, app: FastAPI): - async def get_json_info(path: Path): - if not path: + async def get_json_info(base_path: Path, filename: str = None): + if base_path is None or (not base_path.exists()): return json.dumps({}) try: - if path is not None and path.exists() and path.parent.joinpath(path.stem + ".json").exists(): - return FileResponse(path.parent.joinpath(path.stem + ".json").as_posix()) + name = Path(filename).stem + json_candidates = glob.glob(base_path.as_posix() + f"/**/{name}.json", recursive=True) + if json_candidates is not None and len(json_candidates) > 0: + return FileResponse(json_candidates[0]) except Exception as e: return json.dumps({"error": e}) - @app.get("/tacapi/v1/lora-info/{folder}/{lora_name}") - async def get_lora_info_subfolder(folder, lora_name): - if LORA_PATH is None: - return json.dumps({}) - return await get_json_info(LORA_PATH.joinpath(folder).joinpath(lora_name)) - - @app.get("/tacapi/v1/lyco-info/{folder}/{lyco_name}") - async def get_lyco_info_subfolder(folder, lyco_name): - if LYCO_PATH is None: - return json.dumps({}) - return await get_json_info(LYCO_PATH.joinpath(folder).joinpath(lyco_name)) - @app.get("/tacapi/v1/lora-info/{lora_name}") async def get_lora_info(lora_name): - return await get_lora_info_subfolder(".", lora_name) + return await get_json_info(LORA_PATH, lora_name) @app.get("/tacapi/v1/lyco-info/{lyco_name}") async def get_lyco_info(lyco_name): - return await get_lyco_info_subfolder(".", lyco_name) + return await get_json_info(LYCO_PATH, lyco_name) script_callbacks.on_app_started(api_tac) \ No newline at end of file