Spaces:
Sleeping
Sleeping
Upload pipeline.py
Browse files- pipeline.py +48 -0
pipeline.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
from diffusers.models.autoencoders.autoencoder_kl import AutoencoderKL
|
| 5 |
+
from diffusers.models.unets.unet_2d_condition import UNet2DConditionModel
|
| 6 |
+
from diffusers.models.controlnets.controlnet import ControlNetModel
|
| 7 |
+
from diffusers.pipelines.controlnet.pipeline_controlnet import StableDiffusionControlNetPipeline
|
| 8 |
+
from diffusers.schedulers.scheduling_unipc_multistep import UniPCMultistepScheduler
|
| 9 |
+
|
| 10 |
+
from transformers import CLIPTextModel, CLIPTokenizer
|
| 11 |
+
|
| 12 |
+
def prep_control_image(cond_values: torch.Tensor, device: torch.device) -> torch.Tensor:
|
| 13 |
+
x = cond_values
|
| 14 |
+
if x.min() < 0:
|
| 15 |
+
x = (x * 0.5 + 0.5).clamp(0, 1)
|
| 16 |
+
x = x.to(device=device, dtype=torch.float32)
|
| 17 |
+
return x
|
| 18 |
+
|
| 19 |
+
def build_controlnet_pipe(
|
| 20 |
+
base_model_name: str,
|
| 21 |
+
controlnet: ControlNetModel,
|
| 22 |
+
vae: AutoencoderKL,
|
| 23 |
+
unet: UNet2DConditionModel,
|
| 24 |
+
text_encoder: CLIPTextModel,
|
| 25 |
+
tokenizer: CLIPTokenizer,
|
| 26 |
+
device: torch.device,
|
| 27 |
+
weight_dtype: torch.dtype,
|
| 28 |
+
use_unipc: bool = True,
|
| 29 |
+
|
| 30 |
+
) -> StableDiffusionControlNetPipeline:
|
| 31 |
+
|
| 32 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 33 |
+
base_model_name,
|
| 34 |
+
vae=vae,
|
| 35 |
+
text_encoder=text_encoder,
|
| 36 |
+
tokenizer=tokenizer,
|
| 37 |
+
unet=unet,
|
| 38 |
+
controlnet=controlnet,
|
| 39 |
+
safety_checker=None,
|
| 40 |
+
torch_dtype=weight_dtype,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
if use_unipc:
|
| 44 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
| 45 |
+
|
| 46 |
+
pipe = pipe.to(device)
|
| 47 |
+
pipe.set_progress_bar_config(disable=True)
|
| 48 |
+
return pipe
|