Spaces:
Runtime error
Runtime error
add tile controlnet
Browse files
app.py
CHANGED
|
@@ -14,12 +14,9 @@ from diffusers.models import SD3ControlNetModel, SD3MultiControlNetModel
|
|
| 14 |
from diffusers.utils import load_image
|
| 15 |
|
| 16 |
# load pipeline
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
controlnet=controlnet
|
| 21 |
-
)
|
| 22 |
-
pipe.to("cuda", torch.float16)
|
| 23 |
|
| 24 |
def resize_image(input_path, output_path, target_height):
|
| 25 |
# Open the input image
|
|
@@ -41,34 +38,57 @@ def resize_image(input_path, output_path, target_height):
|
|
| 41 |
return output_path, new_width, target_height
|
| 42 |
|
| 43 |
@spaces.GPU(duration=90)
|
| 44 |
-
def
|
| 45 |
|
| 46 |
n_prompt = 'NSFW, nude, naked, porn, ugly'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# infer
|
| 58 |
image = pipe(
|
| 59 |
prompt=prompt,
|
| 60 |
negative_prompt=n_prompt,
|
| 61 |
-
control_image=
|
| 62 |
controlnet_conditioning_scale=control_weight,
|
| 63 |
num_inference_steps=inference_steps,
|
| 64 |
guidance_scale=guidance_scale,
|
| 65 |
).images[0]
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
image = image.resize((w, h), Image.LANCZOS)
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
css="""
|
| 74 |
#col-container{
|
|
@@ -82,28 +102,40 @@ with gr.Blocks(css=css) as demo:
|
|
| 82 |
# SD3 ControlNet
|
| 83 |
|
| 84 |
Experiment with Stable Diffusion 3 ControlNet models proposed and maintained by the InstantX team.<br />
|
| 85 |
-
Model Card: [InstantX/SD3-Controlnet-Canny](https://huggingface.co/InstantX/SD3-Controlnet-Canny)
|
| 86 |
-
|
| 87 |
""")
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
| 107 |
outputs = [result, canny_used],
|
| 108 |
show_api=False
|
| 109 |
)
|
|
|
|
| 14 |
from diffusers.utils import load_image
|
| 15 |
|
| 16 |
# load pipeline
|
| 17 |
+
controlnet_canny = SD3ControlNetModel.from_pretrained("InstantX/SD3-Controlnet-Canny")
|
| 18 |
+
controlne_tile = SD3ControlNetModel.from_pretrained("InstantX/SD3-Controlnet-Tile")
|
| 19 |
+
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def resize_image(input_path, output_path, target_height):
|
| 22 |
# Open the input image
|
|
|
|
| 38 |
return output_path, new_width, target_height
|
| 39 |
|
| 40 |
@spaces.GPU(duration=90)
|
| 41 |
+
def infer_canny(image_in, prompt, control_type, inference_steps, guidance_scale, control_weight, progress=gr.Progress(track_tqdm=True)):
|
| 42 |
|
| 43 |
n_prompt = 'NSFW, nude, naked, porn, ugly'
|
| 44 |
+
|
| 45 |
+
if control_type == "canny":
|
| 46 |
+
pipe = StableDiffusion3ControlNetPipeline.from_pretrained(
|
| 47 |
+
"stabilityai/stable-diffusion-3-medium-diffusers",
|
| 48 |
+
controlnet=controlnet_canny
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Canny preprocessing
|
| 52 |
+
image_to_canny = load_image(image_in)
|
| 53 |
+
image_to_canny = np.array(image_to_canny)
|
| 54 |
+
image_to_canny = cv2.Canny(image_to_canny, 100, 200)
|
| 55 |
+
image_to_canny = image_to_canny[:, :, None]
|
| 56 |
+
image_to_canny = np.concatenate([image_to_canny, image_to_canny, image_to_canny], axis=2)
|
| 57 |
+
image_to_canny = Image.fromarray(image_to_canny)
|
| 58 |
+
|
| 59 |
+
control_image = image_to_canny
|
| 60 |
|
| 61 |
+
elif control_type == "tile":
|
| 62 |
+
pipe = StableDiffusion3ControlNetPipeline.from_pretrained(
|
| 63 |
+
"stabilityai/stable-diffusion-3-medium-diffusers",
|
| 64 |
+
controlnet=controlnet_tile
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
control_image = load_image(image_in)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
pipe.to("cuda", torch.float16)
|
| 71 |
|
| 72 |
# infer
|
| 73 |
image = pipe(
|
| 74 |
prompt=prompt,
|
| 75 |
negative_prompt=n_prompt,
|
| 76 |
+
control_image=control_image,
|
| 77 |
controlnet_conditioning_scale=control_weight,
|
| 78 |
num_inference_steps=inference_steps,
|
| 79 |
guidance_scale=guidance_scale,
|
| 80 |
).images[0]
|
| 81 |
|
| 82 |
+
if control_type == "canny":
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
image_redim, w, h = resize_image(image_in, "resized_input.jpg", 1024)
|
| 85 |
+
image = image.resize((w, h), Image.LANCZOS)
|
| 86 |
+
|
| 87 |
+
return image, gr.update(value=image_to_canny, visible=True)
|
| 88 |
+
|
| 89 |
+
elif control_type == "tile":
|
| 90 |
+
return image, gr.update(value=None, visible=False)
|
| 91 |
+
|
| 92 |
|
| 93 |
css="""
|
| 94 |
#col-container{
|
|
|
|
| 102 |
# SD3 ControlNet
|
| 103 |
|
| 104 |
Experiment with Stable Diffusion 3 ControlNet models proposed and maintained by the InstantX team.<br />
|
|
|
|
|
|
|
| 105 |
""")
|
| 106 |
+
|
| 107 |
+
with gr.Column():
|
| 108 |
+
gr.Mardown("""
|
| 109 |
+
Model Card: [InstantX/SD3-Controlnet-Canny](https://huggingface.co/InstantX/SD3-Controlnet-Canny)
|
| 110 |
+
""")
|
| 111 |
+
with gr.Row():
|
| 112 |
+
with gr.Column():
|
| 113 |
+
image_in = gr.Image(label="Image reference", sources=["upload"], type="filepath")
|
| 114 |
+
prompt = gr.Textbox(label="Prompt")
|
| 115 |
+
control_type = gr.Radio(
|
| 116 |
+
label="Control type",
|
| 117 |
+
choices = [
|
| 118 |
+
"canny",
|
| 119 |
+
"tile"
|
| 120 |
+
],
|
| 121 |
+
value="canny"
|
| 122 |
+
)
|
| 123 |
+
with gr.Accordion("Advanced settings", open=False):
|
| 124 |
+
with gr.Column():
|
| 125 |
+
with gr.Row():
|
| 126 |
+
inference_steps = gr.Slider(label="Inference steps", minimum=1, maximum=50, step=1, value=25)
|
| 127 |
+
guidance_scale = gr.Slider(label="Guidance scale", minimum=1.0, maximum=10.0, step=0.1, value=7.0)
|
| 128 |
+
control_weight = gr.Slider(label="Control Weight", minimum=0.0, maximum=1.0, step=0.01, value=0.7)
|
| 129 |
|
| 130 |
+
submit_canny_btn = gr.Button("Submit")
|
| 131 |
+
with gr.Column():
|
| 132 |
+
result = gr.Image(label="Result")
|
| 133 |
+
canny_used = gr.Image(label="Preprocessed Canny", visible=False)
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
submit_canny_btn.click(
|
| 137 |
+
fn = infer_canny,
|
| 138 |
+
inputs = [image_in, prompt, control_type, inference_steps, guidance_scale, control_weight],
|
| 139 |
outputs = [result, canny_used],
|
| 140 |
show_api=False
|
| 141 |
)
|