Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for base64 strings when passed in input #92

Open
wants to merge 60 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
4752c02
add log
jetjodh Apr 10, 2024
5cadf36
add check for b64 strings
jetjodh Apr 10, 2024
a2c5087
added alpha layer
jetjodh Apr 10, 2024
ac0abf5
added method to one step
jetjodh May 28, 2024
d20e2ed
modify check
jetjodh May 28, 2024
4e9c8cb
added new mthod
jetjodh May 28, 2024
fa65999
add processing step
jetjodh May 29, 2024
4c91a4d
add logs
jetjodh May 29, 2024
42a84a6
add logs
jetjodh May 29, 2024
a9412e9
add missing signature
jetjodh May 29, 2024
3369050
correct use of P?
jetjodh May 29, 2024
b3a5ecd
modify to correct logic
jetjodh May 29, 2024
7c132e2
add sample call
jetjodh May 29, 2024
4f10546
new func sign
jetjodh May 29, 2024
d48f4fc
new func
jetjodh May 29, 2024
cd2f1cd
logs
jetjodh May 30, 2024
330a44f
add method switch in process
jetjodh May 30, 2024
11c2ec9
fix log
jetjodh May 30, 2024
bee343b
added new dict item
jetjodh May 30, 2024
00ddf11
move to better latent
jetjodh May 30, 2024
8409118
update lib api
jetjodh May 30, 2024
9a90381
update lib api
jetjodh May 30, 2024
9449bfa
debug steps
jetjodh May 30, 2024
4091679
debug steps
jetjodh May 30, 2024
da2eb30
add process
jetjodh May 31, 2024
0efefaa
remove reset state
jetjodh May 31, 2024
b757a62
add pass count
jetjodh May 31, 2024
423dc96
removed pass counter
jetjodh May 31, 2024
a076e08
fixes
jetjodh May 31, 2024
576f4a6
move back to counter
jetjodh May 31, 2024
1b734aa
fixed var
jetjodh May 31, 2024
4369500
added new progress stuff
jetjodh Jun 4, 2024
c5c0ed1
added new progress stuff
jetjodh Jun 4, 2024
3fe55ee
updated
jetjodh Jun 4, 2024
857c399
PIL
jetjodh Jun 4, 2024
40ccc0d
image logging
jetjodh Jun 5, 2024
f67cd86
image logging fix
jetjodh Jun 5, 2024
4e16ce4
method logs
jetjodh Jun 5, 2024
9ab2dc4
logs
jetjodh Jun 5, 2024
203bceb
log all args
jetjodh Jun 5, 2024
0ec1478
log methos
jetjodh Jun 5, 2024
828b377
fix script assignment in processing
jetjodh Jun 5, 2024
58b6641
log script args
jetjodh Jun 5, 2024
46d2336
coment redundant steps
jetjodh Jun 5, 2024
1c04006
log truncated
jetjodh Jun 5, 2024
7051367
log truncated fix
jetjodh Jun 5, 2024
12b20e9
editing script args
jetjodh Jun 5, 2024
67797b8
account for tuple
jetjodh Jun 5, 2024
7ee3a46
account for tuple
jetjodh Jun 5, 2024
76f6aaa
indexing fix
jetjodh Jun 5, 2024
34411a0
remove extra code
jetjodh Jun 5, 2024
1f7507d
add optimal ending step for FG
jetjodh Jun 5, 2024
b0639b4
remove manual repl
jetjodh Jun 5, 2024
d4683cc
print all processd items
jetjodh Jun 5, 2024
49fec4e
remove counter
jetjodh Jun 5, 2024
790696f
checking image
jetjodh Jun 5, 2024
748250d
final update
jetjodh Jun 5, 2024
aa034bc
remove debug
jetjodh Jun 5, 2024
5c03d6f
Merge pull request #1 from jetjodh/make-sdxl-workflows-one-step
jetjodh Jun 5, 2024
abc9637
remove bad api call
jetjodh Jun 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions lib_layerdiffusion/utils.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import numpy as np
from lib_layerdiffusion.enums import ResizeMode
from ldm_patched.modules import model_management
from modules.shared import device
from modules.api import api
import cv2
import torch

from PIL import Image

def forge_clip_encode(clip, text):
if text is None:
return None

tokens = clip.tokenize(text, return_word_ids=True)
cond, pooled = clip.encode_from_tokens(tokens, return_pooled=True)
return cond.to(model_management.get_torch_device())
return cond.to(device)


def rgba2rgbfp32(x):
rgb = x[..., :3].astype(np.float32) / 255.0
a = x[..., 3:4].astype(np.float32) / 255.0
return 0.5 + (rgb - 0.5) * a
if not isinstance(x, np.ndarray):
if isinstance(x, Image.Image):
x = np.array(x.convert('RGBA'))
else:
x = np.array(api.decode_base64_to_image(x).convert('RGBA'))
rgb = x[..., :3].astype(np.float32) / 255.0
a = x[..., 3:4].astype(np.float32) / 255.0
return 0.5 + (rgb - 0.5) * a
else:
return x


def to255unit8(x):
Expand Down
30 changes: 25 additions & 5 deletions scripts/forge_layerdiffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import copy

from modules import scripts
from modules.processing import StableDiffusionProcessing
from modules.processing import StableDiffusionProcessing, process_images
from lib_layerdiffusion.enums import ResizeMode
from lib_layerdiffusion.utils import rgba2rgbfp32, to255unit8, crop_and_resize_image, forge_clip_encode
from lib_layerdiffusion.utils import rgba2rgbfp32, crop_and_resize_image, forge_clip_encode
from enum import Enum
from modules.paths import models_path
from ldm_patched.modules.utils import load_torch_file
Expand All @@ -17,7 +17,6 @@
from modules_forge.forge_sampler import sampling_prepare
from modules.modelloader import load_file_from_url
from lib_layerdiffusion.attention_sharing import AttentionSharingPatcher
from ldm_patched.modules import model_management


def is_model_loaded(model):
Expand All @@ -42,6 +41,8 @@ class LayerMethod(Enum):
FG_BLEND_TO_BG = "(SDXL) From Foreground and Blending to Background"
BG_TO_BLEND = "(SDXL) From Background to Blending"
BG_BLEND_TO_FG = "(SDXL) From Background and Blending to Foreground"
BG_TO_FG = "(SDXL) From Background to Foreground"
FG_TO_BG = "(SDXL) From Foreground to Background"


@functools.lru_cache(maxsize=2)
Expand Down Expand Up @@ -78,6 +79,7 @@ def ui(self, *args, **kwargs):
resize_mode = gr.Radio(choices=[e.value for e in ResizeMode], value=ResizeMode.CROP_AND_RESIZE.value, label="Resize Mode", type='value', visible=False)
output_origin = gr.Checkbox(label='Output original mat for img2img', value=False, visible=False)


def method_changed(m):
m = LayerMethod(m)

Expand All @@ -90,10 +92,12 @@ def method_changed(m):
if m == LayerMethod.JOINT_SD15:
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)

if m == LayerMethod.FG_TO_BLEND:
if m == LayerMethod.FG_TO_BLEND or m == LayerMethod.FG_TO_BG:
m = LayerMethod.FG_TO_BLEND
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False, value=''), gr.update(visible=False, value=''), gr.update(visible=False, value='')

if m == LayerMethod.BG_TO_BLEND:
if m == LayerMethod.BG_TO_BLEND or m == LayerMethod.BG_TO_FG:
m = LayerMethod.BG_TO_BLEND
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False, value=''), gr.update(visible=False, value=''), gr.update(visible=False, value='')

if m == LayerMethod.BG_BLEND_TO_FG:
Expand All @@ -115,6 +119,12 @@ def process_before_every_sampling(self, p: StableDiffusionProcessing, *script_ar
# If you use highres fix, this will be called twice.

enabled, method, weight, ending_step, fg_image, bg_image, blend_image, resize_mode, output_origin, fg_additional_prompt, bg_additional_prompt, blend_additional_prompt = script_args
self.enabled, self.original_method, self.weight, self.ending_step, self.fg_image, self.bg_image, self.blend_image, self.resize_mode, self.output_origin, self.fg_additional_prompt, self.bg_additional_prompt, self.blend_additional_prompt = script_args

if method == LayerMethod.BG_TO_FG.value:
method = LayerMethod.BG_TO_BLEND.value
if method == LayerMethod.FG_TO_BG.value:
method = LayerMethod.FG_TO_BLEND.value

if not enabled:
return
Expand Down Expand Up @@ -357,3 +367,13 @@ def conditioning_modifier(model, x, timestep, uncond, cond, cond_scale, model_op
p.sd_model.forge_objects.unet = unet
p.sd_model.forge_objects.vae = vae
return

def postprocess_image(self, p, pp, *args):
if self.original_method in [LayerMethod.BG_TO_FG.value, LayerMethod.FG_TO_BG.value]:
script_args = (self.enabled, LayerMethod.BG_BLEND_TO_FG.value if self.original_method == LayerMethod.BG_TO_FG.value else LayerMethod.FG_BLEND_TO_BG.value, self.weight, self.ending_step if self.original_method == LayerMethod.BG_TO_FG.value else 0.5, self.fg_image, self.bg_image, pp.image, self.resize_mode, self.output_origin, self.fg_additional_prompt, self.bg_additional_prompt, self.blend_additional_prompt)
# search index for self.original_method in p.script_args_value
index = p.script_args_value.index(self.original_method)
# Replace the script arg values with the new values in script_args from one index before
p.script_args_value = p.script_args_value[:index-1] + script_args + p.script_args_value[index + len(script_args)-1:]
processed = process_images(p)
pp.image = processed.images[0]