|
|
import torch |
|
|
import spaces |
|
|
from diffusers import StableDiffusionPipeline, DDIMScheduler, AutoencoderKL |
|
|
from transformers import AutoFeatureExtractor |
|
|
from ip_adapter.ip_adapter_faceid import IPAdapterFaceID, IPAdapterFaceIDPlus |
|
|
from huggingface_hub import hf_hub_download |
|
|
from insightface.app import FaceAnalysis |
|
|
from insightface.utils import face_align |
|
|
import gradio as gr |
|
|
import cv2 |
|
|
import os |
|
|
import uuid |
|
|
from datetime import datetime |
|
|
|
|
|
|
|
|
base_model_path = "SG161222/Realistic_Vision_V4.0_noVAE" |
|
|
vae_model_path = "stabilityai/sd-vae-ft-mse" |
|
|
image_encoder_path = "laion/CLIP-ViT-H-14-laion2B-s32B-b79K" |
|
|
ip_ckpt = hf_hub_download(repo_id="h94/IP-Adapter-FaceID", filename="ip-adapter-faceid_sd15.bin", repo_type="model") |
|
|
ip_plus_ckpt = hf_hub_download(repo_id="h94/IP-Adapter-FaceID", filename="ip-adapter-faceid-plusv2_sd15.bin", repo_type="model") |
|
|
|
|
|
device = "cuda" |
|
|
|
|
|
|
|
|
noise_scheduler = DDIMScheduler( |
|
|
num_train_timesteps=1000, |
|
|
beta_start=0.00085, |
|
|
beta_end=0.012, |
|
|
beta_schedule="scaled_linear", |
|
|
clip_sample=False, |
|
|
set_alpha_to_one=False, |
|
|
steps_offset=1, |
|
|
) |
|
|
|
|
|
|
|
|
vae = AutoencoderKL.from_pretrained(vae_model_path).to(dtype=torch.float16) |
|
|
pipe = StableDiffusionPipeline.from_pretrained( |
|
|
base_model_path, |
|
|
torch_dtype=torch.float16, |
|
|
scheduler=noise_scheduler, |
|
|
vae=vae |
|
|
).to(device) |
|
|
|
|
|
ip_model = IPAdapterFaceID(pipe, ip_ckpt, device) |
|
|
ip_model_plus = IPAdapterFaceIDPlus(pipe, image_encoder_path, ip_plus_ckpt, device) |
|
|
|
|
|
|
|
|
app = FaceAnalysis(name="buffalo_l", providers=['CPUExecutionProvider']) |
|
|
app.prepare(ctx_id=0, det_size=(640, 640)) |
|
|
|
|
|
cv2.setNumThreads(1) |
|
|
|
|
|
STYLE_PRESETS = [ |
|
|
{ |
|
|
"title": "Mona Lisa", |
|
|
"prompt": "A mesmerizing portrait in the style of Leonardo da Vinci's Mona Lisa, renaissance oil painting, soft sfumato technique, mysterious smile, Florentine background, museum quality, masterpiece", |
|
|
"preview": "π¨" |
|
|
}, |
|
|
{ |
|
|
"title": "Iron Hero", |
|
|
"prompt": "Hyper realistic portrait as a high-tech superhero, wearing advanced metallic suit, arc reactor glow, inside high-tech lab, dramatic lighting, cinematic composition", |
|
|
"preview": "π¦Ύ" |
|
|
}, |
|
|
{ |
|
|
"title": "Ancient Egyptian", |
|
|
"prompt": "Portrait as an ancient Egyptian pharaoh, wearing golden headdress and royal regalia, hieroglyphics background, dramatic desert lighting, archaeological discovery style", |
|
|
"preview": "π" |
|
|
}, |
|
|
{ |
|
|
"title": "Sherlock Holmes", |
|
|
"prompt": "Victorian era detective portrait, wearing deerstalker hat and cape, holding magnifying glass, foggy London background, mysterious atmosphere, detailed illustration", |
|
|
"preview": "π" |
|
|
}, |
|
|
{ |
|
|
"title": "Star Wars Jedi", |
|
|
"prompt": "Epic portrait as a Jedi Master, wearing traditional robes, holding lightsaber, temple background, force aura effect, cinematic lighting, movie poster quality", |
|
|
"preview": "βοΈ" |
|
|
}, |
|
|
{ |
|
|
"title": "Van Gogh Style", |
|
|
"prompt": "Self-portrait in the style of Vincent van Gogh, bold brushstrokes, vibrant colors, post-impressionist style, emotional intensity, starry background", |
|
|
"preview": "π¨" |
|
|
}, |
|
|
{ |
|
|
"title": "Greek God", |
|
|
"prompt": "Mythological portrait as an Olympian deity, wearing flowing robes, golden laurel wreath, Mount Olympus background, godly aura, classical Greek art style", |
|
|
"preview": "β‘" |
|
|
}, |
|
|
{ |
|
|
"title": "Medieval Knight", |
|
|
"prompt": "Noble knight portrait, wearing ornate plate armor, holding sword and shield, castle background, heraldic designs, medieval manuscript style", |
|
|
"preview": "π‘οΈ" |
|
|
}, |
|
|
{ |
|
|
"title": "Matrix Hero", |
|
|
"prompt": "Cyberpunk portrait in digital reality, wearing black trench coat and sunglasses, green code rain effect, dystopian atmosphere, cinematic style", |
|
|
"preview": "πΆοΈ" |
|
|
}, |
|
|
{ |
|
|
"title": "Pirate Captain", |
|
|
"prompt": "Swashbuckling pirate captain portrait, wearing tricorn hat and colonial coat, ship's deck background, dramatic sea storm, golden age of piracy style", |
|
|
"preview": "π΄ββ οΈ" |
|
|
} |
|
|
] |
|
|
|
|
|
css = ''' |
|
|
#component-0 { |
|
|
max-width: 1200px; |
|
|
margin: auto; |
|
|
padding: 20px; |
|
|
} |
|
|
|
|
|
.container { |
|
|
background-color: #ffffff; |
|
|
border-radius: 10px; |
|
|
padding: 20px; |
|
|
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
|
|
} |
|
|
|
|
|
.header { |
|
|
text-align: center; |
|
|
margin-bottom: 2rem; |
|
|
background: linear-gradient(90deg, #2C3E50, #3498DB); |
|
|
padding: 2rem; |
|
|
border-radius: 10px; |
|
|
color: white; |
|
|
} |
|
|
|
|
|
.preset-grid { |
|
|
display: grid; |
|
|
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); |
|
|
gap: 1rem; |
|
|
margin: 1rem 0; |
|
|
} |
|
|
|
|
|
.preset-card { |
|
|
background: #f8f9fa; |
|
|
padding: 1rem; |
|
|
border-radius: 8px; |
|
|
cursor: pointer; |
|
|
transition: all 0.3s ease; |
|
|
border: 1px solid #e9ecef; |
|
|
} |
|
|
|
|
|
.preset-card:hover { |
|
|
transform: translateY(-2px); |
|
|
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); |
|
|
background: #f1f3f5; |
|
|
} |
|
|
|
|
|
.preset-emoji { |
|
|
font-size: 2rem; |
|
|
margin-bottom: 0.5rem; |
|
|
} |
|
|
|
|
|
.input-container { |
|
|
background: #f8f9fa; |
|
|
padding: 1.5rem; |
|
|
border-radius: 8px; |
|
|
margin-bottom: 1rem; |
|
|
} |
|
|
|
|
|
footer {display: none !important} |
|
|
''' |
|
|
|
|
|
@spaces.GPU(enable_queue=True) |
|
|
def generate_image(images, gender, prompt, progress=gr.Progress(track_tqdm=True)): |
|
|
if not prompt: |
|
|
prompt = f"Professional portrait of a {gender.lower()}" |
|
|
|
|
|
|
|
|
prompt = f"{prompt}, single person, solo portrait, one person only, centered composition" |
|
|
|
|
|
|
|
|
negative_prompt = "multiple people, group photo, crowd, double portrait, triple portrait, many faces, multiple faces, two faces, three faces, multiple views, collage, photo grid" |
|
|
|
|
|
faceid_all_embeds = [] |
|
|
first_iteration = True |
|
|
preserve_face_structure = True |
|
|
face_strength = 2.1 |
|
|
likeness_strength = 0.7 |
|
|
|
|
|
for image in images: |
|
|
face = cv2.imread(image) |
|
|
faces = app.get(face) |
|
|
faceid_embed = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0) |
|
|
faceid_all_embeds.append(faceid_embed) |
|
|
|
|
|
if first_iteration and preserve_face_structure: |
|
|
face_image = face_align.norm_crop(face, landmark=faces[0].kps, image_size=224) |
|
|
first_iteration = False |
|
|
|
|
|
average_embedding = torch.mean(torch.stack(faceid_all_embeds, dim=0), dim=0) |
|
|
|
|
|
image = ip_model_plus.generate( |
|
|
prompt=prompt, |
|
|
negative_prompt=negative_prompt, |
|
|
faceid_embeds=average_embedding, |
|
|
scale=likeness_strength, |
|
|
face_image=face_image, |
|
|
shortcut=True, |
|
|
s_scale=face_strength, |
|
|
width=512, |
|
|
height=768, |
|
|
num_inference_steps=100, |
|
|
guidance_scale=7.5 |
|
|
) |
|
|
return image |
|
|
|
|
|
def create_preset_click_handler(idx, prompt_input): |
|
|
def handler(): |
|
|
return {"value": STYLE_PRESETS[idx]["prompt"]} |
|
|
return handler |
|
|
|
|
|
with gr.Blocks(css=css) as demo: |
|
|
with gr.Column(elem_classes="container"): |
|
|
with gr.Column(elem_classes="header"): |
|
|
gr.Markdown("# β¨ Magic Face") |
|
|
gr.Markdown("### Transform Your Face Into Legendary Characters!") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(scale=1): |
|
|
images_input = gr.Files( |
|
|
label="πΈ Upload Your Face Photos", |
|
|
file_types=["image"], |
|
|
elem_classes="input-container" |
|
|
) |
|
|
gender_input = gr.Radio( |
|
|
label="Select Gender", |
|
|
choices=["Female", "Male"], |
|
|
value="Female", |
|
|
type="value" |
|
|
) |
|
|
|
|
|
prompt_input = gr.Textbox( |
|
|
label="π¨ Custom Prompt", |
|
|
placeholder="Describe your desired transformation in detail...", |
|
|
lines=3 |
|
|
) |
|
|
|
|
|
with gr.Column(elem_classes="preset-container"): |
|
|
gr.Markdown("### π Magic Transformations") |
|
|
preset_grid = [] |
|
|
for idx, preset in enumerate(STYLE_PRESETS): |
|
|
preset_button = gr.Button( |
|
|
f"{preset['preview']} {preset['title']}", |
|
|
elem_classes="preset-card" |
|
|
) |
|
|
preset_button.click( |
|
|
fn=create_preset_click_handler(idx, prompt_input), |
|
|
inputs=[], |
|
|
outputs=[prompt_input] |
|
|
) |
|
|
preset_grid.append(preset_button) |
|
|
|
|
|
generate_button = gr.Button("π Generate Magic", variant="primary") |
|
|
|
|
|
with gr.Column(scale=1): |
|
|
output_gallery = gr.Gallery( |
|
|
label="Magic Gallery", |
|
|
elem_classes="output-gallery", |
|
|
columns=2 |
|
|
) |
|
|
|
|
|
with gr.Accordion("π Quick Guide", open=False): |
|
|
gr.Markdown(""" |
|
|
### How to Use Magic Face |
|
|
1. Upload one or more face photos |
|
|
2. Select your gender |
|
|
3. Choose a magical transformation or write your own prompt |
|
|
4. Click 'Generate Magic' |
|
|
|
|
|
### Pro Tips |
|
|
- Upload multiple angles of your face for better results |
|
|
- Try combining different historical or fictional characters |
|
|
- Feel free to modify the preset prompts |
|
|
- Click on generated images to view them in full size |
|
|
""") |
|
|
|
|
|
generate_button.click( |
|
|
fn=generate_image, |
|
|
inputs=[images_input, gender_input, prompt_input], |
|
|
outputs=output_gallery |
|
|
) |
|
|
|
|
|
demo.queue() |
|
|
demo.launch() |