From a3693ccf6d77c36580a345a2f7b6c918d14d6bd7 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 17 Oct 2024 22:45:33 +0300 Subject: [PATCH] Use deepMerge to combine payloads --- .../extensions/stable-diffusion/index.js | 44 +++++++++---------- .../extensions/stable-diffusion/settings.html | 13 ++++-- public/scripts/utils.js | 32 ++++++++++++++ 3 files changed, 63 insertions(+), 26 deletions(-) diff --git a/public/scripts/extensions/stable-diffusion/index.js b/public/scripts/extensions/stable-diffusion/index.js index a41bb4fda..62e61748b 100644 --- a/public/scripts/extensions/stable-diffusion/index.js +++ b/public/scripts/extensions/stable-diffusion/index.js @@ -18,7 +18,7 @@ import { } from '../../../script.js'; import { getApiUrl, getContext, extension_settings, doExtrasFetch, modules, renderExtensionTemplateAsync, writeExtensionField } from '../../extensions.js'; import { selected_group } from '../../group-chats.js'; -import { stringFormat, initScrollHeight, resetScrollHeight, getCharaFilename, saveBase64AsFile, getBase64Async, delay, isTrueBoolean, debounce, isFalseBoolean } from '../../utils.js'; +import { stringFormat, initScrollHeight, resetScrollHeight, getCharaFilename, saveBase64AsFile, getBase64Async, delay, isTrueBoolean, debounce, isFalseBoolean, deepMerge } from '../../utils.js'; import { getMessageTimeStamp, humanizedDateTime } from '../../RossAscends-mods.js'; import { SECRET_KEYS, secret_state, writeSecret } from '../../secrets.js'; import { getNovelUnlimitedImageGeneration, getNovelAnlas, loadNovelSubscriptionData } from '../../nai-settings.js'; @@ -219,7 +219,7 @@ const defaultSettings = { // Automatic1111/Horde exclusives restore_faces: false, enable_hr: false, - aiFaceDetailer: false, + adetailer_face: false, // Horde settings horde: false, @@ -436,7 +436,7 @@ async function loadSettings() { $('#sd_horde_sanitize').prop('checked', extension_settings.sd.horde_sanitize); $('#sd_restore_faces').prop('checked', extension_settings.sd.restore_faces); $('#sd_enable_hr').prop('checked', extension_settings.sd.enable_hr); - $('#sd_aiFaceDetailer').prop('checked', extension_settings.sd.aiFaceDetailer); + $('#sd_adetailer_face').prop('checked', extension_settings.sd.adetailer_face); $('#sd_refine_mode').prop('checked', extension_settings.sd.refine_mode); $('#sd_multimodal_captioning').prop('checked', extension_settings.sd.multimodal_captioning); $('#sd_auto_url').val(extension_settings.sd.auto_url); @@ -855,8 +855,8 @@ function onSamplerChange() { saveSettingsDebounced(); } -function onAiFaceDetailerChange() { - extension_settings.sd.aiFaceDetailer = $('#sd_aiFaceDetailer').prop('checked'); +function onADetailerFaceChange() { + extension_settings.sd.adetailer_face = !!$('#sd_adetailer_face').prop('checked'); saveSettingsDebounced(); } @@ -2955,20 +2955,22 @@ async function generateAutoImage(prompt, negativePrompt, signal) { do_not_save_grid: false, do_not_save_samples: false, }; - // Conditionally add the ADetailer if aiFaceDetailer is enabled - console.log('extension_settings.sd.aiFaceDetailer:', extension_settings.sd.aiFaceDetailer); - if (extension_settings.sd.aiFaceDetailer) { - payload.alwayson_scripts = { - ADetailer: { - args: [ - true, // ad_enable - true, // skip_img2img - { - "ad_model": "face_yolov8n.pt" - } - ] - } - }; + + // Conditionally add the ADetailer if adetailer_face is enabled + if (extension_settings.sd.adetailer_face) { + payload = deepMerge(payload, { + alwayson_scripts: { + ADetailer: { + args: [ + true, // ad_enable + true, // skip_img2img + { + 'ad_model': 'face_yolov8n.pt', + }, + ], + }, + }, + }); } // Make the fetch call with the payload @@ -2978,8 +2980,6 @@ async function generateAutoImage(prompt, negativePrompt, signal) { signal: signal, body: JSON.stringify(payload), }); - - if (result.ok) { const data = await result.json(); @@ -4210,7 +4210,7 @@ jQuery(async () => { $('#sd_horde_sanitize').on('input', onHordeSanitizeInput); $('#sd_restore_faces').on('input', onRestoreFacesInput); $('#sd_enable_hr').on('input', onHighResFixInput); - $('#sd_aiFaceDetailer').on('change', onAiFaceDetailerChange); + $('#sd_adetailer_face').on('change', onADetailerFaceChange); $('#sd_refine_mode').on('input', onRefineModeInput); $('#sd_character_prompt').on('input', onCharacterPromptInput); $('#sd_character_negative_prompt').on('input', onCharacterNegativePromptInput); diff --git a/public/scripts/extensions/stable-diffusion/settings.html b/public/scripts/extensions/stable-diffusion/settings.html index cb69c2e51..e5127e246 100644 --- a/public/scripts/extensions/stable-diffusion/settings.html +++ b/public/scripts/extensions/stable-diffusion/settings.html @@ -354,11 +354,16 @@ Karras - + +
+ +
+ +
diff --git a/public/scripts/utils.js b/public/scripts/utils.js index 6cf92f5c0..1196f5c42 100644 --- a/public/scripts/utils.js +++ b/public/scripts/utils.js @@ -23,6 +23,38 @@ export const navigation_option = { previous: -1000, }; +/** + * Determines if a value is an object. + * @param {any} item The item to check. + * @returns {boolean} True if the item is an object, false otherwise. + */ +function isObject(item) { + return (item && typeof item === 'object' && !Array.isArray(item)); +} + +/** + * Merges properties of two objects. If the property is an object, it will be merged recursively. + * @param {object} target The target object + * @param {object} source The source object + * @returns {object} Merged object + */ +export function deepMerge(target, source) { + let output = Object.assign({}, target); + if (isObject(target) && isObject(source)) { + Object.keys(source).forEach(key => { + if (isObject(source[key])) { + if (!(key in target)) + Object.assign(output, { [key]: source[key] }); + else + output[key] = deepMerge(target[key], source[key]); + } else { + Object.assign(output, { [key]: source[key] }); + } + }); + } + return output; +} + export function escapeHtml(str) { return String(str).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); }