Spaces:
Running
on
Zero
Running
on
Zero
File size: 5,837 Bytes
7d5de5f 8a98406 7d5de5f ad9f6fa 7d5de5f 47a907f 7d5de5f 47a907f 7d5de5f ad9f6fa dd2d342 d840c30 ad9f6fa 7d5de5f e70dd9f 7d5de5f 20e7c98 d840c30 7d5de5f 808dc9c 8a98406 808dc9c 8a98406 808dc9c abd6d57 808dc9c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
import torch
from diffusers.pipelines import FluxPipeline
from OminiControl.src.flux.condition import Condition
from PIL import Image
import random
from OminiControl.src.flux.generate import generate, seed_everything
print("Loading model...")
pipe = FluxPipeline.from_pretrained(
"black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16
)
pipe = pipe.to("cuda")
pipe.unload_lora_weights()
pipe.load_lora_weights(
"vzhizhi6611/OminiControlArt",
weight_name=f"v0/ghibli.safetensors",
adapter_name="ghibli",
)
pipe.load_lora_weights(
"vzhizhi6611/OminiControlArt",
weight_name=f"v0/irasutoya.safetensors",
adapter_name="irasutoya",
)
pipe.load_lora_weights(
"vzhizhi6611/OminiControlArt",
weight_name=f"v0/simpsons.safetensors",
adapter_name="simpsons",
)
pipe.load_lora_weights(
"vzhizhi6611/OminiControlArt",
weight_name=f"v0/snoopy.safetensors",
adapter_name="snoopy",
)
def generate_image(
image,
style,
inference_mode,
image_guidance,
image_ratio,
steps,
use_random_seed,
seed,
):
pipe.enable_lora()
# Prepare Condition
def resize(img, factor=16):
w, h = img.size
new_w, new_h = w // factor * factor, h // factor * factor
padding_w, padding_h = (w - new_w) // 2, (h - new_h) // 2
img = img.crop((padding_w, padding_h, new_w + padding_w, new_h + padding_h))
return img
# Set Adapter
activate_adapter_name = {
"Studio Ghibli": "ghibli",
"Irasutoya Illustration": "irasutoya",
"The Simpsons": "simpsons",
"Snoopy": "snoopy",
}[style]
pipe.set_adapters(activate_adapter_name)
original_width, original_height = image.size
factor = 512 / max(image.size)
image = resize(
image.resize(
(int(image.size[0] * factor), int(image.size[1] * factor)),
Image.LANCZOS,
)
)
delta = -image.size[0] // 16
condition = Condition(
"subject",
# activate_adapter_name,
image,
position_delta=(0, delta),
)
# Prepare seed
if use_random_seed:
seed = random.randint(0, 2**32 - 1)
seed_everything(seed)
# Image guidance scale
image_guidance = 1.0 if inference_mode == "Fast" else image_guidance
# Output size
if image_ratio == "Auto":
r = image.size[0] / image.size[1]
ratio = min([0.67, 1, 1.5], key=lambda x: abs(x - r))
else:
ratio = {
"Square(1:1)": 1,
"Portrait(2:3)": 0.67,
"Landscape(3:2)": 1.5,
}[image_ratio]
width, height = {
0.67: (640, 960),
1: (640, 640),
1.5: (960, 640),
}[ratio]
output_factor = max(width, height) / max(original_width, original_height)
width = int(original_width * output_factor)
height = int(original_height * output_factor)
print(
f"Image Ratio: {image_ratio}, Inference Mode: {inference_mode}, Image Guidance: {image_guidance}, Seed: {seed}, Steps: {steps}, Ratio: {ratio}, Size: {width}x{height}"
)
# Generate
result_img = generate(
pipe,
prompt="",
conditions=[condition],
num_inference_steps=steps,
width=width,
height=height,
image_guidance_scale=image_guidance,
default_lora=True,
max_sequence_length=32,
).images[0]
# result_img = image
result_img = result_img.resize((width, height), Image.LANCZOS)
return result_img
def generate_image_with_prompt(
image,
prompt,
inference_mode,
image_guidance,
image_ratio,
steps,
use_random_seed,
seed,
):
pipe.disable_lora()
# Prepare Condition
def resize(img, factor=16):
w, h = img.size
new_w, new_h = w // factor * factor, h // factor * factor
padding_w, padding_h = (w - new_w) // 2, (h - new_h) // 2
img = img.crop((padding_w, padding_h, new_w + padding_w, new_h + padding_h))
return img
original_width, original_height = image.size
factor = 512 / max(image.size)
image = resize(
image.resize(
(int(image.size[0] * factor), int(image.size[1] * factor)),
Image.LANCZOS,
)
)
delta = -image.size[0] // 16
condition = Condition(
"subject",
# activate_adapter_name,
image,
position_delta=(0, delta),
)
# Prepare seed
if use_random_seed:
seed = random.randint(0, 2**32 - 1)
seed_everything(seed)
# Image guidance scale
image_guidance = 1.0 if inference_mode == "Fast" else image_guidance
# Output size
if image_ratio == "Auto":
r = image.size[0] / image.size[1]
ratio = min([0.67, 1, 1.5], key=lambda x: abs(x - r))
else:
ratio = {
"Square(1:1)": 1,
"Portrait(2:3)": 0.67,
"Landscape(3:2)": 1.5,
}[image_ratio]
width, height = {
0.67: (640, 960),
1: (640, 640),
1.5: (960, 640),
}[ratio]
output_factor = max(width, height) / max(original_width, original_height)
width = int(original_width * output_factor)
height = int(original_height * output_factor)
print(
f"Image Ratio: {image_ratio}, Inference Mode: {inference_mode}, Image Guidance: {image_guidance}, Seed: {seed}, Steps: {steps}, Ratio: {ratio}, Size: {width}x{height}"
)
# Generate
result_img = generate(
pipe,
prompt=prompt,
conditions=[condition],
num_inference_steps=steps,
width=width,
height=height,
image_guidance_scale=image_guidance,
default_lora=True,
max_sequence_length=32,
).images[0]
# result_img = image
result_img = result_img.resize((width, height), Image.LANCZOS)
return result_img
|