Spaces:
Running
on
A10G
Running
on
A10G
Update app.py
Browse files
app.py
CHANGED
@@ -13,6 +13,7 @@ import numpy as np
|
|
13 |
import cv2
|
14 |
|
15 |
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
|
|
16 |
controlnet = ControlNetModel.from_pretrained(
|
17 |
"diffusers/controlnet-canny-sdxl-1.0",
|
18 |
torch_dtype=torch.float16
|
@@ -26,21 +27,15 @@ pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
|
26 |
variant="fp16",
|
27 |
use_safetensors=True
|
28 |
)
|
|
|
29 |
pipe.to("cuda")
|
30 |
-
generator = torch.Generator(device="cuda")
|
31 |
|
32 |
-
#pipe.enable_model_cpu_offload()
|
33 |
|
34 |
-
def infer(use_custom_model, model_name, custom_lora_weight, image_in, prompt, negative_prompt, preprocessor, controlnet_conditioning_scale, guidance_scale, seed):
|
35 |
-
if use_custom_model:
|
36 |
-
custom_model = model_name
|
37 |
|
38 |
-
|
39 |
-
pipe.load_lora_weights(custom_model, use_auth_token=True)
|
40 |
-
|
41 |
-
prompt = prompt
|
42 |
-
negative_prompt = negative_prompt
|
43 |
|
|
|
|
|
44 |
if preprocessor == "canny":
|
45 |
|
46 |
image = load_image(image_in)
|
@@ -50,6 +45,16 @@ def infer(use_custom_model, model_name, custom_lora_weight, image_in, prompt, ne
|
|
50 |
image = image[:, :, None]
|
51 |
image = np.concatenate([image, image, image], axis=2)
|
52 |
image = Image.fromarray(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
if use_custom_model:
|
55 |
lora_scale=custom_lora_weight
|
@@ -60,8 +65,8 @@ def infer(use_custom_model, model_name, custom_lora_weight, image_in, prompt, ne
|
|
60 |
image=image,
|
61 |
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
62 |
guidance_scale = guidance_scale,
|
63 |
-
num_inference_steps=
|
64 |
-
generator=generator
|
65 |
cross_attention_kwargs={"scale": lora_scale}
|
66 |
).images
|
67 |
else:
|
@@ -71,8 +76,8 @@ def infer(use_custom_model, model_name, custom_lora_weight, image_in, prompt, ne
|
|
71 |
image=image,
|
72 |
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
73 |
guidance_scale = guidance_scale,
|
74 |
-
num_inference_steps=
|
75 |
-
generator=generator
|
76 |
).images
|
77 |
|
78 |
images[0].save(f"result.png")
|
@@ -88,9 +93,9 @@ css="""
|
|
88 |
"""
|
89 |
with gr.Blocks(css=css) as demo:
|
90 |
with gr.Column(elem_id="col-container"):
|
91 |
-
gr.
|
92 |
-
|
93 |
-
Use StableDiffusion XL with
|
94 |
|
95 |
""")
|
96 |
|
@@ -100,6 +105,7 @@ Use StableDiffusion XL with ControlNet pretrained LoRas
|
|
100 |
prompt = gr.Textbox(label="Prompt")
|
101 |
negative_prompt = gr.Textbox(label="Negative prompt", value="extra digit, fewer digits, cropped, worst quality, low quality, glitch, deformed, mutated, ugly, disfigured")
|
102 |
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=7.5)
|
|
|
103 |
with gr.Column():
|
104 |
preprocessor = gr.Dropdown(label="Preprocessor", choices=["canny"], value="canny", interactive=False, info="For the moment, only canny is available")
|
105 |
controlnet_conditioning_scale = gr.Slider(label="Controlnet conditioning Scale", minimum=0.1, maximum=0.9, step=0.01, value=0.5, type="float")
|
@@ -113,7 +119,7 @@ Use StableDiffusion XL with ControlNet pretrained LoRas
|
|
113 |
|
114 |
submit_btn.click(
|
115 |
fn = infer,
|
116 |
-
inputs = [use_custom_model, model_name, custom_lora_weight, image_in, prompt, negative_prompt, preprocessor, controlnet_conditioning_scale, guidance_scale, seed],
|
117 |
outputs = [result]
|
118 |
)
|
119 |
|
|
|
13 |
import cv2
|
14 |
|
15 |
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
16 |
+
|
17 |
controlnet = ControlNetModel.from_pretrained(
|
18 |
"diffusers/controlnet-canny-sdxl-1.0",
|
19 |
torch_dtype=torch.float16
|
|
|
27 |
variant="fp16",
|
28 |
use_safetensors=True
|
29 |
)
|
30 |
+
|
31 |
pipe.to("cuda")
|
|
|
32 |
|
|
|
33 |
|
|
|
|
|
|
|
34 |
|
35 |
+
#pipe.enable_model_cpu_offload()
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
def infer(use_custom_model, model_name, custom_lora_weight, image_in, prompt, negative_prompt, preprocessor, controlnet_conditioning_scale, guidance_scale, steps, seed, progress=gr.Progress(track_tqdm=True)):
|
38 |
+
|
39 |
if preprocessor == "canny":
|
40 |
|
41 |
image = load_image(image_in)
|
|
|
45 |
image = image[:, :, None]
|
46 |
image = np.concatenate([image, image, image], axis=2)
|
47 |
image = Image.fromarray(image)
|
48 |
+
|
49 |
+
if use_custom_model:
|
50 |
+
custom_model = model_name
|
51 |
+
|
52 |
+
# This is where you load your trained weights
|
53 |
+
pipe.load_lora_weights(custom_model, use_auth_token=True)
|
54 |
+
|
55 |
+
prompt = prompt
|
56 |
+
negative_prompt = negative_prompt
|
57 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
58 |
|
59 |
if use_custom_model:
|
60 |
lora_scale=custom_lora_weight
|
|
|
65 |
image=image,
|
66 |
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
67 |
guidance_scale = guidance_scale,
|
68 |
+
num_inference_steps=steps,
|
69 |
+
generator=generator,
|
70 |
cross_attention_kwargs={"scale": lora_scale}
|
71 |
).images
|
72 |
else:
|
|
|
76 |
image=image,
|
77 |
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
78 |
guidance_scale = guidance_scale,
|
79 |
+
num_inference_steps=steps,
|
80 |
+
generator=generator,
|
81 |
).images
|
82 |
|
83 |
images[0].save(f"result.png")
|
|
|
93 |
"""
|
94 |
with gr.Blocks(css=css) as demo:
|
95 |
with gr.Column(elem_id="col-container"):
|
96 |
+
gr.HTML("""
|
97 |
+
<h2 style="text-align: center;>SD-XL Control LoRas</h2>
|
98 |
+
<p style="text-align: center;">Use StableDiffusion XL with <a href="https://huggingface.co/collections/diffusers/sdxl-controlnets-64f9c35846f3f06f5abe351f">Diffusers' SDXL ControlNets</a></p>
|
99 |
|
100 |
""")
|
101 |
|
|
|
105 |
prompt = gr.Textbox(label="Prompt")
|
106 |
negative_prompt = gr.Textbox(label="Negative prompt", value="extra digit, fewer digits, cropped, worst quality, low quality, glitch, deformed, mutated, ugly, disfigured")
|
107 |
guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=7.5)
|
108 |
+
steps = gr.Slider(label="Inference Steps", minimum="25", maximum="50", step=1, value=25)
|
109 |
with gr.Column():
|
110 |
preprocessor = gr.Dropdown(label="Preprocessor", choices=["canny"], value="canny", interactive=False, info="For the moment, only canny is available")
|
111 |
controlnet_conditioning_scale = gr.Slider(label="Controlnet conditioning Scale", minimum=0.1, maximum=0.9, step=0.01, value=0.5, type="float")
|
|
|
119 |
|
120 |
submit_btn.click(
|
121 |
fn = infer,
|
122 |
+
inputs = [use_custom_model, model_name, custom_lora_weight, image_in, prompt, negative_prompt, preprocessor, controlnet_conditioning_scale, guidance_scale, steps, seed],
|
123 |
outputs = [result]
|
124 |
)
|
125 |
|