From 90fa3eebb3d0e10e40435b0c49670893b72a67e2 Mon Sep 17 00:00:00 2001 From: blessedcoolant <54517381+blessedcoolant@users.noreply.github.com> Date: Mon, 14 Aug 2023 02:25:39 +1200 Subject: [PATCH] feat: Make SDXL Style Prompt not take spaces --- .../graphBuilders/addSDXLRefinerToGraph.ts | 12 ++++---- .../buildCanvasSDXLImageToImageGraph.ts | 17 ++++++----- .../buildCanvasSDXLInpaintGraph.ts | 22 ++++++--------- .../buildCanvasSDXLOutpaintGraph.ts | 22 ++++++--------- .../buildCanvasSDXLTextToImageGraph.ts | 23 +++++++-------- .../buildLinearSDXLImageToImageGraph.ts | 13 +++++---- .../buildLinearSDXLTextToImageGraph.ts | 15 +++++----- .../helpers/craftSDXLStylePrompt.ts | 28 +++++++++++++++++++ 8 files changed, 86 insertions(+), 66 deletions(-) create mode 100644 invokeai/frontend/web/src/features/nodes/util/graphBuilders/helpers/craftSDXLStylePrompt.ts diff --git a/invokeai/frontend/web/src/features/nodes/util/graphBuilders/addSDXLRefinerToGraph.ts b/invokeai/frontend/web/src/features/nodes/util/graphBuilders/addSDXLRefinerToGraph.ts index 65664c9f2d..53f068c91b 100644 --- a/invokeai/frontend/web/src/features/nodes/util/graphBuilders/addSDXLRefinerToGraph.ts +++ b/invokeai/frontend/web/src/features/nodes/util/graphBuilders/addSDXLRefinerToGraph.ts @@ -16,18 +16,16 @@ import { SDXL_REFINER_NEGATIVE_CONDITIONING, SDXL_REFINER_POSITIVE_CONDITIONING, } from './constants'; +import { craftSDXLStylePrompt } from './helpers/craftSDXLStylePrompt'; export const addSDXLRefinerToGraph = ( state: RootState, graph: NonNullableGraph, baseNodeId: string ): void => { - const { positivePrompt, negativePrompt } = state.generation; const { refinerModel, refinerAestheticScore, - positiveStylePrompt, - negativeStylePrompt, refinerSteps, refinerScheduler, refinerCFGScale, @@ -49,6 +47,10 @@ export const addSDXLRefinerToGraph = ( metadataAccumulator.refiner_steps = refinerSteps; } + // Construct Style Prompt + const { craftedPositiveStylePrompt, craftedNegativeStylePrompt } = + craftSDXLStylePrompt(state, true); + // Unplug SDXL Latents Generation To Latents To Image graph.edges = graph.edges.filter( (e) => @@ -71,13 +73,13 @@ export const addSDXLRefinerToGraph = ( graph.nodes[SDXL_REFINER_POSITIVE_CONDITIONING] = { type: 'sdxl_refiner_compel_prompt', id: SDXL_REFINER_POSITIVE_CONDITIONING, - style: `${positivePrompt} ${positiveStylePrompt}`, + style: craftedPositiveStylePrompt, aesthetic_score: refinerAestheticScore, }; graph.nodes[SDXL_REFINER_NEGATIVE_CONDITIONING] = { type: 'sdxl_refiner_compel_prompt', id: SDXL_REFINER_NEGATIVE_CONDITIONING, - style: `${negativePrompt} ${negativeStylePrompt}`, + style: craftedNegativeStylePrompt, aesthetic_score: refinerAestheticScore, }; graph.nodes[SDXL_REFINER_DENOISE_LATENTS] = { diff --git a/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildCanvasSDXLImageToImageGraph.ts b/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildCanvasSDXLImageToImageGraph.ts index a45ffe7df5..ef32943bc8 100644 --- a/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildCanvasSDXLImageToImageGraph.ts +++ b/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildCanvasSDXLImageToImageGraph.ts @@ -26,6 +26,7 @@ import { SDXL_DENOISE_LATENTS, SDXL_MODEL_LOADER, } from './constants'; +import { craftSDXLStylePrompt } from './helpers/craftSDXLStylePrompt'; /** * Builds the Canvas tab's Image to Image graph. @@ -49,12 +50,10 @@ export const buildCanvasSDXLImageToImageGraph = ( } = state.generation; const { - positiveStylePrompt, - negativeStylePrompt, - shouldConcatSDXLStylePrompt, shouldUseSDXLRefiner, refinerStart, sdxlImg2ImgDenoisingStrength: strength, + shouldConcatSDXLStylePrompt, } = state.sdxl; // The bounding box determines width and height, not the width and height params @@ -71,6 +70,10 @@ export const buildCanvasSDXLImageToImageGraph = ( ? shouldUseCpuNoise : initialGenerationState.shouldUseCpuNoise; + // Construct Style Prompt + const { craftedPositiveStylePrompt, craftedNegativeStylePrompt } = + craftSDXLStylePrompt(state, shouldConcatSDXLStylePrompt); + /** * The easiest way to build linear graphs is to do it in the node editor, then copy and paste the * full graph here as a template. Then use the parameters from app state and set friendlier node @@ -93,17 +96,13 @@ export const buildCanvasSDXLImageToImageGraph = ( type: 'sdxl_compel_prompt', id: POSITIVE_CONDITIONING, prompt: positivePrompt, - style: shouldConcatSDXLStylePrompt - ? `${positivePrompt} ${positiveStylePrompt}` - : positiveStylePrompt, + style: craftedPositiveStylePrompt, }, [NEGATIVE_CONDITIONING]: { type: 'sdxl_compel_prompt', id: NEGATIVE_CONDITIONING, prompt: negativePrompt, - style: shouldConcatSDXLStylePrompt - ? `${negativePrompt} ${negativeStylePrompt}` - : negativeStylePrompt, + style: craftedNegativeStylePrompt, }, [NOISE]: { type: 'noise', diff --git a/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildCanvasSDXLInpaintGraph.ts b/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildCanvasSDXLInpaintGraph.ts index 570439f9e6..7d8586d09c 100644 --- a/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildCanvasSDXLInpaintGraph.ts +++ b/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildCanvasSDXLInpaintGraph.ts @@ -35,6 +35,7 @@ import { SDXL_DENOISE_LATENTS, SDXL_MODEL_LOADER, } from './constants'; +import { craftSDXLStylePrompt } from './helpers/craftSDXLStylePrompt'; /** * Builds the Canvas tab's Inpaint graph. @@ -63,13 +64,8 @@ export const buildCanvasSDXLInpaintGraph = ( maskBlurMethod, } = state.generation; - const { - positiveStylePrompt, - negativeStylePrompt, - shouldConcatSDXLStylePrompt, - shouldUseSDXLRefiner, - refinerStart, - } = state.sdxl; + const { shouldUseSDXLRefiner, refinerStart, shouldConcatSDXLStylePrompt } = + state.sdxl; if (!model) { log.error('No model found in state'); @@ -90,6 +86,10 @@ export const buildCanvasSDXLInpaintGraph = ( ? shouldUseCpuNoise : shouldUseCpuNoise; + // Construct Style Prompt + const { craftedPositiveStylePrompt, craftedNegativeStylePrompt } = + craftSDXLStylePrompt(state, shouldConcatSDXLStylePrompt); + const graph: NonNullableGraph = { id: SDXL_CANVAS_INPAINT_GRAPH, nodes: { @@ -102,17 +102,13 @@ export const buildCanvasSDXLInpaintGraph = ( type: 'sdxl_compel_prompt', id: POSITIVE_CONDITIONING, prompt: positivePrompt, - style: shouldConcatSDXLStylePrompt - ? `${positivePrompt} ${positiveStylePrompt}` - : positiveStylePrompt, + style: craftedPositiveStylePrompt, }, [NEGATIVE_CONDITIONING]: { type: 'sdxl_compel_prompt', id: NEGATIVE_CONDITIONING, prompt: negativePrompt, - style: shouldConcatSDXLStylePrompt - ? `${negativePrompt} ${negativeStylePrompt}` - : negativeStylePrompt, + style: craftedNegativeStylePrompt, }, [MASK_BLUR]: { type: 'img_blur', diff --git a/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildCanvasSDXLOutpaintGraph.ts b/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildCanvasSDXLOutpaintGraph.ts index 404f3501dd..b5260894c4 100644 --- a/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildCanvasSDXLOutpaintGraph.ts +++ b/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildCanvasSDXLOutpaintGraph.ts @@ -41,6 +41,7 @@ import { SDXL_DENOISE_LATENTS, SDXL_MODEL_LOADER, } from './constants'; +import { craftSDXLStylePrompt } from './helpers/craftSDXLStylePrompt'; /** * Builds the Canvas tab's Outpaint graph. @@ -71,13 +72,8 @@ export const buildCanvasSDXLOutpaintGraph = ( infillMethod, } = state.generation; - const { - positiveStylePrompt, - negativeStylePrompt, - shouldConcatSDXLStylePrompt, - shouldUseSDXLRefiner, - refinerStart, - } = state.sdxl; + const { shouldUseSDXLRefiner, refinerStart, shouldConcatSDXLStylePrompt } = + state.sdxl; if (!model) { log.error('No model found in state'); @@ -98,6 +94,10 @@ export const buildCanvasSDXLOutpaintGraph = ( ? shouldUseCpuNoise : shouldUseCpuNoise; + // Construct Style Prompt + const { craftedPositiveStylePrompt, craftedNegativeStylePrompt } = + craftSDXLStylePrompt(state, shouldConcatSDXLStylePrompt); + const graph: NonNullableGraph = { id: SDXL_CANVAS_OUTPAINT_GRAPH, nodes: { @@ -110,17 +110,13 @@ export const buildCanvasSDXLOutpaintGraph = ( type: 'sdxl_compel_prompt', id: POSITIVE_CONDITIONING, prompt: positivePrompt, - style: shouldConcatSDXLStylePrompt - ? `${positivePrompt} ${positiveStylePrompt}` - : positiveStylePrompt, + style: craftedPositiveStylePrompt, }, [NEGATIVE_CONDITIONING]: { type: 'sdxl_compel_prompt', id: NEGATIVE_CONDITIONING, prompt: negativePrompt, - style: shouldConcatSDXLStylePrompt - ? `${negativePrompt} ${negativeStylePrompt}` - : negativeStylePrompt, + style: craftedNegativeStylePrompt, }, [MASK_FROM_ALPHA]: { type: 'tomask', diff --git a/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildCanvasSDXLTextToImageGraph.ts b/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildCanvasSDXLTextToImageGraph.ts index da27ace3b0..e79e08ba41 100644 --- a/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildCanvasSDXLTextToImageGraph.ts +++ b/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildCanvasSDXLTextToImageGraph.ts @@ -24,6 +24,7 @@ import { SDXL_DENOISE_LATENTS, SDXL_MODEL_LOADER, } from './constants'; +import { craftSDXLStylePrompt } from './helpers/craftSDXLStylePrompt'; /** * Builds the Canvas tab's Text to Image graph. @@ -50,13 +51,8 @@ export const buildCanvasSDXLTextToImageGraph = ( const { shouldAutoSave } = state.canvas; - const { - positiveStylePrompt, - negativeStylePrompt, - shouldConcatSDXLStylePrompt, - shouldUseSDXLRefiner, - refinerStart, - } = state.sdxl; + const { shouldUseSDXLRefiner, refinerStart, shouldConcatSDXLStylePrompt } = + state.sdxl; if (!model) { log.error('No model found in state'); @@ -97,6 +93,11 @@ export const buildCanvasSDXLTextToImageGraph = ( denoising_start: 0, denoising_end: shouldUseSDXLRefiner ? refinerStart : 1, }; + + // Construct Style Prompt + const { craftedPositiveStylePrompt, craftedNegativeStylePrompt } = + craftSDXLStylePrompt(state, shouldConcatSDXLStylePrompt); + /** * The easiest way to build linear graphs is to do it in the node editor, then copy and paste the * full graph here as a template. Then use the parameters from app state and set friendlier node @@ -122,18 +123,14 @@ export const buildCanvasSDXLTextToImageGraph = ( id: POSITIVE_CONDITIONING, is_intermediate: true, prompt: positivePrompt, - style: shouldConcatSDXLStylePrompt - ? `${positivePrompt} ${positiveStylePrompt}` - : positiveStylePrompt, + style: craftedPositiveStylePrompt, }, [NEGATIVE_CONDITIONING]: { type: isUsingOnnxModel ? 'prompt_onnx' : 'sdxl_compel_prompt', id: NEGATIVE_CONDITIONING, is_intermediate: true, prompt: negativePrompt, - style: shouldConcatSDXLStylePrompt - ? `${negativePrompt} ${negativeStylePrompt}` - : negativeStylePrompt, + style: craftedNegativeStylePrompt, }, [NOISE]: { type: 'noise', diff --git a/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildLinearSDXLImageToImageGraph.ts b/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildLinearSDXLImageToImageGraph.ts index 0d30fe1c63..42ea07c923 100644 --- a/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildLinearSDXLImageToImageGraph.ts +++ b/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildLinearSDXLImageToImageGraph.ts @@ -25,6 +25,7 @@ import { SDXL_IMAGE_TO_IMAGE_GRAPH, SDXL_MODEL_LOADER, } from './constants'; +import { craftSDXLStylePrompt } from './helpers/craftSDXLStylePrompt'; /** * Builds the Image to Image tab graph. @@ -82,6 +83,10 @@ export const buildLinearSDXLImageToImageGraph = ( ? shouldUseCpuNoise : initialGenerationState.shouldUseCpuNoise; + // Construct Style Prompt + const { craftedPositiveStylePrompt, craftedNegativeStylePrompt } = + craftSDXLStylePrompt(state, shouldConcatSDXLStylePrompt); + // copy-pasted graph from node editor, filled in with state values & friendly node ids const graph: NonNullableGraph = { id: SDXL_IMAGE_TO_IMAGE_GRAPH, @@ -95,17 +100,13 @@ export const buildLinearSDXLImageToImageGraph = ( type: 'sdxl_compel_prompt', id: POSITIVE_CONDITIONING, prompt: positivePrompt, - style: shouldConcatSDXLStylePrompt - ? `${positivePrompt} ${positiveStylePrompt}` - : positiveStylePrompt, + style: craftedPositiveStylePrompt, }, [NEGATIVE_CONDITIONING]: { type: 'sdxl_compel_prompt', id: NEGATIVE_CONDITIONING, prompt: negativePrompt, - style: shouldConcatSDXLStylePrompt - ? `${negativePrompt} ${negativeStylePrompt}` - : negativeStylePrompt, + style: craftedNegativeStylePrompt, }, [NOISE]: { type: 'noise', diff --git a/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildLinearSDXLTextToImageGraph.ts b/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildLinearSDXLTextToImageGraph.ts index 595b6f47cd..a74884f23b 100644 --- a/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildLinearSDXLTextToImageGraph.ts +++ b/invokeai/frontend/web/src/features/nodes/util/graphBuilders/buildLinearSDXLTextToImageGraph.ts @@ -19,6 +19,7 @@ import { SDXL_MODEL_LOADER, SDXL_TEXT_TO_IMAGE_GRAPH, } from './constants'; +import { craftSDXLStylePrompt } from './helpers/craftSDXLStylePrompt'; export const buildLinearSDXLTextToImageGraph = ( state: RootState @@ -42,8 +43,8 @@ export const buildLinearSDXLTextToImageGraph = ( const { positiveStylePrompt, negativeStylePrompt, - shouldConcatSDXLStylePrompt, shouldUseSDXLRefiner, + shouldConcatSDXLStylePrompt, refinerStart, } = state.sdxl; @@ -56,6 +57,10 @@ export const buildLinearSDXLTextToImageGraph = ( throw new Error('No model found in state'); } + // Construct Style Prompt + const { craftedPositiveStylePrompt, craftedNegativeStylePrompt } = + craftSDXLStylePrompt(state, shouldConcatSDXLStylePrompt); + /** * The easiest way to build linear graphs is to do it in the node editor, then copy and paste the * full graph here as a template. Then use the parameters from app state and set friendlier node @@ -78,17 +83,13 @@ export const buildLinearSDXLTextToImageGraph = ( type: 'sdxl_compel_prompt', id: POSITIVE_CONDITIONING, prompt: positivePrompt, - style: shouldConcatSDXLStylePrompt - ? `${positivePrompt} ${positiveStylePrompt}` - : positiveStylePrompt, + style: craftedPositiveStylePrompt, }, [NEGATIVE_CONDITIONING]: { type: 'sdxl_compel_prompt', id: NEGATIVE_CONDITIONING, prompt: negativePrompt, - style: shouldConcatSDXLStylePrompt - ? `${negativePrompt} ${negativeStylePrompt}` - : negativeStylePrompt, + style: craftedNegativeStylePrompt, }, [NOISE]: { type: 'noise', diff --git a/invokeai/frontend/web/src/features/nodes/util/graphBuilders/helpers/craftSDXLStylePrompt.ts b/invokeai/frontend/web/src/features/nodes/util/graphBuilders/helpers/craftSDXLStylePrompt.ts new file mode 100644 index 0000000000..f46d5cc5dc --- /dev/null +++ b/invokeai/frontend/web/src/features/nodes/util/graphBuilders/helpers/craftSDXLStylePrompt.ts @@ -0,0 +1,28 @@ +import { RootState } from 'app/store/store'; + +export const craftSDXLStylePrompt = ( + state: RootState, + shouldConcatSDXLStylePrompt: boolean +) => { + const { positivePrompt, negativePrompt } = state.generation; + const { positiveStylePrompt, negativeStylePrompt } = state.sdxl; + + let craftedPositiveStylePrompt = positiveStylePrompt; + let craftedNegativeStylePrompt = negativeStylePrompt; + + if (shouldConcatSDXLStylePrompt) { + if (positiveStylePrompt.length > 0) { + craftedPositiveStylePrompt = `${positivePrompt} ${positiveStylePrompt}`; + } else { + craftedPositiveStylePrompt = positivePrompt; + } + + if (negativeStylePrompt.length > 0) { + craftedNegativeStylePrompt = `${negativePrompt} ${negativeStylePrompt}`; + } else { + craftedNegativeStylePrompt = negativePrompt; + } + } + + return { craftedPositiveStylePrompt, craftedNegativeStylePrompt }; +};