Significant commited on
Commit
a9d63ba
·
verified ·
1 Parent(s): 85b0050

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -95
app.py CHANGED
@@ -84,9 +84,7 @@ pipe.to(device)
84
  @spaces.GPU()
85
  def generate_image(
86
  upload_images,
87
- # prompt,
88
- prompt_theme, #added
89
- gender, #added
90
  negative_prompt,
91
  aspect_ratio_name,
92
  style_name,
@@ -101,12 +99,6 @@ def generate_image(
101
  adapter_conditioning_factor,
102
  progress=gr.Progress(track_tqdm=True)
103
  ):
104
- #added
105
- prompts = get_prompts_by_gender(gender)
106
- prompt = prompts.get(prompt_theme)
107
- if prompt is None:
108
- raise gr.Error(f"Prompt theme '{prompt_theme}' not found for gender '{gender}'")
109
- #
110
  if use_doodle:
111
  sketch_image = sketch_image["composite"]
112
  r, g, b, a = sketch_image.split()
@@ -120,17 +112,6 @@ def generate_image(
120
  adapter_conditioning_factor = 0.
121
  sketch_image = None
122
 
123
-
124
- #added
125
- # prompt_dict = get_prompts_by_gender(gender)
126
-
127
- # added
128
- # actual_prompt = full_prompts[prompt]
129
- # # prompt = actual_prompt
130
- # full_prompt = current_prompts[prompt_theme]
131
-
132
-
133
-
134
  # check the trigger word
135
  image_token_id = pipe.tokenizer.convert_tokens_to_ids(pipe.trigger_word)
136
  input_ids = pipe.tokenizer.encode(prompt)
@@ -178,9 +159,7 @@ def generate_image(
178
  start_merge_step = 30
179
  print(start_merge_step)
180
  images = pipe(
181
- # prompt=prompt,
182
- # prompt=actual_prompt,
183
- prompt=full_prompt,
184
  width=output_w,
185
  height=output_h,
186
  input_id_images=input_id_images,
@@ -305,7 +284,8 @@ def update_theme_buttons_only(gender):
305
  for j in range(len(prompts), len(theme_buttons)):
306
  updates.append(gr.update(visible=False))
307
  return updates
308
-
 
309
  def update_prompt_on_gender_change(gender, theme_name, current_prompt):
310
  button_updates = update_theme_buttons_only(gender)
311
  if theme_name and theme_name in get_prompts_by_gender(gender):
@@ -358,7 +338,7 @@ For previous version of PhotoMaker, you could use our original gradio demos [Pho
358
  <br>
359
  ❗️❗️❗️[<b>Important</b>] Personalization steps:<br>
360
  1️⃣ Upload images of someone you want to customize. One image is ok, but more is better. Although we do not perform face detection, the face in the uploaded image should <b>occupy the majority of the image</b>.<br>
361
- 2️⃣ Enter a text prompt, making sure to <b>follow the class word</b> you want to customize with the <b>trigger word</b>: `img`, such as: `man img` or `woman img` or `girl img`.<br>
362
  3️⃣ Choose your preferred style template.<br>
363
  4️⃣ <b>(Optional: but new feature)</b> Select the ‘Enable Drawing Doodle...’ option and draw on the canvas<br>
364
  5️⃣ Click the <b>Submit</b> button to start customizing.
@@ -373,14 +353,16 @@ article = r""" """
373
  # <br>
374
  # If our work is useful for your research, please consider citing:
375
 
376
- # ```bibtex
 
377
  # @article{li2023photomaker,
378
  # title={PhotoMaker: Customizing Realistic Human Photos via Stacked ID Embedding},
379
  # author={Li, Zhen and Cao, Mingdeng and Wang, Xintao and Qi, Zhongang and Cheng, Ming-Ming and Shan, Ying},
380
  # booktitle={IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
381
  # year={2024}
382
  # }
383
- # ```
 
384
  # 📋 **License**
385
  # <br>
386
  # Apache-2.0 LICENSE. Please refer to the [LICENSE file](https://huggingface.co/TencentARC/PhotoMaker/blob/main/LICENSE) for details.
@@ -403,47 +385,35 @@ css = '''
403
  .gradio-container {width: 100% !important;
404
  background: linear-gradient(135deg, #CCFED9, #95BAFF);
405
  }
406
-
407
  #generate_btn {
408
  background: #553566 !important;
409
  color: white !important;
410
  }
411
-
412
  html body h1#app-title {
413
  text-align: center;
414
  font-size: 40px;
415
  font-weight: bold;
416
  color: #D46BCA !important;
417
  }
418
-
419
  #reupload_wrapper button {
420
  background: #553566 !important;
421
  color: white !important;
422
  }
423
-
424
  #usage_tips_section {
425
  display: none !important;
426
  }
427
-
428
-
429
  #doodle_section, #advanced_section {
430
  display: none !important;
431
  }
432
-
433
  #enable_doodle_container {
434
  display: none !important;
435
  }
436
-
437
-
438
  footer, [class*="footer"], [class*="api"], [class*="settings"] {
439
  display: none !important;
440
  }
441
-
442
  a[href*="api"], .api-button, #api-button {
443
  display: none !important;
444
-
445
  }
446
-
447
  #prompt_suggestions_box {
448
  background: white;
449
  border: 2px solid #D46BCA;
@@ -456,7 +426,6 @@ a[href*="api"], .api-button, #api-button {
456
  transform: translate(-50%, 0);
457
  width: 40%;
458
  z-index: 9999;
459
-
460
  }
461
  .scrollable-prompts {
462
  flex: 1;
@@ -464,7 +433,6 @@ a[href*="api"], .api-button, #api-button {
464
  padding: 10px;
465
  max-height: 300px; /* or any height you prefer */
466
  }
467
-
468
  #suggested_prompts_btn {
469
  background: #D46BCA !important;
470
  color: white !important;
@@ -474,7 +442,6 @@ a[href*="api"], .api-button, #api-button {
474
  color: white !important;
475
  width: 100%;
476
  }
477
-
478
  input[type="radio"]:checked + span {
479
  border-color: #D46BCA !important;
480
  }
@@ -506,29 +473,13 @@ with gr.Blocks(css=css) as demo:
506
  with gr.Column(visible=False, elem_id="reupload_wrapper") as clear_button:
507
  remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
508
 
509
- # prompt = gr.Dropdown(
510
- # label="Select Prompt",
511
- # choices=list(get_prompts_by_gender("woman").values()), # default to woman
512
- # # value=list(get_prompts_by_gender("woman").values())[0],
513
- # value = "Select Your Prompt",
514
- # interactive=True
515
- # )
516
-
517
- # default_gender = "woman"
518
- # default_prompts = woman_prompts
519
- # default_prompt_keys = list(get_prompts_by_gender(default_gender).keys())
520
- # default_prompts = get_prompts_by_gender(default_gender)
521
- # default_prompt_keys = list(default_prompts.keys())
522
-
523
- # current_prompts = gr.State(default_prompts)
524
-
525
  prompt = gr.Dropdown(
526
- label="Select Prompt Theme",
527
- choices=list(get_prompts_by_gender("woman").keys()),
528
- value=list(get_prompts_by_gender("woman").keys())[0],
529
- interactive=True,
 
530
  )
531
-
532
 
533
  gender_selector = gr.Radio(
534
  label="Choose Gender",
@@ -545,41 +496,17 @@ with gr.Blocks(css=css) as demo:
545
  def update_prompt_dropdown(gender):
546
  prompts = list(get_prompts_by_gender(gender).values())
547
  return gr.update(choices=prompts, value=prompts[0]) # sets default to first
548
- # prompts = get_prompts_by_gender(gender)
549
- # return [
550
- # gr.update(choices=list(prompts.keys()), value=list(prompts.keys())[0]),
551
- # prompts # Update the stored full prompts
552
- # ]
553
-
554
-
555
- # def update_prompts_on_gender_change(gender):
556
- # prompts = woman_prompts if gender == "woman" else man_prompts
557
- # return [
558
- # gr.update(choices=list(prompts.keys())),
559
- # prompts # Update the stored prompts
560
- # ]
561
 
562
  gender_selector.change(
 
 
 
 
563
  fn=update_prompt_dropdown,
564
  inputs=gender_selector,
565
  outputs=prompt
566
  )
567
 
568
- # fn=update_prompts_on_gender_change,
569
- # inputs=gender_selector,
570
- # outputs=[prompt_dropdown, current_prompts]
571
- # )
572
-
573
-
574
- # def update_prompt_dropdown_on_gender_change(gender):
575
- # theme_names = list(get_prompts_by_gender(gender).keys())
576
- # # Update dropdown choices to new gender’s themes and select first theme by default
577
- # return gr.update(choices=theme_names, value=theme_names[0])
578
-
579
- # gender_selector.change(
580
- # fn=update_prompt_dropdown_on_gender_change,
581
- # inputs=gender_selector,
582
- # outputs=prompt, # update the dropdown itself )
583
 
584
  style = gr.Dropdown(label="Style template", choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME, visible=False)
585
  aspect_ratio = gr.Dropdown(label="Output aspect ratio", choices=ASPECT_RATIO_LABELS, value=DEFAULT_ASPECT_RATIO, visible=False)
@@ -675,7 +602,6 @@ with gr.Blocks(css=css) as demo:
675
  input_list = [
676
  files,
677
  prompt,
678
- gender_selector,#added
679
  negative_prompt,
680
  aspect_ratio,
681
  style,
@@ -687,8 +613,8 @@ with gr.Blocks(css=css) as demo:
687
  enable_doodle,
688
  sketch_image,
689
  adapter_conditioning_scale,
690
- adapter_conditioning_factor,
691
- ]
692
 
693
  submit.click(
694
  fn=remove_tips,
@@ -724,4 +650,5 @@ with gr.Blocks(css=css) as demo:
724
  gr.Markdown(article)
725
 
726
  demo.launch(auth=(HF_TEST_USER_NAME, HF_PASS), ssr_mode=False)
727
- # demo.launch(ssr_mode=False)
 
 
84
  @spaces.GPU()
85
  def generate_image(
86
  upload_images,
87
+ prompt,
 
 
88
  negative_prompt,
89
  aspect_ratio_name,
90
  style_name,
 
99
  adapter_conditioning_factor,
100
  progress=gr.Progress(track_tqdm=True)
101
  ):
 
 
 
 
 
 
102
  if use_doodle:
103
  sketch_image = sketch_image["composite"]
104
  r, g, b, a = sketch_image.split()
 
112
  adapter_conditioning_factor = 0.
113
  sketch_image = None
114
 
 
 
 
 
 
 
 
 
 
 
 
115
  # check the trigger word
116
  image_token_id = pipe.tokenizer.convert_tokens_to_ids(pipe.trigger_word)
117
  input_ids = pipe.tokenizer.encode(prompt)
 
159
  start_merge_step = 30
160
  print(start_merge_step)
161
  images = pipe(
162
+ prompt=prompt,
 
 
163
  width=output_w,
164
  height=output_h,
165
  input_id_images=input_id_images,
 
284
  for j in range(len(prompts), len(theme_buttons)):
285
  updates.append(gr.update(visible=False))
286
  return updates
287
+
288
+
289
  def update_prompt_on_gender_change(gender, theme_name, current_prompt):
290
  button_updates = update_theme_buttons_only(gender)
291
  if theme_name and theme_name in get_prompts_by_gender(gender):
 
338
  <br>
339
  ❗️❗️❗️[<b>Important</b>] Personalization steps:<br>
340
  1️⃣ Upload images of someone you want to customize. One image is ok, but more is better. Although we do not perform face detection, the face in the uploaded image should <b>occupy the majority of the image</b>.<br>
341
+ 2️⃣ Enter a text prompt, making sure to <b>follow the class word</b> you want to customize with the <b>trigger word</b>: img, such as: man img or woman img or girl img.<br>
342
  3️⃣ Choose your preferred style template.<br>
343
  4️⃣ <b>(Optional: but new feature)</b> Select the ‘Enable Drawing Doodle...’ option and draw on the canvas<br>
344
  5️⃣ Click the <b>Submit</b> button to start customizing.
 
353
  # <br>
354
  # If our work is useful for your research, please consider citing:
355
 
356
+ #
357
+ bibtex
358
  # @article{li2023photomaker,
359
  # title={PhotoMaker: Customizing Realistic Human Photos via Stacked ID Embedding},
360
  # author={Li, Zhen and Cao, Mingdeng and Wang, Xintao and Qi, Zhongang and Cheng, Ming-Ming and Shan, Ying},
361
  # booktitle={IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
362
  # year={2024}
363
  # }
364
+ #
365
+
366
  # 📋 **License**
367
  # <br>
368
  # Apache-2.0 LICENSE. Please refer to the [LICENSE file](https://huggingface.co/TencentARC/PhotoMaker/blob/main/LICENSE) for details.
 
385
  .gradio-container {width: 100% !important;
386
  background: linear-gradient(135deg, #CCFED9, #95BAFF);
387
  }
 
388
  #generate_btn {
389
  background: #553566 !important;
390
  color: white !important;
391
  }
 
392
  html body h1#app-title {
393
  text-align: center;
394
  font-size: 40px;
395
  font-weight: bold;
396
  color: #D46BCA !important;
397
  }
 
398
  #reupload_wrapper button {
399
  background: #553566 !important;
400
  color: white !important;
401
  }
 
402
  #usage_tips_section {
403
  display: none !important;
404
  }
 
 
405
  #doodle_section, #advanced_section {
406
  display: none !important;
407
  }
 
408
  #enable_doodle_container {
409
  display: none !important;
410
  }
 
 
411
  footer, [class*="footer"], [class*="api"], [class*="settings"] {
412
  display: none !important;
413
  }
 
414
  a[href*="api"], .api-button, #api-button {
415
  display: none !important;
 
416
  }
 
417
  #prompt_suggestions_box {
418
  background: white;
419
  border: 2px solid #D46BCA;
 
426
  transform: translate(-50%, 0);
427
  width: 40%;
428
  z-index: 9999;
 
429
  }
430
  .scrollable-prompts {
431
  flex: 1;
 
433
  padding: 10px;
434
  max-height: 300px; /* or any height you prefer */
435
  }
 
436
  #suggested_prompts_btn {
437
  background: #D46BCA !important;
438
  color: white !important;
 
442
  color: white !important;
443
  width: 100%;
444
  }
 
445
  input[type="radio"]:checked + span {
446
  border-color: #D46BCA !important;
447
  }
 
473
  with gr.Column(visible=False, elem_id="reupload_wrapper") as clear_button:
474
  remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
475
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
476
  prompt = gr.Dropdown(
477
+ label="Select Prompt",
478
+ choices=list(get_prompts_by_gender("woman").values()), # default to woman
479
+ # value=list(get_prompts_by_gender("woman").values())[0],
480
+ value = "Select Your Prompt",
481
+ interactive=True
482
  )
 
483
 
484
  gender_selector = gr.Radio(
485
  label="Choose Gender",
 
496
  def update_prompt_dropdown(gender):
497
  prompts = list(get_prompts_by_gender(gender).values())
498
  return gr.update(choices=prompts, value=prompts[0]) # sets default to first
 
 
 
 
 
 
 
 
 
 
 
 
 
499
 
500
  gender_selector.change(
501
+ # fn=update_prompt_on_gender_change,
502
+ # inputs=[gender_selector, selected_theme, prompt],
503
+ # outputs=[prompt] + theme_buttons
504
+
505
  fn=update_prompt_dropdown,
506
  inputs=gender_selector,
507
  outputs=prompt
508
  )
509
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
510
 
511
  style = gr.Dropdown(label="Style template", choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME, visible=False)
512
  aspect_ratio = gr.Dropdown(label="Output aspect ratio", choices=ASPECT_RATIO_LABELS, value=DEFAULT_ASPECT_RATIO, visible=False)
 
602
  input_list = [
603
  files,
604
  prompt,
 
605
  negative_prompt,
606
  aspect_ratio,
607
  style,
 
613
  enable_doodle,
614
  sketch_image,
615
  adapter_conditioning_scale,
616
+ adapter_conditioning_factor
617
+ ]
618
 
619
  submit.click(
620
  fn=remove_tips,
 
650
  gr.Markdown(article)
651
 
652
  demo.launch(auth=(HF_TEST_USER_NAME, HF_PASS), ssr_mode=False)
653
+ # demo.launch(ssr_mode=False)
654
+