File size: 29,712 Bytes
a866b04 c14f6aa 25a1def c14f6aa a866b04 68221c5 a866b04 0e6b5f9 a866b04 3a57694 a866b04 fb796b9 a866b04 12f63d9 e534478 a866b04 abbf325 a866b04 2c947e9 a9d63ba a866b04 a9d63ba a866b04 c14f6aa a866b04 5b6a745 6c68988 25bee8e 6c68988 25bee8e 5b6a745 6c68988 5b6a745 37e457c d2e0a0f a0ba745 37e457c fe2c725 37e457c fe2c725 a0ba745 a9d63ba 37e457c a8dd021 5b6a745 a8dd021 d2e0a0f d4f7bc2 d2e0a0f a8dd021 5b6a745 a866b04 21e7f5e bf85a4d 061cffb 7016199 a866b04 bf85a4d f38906b a9d63ba f38906b bf85a4d 09551ad 4c80957 09551ad a9d63ba 09551ad bf85a4d 19fc881 a866b04 d2d5bbd 044f0eb 2b7f832 f78b154 76ba1d9 efc09f9 c14f6aa 6c8e4d6 f78b154 9e79798 bfbf6ce 76ba1d9 594ab3a 1e40a7d 76ba1d9 bfbf6ce 1a4ffa5 f78b154 1a4ffa5 b11dc19 2d5a71a b11dc19 e66eb9f 1a4ffa5 860bbde d6f09fd 860bbde 1a4ffa5 a866b04 5b6a745 a8dd021 5b6a745 d4f7bc2 5b6a745 a866b04 21e7f5e 061cffb 4c910c4 5b6a745 12e5bd9 a866b04 e1a894a a866b04 c14f6aa 76ba1d9 a866b04 76ba1d9 4c80957 98cce01 4c80957 98cce01 4c80957 cbb5f96 ebed2d0 5b6a745 1a4ffa5 21e7f5e 44a917a 5b6a745 21e7f5e 85b0050 4c80957 12e5bd9 98cce01 a9d63ba 98cce01 85b0050 ecf36c4 21e7f5e 4f1c62a efc09f9 a866b04 1e40a7d 84b1a25 a866b04 594ab3a a866b04 594ab3a a866b04 4c80957 c14f6aa 594ab3a a866b04 72a9bd4 a866b04 a9d63ba a866b04 c14f6aa a866b04 1a4ffa5 a3f9835 a8dd021 16983e0 5b6a745 a866b04 e3331b4 a9d63ba | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 | import torch
import torchvision.transforms.functional as TF
import numpy as np
import random
import os
import sys
import tempfile
from PIL import Image
from diffusers.utils import load_image
from diffusers import EulerDiscreteScheduler, T2IAdapter
from huggingface_hub import hf_hub_download
import spaces
import gradio as gr
from pipeline_t2i_adapter import PhotoMakerStableDiffusionXLAdapterPipeline
from face_utils import FaceAnalysis2, analyze_faces
from style_template import styles
from aspect_ratio_template import aspect_ratios
# global variable
base_model_path = 'SG161222/RealVisXL_V4.0'
face_detector = FaceAnalysis2(providers=['CPUExecutionProvider', 'CUDAExecutionProvider'], allowed_modules=['detection', 'recognition'])
face_detector.prepare(ctx_id=0, det_size=(640, 640))
try:
if torch.cuda.is_available():
device = "cuda"
elif sys.platform == "darwin" and torch.backends.mps.is_available():
device = "mps"
else:
device = "cpu"
except:
device = "cpu"
HF_TEST_USER_NAME = os.environ["HF_TEST_USER_NAME"]
HF_PASS = os.environ["HF_PASS"]
MAX_SEED = np.iinfo(np.int32).max
STYLE_NAMES = list(styles.keys())
DEFAULT_STYLE_NAME = "Photographic (Default)"
ASPECT_RATIO_LABELS = list(aspect_ratios)
DEFAULT_ASPECT_RATIO = ASPECT_RATIO_LABELS[0]
enable_doodle_arg = False
photomaker_ckpt = hf_hub_download(repo_id="TencentARC/PhotoMaker-V2", filename="photomaker-v2.bin", repo_type="model")
if torch.cuda.is_available() and torch.cuda.is_bf16_supported():
torch_dtype = torch.bfloat16
else:
torch_dtype = torch.float16
if device == "mps":
torch_dtype = torch.float16
# load adapter
adapter = T2IAdapter.from_pretrained(
"TencentARC/t2i-adapter-sketch-sdxl-1.0", torch_dtype=torch_dtype, variant="fp16"
).to(device)
pipe = PhotoMakerStableDiffusionXLAdapterPipeline.from_pretrained(
base_model_path,
adapter=adapter,
torch_dtype=torch_dtype,
use_safetensors=True,
variant="fp16",
).to(device)
pipe.load_photomaker_adapter(
os.path.dirname(photomaker_ckpt),
subfolder="",
weight_name=os.path.basename(photomaker_ckpt),
trigger_word="img",
pm_version="v2",
)
pipe.id_encoder.to(device)
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
# pipe.set_adapters(["photomaker"], adapter_weights=[1.0])
pipe.fuse_lora()
pipe.to(device)
@spaces.GPU()
def generate_image(
upload_images,
prompt,
negative_prompt,
aspect_ratio_name,
style_name,
num_steps,
style_strength_ratio,
num_outputs,
guidance_scale,
seed,
use_doodle,
sketch_image,
adapter_conditioning_scale,
adapter_conditioning_factor,
progress=gr.Progress(track_tqdm=True)
):
if use_doodle:
sketch_image = sketch_image["composite"]
r, g, b, a = sketch_image.split()
sketch_image = a.convert("RGB")
sketch_image = TF.to_tensor(sketch_image) > 0.5 # Inversion
sketch_image = TF.to_pil_image(sketch_image.to(torch.float32))
adapter_conditioning_scale = adapter_conditioning_scale
adapter_conditioning_factor = adapter_conditioning_factor
else:
adapter_conditioning_scale = 0.
adapter_conditioning_factor = 0.
sketch_image = None
# check the trigger word
image_token_id = pipe.tokenizer.convert_tokens_to_ids(pipe.trigger_word)
input_ids = pipe.tokenizer.encode(prompt)
if image_token_id not in input_ids:
raise gr.Error(f"Cannot find the trigger word '{pipe.trigger_word}' in text prompt! Please refer to step 2️⃣")
if input_ids.count(image_token_id) > 1:
raise gr.Error(f"Cannot use multiple trigger words '{pipe.trigger_word}' in text prompt!")
# determine output dimensions by the aspect ratio
output_w, output_h = aspect_ratios[aspect_ratio_name]
print(f"[Debug] Generate image using aspect ratio [{aspect_ratio_name}] => {output_w} x {output_h}")
# apply the style template
prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
if upload_images is None:
raise gr.Error(f"Cannot find any input face image! Please refer to step 1️⃣")
input_id_images = []
for img in upload_images:
input_id_images.append(load_image(img))
id_embed_list = []
for img in input_id_images:
img = np.array(img)
img = img[:, :, ::-1]
faces = analyze_faces(face_detector, img)
if len(faces) > 0:
id_embed_list.append(torch.from_numpy((faces[0]['embedding'])))
if len(id_embed_list) == 0:
raise gr.Error(f"No face detected, please update the input face image(s)")
id_embeds = torch.stack(id_embed_list)
generator = torch.Generator(device=device).manual_seed(seed)
print("Start inference...")
print(f"[Debug] Seed: {seed}")
print(f"[Debug] Prompt: {prompt}, \n[Debug] Neg Prompt: {negative_prompt}")
start_merge_step = int(float(style_strength_ratio) / 100 * num_steps)
if start_merge_step > 30:
start_merge_step = 30
print(start_merge_step)
images = pipe(
prompt=prompt,
width=output_w,
height=output_h,
input_id_images=input_id_images,
negative_prompt=negative_prompt,
num_images_per_prompt=num_outputs,
num_inference_steps=num_steps,
start_merge_step=start_merge_step,
generator=generator,
guidance_scale=guidance_scale,
id_embeds=id_embeds,
image=sketch_image,
adapter_conditioning_scale=adapter_conditioning_scale,
adapter_conditioning_factor=adapter_conditioning_factor,
).images
# Save images to temp files for download
temp_paths = []
for i, img in enumerate(images):
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
img.save(tmp.name)
temp_paths.append(tmp.name)
return images, gr.update(visible=True), temp_paths[0], temp_paths[1]
# return images, gr.update(visible=True)
def swap_to_gallery(images):
return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
def upload_example_to_gallery(images, prompt, style, negative_prompt):
return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
def remove_back_to_files():
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
def change_doodle_space(use_doodle):
if use_doodle:
return gr.update(visible=True)
else:
return gr.update(visible=False)
def remove_tips():
return gr.update(visible=False)
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
if randomize_seed:
seed = random.randint(0, MAX_SEED)
return seed
def apply_style(style_name: str, positive: str, negative: str = "") -> tuple[str, str]:
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
return p.replace("{prompt}", positive), n + ' ' + negative
def get_image_path_list(folder_name):
image_basename_list = os.listdir(folder_name)
image_path_list = sorted([os.path.join(folder_name, basename) for basename in image_basename_list])
return image_path_list
def get_example():
case = [
[
get_image_path_list('./examples/scarletthead_woman'),
"instagram photo, portrait photo of a woman img, colorful, perfect face, natural skin, hard shadows, film grain",
"(No style)",
"(asymmetry, worst quality, low quality, illustration, 3d, 2d, painting, cartoons, sketch), open mouth",
],
[
get_image_path_list('./examples/newton_man'),
"sci-fi, closeup portrait photo of a man img wearing the sunglasses in Iron man suit, face, slim body, high quality, film grain",
"(No style)",
"(asymmetry, worst quality, low quality, illustration, 3d, 2d, painting, cartoons, sketch), open mouth",
],
]
return case
def set_gender_prompt(gender):
return f"A photo of a {gender} img"
man_prompts = {
"Garden":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears a light cream linen shirt with beige trousers, standing in a bloomed garden, with a romantic vibe.",
"Balcony":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears a tailored evening suit, standing on a balcony with an elegant vibe.",
"Park":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears a fitted athletic T-shirt and joggers, posing in a park with an energetic vibe.",
"Cafe":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears a turtleneck sweater with tailored trousers, sitting by a cafe window.",
"Beach":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears stylish swim trunks, standing on a sandy beach at sunset.",
"Sidewalk":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears a tailored wool overcoat and leather ankle boots, standing on a sidewalk with a cozy autumn vibe.",
"Street":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears a long coat with the collar popped up, standing on a street with a cozy, masculine mood.",
"Moders Office":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears a dark tailored suit with a slim tie, standing confidently in a modern office.",
"Posing by a desk":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears a fitted shirt with relaxed wide-leg pants, posing confidently by a desk.",
"Resort Pool":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears swim trunks, standing by a luxurious pool with natural sunlight, giving a resort vibe.",
"Cozy":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears a simple oversized sweater and comfortable pants, lounging on a couch, relaxed atmosphere.",
"Palm Breeze":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears solid-color board shorts, standing confidently on a beach lined with palm trees and crystal-clear water.",
"Beach Shore":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears bright board shorts, standing on the shore with gentle waves and a beachy atmosphere.",
"Vacation":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears swim trunks, vacation-ready vibe, standing by a crystal-clear infinity pool under bright sunlight.",
"Field":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears a rugged denim jacket over a simple white tee and cargo pants, standing in a sunlit meadow with wildflowers, giving a rustic, adventurous vibe.",
"Ocean Edge":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears a jacket and tailored pants, standing on a cliff overlooking the ocean with a dramatic vibe.",
"Club Mood":"Photo of a man img, realistic eyes, natural skin, no hands. The model wears a black leather bomber jacket and dark denim, standing against a dimly lit club wall with a relaxed yet bold atmosphere."
}
woman_prompts = {
"Garden":"Photo of a woman img, realistic eyes, natural skin, no hands. The model wears a floral maxi dress, standing in a bloomed garden, with a romantic vibe.",
"Balcony":"Photo of a woman img, realistic eyes, natural skin, no hands. The model wears an evening gown, standing on a balcony with an elegant vibe.",
"Park":"Photo of a woman img, realistic eyes, natural skin, no hands. The model wears a sporty crop top and leggings, posing in a park with an energetic vibe.",
"Cafe": "Photo of a woman img, realistic eyes, natural skin, no hands. The model wears a high-neck blouse with a skirt, sitting by a cafe window.",
"Beach":"Photo of a woman img, realistic eyes, natural skin, no hands. The model wears a stylish bikini, standing on a sandy beach at sunset.",
"Sidewalk":"Photo of a woman img, realistic eyes, natural skin, no hands. The model wears a trench coat and ankle boots, standing on sidewalk, with a cozy autumn vibe",
"Street":"Photo of a woman img, realistic eyes, natural skin, no hands. The model wears a scarf and long coat, standing on a street, creating a cozy mood.",
"Moders Office":"Photo of a woman img, realistic eyes, natural skin, no hands. The model wears a tailored blazer with trousers, standing in a modern office.",
"Posing by a desk":"Photo of a woman img, realistic eyes, natural skin, no hands. The model wears a shirt with wide-leg pants, posing by a desk.",
"Resort Pool":"Photo of a woman img, realistic eyes, natural skin, no hands. The model wears a minimalist bikini, standing by a luxurious pool with natural sunlight, giving a resort vibe.",
"Cozy":"Photo of a woman img, realistic eyes, natural skin, no hands. The model wears an oversized sweater lounging on a couch with a cozy blanket, relaxed atmosphere.",
"Palm Breeze":"Photo of a woman img, realistic eyes, natural skin, no hands. The model wears a classic triangle bikini, standing on a beach with palm trees and clear water.",
"Beach Shore":"Photo of a woman img, realistic eyes, natural skin, no hands. The model wears a colored bikini with a sunhat, standing on the shore, beachy vibe.",
"Vacation":"Photo of a woman img, realistic eyes, natural skin, no hands. The model wears a sleek one-piece bikini, vacation-ready vibe.",
"Field":"Photo of a woman img, realistic eyes, natural skin, no hands. The model wears a bohemian-style maxi dress, standing in a vibrant field of wildflowers.",
"Ocean Edge":"Photo of a woman img, realistic eyes, natural skin, no hands. The model wears a dress, standing on a cliff overlooking the ocean.",
"Club Mood":"Photo of a woman img, realistic eyes, natural skin, no hands. The model wears a leather dress, standing against a dimly lit club wall"
}
def get_prompts_by_gender(gender):
return man_prompts if gender == "man" else woman_prompts
def update_theme_buttons_only(gender):
prompts = get_prompts_by_gender(gender)
updates = []
for i, theme in enumerate(prompts.keys()):
if i < len(theme_buttons):
updates.append(gr.update(value=theme, visible=True))
for j in range(len(prompts), len(theme_buttons)):
updates.append(gr.update(visible=False))
return updates
def update_prompt_on_gender_change(gender, theme_name, current_prompt):
button_updates = update_theme_buttons_only(gender)
if theme_name and theme_name in get_prompts_by_gender(gender):
# regenerate prompt for same theme but new gender
new_prompt = get_prompts_by_gender(gender)[theme_name]
else:
new_prompt = current_prompt
# ✅ return prompt first, then button updates (match outputs length)
return [new_prompt] + button_updates
def get_prompt_from_theme(gender, theme):
return get_prompts_by_gender(gender)[theme], gr.update(visible=False)
def update_suggestion_buttons(gender):
prompts = get_prompts_by_gender(gender)
return gr.update(visible=True), prompts
def make_click_fn(theme_name):
return lambda gender: get_prompt_from_theme(gender, theme_name)
theme_buttons=[]
for i, btn in enumerate(theme_buttons):
theme = list(man_prompts.keys())[i]
btn.click(
fn=make_click_fn(theme),
inputs=gender_selector,
outputs=[prompt, prompt_suggestions]
)
### Description and style
logo = r"""
# <center><img src='https://photo-maker.github.io/assets/logo.png' alt='PhotoMaker logo' style="width:80px; margin-bottom:10px"></center>
# """
# title = r"""
title_html="""
<h1 id="app-title">Portfolio Generation</h1>
"""
#V2: Improved ID Fidelity and Better Controllability than PhotoMaker V1</h1>
description = r"""
<b>Official 🤗 Gradio demo</b> for <a href='https://github.com/TencentARC/PhotoMaker' target='_blank'><b>PhotoMaker: Customizing Realistic Human Photos via Stacked ID Embedding</b></a>.<br>
How to use PhotoMaker V2 can be found in 🎬 <a href='https://photo-maker.github.io/assets/demo_pm_v2_full.mp4' target='_blank'>this video</a> 🎬.
<br>
<br>
For previous version of PhotoMaker, you could use our original gradio demos [PhotoMaker](https://huggingface.co/spaces/TencentARC/PhotoMaker) and [PhotoMaker-Style](https://huggingface.co/spaces/TencentARC/PhotoMaker-Style).
<br>
❗️❗️❗️[<b>Important</b>] Personalization steps:<br>
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>
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>
3️⃣ Choose your preferred style template.<br>
4️⃣ <b>(Optional: but new feature)</b> Select the ‘Enable Drawing Doodle...’ option and draw on the canvas<br>
5️⃣ Click the <b>Submit</b> button to start customizing.
"""
article = r""" """
# If PhotoMaker V2 is helpful, please help to ⭐ the <a href='https://github.com/TencentARC/PhotoMaker' target='_blank'>Github Repo</a>. Thanks!
# [](https://github.com/TencentARC/PhotoMaker)
# ---
# 📝 **Citation**
# <br>
# If our work is useful for your research, please consider citing:
# bibtex
# @article{li2023photomaker,
# title={PhotoMaker: Customizing Realistic Human Photos via Stacked ID Embedding},
# author={Li, Zhen and Cao, Mingdeng and Wang, Xintao and Qi, Zhongang and Cheng, Ming-Ming and Shan, Ying},
# booktitle={IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
# year={2024}
# }
#
# 📋 **License**
# <br>
# Apache-2.0 LICENSE. Please refer to the [LICENSE file](https://huggingface.co/TencentARC/PhotoMaker/blob/main/LICENSE) for details.
# 📧 **Contact**
# <br>
# If you have any questions, please feel free to reach me out at <b>zhenli1031@gmail.com</b>.
# """
tips = r"""
### Usage tips of PhotoMaker
1. Upload **more photos**of the person to be customized to **improve ID fidelty**.
2. If you find that the image quality is poor when using doodle for control, you can reduce the conditioning scale and factor of the adapter.
If you have any issues, leave the issue in the discussion page of the space. For a more stable (queue-free) experience, you can duplicate the space.
"""
# We have provided some generate examples and comparisons at: [this website]().
css = '''
/*.gradio-container {width: 85% !important;*/
.gradio-container {width: 100% !important;
background: linear-gradient(135deg, #CCFED9, #95BAFF);
}
#huggingface-space-header {
display: none !important;
}
#generate_btn {
background: #553566 !important;
color: white !important;
}
#download_btn1 {
background: #553566 !important;
color: white !important;
}
#download_btn2 {
background: #553566 !important;
color: white !important;
}
html body h1#app-title {
text-align: center;
font-size: 40px;
font-weight: bold;
color: #D46BCA !important;
}
#reupload_wrapper button {
background: #553566 !important;
color: white !important;
}
#usage_tips_section {
display: none !important;
}
#doodle_section, #advanced_section {
display: none !important;
}
#enable_doodle_container {
display: none !important;
}
footer, [class*="footer"], [class*="api"], [class*="settings"] {
display: none !important;
}
a[href*="api"], .api-button, #api-button {
display: none !important;
}
#prompt_suggestions_box {
background: white;
border: 2px solid #D46BCA;
padding: 15px;
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
position: fixed;
top: 20%;
left: 50%;
transform: translate(-50%, 0);
width: 40%;
z-index: 9999;
}
.scrollable-prompts {
flex: 1;
overflow-y: auto;
padding: 10px;
max-height: 300px; /* or any height you prefer */
}
#suggested_prompts_btn {
background: #D46BCA !important;
color: white !important;
}
#close_prompt_box {
background: #553566 !important;
color: white !important;
width: 100%;
}
input[type="radio"]:checked + span {
border-color: #D46BCA !important;
}
'''
def update_prompt_buttons_visibility(gender):
prompts = man_prompts if gender == "man" else woman_prompts
return [gr.update(value=theme, visible=True) for theme in prompts.keys()]
# theme_buttons = []
with gr.Blocks(css=css) as demo:
gr.Markdown(logo)
# gr.Markdown(title)
gr.HTML(title_html)
#gr.Markdown(description)
selected_theme = gr.State("")
with gr.Row():
with gr.Column():
files = gr.Files(
label="Drag (Select) 1 or more photos of your face",
file_types=["image"]
)
uploaded_files = gr.Gallery(label="Your images", visible=False, columns=5, rows=1, height=200, show_share_button=False, show_download_button=False)
with gr.Column(visible=False, elem_id="reupload_wrapper") as clear_button:
remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
prompt = gr.Dropdown(
label="Select Prompt",
choices=list(get_prompts_by_gender("woman").keys()), # default to woman
# value=list(get_prompts_by_gender("woman").values())[0],
# value = "Select Your Prompt",
interactive=True
)
gender_selector = gr.Radio(
label="Choose Gender",
choices=["woman", "man"],
value="woman"
)
with gr.Row():
pass
def update_prompt_dropdown(gender):
prompts = list(get_prompts_by_gender(gender).values())
return gr.update(choices=prompts, value=prompts[0]) # sets default to first
prompt_text = gr.Textbox(label="Prompt Text", visible=False, interactive=False, value=list(get_prompts_by_gender("woman").values())[0])
# Step 3: Map selected theme to prompt text
def theme_to_prompt_text(theme_name, gender):
prompts = get_prompts_by_gender(gender)
return prompts.get(theme_name, "")
# Step 4: On dropdown or gender change, update prompt_text
def update_prompt_text(theme_name, gender):
return theme_to_prompt_text(theme_name, gender)
# Connect dropdown and gender selector changes to update prompt_text
prompt.change(
fn=update_prompt_text,
inputs=[prompt, gender_selector],
outputs=prompt_text
)
gender_selector.change(
fn=update_prompt_text,
inputs=[prompt, gender_selector],
outputs=prompt_text
)
style = gr.Dropdown(label="Style template", choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME, visible=False)
aspect_ratio = gr.Dropdown(label="Output aspect ratio", choices=ASPECT_RATIO_LABELS, value=DEFAULT_ASPECT_RATIO, visible=False)
submit = gr.Button("GENERATE" , elem_id="generate_btn")
with gr.Column(elem_id="enable_doodle_container"):
enable_doodle = gr.Checkbox(
label="Enable Drawing Doodle for Control", value=enable_doodle_arg,
info="After enabling this option, PhotoMaker will generate content based on your doodle on the canvas, driven by the T2I-Adapter (Quality may be decreased)",
)
with gr.Accordion("T2I-Adapter-Doodle (Optional)", visible=False, elem_id="doodle_section") as doodle_space:
#with gr.Accordion("T2I-Adapter-Doodle (Optional)", visible=False) as doodle_space:
with gr.Row():
sketch_image = gr.Sketchpad(
label="Canvas",
type="pil",
crop_size=[1024,1024],
layers=False,
canvas_size=(350, 350),
brush=gr.Brush(default_size=5, colors=["#000000"], color_mode="fixed")
)
with gr.Group():
adapter_conditioning_scale = gr.Slider(
label="Adapter conditioning scale",
minimum=0.5,
maximum=1,
step=0.1,
value=0.7,
)
adapter_conditioning_factor = gr.Slider(
label="Adapter conditioning factor",
info="Fraction of timesteps for which adapter should be applied",
minimum=0.5,
maximum=1,
step=0.1,
value=0.8,
)
with gr.Accordion(open=False, label="Advanced Options", elem_id="advanced_section"):
# with gr.Accordion(open=False, label="Advanced Options"):
negative_prompt = gr.Textbox(
label="Negative Prompt",
placeholder="low quality",
value="nsfw, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry",
)
num_steps = gr.Slider(
label="Number of sample steps",
minimum=20,
maximum=100,
step=1,
value=50,
)
style_strength_ratio = gr.Slider(
label="Style strength (%)",
minimum=15,
maximum=50,
step=1,
value=20,
)
num_outputs = gr.Slider(
label="Number of output images",
minimum=1,
maximum=4,
step=1,
value=2,
)
guidance_scale = gr.Slider(
label="Guidance scale",
minimum=0.1,
maximum=10.0,
step=0.1,
value=5,
)
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=0,
)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
with gr.Column():
# gallery = gr.Gallery(label="Generated Images")
gallery = gr.Gallery(label="Generated Images", show_share_button=False, show_download_button=False)
download_btn1 = gr.DownloadButton(label="Download Image 1", elem_id="download_btn1")
download_btn2 = gr.DownloadButton(label="Download Image 2", elem_id="download_btn2")
usage_tips = gr.Markdown(label="Usage tips of PhotoMaker", value=tips ,visible=False, elem_id="usage_tips_section")
# usage_tips = gr.Markdown(label="Usage tips of PhotoMaker", value=tips ,visible=False)
files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files, clear_button, files])
remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files, clear_button, files])
enable_doodle.select(fn=change_doodle_space, inputs=enable_doodle, outputs=doodle_space)
input_list = [
files,
prompt_text,
negative_prompt,
aspect_ratio,
style,
num_steps,
style_strength_ratio,
num_outputs,
guidance_scale,
seed,
enable_doodle,
sketch_image,
adapter_conditioning_scale,
adapter_conditioning_factor
]
submit.click(
fn=remove_tips,
outputs=usage_tips,
).then(
fn=randomize_seed_fn,
inputs=[seed, randomize_seed],
outputs=seed,
queue=False,
api_name=False,
).then(
fn=generate_image,
inputs=input_list,
outputs=[gallery, usage_tips, download_btn1, download_btn2]
)
# When user clicks on a suggestion, fill the prompt textbox and close popup
for btn in theme_buttons:
btn.click(
fn=lambda theme_name, g: get_prompt_from_theme(g, theme_name),
inputs=[btn, gender_selector],
outputs=[prompt, prompt_suggestions]
)
# gr.Examples(
# examples=get_example(),
# inputs=[files, prompt, style, negative_prompt],
# run_on_click=True,
# fn=upload_example_to_gallery,
# outputs=[uploaded_files, clear_button, files],
# )
gr.Markdown(article)
demo.launch(auth=(HF_TEST_USER_NAME, HF_PASS), ssr_mode=False)
# demo.launch(ssr_mode=False)
|