Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from diffusers import ControlNetModel
|
|
| 7 |
import torch
|
| 8 |
from PIL import Image
|
| 9 |
from rembg import remove
|
|
|
|
| 10 |
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
if torch.cuda.is_available():
|
|
@@ -54,7 +55,11 @@ def infer(
|
|
| 54 |
ip_adapter_enabled=False,
|
| 55 |
ip_adapter_scale=0.0,
|
| 56 |
ip_adapter_image=None,
|
|
|
|
|
|
|
|
|
|
| 57 |
progress=gr.Progress(track_tqdm=True),
|
|
|
|
| 58 |
):
|
| 59 |
control_strength=float(control_strength)
|
| 60 |
if randomize_seed:
|
|
@@ -69,7 +74,7 @@ def infer(
|
|
| 69 |
if controlnet_enabled and control_image:
|
| 70 |
controlnet_model = ControlNetModel.from_pretrained(CONTROLNET_MODES.get(control_mode))
|
| 71 |
if model_id == "SD1.5 + lora Unet TextEncoder":
|
| 72 |
-
pipe=StableDiffusionControlNetPipeline.from_pretrained("stable-
|
| 73 |
pipe.unet = PeftModel.from_pretrained(pipe.unet, "um235/vCat_v2", subfolder="unet")
|
| 74 |
pipe.text_encoder = PeftModel.from_pretrained(pipe.text_encoder, "um235/vCat_v2", subfolder="text_encoder")
|
| 75 |
elif model_id == "SD1.5 + lora Unet TextEncoder" or model_id == "SD1.5 + lora Unet":
|
|
@@ -93,7 +98,7 @@ def infer(
|
|
| 93 |
pipe.set_ip_adapter_scale(ip_adapter_scale)
|
| 94 |
|
| 95 |
pipe.safety_checker = None
|
| 96 |
-
|
| 97 |
pipe = pipe.to(device)
|
| 98 |
|
| 99 |
image = pipe(
|
|
@@ -108,12 +113,13 @@ def infer(
|
|
| 108 |
cross_attention_kwargs={"scale": lscale},
|
| 109 |
controlnet_conditioning_scale=control_strength,
|
| 110 |
ip_adapter_image=ip_adapter_image,
|
|
|
|
| 111 |
).images[0]
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
|
| 116 |
-
|
| 117 |
return image, seed
|
| 118 |
|
| 119 |
|
|
@@ -207,7 +213,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 207 |
minimum=0.0,
|
| 208 |
maximum=2.0,
|
| 209 |
step=0.05,
|
| 210 |
-
value=
|
| 211 |
visible=False,
|
| 212 |
)
|
| 213 |
|
|
@@ -295,6 +301,9 @@ with gr.Blocks(css=css) as demo:
|
|
| 295 |
ip_adapter_enabled,
|
| 296 |
ip_adapter_scale,
|
| 297 |
ip_adapter_image,
|
|
|
|
|
|
|
|
|
|
| 298 |
],
|
| 299 |
outputs=[result, seed],
|
| 300 |
)
|
|
@@ -310,7 +319,5 @@ with gr.Blocks(css=css) as demo:
|
|
| 310 |
inputs=[ip_adapter_enabled],
|
| 311 |
outputs=[ip_adapter_scale, ip_adapter_image],
|
| 312 |
)
|
| 313 |
-
|
| 314 |
-
|
| 315 |
if __name__ == "__main__":
|
| 316 |
demo.launch()
|
|
|
|
| 7 |
import torch
|
| 8 |
from PIL import Image
|
| 9 |
from rembg import remove
|
| 10 |
+
from diffusers import DiffusionPipeline, DDIMScheduler
|
| 11 |
|
| 12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
if torch.cuda.is_available():
|
|
|
|
| 55 |
ip_adapter_enabled=False,
|
| 56 |
ip_adapter_scale=0.0,
|
| 57 |
ip_adapter_image=None,
|
| 58 |
+
d_bckg=False,
|
| 59 |
+
ddim_use=False,
|
| 60 |
+
distill_vae=False,
|
| 61 |
progress=gr.Progress(track_tqdm=True),
|
| 62 |
+
|
| 63 |
):
|
| 64 |
control_strength=float(control_strength)
|
| 65 |
if randomize_seed:
|
|
|
|
| 74 |
if controlnet_enabled and control_image:
|
| 75 |
controlnet_model = ControlNetModel.from_pretrained(CONTROLNET_MODES.get(control_mode))
|
| 76 |
if model_id == "SD1.5 + lora Unet TextEncoder":
|
| 77 |
+
pipe=StableDiffusionControlNetPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5",controlnet=controlnet_model)
|
| 78 |
pipe.unet = PeftModel.from_pretrained(pipe.unet, "um235/vCat_v2", subfolder="unet")
|
| 79 |
pipe.text_encoder = PeftModel.from_pretrained(pipe.text_encoder, "um235/vCat_v2", subfolder="text_encoder")
|
| 80 |
elif model_id == "SD1.5 + lora Unet TextEncoder" or model_id == "SD1.5 + lora Unet":
|
|
|
|
| 98 |
pipe.set_ip_adapter_scale(ip_adapter_scale)
|
| 99 |
|
| 100 |
pipe.safety_checker = None
|
| 101 |
+
if ddim_use: pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config, rescale_betas_zero_snr=True)
|
| 102 |
pipe = pipe.to(device)
|
| 103 |
|
| 104 |
image = pipe(
|
|
|
|
| 113 |
cross_attention_kwargs={"scale": lscale},
|
| 114 |
controlnet_conditioning_scale=control_strength,
|
| 115 |
ip_adapter_image=ip_adapter_image,
|
| 116 |
+
|
| 117 |
).images[0]
|
| 118 |
|
| 119 |
+
if d_bckg:
|
| 120 |
+
image=remove(image)
|
| 121 |
|
| 122 |
+
|
| 123 |
return image, seed
|
| 124 |
|
| 125 |
|
|
|
|
| 213 |
minimum=0.0,
|
| 214 |
maximum=2.0,
|
| 215 |
step=0.05,
|
| 216 |
+
value=0.55,
|
| 217 |
visible=False,
|
| 218 |
)
|
| 219 |
|
|
|
|
| 301 |
ip_adapter_enabled,
|
| 302 |
ip_adapter_scale,
|
| 303 |
ip_adapter_image,
|
| 304 |
+
d_bckg,
|
| 305 |
+
ddim_use,
|
| 306 |
+
distill_vae
|
| 307 |
],
|
| 308 |
outputs=[result, seed],
|
| 309 |
)
|
|
|
|
| 319 |
inputs=[ip_adapter_enabled],
|
| 320 |
outputs=[ip_adapter_scale, ip_adapter_image],
|
| 321 |
)
|
|
|
|
|
|
|
| 322 |
if __name__ == "__main__":
|
| 323 |
demo.launch()
|