primerz commited on
Commit
26e46a4
·
verified ·
1 Parent(s): 44e46b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -91
app.py CHANGED
@@ -173,6 +173,23 @@ button.addEventListener('click', function() {
173
  '''
174
  lora_archive = "/data"
175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  def update_selection(selected_state: gr.SelectData, sdxl_loras, face_strength, image_strength, weight, depth_control_scale, negative, is_new=False):
177
  lora_repo = sdxl_loras[selected_state.index]["repo"]
178
  new_placeholder = "Type a prompt to use your selected LoRA"
@@ -204,17 +221,6 @@ def update_selection(selected_state: gr.SelectData, sdxl_loras, face_strength, i
204
  selected_state
205
  )
206
 
207
- def center_crop_image_as_square(img):
208
- square_size = min(img.size)
209
-
210
- left = (img.width - square_size) / 2
211
- top = (img.height - square_size) / 2
212
- right = (img.width + square_size) / 2
213
- bottom = (img.height + square_size) / 2
214
-
215
- img_cropped = img.crop((left, top, right, bottom))
216
- return img_cropped
217
-
218
  def check_selected(selected_state, custom_lora):
219
  if not selected_state and not custom_lora:
220
  raise gr.Error("You must select a style")
@@ -241,120 +247,84 @@ def merge_incompatible_lora(full_path_lora, lora_scale):
241
  del weights_sd
242
  del lora_model
243
 
244
- @spaces.GPU(duration=100)
245
- def generate_image(prompt, negative, face_emb, face_image, face_kps, image_strength, guidance_scale, face_strength, depth_control_scale, repo_name, loaded_state_dict, lora_scale, sdxl_loras, selected_state_index, st):
246
- print(loaded_state_dict)
247
- et = time.time()
248
- elapsed_time = et - st
249
- print('Getting into the decorated function took: ', elapsed_time, 'seconds')
250
  global last_fused, last_lora
251
- print("Last LoRA: ", last_lora)
252
- print("Current LoRA: ", repo_name)
253
- print("Last fused: ", last_fused)
254
- #prepare face zoe
255
- st = time.time()
256
- with torch.no_grad():
257
- image_zoe = zoe(face_image)
258
- width, height = face_kps.size
259
- images = [face_kps, image_zoe.resize((height, width))]
260
- et = time.time()
261
- elapsed_time = et - st
262
- print('Zoe Depth calculations took: ', elapsed_time, 'seconds')
263
  if last_lora != repo_name:
264
- if(last_fused):
265
- st = time.time()
266
  pipe.unfuse_lora()
267
  pipe.unload_lora_weights()
268
  pipe.unload_textual_inversion()
269
- et = time.time()
270
- elapsed_time = et - st
271
- print('Unfuse and unload LoRA took: ', elapsed_time, 'seconds')
272
- st = time.time()
273
  pipe.load_lora_weights(loaded_state_dict)
274
  pipe.fuse_lora(lora_scale)
275
- et = time.time()
276
- elapsed_time = et - st
277
- print('Fuse and load LoRA took: ', elapsed_time, 'seconds')
278
  last_fused = True
279
  is_pivotal = sdxl_loras[selected_state_index]["is_pivotal"]
280
- if(is_pivotal):
281
- #Add the textual inversion embeddings from pivotal tuning models
282
  text_embedding_name = sdxl_loras[selected_state_index]["text_embedding_weights"]
283
  embedding_path = hf_hub_download(repo_id=repo_name, filename=text_embedding_name, repo_type="model")
284
  state_dict_embedding = load_file(embedding_path)
285
  pipe.load_textual_inversion(state_dict_embedding["clip_l" if "clip_l" in state_dict_embedding else "text_encoders_0"], token=["<s0>", "<s1>"], text_encoder=pipe.text_encoder, tokenizer=pipe.tokenizer)
286
  pipe.load_textual_inversion(state_dict_embedding["clip_g" if "clip_g" in state_dict_embedding else "text_encoders_1"], token=["<s0>", "<s1>"], text_encoder=pipe.text_encoder_2, tokenizer=pipe.tokenizer_2)
287
-
288
- print("Processing prompt...")
289
- st = time.time()
290
  conditioning, pooled = compel(prompt)
291
- if(negative):
292
- negative_conditioning, negative_pooled = compel(negative)
293
- else:
294
- negative_conditioning, negative_pooled = None, None
295
- et = time.time()
296
- elapsed_time = et - st
297
- print('Prompt processing took: ', elapsed_time, 'seconds')
298
- print("Processing image...")
299
- st = time.time()
300
  image = pipe(
301
  prompt_embeds=conditioning,
302
  pooled_prompt_embeds=pooled,
303
  negative_prompt_embeds=negative_conditioning,
304
  negative_pooled_prompt_embeds=negative_pooled,
305
- width=1024,
306
- height=1024,
307
- image_embeds=face_emb,
308
  image=face_image,
309
  strength=1-image_strength,
310
- control_image=images,
311
  num_inference_steps=36,
312
- guidance_scale = guidance_scale,
313
- controlnet_conditioning_scale=[face_strength, depth_control_scale],
314
  ).images[0]
315
- et = time.time()
316
- elapsed_time = et - st
317
- print('Image processing took: ', elapsed_time, 'seconds')
318
  last_lora = repo_name
319
  return image
320
-
321
  def run_lora(face_image, prompt, negative, lora_scale, selected_state, face_strength, image_strength, guidance_scale, depth_control_scale, sdxl_loras, custom_lora, progress=gr.Progress(track_tqdm=True)):
322
  print("Custom LoRA: ", custom_lora)
323
  custom_lora_path = custom_lora[0] if custom_lora else None
324
  selected_state_index = selected_state.index if selected_state else -1
325
- st = time.time()
326
- face_image = center_crop_image_as_square(face_image)
 
 
327
  try:
328
  face_info = app.get(cv2.cvtColor(np.array(face_image), cv2.COLOR_RGB2BGR))
329
- face_info = sorted(face_info, key=lambda x:(x['bbox'][2]-x['bbox'][0])*x['bbox'][3]-x['bbox'][1])[-1] # only use the maximum face
330
  face_emb = face_info['embedding']
331
  face_kps = draw_kps(face_image, face_info['kps'])
332
- except:
333
- raise gr.Error("No face found in your image. Only face images work here. Try again")
334
- et = time.time()
335
- elapsed_time = et - st
336
- print('Cropping and calculating face embeds took: ', elapsed_time, 'seconds')
337
-
338
- st = time.time()
339
 
340
- if(custom_lora_path and custom_lora[1]):
341
  prompt = f"{prompt} {custom_lora[1]}"
342
  else:
343
  for lora_list in lora_defaults:
344
  if lora_list["model"] == sdxl_loras[selected_state_index]["repo"]:
345
  prompt_full = lora_list.get("prompt", None)
346
- if(prompt_full):
347
  prompt = prompt_full.replace("<subject>", prompt)
348
 
349
- print("Prompt:", prompt)
350
- if(prompt == ""):
351
  prompt = "a person"
352
- print(f"Executing prompt: {prompt}")
353
- #print("Selected State: ", selected_state_index)
354
- #print(sdxl_loras[selected_state_index]["repo"])
355
  if negative == "":
356
  negative = None
357
- print("Custom Loaded LoRA: ", custom_lora_path)
358
  if not selected_state and not custom_lora_path:
359
  raise gr.Error("You must select a style")
360
  elif custom_lora_path:
@@ -362,18 +332,17 @@ def run_lora(face_image, prompt, negative, lora_scale, selected_state, face_stre
362
  full_path_lora = custom_lora_path
363
  else:
364
  repo_name = sdxl_loras[selected_state_index]["repo"]
365
- weight_name = sdxl_loras[selected_state_index]["weights"]
366
  full_path_lora = state_dicts[repo_name]["saved_name"]
367
- print("Full path LoRA ", full_path_lora)
368
- #loaded_state_dict = copy.deepcopy(state_dicts[repo_name]["state_dict"])
369
- cross_attention_kwargs = None
370
- et = time.time()
371
- elapsed_time = et - st
372
- print('Small content processing took: ', elapsed_time, 'seconds')
373
-
374
- st = time.time()
375
- image = generate_image(prompt, negative, face_emb, face_image, face_kps, image_strength, guidance_scale, face_strength, depth_control_scale, repo_name, full_path_lora, lora_scale, sdxl_loras, selected_state_index, st)
376
- return (face_image, image), gr.update(visible=True)
377
 
378
  run_lora.zerogpu = True
379
 
 
173
  '''
174
  lora_archive = "/data"
175
 
176
+ def resize_image_aspect_ratio(img, max_dim=512):
177
+ width, height = img.size
178
+ aspect_ratio = width / height
179
+
180
+ if aspect_ratio >= 1: # Landscape or square
181
+ new_width = min(max_dim, width)
182
+ new_height = int(new_width / aspect_ratio)
183
+ else: # Portrait
184
+ new_height = min(max_dim, height)
185
+ new_width = int(new_height * aspect_ratio)
186
+
187
+ new_width = (new_width // 8) * 8
188
+ new_height = (new_height // 8) * 8
189
+
190
+ return img.resize((new_width, new_height), Image.LANCZOS)
191
+
192
+
193
  def update_selection(selected_state: gr.SelectData, sdxl_loras, face_strength, image_strength, weight, depth_control_scale, negative, is_new=False):
194
  lora_repo = sdxl_loras[selected_state.index]["repo"]
195
  new_placeholder = "Type a prompt to use your selected LoRA"
 
221
  selected_state
222
  )
223
 
 
 
 
 
 
 
 
 
 
 
 
224
  def check_selected(selected_state, custom_lora):
225
  if not selected_state and not custom_lora:
226
  raise gr.Error("You must select a style")
 
247
  del weights_sd
248
  del lora_model
249
 
250
+ @spaces.GPU(duration=85)
251
+ def generate_image(prompt, negative, face_emb, face_image, face_kps, image_strength, guidance_scale, face_strength, depth_control_scale, repo_name, loaded_state_dict, lora_scale, sdxl_loras, selected_state_index, face_detected):
 
 
 
 
252
  global last_fused, last_lora
253
+
254
+ control_images = [face_kps, zoe(face_image)] if face_detected else [zoe(face_image)]
255
+ control_scales = [face_strength, depth_control_scale] if face_detected else [depth_control_scale]
256
+
 
 
 
 
 
 
 
 
257
  if last_lora != repo_name:
258
+ if last_fused:
 
259
  pipe.unfuse_lora()
260
  pipe.unload_lora_weights()
261
  pipe.unload_textual_inversion()
 
 
 
 
262
  pipe.load_lora_weights(loaded_state_dict)
263
  pipe.fuse_lora(lora_scale)
 
 
 
264
  last_fused = True
265
  is_pivotal = sdxl_loras[selected_state_index]["is_pivotal"]
266
+ if is_pivotal:
 
267
  text_embedding_name = sdxl_loras[selected_state_index]["text_embedding_weights"]
268
  embedding_path = hf_hub_download(repo_id=repo_name, filename=text_embedding_name, repo_type="model")
269
  state_dict_embedding = load_file(embedding_path)
270
  pipe.load_textual_inversion(state_dict_embedding["clip_l" if "clip_l" in state_dict_embedding else "text_encoders_0"], token=["<s0>", "<s1>"], text_encoder=pipe.text_encoder, tokenizer=pipe.tokenizer)
271
  pipe.load_textual_inversion(state_dict_embedding["clip_g" if "clip_g" in state_dict_embedding else "text_encoders_1"], token=["<s0>", "<s1>"], text_encoder=pipe.text_encoder_2, tokenizer=pipe.tokenizer_2)
272
+
 
 
273
  conditioning, pooled = compel(prompt)
274
+ negative_conditioning, negative_pooled = compel(negative) if negative else (None, None)
275
+
 
 
 
 
 
 
 
276
  image = pipe(
277
  prompt_embeds=conditioning,
278
  pooled_prompt_embeds=pooled,
279
  negative_prompt_embeds=negative_conditioning,
280
  negative_pooled_prompt_embeds=negative_pooled,
281
+ width=face_image.width,
282
+ height=face_image.height,
283
+ image_embeds=face_emb if face_detected else None,
284
  image=face_image,
285
  strength=1-image_strength,
286
+ control_image=control_images,
287
  num_inference_steps=36,
288
+ guidance_scale=guidance_scale,
289
+ controlnet_conditioning_scale=control_scales,
290
  ).images[0]
291
+
 
 
292
  last_lora = repo_name
293
  return image
294
+
295
  def run_lora(face_image, prompt, negative, lora_scale, selected_state, face_strength, image_strength, guidance_scale, depth_control_scale, sdxl_loras, custom_lora, progress=gr.Progress(track_tqdm=True)):
296
  print("Custom LoRA: ", custom_lora)
297
  custom_lora_path = custom_lora[0] if custom_lora else None
298
  selected_state_index = selected_state.index if selected_state else -1
299
+
300
+ original_width, original_height = face_image.size
301
+
302
+ face_detected = True
303
  try:
304
  face_info = app.get(cv2.cvtColor(np.array(face_image), cv2.COLOR_RGB2BGR))
305
+ face_info = sorted(face_info, key=lambda x:(x['bbox'][2]-x['bbox'][0])*x['bbox'][3]-x['bbox'][1])[-1]
306
  face_emb = face_info['embedding']
307
  face_kps = draw_kps(face_image, face_info['kps'])
308
+ except:
309
+ face_detected = False
310
+ face_emb = None
311
+ face_kps = face_image
 
 
 
312
 
313
+ if custom_lora_path and custom_lora[1]:
314
  prompt = f"{prompt} {custom_lora[1]}"
315
  else:
316
  for lora_list in lora_defaults:
317
  if lora_list["model"] == sdxl_loras[selected_state_index]["repo"]:
318
  prompt_full = lora_list.get("prompt", None)
319
+ if prompt_full:
320
  prompt = prompt_full.replace("<subject>", prompt)
321
 
322
+ if prompt == "":
 
323
  prompt = "a person"
324
+
 
 
325
  if negative == "":
326
  negative = None
327
+
328
  if not selected_state and not custom_lora_path:
329
  raise gr.Error("You must select a style")
330
  elif custom_lora_path:
 
332
  full_path_lora = custom_lora_path
333
  else:
334
  repo_name = sdxl_loras[selected_state_index]["repo"]
 
335
  full_path_lora = state_dicts[repo_name]["saved_name"]
336
+
337
+ resized_image = resize_with_aspect_ratio(face_image)
338
+
339
+ image = generate_image(
340
+ prompt, negative, face_emb, resized_image, face_kps, image_strength, guidance_scale, face_strength, depth_control_scale,
341
+ repo_name, full_path_lora, lora_scale, sdxl_loras, selected_state_index, face_detected
342
+ )
343
+
344
+ return (resized_image, image), gr.update(visible=True)
345
+
346
 
347
  run_lora.zerogpu = True
348