Update app.py
Browse files
app.py
CHANGED
|
@@ -5,37 +5,33 @@ import torch
|
|
| 5 |
from PIL import Image, ImageDraw
|
| 6 |
import spaces
|
| 7 |
|
| 8 |
-
# --- alkuperäiset importit jotka sulla oli ---
|
| 9 |
from controlnet_union import ControlNetModel_Union
|
| 10 |
from pipeline_fill_sd_xl import StableDiffusionXLFillPipeline
|
| 11 |
from diffusers import UNet2DConditionModel, AutoencoderKL
|
| 12 |
|
| 13 |
-
# VAE
|
| 14 |
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
| 15 |
|
|
|
|
| 16 |
config_file = hf_hub_download("xinsir/controlnet-union-sdxl-1.0", filename="config_promax.json")
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
unet_path = hf_hub_download(repo_id="KKNTR/outpaint", filename="kkntr_outpaint.safetensors")
|
| 25 |
sd = load_file(unet_path, device="cpu")
|
| 26 |
-
|
| 27 |
-
# jos malli on 9-kanavainen, otetaan vain 4 ensimmäistä
|
| 28 |
if sd["conv_in.weight"].shape[1] == 9:
|
| 29 |
-
print("Leikataan custom unet 9→4 kanavaa")
|
| 30 |
sd["conv_in.weight"] = sd["conv_in.weight"][:, :4, :, :]
|
| 31 |
-
# conv_out on jo 4, joten ei kosketa
|
| 32 |
|
| 33 |
custom_unet = UNet2DConditionModel.from_pretrained(
|
| 34 |
"SG161222/RealVisXL_V5.0_Lightning", subfolder="unet", torch_dtype=torch.float16
|
| 35 |
)
|
| 36 |
custom_unet.load_state_dict(sd, strict=False)
|
| 37 |
|
| 38 |
-
#
|
| 39 |
pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
| 40 |
"SG161222/RealVisXL_V5.0_Lightning",
|
| 41 |
torch_dtype=torch.float16,
|
|
@@ -45,7 +41,6 @@ pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
|
| 45 |
variant="fp16",
|
| 46 |
)
|
| 47 |
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
| 48 |
-
# ----------------------------------------
|
| 49 |
|
| 50 |
|
| 51 |
def can_expand(source_width, source_height, target_width, target_height, alignment):
|
|
|
|
| 5 |
from PIL import Image, ImageDraw
|
| 6 |
import spaces
|
| 7 |
|
|
|
|
| 8 |
from controlnet_union import ControlNetModel_Union
|
| 9 |
from pipeline_fill_sd_xl import StableDiffusionXLFillPipeline
|
| 10 |
from diffusers import UNet2DConditionModel, AutoencoderKL
|
| 11 |
|
| 12 |
+
# VAE
|
| 13 |
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
| 14 |
|
| 15 |
+
# ControlNet - ALKUPERÄINEN tapa
|
| 16 |
config_file = hf_hub_download("xinsir/controlnet-union-sdxl-1.0", filename="config_promax.json")
|
| 17 |
+
config = ControlNetModel_Union.load_config(config_file)
|
| 18 |
+
controlnet = ControlNetModel_Union.from_config(config)
|
| 19 |
+
model_file = hf_hub_download("xinsir/controlnet-union-sdxl-1.0", filename="diffusion_pytorch_model_promax.safetensors")
|
| 20 |
+
state_dict = load_file(model_file)
|
| 21 |
+
controlnet.load_state_dict(state_dict)
|
| 22 |
|
| 23 |
+
# SUN UNET - leikattuna
|
| 24 |
unet_path = hf_hub_download(repo_id="KKNTR/outpaint", filename="kkntr_outpaint.safetensors")
|
| 25 |
sd = load_file(unet_path, device="cpu")
|
|
|
|
|
|
|
| 26 |
if sd["conv_in.weight"].shape[1] == 9:
|
|
|
|
| 27 |
sd["conv_in.weight"] = sd["conv_in.weight"][:, :4, :, :]
|
|
|
|
| 28 |
|
| 29 |
custom_unet = UNet2DConditionModel.from_pretrained(
|
| 30 |
"SG161222/RealVisXL_V5.0_Lightning", subfolder="unet", torch_dtype=torch.float16
|
| 31 |
)
|
| 32 |
custom_unet.load_state_dict(sd, strict=False)
|
| 33 |
|
| 34 |
+
# Pipeline - EI.to("cuda")
|
| 35 |
pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
| 36 |
"SG161222/RealVisXL_V5.0_Lightning",
|
| 37 |
torch_dtype=torch.float16,
|
|
|
|
| 41 |
variant="fp16",
|
| 42 |
)
|
| 43 |
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
|
|
|
| 44 |
|
| 45 |
|
| 46 |
def can_expand(source_width, source_height, target_width, target_height, alignment):
|