primerz commited on
Commit
8fa2bfa
·
verified ·
1 Parent(s): f090813

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -12
app.py CHANGED
@@ -181,9 +181,9 @@ def update_selection(selected_state: gr.SelectData, sdxl_loras, face_strength, i
181
 
182
  for lora_list in lora_defaults:
183
  if lora_list["model"] == sdxl_loras[selected_state.index]["repo"]:
184
- face_strength = lora_list.get("face_strength", 0.85)
185
- image_strength = lora_list.get("image_strength", 0.15)
186
- weight = lora_list.get("weight", 0.9)
187
  depth_control_scale = lora_list.get("depth_control_scale", 0.8)
188
  negative = lora_list.get("negative", "")
189
 
@@ -229,6 +229,7 @@ def merge_incompatible_lora(full_path_lora, lora_scale):
229
  )
230
  del weights_sd
231
  del lora_model
 
232
  def resize_image_aspect_ratio(img, max_dim=512):
233
  width, height = img.size
234
  aspect_ratio = width / height
@@ -248,22 +249,28 @@ def resize_image_aspect_ratio(img, max_dim=512):
248
 
249
 
250
  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)):
251
- print("Custom LoRA: ", custom_lora)
252
  custom_lora_path = custom_lora[0] if custom_lora else None
253
  selected_state_index = selected_state.index if selected_state else -1
254
 
255
- original_width, original_height = face_image.size
 
256
 
257
  face_detected = True
258
  try:
259
  face_info = app.get(cv2.cvtColor(np.array(face_image), cv2.COLOR_RGB2BGR))
260
- face_info = sorted(face_info, key=lambda x:(x['bbox'][2]-x['bbox'][0])*x['bbox'][3]-x['bbox'][1])[-1]
261
  face_emb = face_info['embedding']
262
  face_kps = draw_kps(face_image, face_info['kps'])
263
  except:
264
  face_detected = False
265
  face_emb = None
266
- face_kps = face_image
 
 
 
 
 
267
 
268
  if custom_lora_path and custom_lora[1]:
269
  prompt = f"{prompt} {custom_lora[1]}"
@@ -274,11 +281,14 @@ def run_lora(face_image, prompt, negative, lora_scale, selected_state, face_stre
274
  if prompt_full:
275
  prompt = prompt_full.replace("<subject>", prompt)
276
 
 
277
  if prompt == "":
278
  prompt = "a person"
 
279
 
280
  if negative == "":
281
  negative = None
 
282
 
283
  if not selected_state and not custom_lora_path:
284
  raise gr.Error("You must select a style")
@@ -289,17 +299,24 @@ def run_lora(face_image, prompt, negative, lora_scale, selected_state, face_stre
289
  repo_name = sdxl_loras[selected_state_index]["repo"]
290
  full_path_lora = state_dicts[repo_name]["saved_name"]
291
 
292
- resized_image = resize_with_aspect_ratio(face_image)
293
 
 
 
 
 
294
  image = generate_image(
295
- prompt, negative, face_emb, resized_image, face_kps, image_strength, guidance_scale, face_strength, depth_control_scale,
296
- repo_name, full_path_lora, lora_scale, sdxl_loras, selected_state_index, face_detected
 
297
  )
298
 
299
- return (resized_image, image), gr.update(visible=True)
 
 
300
 
301
  @spaces.GPU(duration=100)
302
- 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):
303
  global last_fused, last_lora
304
 
305
  print("Loaded state dict:", loaded_state_dict)
 
181
 
182
  for lora_list in lora_defaults:
183
  if lora_list["model"] == sdxl_loras[selected_state.index]["repo"]:
184
+ face_strength = lora_list.get("face_strength", 0.95)
185
+ image_strength = lora_list.get("image_strength", 0.2)
186
+ weight = lora_list.get("weight", 0.95)
187
  depth_control_scale = lora_list.get("depth_control_scale", 0.8)
188
  negative = lora_list.get("negative", "")
189
 
 
229
  )
230
  del weights_sd
231
  del lora_model
232
+
233
  def resize_image_aspect_ratio(img, max_dim=512):
234
  width, height = img.size
235
  aspect_ratio = width / height
 
249
 
250
 
251
  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)):
252
+ print("Custom LoRA:", custom_lora)
253
  custom_lora_path = custom_lora[0] if custom_lora else None
254
  selected_state_index = selected_state.index if selected_state else -1
255
 
256
+ st = time.time()
257
+ face_image = resize_image_aspect_ratio(face_image)
258
 
259
  face_detected = True
260
  try:
261
  face_info = app.get(cv2.cvtColor(np.array(face_image), cv2.COLOR_RGB2BGR))
262
+ face_info = sorted(face_info, key=lambda x: (x['bbox'][2]-x['bbox'][0]) * x['bbox'][3]-x['bbox'][1])[-1]
263
  face_emb = face_info['embedding']
264
  face_kps = draw_kps(face_image, face_info['kps'])
265
  except:
266
  face_detected = False
267
  face_emb = None
268
+ face_kps = face_image # Optional face handling, verify intentional
269
+
270
+ et = time.time()
271
+ print('Cropping and calculating face embeds took:', et - st, 'seconds')
272
+
273
+ st = time.time()
274
 
275
  if custom_lora_path and custom_lora[1]:
276
  prompt = f"{prompt} {custom_lora[1]}"
 
281
  if prompt_full:
282
  prompt = prompt_full.replace("<subject>", prompt)
283
 
284
+ print("Prompt:", prompt)
285
  if prompt == "":
286
  prompt = "a person"
287
+ print(f"Executing prompt: {prompt}")
288
 
289
  if negative == "":
290
  negative = None
291
+ print("Custom Loaded LoRA:", custom_lora_path)
292
 
293
  if not selected_state and not custom_lora_path:
294
  raise gr.Error("You must select a style")
 
299
  repo_name = sdxl_loras[selected_state_index]["repo"]
300
  full_path_lora = state_dicts[repo_name]["saved_name"]
301
 
302
+ print("Full path LoRA", full_path_lora)
303
 
304
+ et = time.time()
305
+ print('Small content processing took:', et - st, 'seconds')
306
+
307
+ st = time.time()
308
  image = generate_image(
309
+ prompt, negative, face_emb, face_image, face_kps, image_strength,
310
+ guidance_scale, face_strength, depth_control_scale, repo_name,
311
+ full_path_lora, lora_scale, sdxl_loras, selected_state_index, face_detected, st
312
  )
313
 
314
+ return (face_image, image), gr.update(visible=True)
315
+
316
+ run_lora.zerogpu = True
317
 
318
  @spaces.GPU(duration=100)
319
+ 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, st):
320
  global last_fused, last_lora
321
 
322
  print("Loaded state dict:", loaded_state_dict)