Spaces:
Runtime error
Runtime error
Adding FLUX schnell
Browse files
app.py
CHANGED
|
@@ -5,13 +5,46 @@ import os
|
|
| 5 |
import spaces #[uncomment to use ZeroGPU]
|
| 6 |
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
| 8 |
-
from diffusers import DiffusionPipeline, UNet2DConditionModel, LCMScheduler
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
from safetensors.torch import load_file
|
| 11 |
import sys
|
| 12 |
sys.path.append('.')
|
| 13 |
from utils.lora import LoRANetwork, DEFAULT_TARGET_REPLACE, UNET_TARGET_REPLACE_MODULE_CONV
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
model_repo_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 16 |
repo_name = "tianweiy/DMD2"
|
| 17 |
ckpt_name = "dmd2_sdxl_4step_unet_fp16.bin"
|
|
@@ -28,7 +61,7 @@ unet = UNet2DConditionModel.from_config(model_repo_id, subfolder="unet").to(devi
|
|
| 28 |
unet.load_state_dict(torch.load(hf_hub_download(repo_name, ckpt_name)))
|
| 29 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, unet=unet, torch_dtype=torch_dtype).to(device)
|
| 30 |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
| 31 |
-
|
| 32 |
|
| 33 |
unet = pipe.unet
|
| 34 |
|
|
@@ -55,6 +88,39 @@ MAX_SEED = np.iinfo(np.int32).max
|
|
| 55 |
MAX_IMAGE_SIZE = 1024
|
| 56 |
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
@spaces.GPU #[uncomment to use ZeroGPU]
|
| 59 |
def infer(
|
| 60 |
prompt,
|
|
@@ -68,47 +134,77 @@ def infer(
|
|
| 68 |
slider_space,
|
| 69 |
discovered_directions,
|
| 70 |
slider_scale,
|
|
|
|
| 71 |
progress=gr.Progress(track_tqdm=True),
|
| 72 |
):
|
| 73 |
if randomize_seed:
|
| 74 |
seed = random.randint(0, MAX_SEED)
|
| 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 |
-
# edited image
|
| 100 |
-
generator = torch.Generator().manual_seed(seed)
|
| 101 |
-
with networks[0]:
|
| 102 |
-
slider_image = pipe(
|
| 103 |
-
prompt=prompt,
|
| 104 |
-
negative_prompt=negative_prompt,
|
| 105 |
-
guidance_scale=guidance_scale,
|
| 106 |
-
num_inference_steps=num_inference_steps,
|
| 107 |
-
width=width,
|
| 108 |
-
height=height,
|
| 109 |
-
generator=generator,
|
| 110 |
-
).images[0]
|
| 111 |
-
|
| 112 |
|
| 113 |
return image, slider_image, seed
|
| 114 |
|
|
@@ -153,35 +249,18 @@ with gr.Blocks(css=css) as demo:
|
|
| 153 |
|
| 154 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 155 |
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
# New dropdowns side by side
|
| 158 |
with gr.Row():
|
| 159 |
slider_space = gr.Dropdown(
|
| 160 |
-
choices=
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
"animal",
|
| 164 |
-
"bike",
|
| 165 |
-
"car",
|
| 166 |
-
"Citadel",
|
| 167 |
-
"coral",
|
| 168 |
-
"cowboy",
|
| 169 |
-
"face",
|
| 170 |
-
"futuristic cities",
|
| 171 |
-
"monster",
|
| 172 |
-
"mystical creature",
|
| 173 |
-
"planet",
|
| 174 |
-
"plant",
|
| 175 |
-
"robot",
|
| 176 |
-
"sculpture",
|
| 177 |
-
"spaceship",
|
| 178 |
-
"statue",
|
| 179 |
-
"studio",
|
| 180 |
-
"video game",
|
| 181 |
-
"wizard"
|
| 182 |
-
],
|
| 183 |
-
label="SliderSpace",
|
| 184 |
-
value="spaceship"
|
| 185 |
)
|
| 186 |
discovered_directions = gr.Dropdown(
|
| 187 |
choices=[f"Slider {i}" for i in range(1, 11)],
|
|
@@ -253,7 +332,12 @@ with gr.Blocks(css=css) as demo:
|
|
| 253 |
step=1,
|
| 254 |
value=4, # Replace with defaults that work for your model
|
| 255 |
)
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
# gr.Examples(examples=examples, inputs=[prompt])
|
| 258 |
gr.on(
|
| 259 |
triggers=[run_button.click, prompt.submit],
|
|
@@ -269,10 +353,306 @@ with gr.Blocks(css=css) as demo:
|
|
| 269 |
num_inference_steps,
|
| 270 |
slider_space,
|
| 271 |
discovered_directions,
|
| 272 |
-
slider_scale
|
|
|
|
| 273 |
],
|
| 274 |
outputs=[result, slider_result, seed],
|
| 275 |
)
|
| 276 |
|
| 277 |
if __name__ == "__main__":
|
| 278 |
-
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import spaces #[uncomment to use ZeroGPU]
|
| 6 |
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
| 8 |
+
from diffusers import DiffusionPipeline, UNet2DConditionModel, LCMScheduler, AutoencoderTiny
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
from safetensors.torch import load_file
|
| 11 |
import sys
|
| 12 |
sys.path.append('.')
|
| 13 |
from utils.lora import LoRANetwork, DEFAULT_TARGET_REPLACE, UNET_TARGET_REPLACE_MODULE_CONV
|
| 14 |
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Model configurations
|
| 18 |
+
SDXL_CONCEPTS = [
|
| 19 |
+
"alien", "ancient ruins", "animal", "bike", "car", "Citadel",
|
| 20 |
+
"coral", "cowboy", "face", "futuristic cities", "monster",
|
| 21 |
+
"mystical creature", "planet", "plant", "robot", "sculpture",
|
| 22 |
+
"spaceship", "statue", "studio", "video game", "wizard"
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
FLUX_CONCEPTS = [
|
| 26 |
+
"alien",
|
| 27 |
+
"ancient ruins",
|
| 28 |
+
"animal",
|
| 29 |
+
"bike",
|
| 30 |
+
"car",
|
| 31 |
+
"Citadel",
|
| 32 |
+
"face",
|
| 33 |
+
"futuristic cities",
|
| 34 |
+
"mystical creature",
|
| 35 |
+
"planet",
|
| 36 |
+
"plant",
|
| 37 |
+
"robot",
|
| 38 |
+
"spaceship",
|
| 39 |
+
"statue",
|
| 40 |
+
"studio",
|
| 41 |
+
"video game",
|
| 42 |
+
"wizard"
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
model_repo_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 49 |
repo_name = "tianweiy/DMD2"
|
| 50 |
ckpt_name = "dmd2_sdxl_4step_unet_fp16.bin"
|
|
|
|
| 61 |
unet.load_state_dict(torch.load(hf_hub_download(repo_name, ckpt_name)))
|
| 62 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, unet=unet, torch_dtype=torch_dtype).to(device)
|
| 63 |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
| 64 |
+
pipe.vae = AutoencoderTiny.from_pretrained("madebyollin/taesdxl", torch_dtype=torch_dtype).to(devoce)
|
| 65 |
|
| 66 |
unet = pipe.unet
|
| 67 |
|
|
|
|
| 88 |
MAX_IMAGE_SIZE = 1024
|
| 89 |
|
| 90 |
|
| 91 |
+
base_model_id = "black-forest-labs/FLUX.1-schnell"
|
| 92 |
+
max_sequence_length = 256
|
| 93 |
+
flux_pipe = FluxPipeline.from_pretrained(base_model_id, torch_dtype=torch_dtype)
|
| 94 |
+
flux_pipe.vae = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=torch_dtype)
|
| 95 |
+
flux_pipe = flux_pipe.to(device)
|
| 96 |
+
# pipe.enable_sequential_cpu_offload()
|
| 97 |
+
transformer = flux_pipe.transformer
|
| 98 |
+
|
| 99 |
+
## Change these parameters based on how you trained your sliderspace sliders
|
| 100 |
+
train_method = 'flux-attn'
|
| 101 |
+
rank = 1
|
| 102 |
+
alpha =1
|
| 103 |
+
|
| 104 |
+
flux_networks = {}
|
| 105 |
+
modules = DEFAULT_TARGET_REPLACE
|
| 106 |
+
modules += UNET_TARGET_REPLACE_MODULE_CONV
|
| 107 |
+
for i in range(numsliders_to_sample):
|
| 108 |
+
flux_networks[i] = LoRANetwork(
|
| 109 |
+
transformer,
|
| 110 |
+
rank=int(rank),
|
| 111 |
+
multiplier=1.0,
|
| 112 |
+
alpha=int(alpha),
|
| 113 |
+
train_method=train_method,
|
| 114 |
+
fast_init=True,
|
| 115 |
+
).to(device, dtype=torch_dtype)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def update_sliderspace_choices(model_choice):
|
| 119 |
+
return gr.Dropdown.update(
|
| 120 |
+
choices=SDXL_CONCEPTS if model_choice == "SDXL-DMD" else FLUX_CONCEPTS,
|
| 121 |
+
value=SDXL_CONCEPTS[0] if model_choice == "SDXL-DMD" else FLUX_CONCEPTS[0]
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
@spaces.GPU #[uncomment to use ZeroGPU]
|
| 125 |
def infer(
|
| 126 |
prompt,
|
|
|
|
| 134 |
slider_space,
|
| 135 |
discovered_directions,
|
| 136 |
slider_scale,
|
| 137 |
+
model_choice,
|
| 138 |
progress=gr.Progress(track_tqdm=True),
|
| 139 |
):
|
| 140 |
if randomize_seed:
|
| 141 |
seed = random.randint(0, MAX_SEED)
|
| 142 |
|
| 143 |
+
if model_choice == 'SDXL-DMD':
|
| 144 |
+
sliderspace_path = f"sliderspace_weights/{slider_space}/slider_{int(discovered_directions.split(' ')[-1])-1}.pt"
|
| 145 |
+
|
| 146 |
+
for net in networks:
|
| 147 |
+
networks[net].load_state_dict(torch.load(sliderspace_path))
|
| 148 |
+
networks[net].set_lora_slider(slider_scale)
|
| 149 |
+
with networks[0]:
|
| 150 |
+
pass
|
| 151 |
+
|
| 152 |
+
# original image
|
| 153 |
+
generator = torch.Generator().manual_seed(seed)
|
| 154 |
+
image = pipe(
|
| 155 |
+
prompt=prompt,
|
| 156 |
+
negative_prompt=negative_prompt,
|
| 157 |
+
guidance_scale=guidance_scale,
|
| 158 |
+
num_inference_steps=num_inference_steps,
|
| 159 |
+
width=width,
|
| 160 |
+
height=height,
|
| 161 |
+
generator=generator,
|
| 162 |
+
).images[0]
|
| 163 |
|
| 164 |
+
# edited image
|
| 165 |
+
generator = torch.Generator().manual_seed(seed)
|
| 166 |
+
with networks[0]:
|
| 167 |
+
slider_image = pipe(
|
| 168 |
+
prompt=prompt,
|
| 169 |
+
negative_prompt=negative_prompt,
|
| 170 |
+
guidance_scale=guidance_scale,
|
| 171 |
+
num_inference_steps=num_inference_steps,
|
| 172 |
+
width=width,
|
| 173 |
+
height=height,
|
| 174 |
+
generator=generator,
|
| 175 |
+
).images[0]
|
| 176 |
+
else:
|
| 177 |
+
sliderspace_path = f"flux_sliderspace_weights/{slider_space}/slider_{int(discovered_directions.split(' ')[-1])-1}.pt"
|
| 178 |
+
for net in flux_networks:
|
| 179 |
+
flux_networks[net].load_state_dict(torch.load(sliderspace_path))
|
| 180 |
+
flux_networks[net].set_lora_slider(slider_scale)
|
| 181 |
+
with flux_networks[0]:
|
| 182 |
+
pass
|
| 183 |
+
|
| 184 |
+
# original image
|
| 185 |
+
generator = torch.Generator().manual_seed(seed)
|
| 186 |
+
image = flux_pipe(
|
| 187 |
+
prompt=prompt,
|
| 188 |
+
guidance_scale=guidance_scale,
|
| 189 |
+
num_inference_steps=num_inference_steps,
|
| 190 |
+
width=width,
|
| 191 |
+
height=height,
|
| 192 |
+
generator=generator,
|
| 193 |
+
max_sequence_length = 256,
|
| 194 |
+
).images[0]
|
| 195 |
|
| 196 |
+
# edited image
|
| 197 |
+
generator = torch.Generator().manual_seed(seed)
|
| 198 |
+
with flux_networks[0]:
|
| 199 |
+
slider_image = flux_pipe(
|
| 200 |
+
prompt=prompt,
|
| 201 |
+
guidance_scale=guidance_scale,
|
| 202 |
+
num_inference_steps=num_inference_steps,
|
| 203 |
+
width=width,
|
| 204 |
+
height=height,
|
| 205 |
+
generator=generator,
|
| 206 |
+
max_sequence_length = 256,
|
| 207 |
+
).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
return image, slider_image, seed
|
| 210 |
|
|
|
|
| 249 |
|
| 250 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 251 |
|
| 252 |
+
# Add model selection dropdown
|
| 253 |
+
model_choice = gr.Dropdown(
|
| 254 |
+
choices=["SDXL-DMD", "FLUX-Schnell"],
|
| 255 |
+
label="Model",
|
| 256 |
+
value="SDXL-DMD"
|
| 257 |
+
)
|
| 258 |
# New dropdowns side by side
|
| 259 |
with gr.Row():
|
| 260 |
slider_space = gr.Dropdown(
|
| 261 |
+
choices=SDXL_CONCEPTS,
|
| 262 |
+
label="SliderSpace Concept",
|
| 263 |
+
value=SDXL_CONCEPTS[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
)
|
| 265 |
discovered_directions = gr.Dropdown(
|
| 266 |
choices=[f"Slider {i}" for i in range(1, 11)],
|
|
|
|
| 332 |
step=1,
|
| 333 |
value=4, # Replace with defaults that work for your model
|
| 334 |
)
|
| 335 |
+
# Add event handler for model selection
|
| 336 |
+
model_choice.change(
|
| 337 |
+
fn=update_sliderspace_choices,
|
| 338 |
+
inputs=[model_choice],
|
| 339 |
+
outputs=[slider_space]
|
| 340 |
+
)
|
| 341 |
# gr.Examples(examples=examples, inputs=[prompt])
|
| 342 |
gr.on(
|
| 343 |
triggers=[run_button.click, prompt.submit],
|
|
|
|
| 353 |
num_inference_steps,
|
| 354 |
slider_space,
|
| 355 |
discovered_directions,
|
| 356 |
+
slider_scale,
|
| 357 |
+
model_choice
|
| 358 |
],
|
| 359 |
outputs=[result, slider_result, seed],
|
| 360 |
)
|
| 361 |
|
| 362 |
if __name__ == "__main__":
|
| 363 |
+
demo.launch(share=True)
|
| 364 |
+
|
| 365 |
+
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
|
| 372 |
+
|
| 373 |
+
|
| 374 |
+
|
| 375 |
+
|
| 376 |
+
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
|
| 381 |
+
# import gradio as gr
|
| 382 |
+
# import numpy as np
|
| 383 |
+
# import random
|
| 384 |
+
# import os
|
| 385 |
+
# import spaces #[uncomment to use ZeroGPU]
|
| 386 |
+
# from diffusers import DiffusionPipeline
|
| 387 |
+
# import torch
|
| 388 |
+
# from diffusers import DiffusionPipeline, UNet2DConditionModel, LCMScheduler
|
| 389 |
+
# from huggingface_hub import hf_hub_download
|
| 390 |
+
# from safetensors.torch import load_file
|
| 391 |
+
# import sys
|
| 392 |
+
# sys.path.append('.')
|
| 393 |
+
# from utils.lora import LoRANetwork, DEFAULT_TARGET_REPLACE, UNET_TARGET_REPLACE_MODULE_CONV
|
| 394 |
+
|
| 395 |
+
# model_repo_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 396 |
+
# repo_name = "tianweiy/DMD2"
|
| 397 |
+
# ckpt_name = "dmd2_sdxl_4step_unet_fp16.bin"
|
| 398 |
+
|
| 399 |
+
|
| 400 |
+
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 401 |
+
# if torch.cuda.is_available():
|
| 402 |
+
# torch_dtype = torch.bfloat16
|
| 403 |
+
# else:
|
| 404 |
+
# torch_dtype = torch.float32
|
| 405 |
+
|
| 406 |
+
# # Load model.
|
| 407 |
+
# unet = UNet2DConditionModel.from_config(model_repo_id, subfolder="unet").to(device, torch_dtype)
|
| 408 |
+
# unet.load_state_dict(torch.load(hf_hub_download(repo_name, ckpt_name)))
|
| 409 |
+
# pipe = DiffusionPipeline.from_pretrained(model_repo_id, unet=unet, torch_dtype=torch_dtype).to(device)
|
| 410 |
+
# pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
| 411 |
+
|
| 412 |
+
|
| 413 |
+
# unet = pipe.unet
|
| 414 |
+
|
| 415 |
+
# ## Change these parameters based on how you trained your sliderspace sliders
|
| 416 |
+
# train_method = 'xattn-strict'
|
| 417 |
+
# rank = 1
|
| 418 |
+
# alpha =1
|
| 419 |
+
# networks = {}
|
| 420 |
+
# modules = DEFAULT_TARGET_REPLACE
|
| 421 |
+
# modules += UNET_TARGET_REPLACE_MODULE_CONV
|
| 422 |
+
# for i in range(1):
|
| 423 |
+
# networks[i] = LoRANetwork(
|
| 424 |
+
# unet,
|
| 425 |
+
# rank=int(rank),
|
| 426 |
+
# multiplier=1.0,
|
| 427 |
+
# alpha=int(alpha),
|
| 428 |
+
# train_method=train_method,
|
| 429 |
+
# fast_init=True,
|
| 430 |
+
# ).to(device, dtype=torch_dtype)
|
| 431 |
+
|
| 432 |
+
|
| 433 |
+
|
| 434 |
+
# MAX_SEED = np.iinfo(np.int32).max
|
| 435 |
+
# MAX_IMAGE_SIZE = 1024
|
| 436 |
+
|
| 437 |
+
|
| 438 |
+
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 439 |
+
# def infer(
|
| 440 |
+
# prompt,
|
| 441 |
+
# negative_prompt,
|
| 442 |
+
# seed,
|
| 443 |
+
# randomize_seed,
|
| 444 |
+
# width,
|
| 445 |
+
# height,
|
| 446 |
+
# guidance_scale,
|
| 447 |
+
# num_inference_steps,
|
| 448 |
+
# slider_space,
|
| 449 |
+
# discovered_directions,
|
| 450 |
+
# slider_scale,
|
| 451 |
+
# progress=gr.Progress(track_tqdm=True),
|
| 452 |
+
# ):
|
| 453 |
+
# if randomize_seed:
|
| 454 |
+
# seed = random.randint(0, MAX_SEED)
|
| 455 |
+
|
| 456 |
+
# sliderspace_path = f"sliderspace_weights/{slider_space}/slider_{int(discovered_directions.split(' ')[-1])-1}.pt"
|
| 457 |
+
|
| 458 |
+
# for net in networks:
|
| 459 |
+
# networks[net].load_state_dict(torch.load(sliderspace_path))
|
| 460 |
+
|
| 461 |
+
# for net in networks:
|
| 462 |
+
# networks[net].set_lora_slider(slider_scale)
|
| 463 |
+
|
| 464 |
+
# with networks[0]:
|
| 465 |
+
# pass
|
| 466 |
+
|
| 467 |
+
# # original image
|
| 468 |
+
# generator = torch.Generator().manual_seed(seed)
|
| 469 |
+
# image = pipe(
|
| 470 |
+
# prompt=prompt,
|
| 471 |
+
# negative_prompt=negative_prompt,
|
| 472 |
+
# guidance_scale=guidance_scale,
|
| 473 |
+
# num_inference_steps=num_inference_steps,
|
| 474 |
+
# width=width,
|
| 475 |
+
# height=height,
|
| 476 |
+
# generator=generator,
|
| 477 |
+
# ).images[0]
|
| 478 |
+
|
| 479 |
+
# # edited image
|
| 480 |
+
# generator = torch.Generator().manual_seed(seed)
|
| 481 |
+
# with networks[0]:
|
| 482 |
+
# slider_image = pipe(
|
| 483 |
+
# prompt=prompt,
|
| 484 |
+
# negative_prompt=negative_prompt,
|
| 485 |
+
# guidance_scale=guidance_scale,
|
| 486 |
+
# num_inference_steps=num_inference_steps,
|
| 487 |
+
# width=width,
|
| 488 |
+
# height=height,
|
| 489 |
+
# generator=generator,
|
| 490 |
+
# ).images[0]
|
| 491 |
+
|
| 492 |
+
|
| 493 |
+
# return image, slider_image, seed
|
| 494 |
+
|
| 495 |
+
|
| 496 |
+
# examples = [
|
| 497 |
+
# "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 498 |
+
# "An astronaut riding a green horse",
|
| 499 |
+
# "A delicious ceviche cheesecake slice",
|
| 500 |
+
# ]
|
| 501 |
+
|
| 502 |
+
# css = """
|
| 503 |
+
# #col-container {
|
| 504 |
+
# margin: 0 auto;
|
| 505 |
+
# max-width: 640px;
|
| 506 |
+
# }
|
| 507 |
+
# """
|
| 508 |
+
|
| 509 |
+
# ORIGINAL_SPACE_ID = 'baulab/SliderSpace'
|
| 510 |
+
# SPACE_ID = os.getenv('SPACE_ID')
|
| 511 |
+
|
| 512 |
+
# SHARED_UI_WARNING = f'''## You can duplicate and use it with a gpu with at least 24GB, or clone this repository to run on your own machine.
|
| 513 |
+
# <center><a class="duplicate-button" style="display:inline-block" target="_blank" href="https://huggingface.co/spaces/{SPACE_ID}?duplicate=true"><img style="margin-top:0;margin-bottom:0" src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></center>
|
| 514 |
+
# '''
|
| 515 |
+
|
| 516 |
+
# with gr.Blocks(css=css) as demo:
|
| 517 |
+
# with gr.Column(elem_id="col-container"):
|
| 518 |
+
# gr.Markdown(" # SliderSpace: Decomposing Visual Capabilities of Diffusion Models")
|
| 519 |
+
# # Adding links under the title
|
| 520 |
+
# gr.Markdown("""
|
| 521 |
+
# 🔗 [Project Page](https://sliderspace.baulab.info) |
|
| 522 |
+
# 💻 [GitHub Code](https://github.com/rohitgandikota/sliderspace)
|
| 523 |
+
# """)
|
| 524 |
+
|
| 525 |
+
# with gr.Row():
|
| 526 |
+
# prompt = gr.Text(
|
| 527 |
+
# label="Prompt",
|
| 528 |
+
# show_label=False,
|
| 529 |
+
# max_lines=1,
|
| 530 |
+
# placeholder="Enter your prompt",
|
| 531 |
+
# container=False,
|
| 532 |
+
# )
|
| 533 |
+
|
| 534 |
+
# run_button = gr.Button("Run", scale=0, variant="primary")
|
| 535 |
+
|
| 536 |
+
|
| 537 |
+
# # New dropdowns side by side
|
| 538 |
+
# with gr.Row():
|
| 539 |
+
# slider_space = gr.Dropdown(
|
| 540 |
+
# choices= [
|
| 541 |
+
# "alien",
|
| 542 |
+
# "ancient ruins",
|
| 543 |
+
# "animal",
|
| 544 |
+
# "bike",
|
| 545 |
+
# "car",
|
| 546 |
+
# "Citadel",
|
| 547 |
+
# "coral",
|
| 548 |
+
# "cowboy",
|
| 549 |
+
# "face",
|
| 550 |
+
# "futuristic cities",
|
| 551 |
+
# "monster",
|
| 552 |
+
# "mystical creature",
|
| 553 |
+
# "planet",
|
| 554 |
+
# "plant",
|
| 555 |
+
# "robot",
|
| 556 |
+
# "sculpture",
|
| 557 |
+
# "spaceship",
|
| 558 |
+
# "statue",
|
| 559 |
+
# "studio",
|
| 560 |
+
# "video game",
|
| 561 |
+
# "wizard"
|
| 562 |
+
# ],
|
| 563 |
+
# label="SliderSpace",
|
| 564 |
+
# value="spaceship"
|
| 565 |
+
# )
|
| 566 |
+
# discovered_directions = gr.Dropdown(
|
| 567 |
+
# choices=[f"Slider {i}" for i in range(1, 11)],
|
| 568 |
+
# label="Discovered Directions",
|
| 569 |
+
# value="Slider 1"
|
| 570 |
+
# )
|
| 571 |
+
|
| 572 |
+
# slider_scale = gr.Slider(
|
| 573 |
+
# label="Slider Scale",
|
| 574 |
+
# minimum=-4,
|
| 575 |
+
# maximum=4,
|
| 576 |
+
# step=0.1,
|
| 577 |
+
# value=1,
|
| 578 |
+
# )
|
| 579 |
+
|
| 580 |
+
# with gr.Row():
|
| 581 |
+
# result = gr.Image(label="Original Image", show_label=True)
|
| 582 |
+
# slider_result = gr.Image(label="Discovered Edit Direction", show_label=True)
|
| 583 |
+
|
| 584 |
+
|
| 585 |
+
# with gr.Accordion("Advanced Settings", open=False):
|
| 586 |
+
# negative_prompt = gr.Text(
|
| 587 |
+
# label="Negative prompt",
|
| 588 |
+
# max_lines=1,
|
| 589 |
+
# placeholder="Enter a negative prompt",
|
| 590 |
+
# visible=False,
|
| 591 |
+
# )
|
| 592 |
+
|
| 593 |
+
# seed = gr.Slider(
|
| 594 |
+
# label="Seed",
|
| 595 |
+
# minimum=0,
|
| 596 |
+
# maximum=MAX_SEED,
|
| 597 |
+
# step=1,
|
| 598 |
+
# value=0,
|
| 599 |
+
# )
|
| 600 |
+
|
| 601 |
+
# randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 602 |
+
|
| 603 |
+
# with gr.Row():
|
| 604 |
+
# width = gr.Slider(
|
| 605 |
+
# label="Width",
|
| 606 |
+
# minimum=256,
|
| 607 |
+
# maximum=MAX_IMAGE_SIZE,
|
| 608 |
+
# step=32,
|
| 609 |
+
# value=1024, # Replace with defaults that work for your model
|
| 610 |
+
# )
|
| 611 |
+
|
| 612 |
+
# height = gr.Slider(
|
| 613 |
+
# label="Height",
|
| 614 |
+
# minimum=256,
|
| 615 |
+
# maximum=MAX_IMAGE_SIZE,
|
| 616 |
+
# step=32,
|
| 617 |
+
# value=1024, # Replace with defaults that work for your model
|
| 618 |
+
# )
|
| 619 |
+
|
| 620 |
+
# with gr.Row():
|
| 621 |
+
# guidance_scale = gr.Slider(
|
| 622 |
+
# label="Guidance scale",
|
| 623 |
+
# minimum=0.0,
|
| 624 |
+
# maximum=2.0,
|
| 625 |
+
# step=0.1,
|
| 626 |
+
# value=0.0, # Replace with defaults that work for your model
|
| 627 |
+
# )
|
| 628 |
+
|
| 629 |
+
# num_inference_steps = gr.Slider(
|
| 630 |
+
# label="Number of inference steps",
|
| 631 |
+
# minimum=1,
|
| 632 |
+
# maximum=50,
|
| 633 |
+
# step=1,
|
| 634 |
+
# value=4, # Replace with defaults that work for your model
|
| 635 |
+
# )
|
| 636 |
+
|
| 637 |
+
# # gr.Examples(examples=examples, inputs=[prompt])
|
| 638 |
+
# gr.on(
|
| 639 |
+
# triggers=[run_button.click, prompt.submit],
|
| 640 |
+
# fn=infer,
|
| 641 |
+
# inputs=[
|
| 642 |
+
# prompt,
|
| 643 |
+
# negative_prompt,
|
| 644 |
+
# seed,
|
| 645 |
+
# randomize_seed,
|
| 646 |
+
# width,
|
| 647 |
+
# height,
|
| 648 |
+
# guidance_scale,
|
| 649 |
+
# num_inference_steps,
|
| 650 |
+
# slider_space,
|
| 651 |
+
# discovered_directions,
|
| 652 |
+
# slider_scale
|
| 653 |
+
# ],
|
| 654 |
+
# outputs=[result, slider_result, seed],
|
| 655 |
+
# )
|
| 656 |
+
|
| 657 |
+
# if __name__ == "__main__":
|
| 658 |
+
# demo.launch(share=True)
|