Adding negative character prompts for img sources that support it

This commit is contained in:
Doa 2023-12-23 16:19:22 +00:00
parent db3bf42d63
commit 41ac2c07b2
3 changed files with 60 additions and 19 deletions

View File

@ -110,6 +110,7 @@ const extension_settings = {
sd: { sd: {
prompts: {}, prompts: {},
character_prompts: {}, character_prompts: {},
character_negative_prompts: {},
}, },
chromadb: {}, chromadb: {},
translate: {}, translate: {},

View File

@ -351,6 +351,10 @@ async function loadSettings() {
extension_settings.sd.character_prompts = {}; extension_settings.sd.character_prompts = {};
} }
if (extension_settings.sd.character_negative_prompts === undefined) {
extension_settings.sd.character_negative_prompts = {};
}
if (!Array.isArray(extension_settings.sd.styles)) { if (!Array.isArray(extension_settings.sd.styles)) {
extension_settings.sd.styles = defaultStyles; extension_settings.sd.styles = defaultStyles;
} }
@ -575,6 +579,7 @@ function onChatChanged() {
$('#sd_character_prompt_block').show(); $('#sd_character_prompt_block').show();
const key = getCharaFilename(this_chid); const key = getCharaFilename(this_chid);
$('#sd_character_prompt').val(key ? (extension_settings.sd.character_prompts[key] || '') : ''); $('#sd_character_prompt').val(key ? (extension_settings.sd.character_prompts[key] || '') : '');
$('#sd_character_negative_prompt').val(key ? (extension_settings.sd.character_negative_prompts[key] || '') : '');
} }
function onCharacterPromptInput() { function onCharacterPromptInput() {
@ -584,6 +589,13 @@ function onCharacterPromptInput() {
saveSettingsDebounced(); saveSettingsDebounced();
} }
function onCharacterNegativePromptInput() {
const key = getCharaFilename(this_chid);
extension_settings.sd.character_negative_prompts[key] = $('#sd_character_negative_prompt').val();
resetScrollHeight($(this));
saveSettingsDebounced();
}
function getCharacterPrefix() { function getCharacterPrefix() {
if (!this_chid || selected_group) { if (!this_chid || selected_group) {
return ''; return '';
@ -598,6 +610,20 @@ function getCharacterPrefix() {
return ''; return '';
} }
function getCharacterNegativePrefix() {
if (!this_chid || selected_group) {
return '';
}
const key = getCharaFilename(this_chid);
if (key) {
return extension_settings.sd.character_negative_prompts[key] || '';
}
return '';
}
/** /**
* Combines two prompt prefixes into one. * Combines two prompt prefixes into one.
* @param {string} str1 Base string * @param {string} str1 Base string
@ -1885,34 +1911,38 @@ async function sendGenerationRequest(generationType, prompt, characterName = nul
const prefixedPrompt = combinePrefixes(prefix, prompt, '{prompt}'); const prefixedPrompt = combinePrefixes(prefix, prompt, '{prompt}');
const negativePrompt = noCharPrefix.includes(generationType)
? extension_settings.sd.negative_prompt
: combinePrefixes(extension_settings.sd.negative_prompt, getCharacterNegativePrefix());
let result = { format: '', data: '' }; let result = { format: '', data: '' };
const currentChatId = getCurrentChatId(); const currentChatId = getCurrentChatId();
try { try {
switch (extension_settings.sd.source) { switch (extension_settings.sd.source) {
case sources.extras: case sources.extras:
result = await generateExtrasImage(prefixedPrompt); result = await generateExtrasImage(prefixedPrompt, negativePrompt);
break; break;
case sources.horde: case sources.horde:
result = await generateHordeImage(prefixedPrompt); result = await generateHordeImage(prefixedPrompt, negativePrompt);
break; break;
case sources.vlad: case sources.vlad:
result = await generateAutoImage(prefixedPrompt); result = await generateAutoImage(prefixedPrompt, negativePrompt);
break; break;
case sources.auto: case sources.auto:
result = await generateAutoImage(prefixedPrompt); result = await generateAutoImage(prefixedPrompt, negativePrompt);
break; break;
case sources.novel: case sources.novel:
result = await generateNovelImage(prefixedPrompt); result = await generateNovelImage(prefixedPrompt, negativePrompt);
break; break;
case sources.openai: case sources.openai:
result = await generateOpenAiImage(prefixedPrompt); result = await generateOpenAiImage(prefixedPrompt);
break; break;
case sources.comfy: case sources.comfy:
result = await generateComfyImage(prefixedPrompt); result = await generateComfyImage(prefixedPrompt, negativePrompt);
break; break;
case sources.togetherai: case sources.togetherai:
result = await generateTogetherAIImage(prefixedPrompt); result = await generateTogetherAIImage(prefixedPrompt, negativePrompt);
break; break;
} }
@ -1936,13 +1966,13 @@ async function sendGenerationRequest(generationType, prompt, characterName = nul
callback ? callback(prompt, base64Image, generationType) : sendMessage(prompt, base64Image, generationType); callback ? callback(prompt, base64Image, generationType) : sendMessage(prompt, base64Image, generationType);
} }
async function generateTogetherAIImage(prompt) { async function generateTogetherAIImage(prompt, negativePrompt) {
const result = await fetch('/api/sd/together/generate', { const result = await fetch('/api/sd/together/generate', {
method: 'POST', method: 'POST',
headers: getRequestHeaders(), headers: getRequestHeaders(),
body: JSON.stringify({ body: JSON.stringify({
prompt: prompt, prompt: prompt,
negative_prompt: extension_settings.sd.negative_prompt, negative_prompt: negativePrompt,
model: extension_settings.sd.model, model: extension_settings.sd.model,
steps: extension_settings.sd.steps, steps: extension_settings.sd.steps,
width: extension_settings.sd.width, width: extension_settings.sd.width,
@ -1963,9 +1993,10 @@ async function generateTogetherAIImage(prompt) {
* Generates an "extras" image using a provided prompt and other settings. * Generates an "extras" image using a provided prompt and other settings.
* *
* @param {string} prompt - The main instruction used to guide the image generation. * @param {string} prompt - The main instruction used to guide the image generation.
* @param {string} negativePrompt - The instruction used to restrict the image generation.
* @returns {Promise<{format: string, data: string}>} - A promise that resolves when the image generation and processing are complete. * @returns {Promise<{format: string, data: string}>} - A promise that resolves when the image generation and processing are complete.
*/ */
async function generateExtrasImage(prompt) { async function generateExtrasImage(prompt, negativePrompt) {
const url = new URL(getApiUrl()); const url = new URL(getApiUrl());
url.pathname = '/api/image'; url.pathname = '/api/image';
const result = await doExtrasFetch(url, { const result = await doExtrasFetch(url, {
@ -1980,7 +2011,7 @@ async function generateExtrasImage(prompt) {
scale: extension_settings.sd.scale, scale: extension_settings.sd.scale,
width: extension_settings.sd.width, width: extension_settings.sd.width,
height: extension_settings.sd.height, height: extension_settings.sd.height,
negative_prompt: extension_settings.sd.negative_prompt, negative_prompt: negativePrompt,
restore_faces: !!extension_settings.sd.restore_faces, restore_faces: !!extension_settings.sd.restore_faces,
enable_hr: !!extension_settings.sd.enable_hr, enable_hr: !!extension_settings.sd.enable_hr,
karras: !!extension_settings.sd.horde_karras, karras: !!extension_settings.sd.horde_karras,
@ -2004,9 +2035,10 @@ async function generateExtrasImage(prompt) {
* Generates a "horde" image using the provided prompt and configuration settings. * Generates a "horde" image using the provided prompt and configuration settings.
* *
* @param {string} prompt - The main instruction used to guide the image generation. * @param {string} prompt - The main instruction used to guide the image generation.
* @param {string} negativePrompt - The instruction used to restrict the image generation.
* @returns {Promise<{format: string, data: string}>} - A promise that resolves when the image generation and processing are complete. * @returns {Promise<{format: string, data: string}>} - A promise that resolves when the image generation and processing are complete.
*/ */
async function generateHordeImage(prompt) { async function generateHordeImage(prompt, negativePrompt) {
const result = await fetch('/api/horde/generate-image', { const result = await fetch('/api/horde/generate-image', {
method: 'POST', method: 'POST',
headers: getRequestHeaders(), headers: getRequestHeaders(),
@ -2017,7 +2049,7 @@ async function generateHordeImage(prompt) {
scale: extension_settings.sd.scale, scale: extension_settings.sd.scale,
width: extension_settings.sd.width, width: extension_settings.sd.width,
height: extension_settings.sd.height, height: extension_settings.sd.height,
negative_prompt: extension_settings.sd.negative_prompt, negative_prompt: negativePrompt,
model: extension_settings.sd.model, model: extension_settings.sd.model,
nsfw: extension_settings.sd.horde_nsfw, nsfw: extension_settings.sd.horde_nsfw,
restore_faces: !!extension_settings.sd.restore_faces, restore_faces: !!extension_settings.sd.restore_faces,
@ -2039,16 +2071,17 @@ async function generateHordeImage(prompt) {
* Generates an image in SD WebUI API using the provided prompt and configuration settings. * Generates an image in SD WebUI API using the provided prompt and configuration settings.
* *
* @param {string} prompt - The main instruction used to guide the image generation. * @param {string} prompt - The main instruction used to guide the image generation.
* @param {string} negativePrompt - The instruction used to restrict the image generation.
* @returns {Promise<{format: string, data: string}>} - A promise that resolves when the image generation and processing are complete. * @returns {Promise<{format: string, data: string}>} - A promise that resolves when the image generation and processing are complete.
*/ */
async function generateAutoImage(prompt) { async function generateAutoImage(prompt, negativePrompt) {
const result = await fetch('/api/sd/generate', { const result = await fetch('/api/sd/generate', {
method: 'POST', method: 'POST',
headers: getRequestHeaders(), headers: getRequestHeaders(),
body: JSON.stringify({ body: JSON.stringify({
...getSdRequestBody(), ...getSdRequestBody(),
prompt: prompt, prompt: prompt,
negative_prompt: extension_settings.sd.negative_prompt, negative_prompt: negativePrompt,
sampler_name: extension_settings.sd.sampler, sampler_name: extension_settings.sd.sampler,
steps: extension_settings.sd.steps, steps: extension_settings.sd.steps,
cfg_scale: extension_settings.sd.scale, cfg_scale: extension_settings.sd.scale,
@ -2081,9 +2114,10 @@ async function generateAutoImage(prompt) {
* Generates an image in NovelAI API using the provided prompt and configuration settings. * Generates an image in NovelAI API using the provided prompt and configuration settings.
* *
* @param {string} prompt - The main instruction used to guide the image generation. * @param {string} prompt - The main instruction used to guide the image generation.
* @param {string} negativePrompt - The instruction used to restrict the image generation.
* @returns {Promise<{format: string, data: string}>} - A promise that resolves when the image generation and processing are complete. * @returns {Promise<{format: string, data: string}>} - A promise that resolves when the image generation and processing are complete.
*/ */
async function generateNovelImage(prompt) { async function generateNovelImage(prompt, negativePrompt) {
const { steps, width, height } = getNovelParams(); const { steps, width, height } = getNovelParams();
const result = await fetch('/api/novelai/generate-image', { const result = await fetch('/api/novelai/generate-image', {
@ -2097,7 +2131,7 @@ async function generateNovelImage(prompt) {
scale: extension_settings.sd.scale, scale: extension_settings.sd.scale,
width: width, width: width,
height: height, height: height,
negative_prompt: extension_settings.sd.negative_prompt, negative_prompt: negativePrompt,
upscale_ratio: extension_settings.sd.novel_upscale_ratio, upscale_ratio: extension_settings.sd.novel_upscale_ratio,
}), }),
}); });
@ -2225,11 +2259,11 @@ async function generateOpenAiImage(prompt) {
* Generates an image in ComfyUI using the provided prompt and configuration settings. * Generates an image in ComfyUI using the provided prompt and configuration settings.
* *
* @param {string} prompt - The main instruction used to guide the image generation. * @param {string} prompt - The main instruction used to guide the image generation.
* @param {string} negativePrompt - The instruction used to restrict the image generation.
* @returns {Promise<{format: string, data: string}>} - A promise that resolves when the image generation and processing are complete. * @returns {Promise<{format: string, data: string}>} - A promise that resolves when the image generation and processing are complete.
*/ */
async function generateComfyImage(prompt) { async function generateComfyImage(prompt, negativePrompt) {
const placeholders = [ const placeholders = [
'negative_prompt',
'model', 'model',
'vae', 'vae',
'sampler', 'sampler',
@ -2252,6 +2286,7 @@ async function generateComfyImage(prompt) {
toastr.error(`Failed to load workflow.\n\n${text}`); toastr.error(`Failed to load workflow.\n\n${text}`);
} }
let workflow = (await workflowResponse.json()).replace('"%prompt%"', JSON.stringify(prompt)); let workflow = (await workflowResponse.json()).replace('"%prompt%"', JSON.stringify(prompt));
workflow = (await workflowResponse.json()).replace('"%negative_prompt%"', JSON.stringify(negativePrompt));
workflow = workflow.replace('"%seed%"', JSON.stringify(Math.round(Math.random() * Number.MAX_SAFE_INTEGER))); workflow = workflow.replace('"%seed%"', JSON.stringify(Math.round(Math.random() * Number.MAX_SAFE_INTEGER)));
placeholders.forEach(ph => { placeholders.forEach(ph => {
workflow = workflow.replace(`"%${ph}%"`, JSON.stringify(extension_settings.sd[ph])); workflow = workflow.replace(`"%${ph}%"`, JSON.stringify(extension_settings.sd[ph]));
@ -2629,6 +2664,7 @@ jQuery(async () => {
$('#sd_enable_hr').on('input', onHighResFixInput); $('#sd_enable_hr').on('input', onHighResFixInput);
$('#sd_refine_mode').on('input', onRefineModeInput); $('#sd_refine_mode').on('input', onRefineModeInput);
$('#sd_character_prompt').on('input', onCharacterPromptInput); $('#sd_character_prompt').on('input', onCharacterPromptInput);
$('#sd_character_negative_prompt').on('input', onCharacterNegativePromptInput);
$('#sd_auto_validate').on('click', validateAutoUrl); $('#sd_auto_validate').on('click', validateAutoUrl);
$('#sd_auto_url').on('input', onAutoUrlInput); $('#sd_auto_url').on('input', onAutoUrlInput);
$('#sd_auto_auth').on('input', onAutoAuthInput); $('#sd_auto_auth').on('input', onAutoAuthInput);
@ -2661,6 +2697,7 @@ jQuery(async () => {
initScrollHeight($('#sd_prompt_prefix')); initScrollHeight($('#sd_prompt_prefix'));
initScrollHeight($('#sd_negative_prompt')); initScrollHeight($('#sd_negative_prompt'));
initScrollHeight($('#sd_character_prompt')); initScrollHeight($('#sd_character_prompt'));
initScrollHeight($('#sd_character_negative_prompt'));
}); });
for (const [key, value] of Object.entries(resolutionOptions)) { for (const [key, value] of Object.entries(resolutionOptions)) {

View File

@ -208,6 +208,9 @@
<label for="sd_character_prompt">Character-specific prompt prefix</label> <label for="sd_character_prompt">Character-specific prompt prefix</label>
<small>Won't be used in groups.</small> <small>Won't be used in groups.</small>
<textarea id="sd_character_prompt" class="text_pole textarea_compact" rows="3" placeholder="Any characteristics that describe the currently selected character. Will be added after a common prefix.&#10;Example: female, green eyes, brown hair, pink shirt"></textarea> <textarea id="sd_character_prompt" class="text_pole textarea_compact" rows="3" placeholder="Any characteristics that describe the currently selected character. Will be added after a common prefix.&#10;Example: female, green eyes, brown hair, pink shirt"></textarea>
<label for="sd_character_negative_prompt">Character-specific negative prompt prefix</label>
<small>Won't be used in groups.</small>
<textarea id="sd_character_negative_prompt" class="text_pole textarea_compact" rows="3" placeholder="Any characteristics that should not appear for the selected character. Will be added after a negative common prefix.&#10;Example: jewellery, shoes, glasses"></textarea>
</div> </div>
</div> </div>
</div> </div>