Skip to content

Commit

Permalink
use controolnet and soft inpainting via api + little fixes for hires fix
Browse files Browse the repository at this point in the history
  • Loading branch information
light-and-ray committed Mar 3, 2024
1 parent 1fa0fe7 commit 27b965c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
15 changes: 15 additions & 0 deletions apiExample.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@
"positive_prompt": "waterfall",
"sam_model_name": "sam_hq_vit_h.pth",
"dino_model_name": "GroundingDINO_SwinB (938MB)",
# "scripts" has the same format with "alwayson_scripts" in /sdapi/v1/txt2img
"scripts": {
"controlnet": {
"args": [
{
"module": "openpose_full",
"model": "control_v11p_sd15_openpose [cab727d4]",
"inpaint_crop_input_image": True,
},
]
},
"soft inpainting": {
"args": [True, 1, 0.5, 4, 0, 1, 2]
},
},
}

response = requests.post(url=f'{SD_WEBUI}/replacer/replace', json=payload)
Expand Down
8 changes: 4 additions & 4 deletions replacer/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def inpaint(
batch_processed : Processed = None
):
override_settings = {}
if (gArgs.upscalerForImg2Img is not None and gArgs.upscalerForImg2Img != "" and gArgs.upscalerForImg2Img != "None"):
if gArgs.upscalerForImg2Img is not None and gArgs.upscalerForImg2Img != "" and gArgs.upscalerForImg2Img != "None":
override_settings["upscaler_for_img2img"] = gArgs.upscalerForImg2Img
if (gArgs.sd_model_checkpoint is not None and gArgs.sd_model_checkpoint != ""):
if gArgs.sd_model_checkpoint is not None and gArgs.sd_model_checkpoint != "":
override_settings["sd_model_checkpoint"] = gArgs.sd_model_checkpoint
override_settings["img2img_fix_steps"] = gArgs.img2img_fix_steps

Expand Down Expand Up @@ -87,7 +87,7 @@ def inpaint(
p.do_not_save_grid = True
try:
if replacer_scripts.script_controlnet and gArgs.cn_args is not None and len(gArgs.cn_args) != 0:
replacer_scripts.enableInpaintModeForCN(gArgs.cn_args, p)
replacer_scripts.enableInpaintModeForCN(gArgs, p)
except Exception as e:
errors.report(f"Error {e}", exc_info=True)
replacer_scripts.applyScripts(p, gArgs.cn_args, gArgs.soft_inpaint_args)
Expand Down Expand Up @@ -543,7 +543,7 @@ def applyHiresFix(
if hf_negativePrompt != "":
hrArgs.negativePrompt = hf_negativePrompt
if hf_sd_model_checkpoint is not None and hf_sd_model_checkpoint != 'Use same model'\
and hf_sd_model_checkpoint != "":
and hf_sd_model_checkpoint != 'Use same checkpoint' and hf_sd_model_checkpoint != "":
hrArgs.sd_model_checkpoint = hf_sd_model_checkpoint
hrArgs.inpaint_full_res_padding += hf_extra_inpaint_padding
hrArgs.mask_blur += hf_extra_mask_blur
Expand Down
56 changes: 41 additions & 15 deletions replacer/replacer_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
# --- ControlNet ----

try:
from lib_controlnet.external_code import ResizeMode
from lib_controlnet import external_code
IS_SD_WEBUI_FORGE = True
except:
ResizeMode = None
external_code = None
IS_SD_WEBUI_FORGE = False

script_controlnet : scripts.Script = None
ControlNetUiGroup = None


def initCNScript():
global script_controlnet, ControlNetUiGroup
global script_controlnet, ControlNetUiGroup, external_code
cnet_idx = None
for idx, script in enumerate(scripts.scripts_img2img.alwayson_scripts):
if script.title().lower() == "controlnet":
Expand All @@ -33,6 +34,7 @@ def initCNScript():
try:
if not IS_SD_WEBUI_FORGE:
from scripts.controlnet_ui.controlnet_ui_group import ControlNetUiGroup
from scripts import external_code
else:
from lib_controlnet.controlnet_ui.controlnet_ui_group import ControlNetUiGroup
except:
Expand Down Expand Up @@ -69,15 +71,13 @@ def restoreAfterCN(origImage, gArgs: GenerationArgs, processed):
processed.images[i] = imageOrg


def initResizeMode():
global ResizeMode
from internal_controlnet.external_code import ResizeMode


def enableInpaintModeForCN(controlNetUnits, p):
def enableInpaintModeForCN(gArgs, p):
global external_code
mask = None

for controlNetUnit in controlNetUnits:
gArgs.cn_args = list(gArgs.cn_args)
for i in range(len(gArgs.cn_args)):
gArgs.cn_args[i] = external_code.to_processing_unit(gArgs.cn_args[i])
controlNetUnit = gArgs.cn_args[i]
if not controlNetUnit.enabled:
continue

Expand All @@ -87,8 +87,6 @@ def enableInpaintModeForCN(controlNetUnits, p):
if p.inpainting_mask_invert:
mask = ImageChops.invert(mask)
mask = applyMaskBlur(mask, p.mask_blur)
if ResizeMode is None:
initResizeMode()

print('Use cn inpaint instead of sd inpaint')
image = limitSizeByOneDemention(p.init_images[0], max(p.width, p.height))
Expand All @@ -100,7 +98,7 @@ def enableInpaintModeForCN(controlNetUnits, p):
p.inpaint_full_res = False
p.width, p.height = image.size
controlNetUnit.inpaint_crop_input_image = False
controlNetUnit.resize_mode = ResizeMode.RESIZE
controlNetUnit.resize_mode = external_code.ResizeMode.RESIZE
p.needRestoreAfterCN = True


Expand Down Expand Up @@ -216,8 +214,11 @@ def initAllScripts():

def prepareScriptsArgs(scripts_args):
global script_controlnet, script_soft_inpaint
result = []

if len(scripts_args) > 0 and scripts_args[0] == 'args_from_api':
return scripts_args[1:]

result = []
lastIndex = 0

if script_controlnet:
Expand Down Expand Up @@ -265,3 +266,28 @@ def applyScripts(p, cn_args, soft_inpaint_args):
for i in range(len(soft_inpaint_args)):
p.script_args[script_soft_inpaint.args_from + i] = soft_inpaint_args[i]


def prepareScriptsArgs_api(scriptsApi : dict):
global script_controlnet, script_soft_inpaint
cn_args = []
soft_inpaint_args = []

for scriptApi in scriptsApi.items():
if scriptApi[0] == script_controlnet.name:
cn_args = scriptApi[1]["args"]
continue
if scriptApi[0] == script_soft_inpaint.name:
soft_inpaint_args = scriptApi[1]["args"]
continue
return ['args_from_api', cn_args, soft_inpaint_args]


def getAvaliableScripts_api():
global script_controlnet, script_soft_inpaint
result = []
if script_controlnet:
result.append(script_controlnet.name)
if script_soft_inpaint:
result.append(script_soft_inpaint.name)
return result

2 changes: 1 addition & 1 deletion replacer/tab_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def getHiresFixCheckpoints():
if IS_WEBUI_1_5:
return ["Use same checkpoint"] + modules.sd_models.checkpoint_tiles()
else:
return ["Use same checkpoint"] + modules.sd_models.checkpoint_tiles(use_short=True)
return ["Use same checkpoint"] + modules.sd_models.checkpoint_tiles(use_short=False)


def update_mask_brush_color(color):
Expand Down
5 changes: 4 additions & 1 deletion scripts/replacer_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from modules import shared
from modules.api.api import encode_pil_to_base64, decode_base64_to_image
from replacer.generate import generate
from replacer import replacer_scripts



Expand Down Expand Up @@ -43,6 +44,7 @@ class ReplaceRequest(BaseModel):
fix_steps : bool = False
inpainting_fill : int = 0
sd_model_checkpoint : str = ""
scripts : dict = {}


@app.post("/replacer/replace")
Expand All @@ -56,7 +58,7 @@ async def api_replacer_replace(data: ReplaceRequest = Body(...)) -> Any:
data.max_resolution_on_detection, data.sam_model_name, data.dino_model_name, data.cfg_scale,
data.denoise, data.inpaint_padding, data.inpainting_fill, data.width, data.height, 1, 1,
data.inpainting_mask_invert, [], data.fix_steps, True, data.sd_model_checkpoint, 'Random', [], None,
False, [], None, False, None,
False, [], None, False, None, *replacer_scripts.prepareScriptsArgs_api(data.scripts)
)[0][0]

return {"image": encode_pil_to_base64(result).decode()}
Expand All @@ -69,6 +71,7 @@ async def api_replacer_avaliable_options() -> Any:
"dino_model_name": dino_model_list,
"upscalers": [""] + [x.name for x in shared.sd_upscalers],
"lama_cleaner_avaliable": lama_cleaner_avaliable, # inpainting_fill=4, https://github.com/light-and-ray/sd-webui-lama-cleaner-masked-content
"avaliable_scripts": replacer_scripts.getAvaliableScripts_api(),
}


Expand Down

0 comments on commit 27b965c

Please sign in to comment.