Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,6 +27,9 @@ def get_lora_sd_pipeline(
|
|
| 27 |
|
| 28 |
pipe = StableDiffusionPipeline.from_pretrained(base_model_name_or_path, torch_dtype=dtype)
|
| 29 |
pipe.unet = PeftModel.from_pretrained(pipe.unet, unet_sub_dir, adapter_name=adapter_name)
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
if os.path.exists(text_encoder_sub_dir):
|
| 32 |
pipe.text_encoder = PeftModel.from_pretrained(
|
|
@@ -36,9 +39,52 @@ def get_lora_sd_pipeline(
|
|
| 36 |
if dtype in (torch.float16, torch.bfloat16):
|
| 37 |
pipe.unet.half()
|
| 38 |
pipe.text_encoder.half()
|
| 39 |
-
|
| 40 |
return pipe
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 43 |
model_id_default = "CompVis/stable-diffusion-v1-4"
|
| 44 |
|
|
@@ -76,8 +122,8 @@ def infer(
|
|
| 76 |
generator = torch.Generator().manual_seed(seed)
|
| 77 |
|
| 78 |
params = {
|
| 79 |
-
'prompt': prompt,
|
| 80 |
-
'negative_prompt': negative_prompt,
|
| 81 |
'guidance_scale': guidance_scale,
|
| 82 |
'num_inference_steps': num_inference_steps,
|
| 83 |
'width': width,
|
|
@@ -88,9 +134,20 @@ def infer(
|
|
| 88 |
if model_id != model_id_default:
|
| 89 |
pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch_dtype)
|
| 90 |
pipe = pipe.to(device)
|
| 91 |
-
pipe.fuse_lora(lora_scale=lora_scale)
|
| 92 |
image = pipe(**params).images[0]
|
| 93 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
pipe_default.fuse_lora(lora_scale=lora_scale)
|
| 95 |
image = pipe_default(**params).images[0]
|
| 96 |
|
|
|
|
| 27 |
|
| 28 |
pipe = StableDiffusionPipeline.from_pretrained(base_model_name_or_path, torch_dtype=dtype)
|
| 29 |
pipe.unet = PeftModel.from_pretrained(pipe.unet, unet_sub_dir, adapter_name=adapter_name)
|
| 30 |
+
print(os.path.exists(unet_sub_dir))
|
| 31 |
+
print(unet_sub_dir)
|
| 32 |
+
print(dtype)
|
| 33 |
|
| 34 |
if os.path.exists(text_encoder_sub_dir):
|
| 35 |
pipe.text_encoder = PeftModel.from_pretrained(
|
|
|
|
| 39 |
if dtype in (torch.float16, torch.bfloat16):
|
| 40 |
pipe.unet.half()
|
| 41 |
pipe.text_encoder.half()
|
|
|
|
| 42 |
return pipe
|
| 43 |
|
| 44 |
+
def split_prompt(prompt, tokenizer, max_length=77):
|
| 45 |
+
tokens = tokenizer(prompt, truncation=False)["input_ids"]
|
| 46 |
+
chunks = [tokens[i:i + max_length] for i in range(0, len(tokens), max_length)]
|
| 47 |
+
return chunks
|
| 48 |
+
|
| 49 |
+
def get_prompt_embeds(prompt_chunks, text_encoder):
|
| 50 |
+
prompt_embeds = []
|
| 51 |
+
for chunk in prompt_chunks:
|
| 52 |
+
chunk_tensor = torch.tensor([chunk]).to(text_encoder.device)
|
| 53 |
+
with torch.no_grad():
|
| 54 |
+
embeds = text_encoder(chunk_tensor)[0]
|
| 55 |
+
prompt_embeds.append(embeds)
|
| 56 |
+
return torch.cat(prompt_embeds, dim=1)
|
| 57 |
+
|
| 58 |
+
def shape_alignment(prompt_embeds, negative_prompt_embeds):
|
| 59 |
+
max_length = max(prompt_embeds.shape[1], negative_prompt_embeds.shape[1])
|
| 60 |
+
|
| 61 |
+
def pad_to_max_length(tensor, target_length):
|
| 62 |
+
padding = target_length - tensor.shape[1]
|
| 63 |
+
if padding > 0:
|
| 64 |
+
pad_tensor = torch.zeros(
|
| 65 |
+
tensor.shape[0], padding, tensor.shape[2], device=tensor.device
|
| 66 |
+
)
|
| 67 |
+
tensor = torch.cat([tensor, pad_tensor], dim=1)
|
| 68 |
+
return tensor
|
| 69 |
+
|
| 70 |
+
prompt_embeds = pad_to_max_length(prompt_embeds, max_length)
|
| 71 |
+
negative_prompt_embeds = pad_to_max_length(negative_prompt_embeds, max_length)
|
| 72 |
+
|
| 73 |
+
assert prompt_embeds.shape == negative_prompt_embeds.shape, "Shapes do not match!"
|
| 74 |
+
return prompt_embeds, negative_prompt_embeds
|
| 75 |
+
|
| 76 |
+
def prompts_embeddings(prompt, negative_promt, tokenizer, text_encoder):
|
| 77 |
+
prompt_chunks = split_prompt(prompt, tokenizer)
|
| 78 |
+
negative_prompt_chunks = split_prompt(negative_prompt, tokenizer)
|
| 79 |
+
|
| 80 |
+
prompt_embeds = get_prompt_embeds(prompt_chunks, text_encoder)
|
| 81 |
+
negative_prompt_embeds = get_prompt_embeds(negative_prompt_chunks, text_encoder)
|
| 82 |
+
|
| 83 |
+
prompt_embeds, negative_prompt_embeds = shape_alignment(prompt_embeds, negative_prompt_embeds)
|
| 84 |
+
|
| 85 |
+
return prompt_embeds, negative_prompt_embeds
|
| 86 |
+
|
| 87 |
+
|
| 88 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 89 |
model_id_default = "CompVis/stable-diffusion-v1-4"
|
| 90 |
|
|
|
|
| 122 |
generator = torch.Generator().manual_seed(seed)
|
| 123 |
|
| 124 |
params = {
|
| 125 |
+
# 'prompt': prompt,
|
| 126 |
+
# 'negative_prompt': negative_prompt,
|
| 127 |
'guidance_scale': guidance_scale,
|
| 128 |
'num_inference_steps': num_inference_steps,
|
| 129 |
'width': width,
|
|
|
|
| 134 |
if model_id != model_id_default:
|
| 135 |
pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch_dtype)
|
| 136 |
pipe = pipe.to(device)
|
|
|
|
| 137 |
image = pipe(**params).images[0]
|
| 138 |
else:
|
| 139 |
+
print('----')
|
| 140 |
+
print(lora_scale)
|
| 141 |
+
print(prompt)
|
| 142 |
+
print(negative_prompt)
|
| 143 |
+
prompt_embeds, negative_prompt_embeds = prompts_embeddings(
|
| 144 |
+
prompt,
|
| 145 |
+
negative_prompt,
|
| 146 |
+
pipe_default.tokenizer,
|
| 147 |
+
pipe_default.text_encoder
|
| 148 |
+
)
|
| 149 |
+
params['prompt_embeds'] = prompt_embeds
|
| 150 |
+
params['negative_prompt_embeds']=negative_prompt_embeds
|
| 151 |
pipe_default.fuse_lora(lora_scale=lora_scale)
|
| 152 |
image = pipe_default(**params).images[0]
|
| 153 |
|