Fucius commited on
Commit
c3f651b
1 Parent(s): 3357490

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -107
app.py CHANGED
@@ -372,75 +372,103 @@ def main(device, segment_type):
372
 
373
  @spaces.GPU
374
  def generate_image(prompt1, negative_prompt, man, woman, resolution, local_prompt1, local_prompt2, seed, condition, condition_img1, style):
375
- try:
376
- path1 = lorapath_man[man]
377
- path2 = lorapath_woman[woman]
378
- pipe_concept.unload_lora_weights()
379
- pipe.unload_lora_weights()
380
- pipe_list = build_model_lora(pipe_concept, path1 + "|" + path2, lorapath_styles[style], condition, args, pipe)
381
-
382
- if lorapath_styles[style] is not None and os.path.exists(lorapath_styles[style]):
383
- styleL = True
384
- else:
385
- styleL = False
386
-
387
- input_list = [prompt1]
388
- condition_list = [condition_img1]
389
- output_list = []
390
-
391
- width, height = int(resolution.split("*")[0]), int(resolution.split("*")[1])
392
-
393
- kwargs = {
394
- 'height': height,
395
- 'width': width,
396
- }
397
-
398
- for prompt, condition_img in zip(input_list, condition_list):
399
- if prompt!='':
400
- input_prompt = []
401
- p = '{prompt}, 35mm photograph, film, professional, 4k, highly detailed.'
402
- if styleL:
403
- p = styles[style] + p
404
- input_prompt.append([p.replace("{prompt}", prompt), p.replace("{prompt}", prompt)])
405
- if styleL:
406
- input_prompt.append([(styles[style] + local_prompt1, character_man.get(man)[1]),
407
- (styles[style] + local_prompt2, character_woman.get(woman)[1])])
408
- else:
409
- input_prompt.append([(local_prompt1, character_man.get(man)[1]),
410
- (local_prompt2, character_woman.get(woman)[1])])
411
-
412
- if condition == 'Human pose' and condition_img is not None:
413
- index = ratio_list.index(
414
- min(ratio_list, key=lambda x: abs(x - condition_img.shape[1] / condition_img.shape[0])))
415
- resolution = resolution_list[index]
416
- width, height = int(resolution.split("*")[0]), int(resolution.split("*")[1])
417
- kwargs['height'] = height
418
- kwargs['width'] = width
419
- condition_img = resize_and_center_crop(Image.fromarray(condition_img), (width, height))
420
- spatial_condition = get_humanpose(condition_img)
421
- elif condition == 'Canny Edge' and condition_img is not None:
422
- index = ratio_list.index(
423
- min(ratio_list, key=lambda x: abs(x - condition_img.shape[1] / condition_img.shape[0])))
424
- resolution = resolution_list[index]
425
- width, height = int(resolution.split("*")[0]), int(resolution.split("*")[1])
426
- kwargs['height'] = height
427
- kwargs['width'] = width
428
- condition_img = resize_and_center_crop(Image.fromarray(condition_img), (width, height))
429
- spatial_condition = get_cannyedge(condition_img)
430
- elif condition == 'Depth' and condition_img is not None:
431
- index = ratio_list.index(
432
- min(ratio_list, key=lambda x: abs(x - condition_img.shape[1] / condition_img.shape[0])))
433
- resolution = resolution_list[index]
434
- width, height = int(resolution.split("*")[0]), int(resolution.split("*")[1])
435
- kwargs['height'] = height
436
- kwargs['width'] = width
437
- condition_img = resize_and_center_crop(Image.fromarray(condition_img), (width, height))
438
- spatial_condition = get_depth(condition_img)
439
- else:
440
- spatial_condition = None
441
-
442
- kwargs['spatial_condition'] = spatial_condition
443
- controller.reset()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
444
  image = sample_image(
445
  pipe,
446
  input_prompt=input_prompt,
@@ -448,47 +476,19 @@ def main(device, segment_type):
448
  input_neg_prompt=[negative_prompt] * len(input_prompt),
449
  generator=torch.Generator(device).manual_seed(seed),
450
  controller=controller,
451
- stage=1,
 
452
  lora_list=pipe_list,
453
  styleL=styleL,
454
  **kwargs)
455
-
456
- controller.reset()
457
- if pipe.tokenizer("man")["input_ids"][1] in pipe.tokenizer(args.prompt)["input_ids"][1:-1]:
458
- mask1 = predict_mask(detect_model, sam, image[0], 'man', args.segment_type, confidence=0.15,
459
- threshold=0.5)
460
- else:
461
- mask1 = None
462
-
463
- if pipe.tokenizer("woman")["input_ids"][1] in pipe.tokenizer(args.prompt)["input_ids"][1:-1]:
464
- mask2 = predict_mask(detect_model, sam, image[0], 'woman', args.segment_type, confidence=0.15,
465
- threshold=0.5)
466
- else:
467
- mask2 = None
468
-
469
- if mask1 is None and mask2 is None:
470
- output_list.append(image[1])
471
- else:
472
- image = sample_image(
473
- pipe,
474
- input_prompt=input_prompt,
475
- concept_models=pipe_concept,
476
- input_neg_prompt=[negative_prompt] * len(input_prompt),
477
- generator=torch.Generator(device).manual_seed(seed),
478
- controller=controller,
479
- stage=2,
480
- region_masks=[mask1, mask2],
481
- lora_list=pipe_list,
482
- styleL=styleL,
483
- **kwargs)
484
- output_list.append(image[1])
485
- else:
486
- output_list.append(None)
487
- output_list.append(spatial_condition)
488
- return output_list
489
- except:
490
- print("error")
491
- return
492
 
493
  def get_local_value_man(input):
494
  return character_man[input][0]
 
372
 
373
  @spaces.GPU
374
  def generate_image(prompt1, negative_prompt, man, woman, resolution, local_prompt1, local_prompt2, seed, condition, condition_img1, style):
375
+ # try:
376
+ path1 = lorapath_man[man]
377
+ path2 = lorapath_woman[woman]
378
+ pipe_concept.unload_lora_weights()
379
+ pipe.unload_lora_weights()
380
+ pipe_list = build_model_lora(pipe_concept, path1 + "|" + path2, lorapath_styles[style], condition, args, pipe)
381
+
382
+ if lorapath_styles[style] is not None and os.path.exists(lorapath_styles[style]):
383
+ styleL = True
384
+ else:
385
+ styleL = False
386
+
387
+ input_list = [prompt1]
388
+ condition_list = [condition_img1]
389
+ output_list = []
390
+
391
+ width, height = int(resolution.split("*")[0]), int(resolution.split("*")[1])
392
+
393
+ kwargs = {
394
+ 'height': height,
395
+ 'width': width,
396
+ }
397
+
398
+ for prompt, condition_img in zip(input_list, condition_list):
399
+ if prompt!='':
400
+ input_prompt = []
401
+ p = '{prompt}, 35mm photograph, film, professional, 4k, highly detailed.'
402
+ if styleL:
403
+ p = styles[style] + p
404
+ input_prompt.append([p.replace("{prompt}", prompt), p.replace("{prompt}", prompt)])
405
+ if styleL:
406
+ input_prompt.append([(styles[style] + local_prompt1, character_man.get(man)[1]),
407
+ (styles[style] + local_prompt2, character_woman.get(woman)[1])])
408
+ else:
409
+ input_prompt.append([(local_prompt1, character_man.get(man)[1]),
410
+ (local_prompt2, character_woman.get(woman)[1])])
411
+
412
+ if condition == 'Human pose' and condition_img is not None:
413
+ index = ratio_list.index(
414
+ min(ratio_list, key=lambda x: abs(x - condition_img.shape[1] / condition_img.shape[0])))
415
+ resolution = resolution_list[index]
416
+ width, height = int(resolution.split("*")[0]), int(resolution.split("*")[1])
417
+ kwargs['height'] = height
418
+ kwargs['width'] = width
419
+ condition_img = resize_and_center_crop(Image.fromarray(condition_img), (width, height))
420
+ spatial_condition = get_humanpose(condition_img)
421
+ elif condition == 'Canny Edge' and condition_img is not None:
422
+ index = ratio_list.index(
423
+ min(ratio_list, key=lambda x: abs(x - condition_img.shape[1] / condition_img.shape[0])))
424
+ resolution = resolution_list[index]
425
+ width, height = int(resolution.split("*")[0]), int(resolution.split("*")[1])
426
+ kwargs['height'] = height
427
+ kwargs['width'] = width
428
+ condition_img = resize_and_center_crop(Image.fromarray(condition_img), (width, height))
429
+ spatial_condition = get_cannyedge(condition_img)
430
+ elif condition == 'Depth' and condition_img is not None:
431
+ index = ratio_list.index(
432
+ min(ratio_list, key=lambda x: abs(x - condition_img.shape[1] / condition_img.shape[0])))
433
+ resolution = resolution_list[index]
434
+ width, height = int(resolution.split("*")[0]), int(resolution.split("*")[1])
435
+ kwargs['height'] = height
436
+ kwargs['width'] = width
437
+ condition_img = resize_and_center_crop(Image.fromarray(condition_img), (width, height))
438
+ spatial_condition = get_depth(condition_img)
439
+ else:
440
+ spatial_condition = None
441
+
442
+ kwargs['spatial_condition'] = spatial_condition
443
+ controller.reset()
444
+ image = sample_image(
445
+ pipe,
446
+ input_prompt=input_prompt,
447
+ concept_models=pipe_concept,
448
+ input_neg_prompt=[negative_prompt] * len(input_prompt),
449
+ generator=torch.Generator(device).manual_seed(seed),
450
+ controller=controller,
451
+ stage=1,
452
+ lora_list=pipe_list,
453
+ styleL=styleL,
454
+ **kwargs)
455
+
456
+ controller.reset()
457
+ if pipe.tokenizer("man")["input_ids"][1] in pipe.tokenizer(args.prompt)["input_ids"][1:-1]:
458
+ mask1 = predict_mask(detect_model, sam, image[0], 'man', args.segment_type, confidence=0.15,
459
+ threshold=0.5)
460
+ else:
461
+ mask1 = None
462
+
463
+ if pipe.tokenizer("woman")["input_ids"][1] in pipe.tokenizer(args.prompt)["input_ids"][1:-1]:
464
+ mask2 = predict_mask(detect_model, sam, image[0], 'woman', args.segment_type, confidence=0.15,
465
+ threshold=0.5)
466
+ else:
467
+ mask2 = None
468
+
469
+ if mask1 is None and mask2 is None:
470
+ output_list.append(image[1])
471
+ else:
472
  image = sample_image(
473
  pipe,
474
  input_prompt=input_prompt,
 
476
  input_neg_prompt=[negative_prompt] * len(input_prompt),
477
  generator=torch.Generator(device).manual_seed(seed),
478
  controller=controller,
479
+ stage=2,
480
+ region_masks=[mask1, mask2],
481
  lora_list=pipe_list,
482
  styleL=styleL,
483
  **kwargs)
484
+ output_list.append(image[1])
485
+ else:
486
+ output_list.append(None)
487
+ output_list.append(spatial_condition)
488
+ return output_list
489
+ # except:
490
+ # print("error")
491
+ # return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
492
 
493
  def get_local_value_man(input):
494
  return character_man[input][0]