Spaces:
Runtime error
Runtime error
back to solo canny
Browse files
app.py
CHANGED
@@ -15,7 +15,10 @@ from diffusers.utils import load_image
|
|
15 |
|
16 |
# load pipeline
|
17 |
controlnet_canny = SD3ControlNetModel.from_pretrained("InstantX/SD3-Controlnet-Canny")
|
18 |
-
|
|
|
|
|
|
|
19 |
|
20 |
def resize_image(input_path, output_path, target_height):
|
21 |
# Open the input image
|
@@ -36,47 +39,21 @@ def resize_image(input_path, output_path, target_height):
|
|
36 |
|
37 |
return output_path, new_width, target_height
|
38 |
|
39 |
-
def show_hidden():
|
40 |
-
return gr.update(visible=True)
|
41 |
-
|
42 |
-
def load_pipeline(control_type, progress=gr.Progress(track_tqdm=True)):
|
43 |
-
global pipe_canny, pipe_tile
|
44 |
-
if control_type == "canny":
|
45 |
-
global pipe_canny
|
46 |
-
pipe_canny = StableDiffusion3ControlNetPipeline.from_pretrained(
|
47 |
-
"stabilityai/stable-diffusion-3-medium-diffusers",
|
48 |
-
controlnet=controlnet_canny
|
49 |
-
)
|
50 |
-
elif control_type == "tile":
|
51 |
-
global pipe_tile
|
52 |
-
pipe_tile = StableDiffusion3ControlNetPipeline.from_pretrained(
|
53 |
-
"stabilityai/stable-diffusion-3-medium-diffusers",
|
54 |
-
controlnet=controlnet_tile
|
55 |
-
)
|
56 |
-
return gr.update(value="pipeline ready", visible=True)
|
57 |
|
58 |
@spaces.GPU(duration=90)
|
59 |
-
def infer(image_in, prompt,
|
60 |
-
|
61 |
n_prompt = 'NSFW, nude, naked, porn, ugly'
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
image_to_canny = Image.fromarray(image_to_canny)
|
73 |
-
|
74 |
-
control_image = image_to_canny
|
75 |
-
|
76 |
-
elif control_type == "tile":
|
77 |
-
pipe = pipe_tile
|
78 |
-
pipe.to("cuda", torch.float16)
|
79 |
-
control_image = load_image(image_in)
|
80 |
|
81 |
# infer
|
82 |
image = pipe(
|
@@ -88,15 +65,11 @@ def infer(image_in, prompt, control_type, inference_steps, guidance_scale, contr
|
|
88 |
guidance_scale=guidance_scale,
|
89 |
).images[0]
|
90 |
|
91 |
-
if control_type == "canny":
|
92 |
-
|
93 |
-
image_redim, w, h = resize_image(image_in, "resized_input.jpg", 1024)
|
94 |
-
image = image.resize((w, h), Image.LANCZOS)
|
95 |
|
96 |
-
|
|
|
97 |
|
98 |
-
|
99 |
-
return image, gr.update(value=None, visible=False)
|
100 |
|
101 |
|
102 |
css="""
|
@@ -111,6 +84,7 @@ with gr.Blocks(css=css) as demo:
|
|
111 |
# SD3 ControlNet
|
112 |
|
113 |
Experiment with Stable Diffusion 3 ControlNet models proposed and maintained by the InstantX team.<br />
|
|
|
114 |
""")
|
115 |
|
116 |
with gr.Column():
|
@@ -119,14 +93,7 @@ with gr.Blocks(css=css) as demo:
|
|
119 |
with gr.Column():
|
120 |
image_in = gr.Image(label="Image reference", sources=["upload"], type="filepath")
|
121 |
prompt = gr.Textbox(label="Prompt")
|
122 |
-
|
123 |
-
label="Control type",
|
124 |
-
choices = [
|
125 |
-
"canny",
|
126 |
-
"tile"
|
127 |
-
],
|
128 |
-
value="canny"
|
129 |
-
)
|
130 |
with gr.Accordion("Advanced settings", open=False):
|
131 |
with gr.Column():
|
132 |
with gr.Row():
|
@@ -137,23 +104,14 @@ with gr.Blocks(css=css) as demo:
|
|
137 |
submit_canny_btn = gr.Button("Submit")
|
138 |
|
139 |
with gr.Column():
|
140 |
-
models = gr.Textbox(label="Plug-in pipes", visible=False)
|
141 |
result = gr.Image(label="Result")
|
142 |
canny_used = gr.Image(label="Preprocessed Canny", visible=False)
|
143 |
|
144 |
|
145 |
|
146 |
submit_canny_btn.click(
|
147 |
-
fn = show_hidden,
|
148 |
-
inputs = None,
|
149 |
-
outputs = [models]
|
150 |
-
).then(
|
151 |
-
fn = load_pipeline,
|
152 |
-
inputs = [control_type],
|
153 |
-
outputs = [models]
|
154 |
-
).then(
|
155 |
fn = infer,
|
156 |
-
inputs = [image_in, prompt,
|
157 |
outputs = [result, canny_used],
|
158 |
show_api=False
|
159 |
)
|
|
|
15 |
|
16 |
# load pipeline
|
17 |
controlnet_canny = SD3ControlNetModel.from_pretrained("InstantX/SD3-Controlnet-Canny")
|
18 |
+
pipe = StableDiffusion3ControlNetPipeline.from_pretrained(
|
19 |
+
"stabilityai/stable-diffusion-3-medium-diffusers",
|
20 |
+
controlnet=controlnet_canny
|
21 |
+
).to("cuda", torch.float16)
|
22 |
|
23 |
def resize_image(input_path, output_path, target_height):
|
24 |
# Open the input image
|
|
|
39 |
|
40 |
return output_path, new_width, target_height
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
@spaces.GPU(duration=90)
|
44 |
+
def infer(image_in, prompt, inference_steps, guidance_scale, control_weight, progress=gr.Progress(track_tqdm=True)):
|
45 |
+
|
46 |
n_prompt = 'NSFW, nude, naked, porn, ugly'
|
47 |
|
48 |
+
# Canny preprocessing
|
49 |
+
image_to_canny = load_image(image_in)
|
50 |
+
image_to_canny = np.array(image_to_canny)
|
51 |
+
image_to_canny = cv2.Canny(image_to_canny, 100, 200)
|
52 |
+
image_to_canny = image_to_canny[:, :, None]
|
53 |
+
image_to_canny = np.concatenate([image_to_canny, image_to_canny, image_to_canny], axis=2)
|
54 |
+
image_to_canny = Image.fromarray(image_to_canny)
|
55 |
+
|
56 |
+
control_image = image_to_canny
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
# infer
|
59 |
image = pipe(
|
|
|
65 |
guidance_scale=guidance_scale,
|
66 |
).images[0]
|
67 |
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
image_redim, w, h = resize_image(image_in, "resized_input.jpg", 1024)
|
70 |
+
image = image.resize((w, h), Image.LANCZOS)
|
71 |
|
72 |
+
return image, gr.update(value=image_to_canny, visible=True)
|
|
|
73 |
|
74 |
|
75 |
css="""
|
|
|
84 |
# SD3 ControlNet
|
85 |
|
86 |
Experiment with Stable Diffusion 3 ControlNet models proposed and maintained by the InstantX team.<br />
|
87 |
+
Model card: (InstantX/SD3-Controlnet-Canny)[https://huggingface.co/InstantX/SD3-Controlnet-Canny]
|
88 |
""")
|
89 |
|
90 |
with gr.Column():
|
|
|
93 |
with gr.Column():
|
94 |
image_in = gr.Image(label="Image reference", sources=["upload"], type="filepath")
|
95 |
prompt = gr.Textbox(label="Prompt")
|
96 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
with gr.Accordion("Advanced settings", open=False):
|
98 |
with gr.Column():
|
99 |
with gr.Row():
|
|
|
104 |
submit_canny_btn = gr.Button("Submit")
|
105 |
|
106 |
with gr.Column():
|
|
|
107 |
result = gr.Image(label="Result")
|
108 |
canny_used = gr.Image(label="Preprocessed Canny", visible=False)
|
109 |
|
110 |
|
111 |
|
112 |
submit_canny_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
fn = infer,
|
114 |
+
inputs = [image_in, prompt, inference_steps, guidance_scale, control_weight],
|
115 |
outputs = [result, canny_used],
|
116 |
show_api=False
|
117 |
)
|