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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import argparse
|
| 3 |
+
import os
|
| 4 |
+
import shutil
|
| 5 |
+
import cv2
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import numpy as np
|
| 8 |
+
import torch
|
| 9 |
+
from facexlib.utils.face_restoration_helper import FaceRestoreHelper
|
| 10 |
+
import huggingface_hub
|
| 11 |
+
from huggingface_hub import hf_hub_download
|
| 12 |
+
from PIL import Image
|
| 13 |
+
from torchvision.transforms.functional import normalize
|
| 14 |
+
|
| 15 |
+
from dreamo.dreamo_pipeline import DreamOPipeline
|
| 16 |
+
from dreamo.utils import img2tensor, resize_numpy_image_area, tensor2img, resize_numpy_image_long
|
| 17 |
+
from tools import BEN2
|
| 18 |
+
|
| 19 |
+
parser = argparse.ArgumentParser()
|
| 20 |
+
parser.add_argument('--port', type=int, default=8080)
|
| 21 |
+
parser.add_argument('--no_turbo', action='store_true')
|
| 22 |
+
args = parser.parse_args()
|
| 23 |
+
|
| 24 |
+
huggingface_hub.login(os.getenv('HF_TOKEN'))
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
shutil.rmtree('gradio_cached_examples')
|
| 28 |
+
except FileNotFoundError:
|
| 29 |
+
print("cache folder not exist")
|
| 30 |
+
|
| 31 |
+
class Generator:
|
| 32 |
+
def __init__(self):
|
| 33 |
+
device = torch.device('cuda')
|
| 34 |
+
# preprocessing models
|
| 35 |
+
# background remove model: BEN2
|
| 36 |
+
self.bg_rm_model = BEN2.BEN_Base().to(device).eval()
|
| 37 |
+
hf_hub_download(repo_id='PramaLLC/BEN2', filename='BEN2_Base.pth', local_dir='models')
|
| 38 |
+
self.bg_rm_model.loadcheckpoints('models/BEN2_Base.pth')
|
| 39 |
+
# face crop and align tool: facexlib
|
| 40 |
+
self.face_helper = FaceRestoreHelper(
|
| 41 |
+
upscale_factor=1,
|
| 42 |
+
face_size=512,
|
| 43 |
+
crop_ratio=(1, 1),
|
| 44 |
+
det_model='retinaface_resnet50',
|
| 45 |
+
save_ext='png',
|
| 46 |
+
device=device,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# load dreamo
|
| 50 |
+
model_root = 'black-forest-labs/FLUX.1-dev'
|
| 51 |
+
dreamo_pipeline = DreamOPipeline.from_pretrained(model_root, torch_dtype=torch.bfloat16)
|
| 52 |
+
dreamo_pipeline.load_dreamo_model(device, use_turbo=not args.no_turbo)
|
| 53 |
+
self.dreamo_pipeline = dreamo_pipeline.to(device)
|
| 54 |
+
|
| 55 |
+
@torch.no_grad()
|
| 56 |
+
def get_align_face(self, img):
|
| 57 |
+
# the face preprocessing code is same as PuLID
|
| 58 |
+
self.face_helper.clean_all()
|
| 59 |
+
image_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 60 |
+
self.face_helper.read_image(image_bgr)
|
| 61 |
+
self.face_helper.get_face_landmarks_5(only_center_face=True)
|
| 62 |
+
self.face_helper.align_warp_face()
|
| 63 |
+
if len(self.face_helper.cropped_faces) == 0:
|
| 64 |
+
return None
|
| 65 |
+
align_face = self.face_helper.cropped_faces[0]
|
| 66 |
+
|
| 67 |
+
input = img2tensor(align_face, bgr2rgb=True).unsqueeze(0) / 255.0
|
| 68 |
+
input = input.to(torch.device("cuda"))
|
| 69 |
+
parsing_out = self.face_helper.face_parse(normalize(input, [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]))[0]
|
| 70 |
+
parsing_out = parsing_out.argmax(dim=1, keepdim=True)
|
| 71 |
+
bg_label = [0, 16, 18, 7, 8, 9, 14, 15]
|
| 72 |
+
bg = sum(parsing_out == i for i in bg_label).bool()
|
| 73 |
+
white_image = torch.ones_like(input)
|
| 74 |
+
# only keep the face features
|
| 75 |
+
face_features_image = torch.where(bg, white_image, input)
|
| 76 |
+
face_features_image = tensor2img(face_features_image, rgb2bgr=False)
|
| 77 |
+
|
| 78 |
+
return face_features_image
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
generator = Generator()
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
@spaces.GPU
|
| 85 |
+
@torch.inference_mode()
|
| 86 |
+
def generate_image(
|
| 87 |
+
ref_image1,
|
| 88 |
+
ref_image2,
|
| 89 |
+
ref_task1,
|
| 90 |
+
ref_task2,
|
| 91 |
+
prompt,
|
| 92 |
+
seed,
|
| 93 |
+
width=1024,
|
| 94 |
+
height=1024,
|
| 95 |
+
ref_res=512,
|
| 96 |
+
num_steps=12,
|
| 97 |
+
guidance=3.5,
|
| 98 |
+
true_cfg=1,
|
| 99 |
+
cfg_start_step=0,
|
| 100 |
+
cfg_end_step=0,
|
| 101 |
+
neg_prompt='',
|
| 102 |
+
neg_guidance=3.5,
|
| 103 |
+
first_step_guidance=0,
|
| 104 |
+
):
|
| 105 |
+
print(prompt)
|
| 106 |
+
ref_conds = []
|
| 107 |
+
debug_images = []
|
| 108 |
+
|
| 109 |
+
ref_images = [ref_image1, ref_image2]
|
| 110 |
+
ref_tasks = [ref_task1, ref_task2]
|
| 111 |
+
|
| 112 |
+
for idx, (ref_image, ref_task) in enumerate(zip(ref_images, ref_tasks)):
|
| 113 |
+
if ref_image is not None:
|
| 114 |
+
if ref_task == "id":
|
| 115 |
+
ref_image = resize_numpy_image_long(ref_image, 1024)
|
| 116 |
+
ref_image = generator.get_align_face(ref_image)
|
| 117 |
+
elif ref_task != "style":
|
| 118 |
+
ref_image = generator.bg_rm_model.inference(Image.fromarray(ref_image))
|
| 119 |
+
if ref_task != "id":
|
| 120 |
+
ref_image = resize_numpy_image_area(np.array(ref_image), ref_res * ref_res)
|
| 121 |
+
debug_images.append(ref_image)
|
| 122 |
+
ref_image = img2tensor(ref_image, bgr2rgb=False).unsqueeze(0) / 255.0
|
| 123 |
+
ref_image = 2 * ref_image - 1.0
|
| 124 |
+
ref_conds.append(
|
| 125 |
+
{
|
| 126 |
+
'img': ref_image,
|
| 127 |
+
'task': ref_task,
|
| 128 |
+
'idx': idx + 1,
|
| 129 |
+
}
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
seed = int(seed)
|
| 133 |
+
if seed == -1:
|
| 134 |
+
seed = torch.Generator(device="cpu").seed()
|
| 135 |
+
|
| 136 |
+
image = generator.dreamo_pipeline(
|
| 137 |
+
prompt=prompt,
|
| 138 |
+
width=width,
|
| 139 |
+
height=height,
|
| 140 |
+
num_inference_steps=num_steps,
|
| 141 |
+
guidance_scale=guidance,
|
| 142 |
+
ref_conds=ref_conds,
|
| 143 |
+
generator=torch.Generator(device="cpu").manual_seed(seed),
|
| 144 |
+
true_cfg_scale=true_cfg,
|
| 145 |
+
true_cfg_start_step=cfg_start_step,
|
| 146 |
+
true_cfg_end_step=cfg_end_step,
|
| 147 |
+
negative_prompt=neg_prompt,
|
| 148 |
+
neg_guidance_scale=neg_guidance,
|
| 149 |
+
first_step_guidance_scale=first_step_guidance if first_step_guidance > 0 else guidance,
|
| 150 |
+
).images[0]
|
| 151 |
+
|
| 152 |
+
return image, debug_images, seed
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
# Custom CSS for pastel theme
|
| 156 |
+
_CUSTOM_CSS_ = """
|
| 157 |
+
:root {
|
| 158 |
+
--primary-color: #f8c3cd; /* Sakura pink - primary accent */
|
| 159 |
+
--secondary-color: #b3e5fc; /* Pastel blue - secondary accent */
|
| 160 |
+
--background-color: #f5f5f7; /* Very light gray background */
|
| 161 |
+
--card-background: #ffffff; /* White for cards */
|
| 162 |
+
--text-color: #424242; /* Dark gray for text */
|
| 163 |
+
--accent-color: #ffb6c1; /* Light pink for accents */
|
| 164 |
+
--success-color: #c8e6c9; /* Pastel green for success */
|
| 165 |
+
--warning-color: #fff9c4; /* Pastel yellow for warnings */
|
| 166 |
+
--shadow-color: rgba(0, 0, 0, 0.1); /* Shadow color */
|
| 167 |
+
--border-radius: 12px; /* Rounded corners */
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
body {
|
| 171 |
+
background-color: var(--background-color) !important;
|
| 172 |
+
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important;
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
.gradio-container {
|
| 176 |
+
max-width: 1200px !important;
|
| 177 |
+
margin: 0 auto !important;
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
/* Header styling */
|
| 181 |
+
h1 {
|
| 182 |
+
color: #9c27b0 !important;
|
| 183 |
+
font-weight: 800 !important;
|
| 184 |
+
text-shadow: 2px 2px 4px rgba(156, 39, 176, 0.2) !important;
|
| 185 |
+
letter-spacing: -0.5px !important;
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
/* Card styling for panels */
|
| 189 |
+
.panel-box {
|
| 190 |
+
border-radius: var(--border-radius) !important;
|
| 191 |
+
box-shadow: 0 8px 16px var(--shadow-color) !important;
|
| 192 |
+
background-color: var(--card-background) !important;
|
| 193 |
+
border: none !important;
|
| 194 |
+
overflow: hidden !important;
|
| 195 |
+
padding: 20px !important;
|
| 196 |
+
margin-bottom: 20px !important;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
/* Button styling */
|
| 200 |
+
button.gr-button {
|
| 201 |
+
background: linear-gradient(135deg, var(--primary-color), #e1bee7) !important;
|
| 202 |
+
border-radius: var(--border-radius) !important;
|
| 203 |
+
color: #4a148c !important;
|
| 204 |
+
font-weight: 600 !important;
|
| 205 |
+
border: none !important;
|
| 206 |
+
padding: 10px 20px !important;
|
| 207 |
+
transition: all 0.3s ease !important;
|
| 208 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1) !important;
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
button.gr-button:hover {
|
| 212 |
+
transform: translateY(-2px) !important;
|
| 213 |
+
box-shadow: 0 6px 10px rgba(0, 0, 0, 0.15) !important;
|
| 214 |
+
background: linear-gradient(135deg, #e1bee7, var(--primary-color)) !important;
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
/* Input fields styling */
|
| 218 |
+
input, select, textarea, .gr-input {
|
| 219 |
+
border-radius: 8px !important;
|
| 220 |
+
border: 2px solid #e0e0e0 !important;
|
| 221 |
+
padding: 10px 15px !important;
|
| 222 |
+
transition: all 0.3s ease !important;
|
| 223 |
+
background-color: #fafafa !important;
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
input:focus, select:focus, textarea:focus, .gr-input:focus {
|
| 227 |
+
border-color: var(--primary-color) !important;
|
| 228 |
+
box-shadow: 0 0 0 3px rgba(248, 195, 205, 0.3) !important;
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
/* Slider styling */
|
| 232 |
+
.gr-form input[type=range] {
|
| 233 |
+
appearance: none !important;
|
| 234 |
+
width: 100% !important;
|
| 235 |
+
height: 6px !important;
|
| 236 |
+
background: #e0e0e0 !important;
|
| 237 |
+
border-radius: 5px !important;
|
| 238 |
+
outline: none !important;
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
.gr-form input[type=range]::-webkit-slider-thumb {
|
| 242 |
+
appearance: none !important;
|
| 243 |
+
width: 16px !important;
|
| 244 |
+
height: 16px !important;
|
| 245 |
+
background: var(--primary-color) !important;
|
| 246 |
+
border-radius: 50% !important;
|
| 247 |
+
cursor: pointer !important;
|
| 248 |
+
border: 2px solid white !important;
|
| 249 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1) !important;
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
/* Dropdown styling */
|
| 253 |
+
.gr-form select {
|
| 254 |
+
background-color: white !important;
|
| 255 |
+
border: 2px solid #e0e0e0 !important;
|
| 256 |
+
border-radius: 8px !important;
|
| 257 |
+
padding: 10px 15px !important;
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
.gr-form select option {
|
| 261 |
+
padding: 10px !important;
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
/* Image upload area */
|
| 265 |
+
.gr-image-input {
|
| 266 |
+
border: 2px dashed #b39ddb !important;
|
| 267 |
+
border-radius: var(--border-radius) !important;
|
| 268 |
+
background-color: #f3e5f5 !important;
|
| 269 |
+
padding: 20px !important;
|
| 270 |
+
display: flex !important;
|
| 271 |
+
flex-direction: column !important;
|
| 272 |
+
align-items: center !important;
|
| 273 |
+
justify-content: center !important;
|
| 274 |
+
transition: all 0.3s ease !important;
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
.gr-image-input:hover {
|
| 278 |
+
background-color: #ede7f6 !important;
|
| 279 |
+
border-color: #9575cd !important;
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
/* Add a nice pattern to the background */
|
| 283 |
+
body::before {
|
| 284 |
+
content: "" !important;
|
| 285 |
+
position: fixed !important;
|
| 286 |
+
top: 0 !important;
|
| 287 |
+
left: 0 !important;
|
| 288 |
+
width: 100% !important;
|
| 289 |
+
height: 100% !important;
|
| 290 |
+
background:
|
| 291 |
+
radial-gradient(circle at 10% 20%, rgba(248, 195, 205, 0.1) 0%, rgba(245, 245, 247, 0) 20%),
|
| 292 |
+
radial-gradient(circle at 80% 70%, rgba(179, 229, 252, 0.1) 0%, rgba(245, 245, 247, 0) 20%) !important;
|
| 293 |
+
pointer-events: none !important;
|
| 294 |
+
z-index: -1 !important;
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
/* Gallery styling */
|
| 298 |
+
.gr-gallery {
|
| 299 |
+
grid-gap: 15px !important;
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
.gr-gallery-item {
|
| 303 |
+
border-radius: var(--border-radius) !important;
|
| 304 |
+
overflow: hidden !important;
|
| 305 |
+
box-shadow: 0 4px 8px var(--shadow-color) !important;
|
| 306 |
+
transition: transform 0.3s ease !important;
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
.gr-gallery-item:hover {
|
| 310 |
+
transform: scale(1.02) !important;
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
/* Label styling */
|
| 314 |
+
.gr-form label {
|
| 315 |
+
font-weight: 600 !important;
|
| 316 |
+
color: #673ab7 !important;
|
| 317 |
+
margin-bottom: 5px !important;
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
/* Improve spacing */
|
| 321 |
+
.gr-padded {
|
| 322 |
+
padding: 20px !important;
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
.gr-compact {
|
| 326 |
+
gap: 15px !important;
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
.gr-form > div {
|
| 330 |
+
margin-bottom: 16px !important;
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
/* Headings */
|
| 334 |
+
.gr-form h3 {
|
| 335 |
+
color: #7b1fa2 !important;
|
| 336 |
+
margin-top: 5px !important;
|
| 337 |
+
margin-bottom: 15px !important;
|
| 338 |
+
border-bottom: 2px solid #e1bee7 !important;
|
| 339 |
+
padding-bottom: 8px !important;
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
/* Examples section */
|
| 343 |
+
#examples-panel {
|
| 344 |
+
background-color: #f3e5f5 !important;
|
| 345 |
+
border-radius: var(--border-radius) !important;
|
| 346 |
+
padding: 15px !important;
|
| 347 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05) !important;
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
#examples-panel h2 {
|
| 351 |
+
color: #7b1fa2 !important;
|
| 352 |
+
font-size: 1.5rem !important;
|
| 353 |
+
margin-bottom: 15px !important;
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
/* Accordion styling */
|
| 357 |
+
.gr-accordion {
|
| 358 |
+
border: 1px solid #e0e0e0 !important;
|
| 359 |
+
border-radius: var(--border-radius) !important;
|
| 360 |
+
overflow: hidden !important;
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
.gr-accordion summary {
|
| 364 |
+
padding: 12px 16px !important;
|
| 365 |
+
background-color: #f9f9f9 !important;
|
| 366 |
+
cursor: pointer !important;
|
| 367 |
+
font-weight: 600 !important;
|
| 368 |
+
color: #673ab7 !important;
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
/* Generate button special styling */
|
| 372 |
+
#generate-btn {
|
| 373 |
+
background: linear-gradient(135deg, #ff9a9e, #fad0c4) !important;
|
| 374 |
+
font-size: 1.1rem !important;
|
| 375 |
+
padding: 12px 24px !important;
|
| 376 |
+
margin-top: 10px !important;
|
| 377 |
+
margin-bottom: 15px !important;
|
| 378 |
+
width: 100% !important;
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
#generate-btn:hover {
|
| 382 |
+
background: linear-gradient(135deg, #fad0c4, #ff9a9e) !important;
|
| 383 |
+
}
|
| 384 |
+
"""
|
| 385 |
+
|
| 386 |
+
_HEADER_ = '''
|
| 387 |
+
<div style="text-align: center; max-width: 850px; margin: 0 auto; padding: 25px 0;">
|
| 388 |
+
<div style="background: linear-gradient(135deg, #f8c3cd, #e1bee7, #b3e5fc); color: white; padding: 15px; border-radius: 15px; box-shadow: 0 10px 20px rgba(0,0,0,0.1); margin-bottom: 20px;">
|
| 389 |
+
<h1 style="font-size: 3rem; font-weight: 800; margin: 0; color: white; text-shadow: 2px 2px 4px rgba(0,0,0,0.2);">✨ DreamO Video ✨</h1>
|
| 390 |
+
<p style="font-size: 1.2rem; margin: 10px 0 0;">Create customized images with advanced AI</p>
|
| 391 |
+
</div>
|
| 392 |
+
<div style="background: white; padding: 15px; border-radius: 12px; box-shadow: 0 5px 15px rgba(0,0,0,0.05);">
|
| 393 |
+
<p style="font-size: 1rem; margin: 0;">Paper: <a href='https://arxiv.org/abs/2504.16915' target='_blank' style="color: #9c27b0; font-weight: 600;">DreamO: A Unified Framework for Image Customization</a> |
|
| 394 |
+
Codes: <a href='https://github.com/bytedance/DreamO' target='_blank' style="color: #9c27b0; font-weight: 600;">GitHub</a></p>
|
| 395 |
+
</div>
|
| 396 |
+
</div>
|
| 397 |
+
|
| 398 |
+
<div style="background: #fff9c4; padding: 15px; border-radius: 12px; margin-bottom: 20px; border-left: 5px solid #ffd54f; box-shadow: 0 5px 15px rgba(0,0,0,0.05);">
|
| 399 |
+
<h3 style="margin-top: 0; color: #ff6f00;">🚩 Update Notes:</h3>
|
| 400 |
+
<ul style="margin-bottom: 0; padding-left: 20px;">
|
| 401 |
+
<li><b>2025.05.11:</b> We have updated the model to mitigate over-saturation and plastic-face issues. The new version shows consistent improvements over the previous release.</li>
|
| 402 |
+
<li><b>2025.05.13:</b> 'DreamO Video' Integration version Release</li>
|
| 403 |
+
</ul>
|
| 404 |
+
</div>
|
| 405 |
+
'''
|
| 406 |
+
|
| 407 |
+
_CITE_ = r"""
|
| 408 |
+
<div style="background: white; padding: 20px; border-radius: 12px; margin-top: 20px; box-shadow: 0 5px 15px rgba(0,0,0,0.05);">
|
| 409 |
+
<p style="margin: 0; font-size: 1.1rem;">If DreamO is helpful, please help to ⭐ the <a href='https://discord.gg/openfreeai' target='_blank' style="color: #9c27b0; font-weight: 600;">community</a>. Thanks!</p>
|
| 410 |
+
<hr style="border: none; height: 1px; background-color: #e0e0e0; margin: 15px 0;">
|
| 411 |
+
<h4 style="margin: 0 0 10px; color: #7b1fa2;">📧 Contact</h4>
|
| 412 |
+
<p style="margin: 0;">If you have any questions or feedback, feel free to open a discussion or contact <b>[email protected]</b></p>
|
| 413 |
+
</div>
|
| 414 |
+
"""
|
| 415 |
+
|
| 416 |
+
def create_demo():
|
| 417 |
+
with gr.Blocks(css=_CUSTOM_CSS_) as demo:
|
| 418 |
+
gr.HTML(_HEADER_)
|
| 419 |
+
|
| 420 |
+
with gr.Row():
|
| 421 |
+
with gr.Column(scale=6):
|
| 422 |
+
# Input panel - using a Group div with custom class instead of Box
|
| 423 |
+
with gr.Group(elem_id="input-panel", elem_classes="panel-box"):
|
| 424 |
+
gr.Markdown("### 📸 Reference Images")
|
| 425 |
+
with gr.Row():
|
| 426 |
+
with gr.Column():
|
| 427 |
+
ref_image1 = gr.Image(label="Reference Image 1", type="numpy", height=256, elem_id="ref-image-1")
|
| 428 |
+
ref_task1 = gr.Dropdown(choices=["ip", "id", "style"], value="ip", label="Task for Reference Image 1", elem_id="ref-task-1")
|
| 429 |
+
|
| 430 |
+
with gr.Column():
|
| 431 |
+
ref_image2 = gr.Image(label="Reference Image 2", type="numpy", height=256, elem_id="ref-image-2")
|
| 432 |
+
ref_task2 = gr.Dropdown(choices=["ip", "id", "style"], value="ip", label="Task for Reference Image 2", elem_id="ref-task-2")
|
| 433 |
+
|
| 434 |
+
gr.Markdown("### ✏️ Generation Parameters")
|
| 435 |
+
prompt = gr.Textbox(label="Prompt", value="a person playing guitar in the street", elem_id="prompt-input")
|
| 436 |
+
|
| 437 |
+
with gr.Row():
|
| 438 |
+
width = gr.Slider(768, 1024, 1024, step=16, label="Width", elem_id="width-slider")
|
| 439 |
+
height = gr.Slider(768, 1024, 1024, step=16, label="Height", elem_id="height-slider")
|
| 440 |
+
|
| 441 |
+
with gr.Row():
|
| 442 |
+
num_steps = gr.Slider(8, 30, 12, step=1, label="Number of Steps", elem_id="steps-slider")
|
| 443 |
+
guidance = gr.Slider(1.0, 10.0, 3.5, step=0.1, label="Guidance Scale", elem_id="guidance-slider")
|
| 444 |
+
|
| 445 |
+
seed = gr.Textbox(label="Seed (-1 for random)", value="-1", elem_id="seed-input")
|
| 446 |
+
|
| 447 |
+
with gr.Accordion("Advanced Options", open=False):
|
| 448 |
+
ref_res = gr.Slider(512, 1024, 512, step=16, label="Resolution for Reference Image")
|
| 449 |
+
neg_prompt = gr.Textbox(label="Negative Prompt", value="")
|
| 450 |
+
neg_guidance = gr.Slider(1.0, 10.0, 3.5, step=0.1, label="Negative Guidance")
|
| 451 |
+
|
| 452 |
+
with gr.Row():
|
| 453 |
+
true_cfg = gr.Slider(1, 5, 1, step=0.1, label="True CFG")
|
| 454 |
+
first_step_guidance = gr.Slider(0, 10, 0, step=0.1, label="First Step Guidance")
|
| 455 |
+
|
| 456 |
+
with gr.Row():
|
| 457 |
+
cfg_start_step = gr.Slider(0, 30, 0, step=1, label="CFG Start Step")
|
| 458 |
+
cfg_end_step = gr.Slider(0, 30, 0, step=1, label="CFG End Step")
|
| 459 |
+
|
| 460 |
+
generate_btn = gr.Button("✨ Generate Image", elem_id="generate-btn")
|
| 461 |
+
gr.HTML(_CITE_)
|
| 462 |
+
|
| 463 |
+
with gr.Column(scale=6):
|
| 464 |
+
# Output panel - using a Group div with custom class instead of Box
|
| 465 |
+
with gr.Group(elem_id="output-panel", elem_classes="panel-box"):
|
| 466 |
+
gr.Markdown("### 🖼️ Generated Result")
|
| 467 |
+
output_image = gr.Image(label="Generated Image", elem_id="output-image", format='png')
|
| 468 |
+
seed_output = gr.Textbox(label="Used Seed", elem_id="seed-output")
|
| 469 |
+
|
| 470 |
+
gr.Markdown("### 🔍 Preprocessing")
|
| 471 |
+
debug_image = gr.Gallery(
|
| 472 |
+
label="Preprocessing Results (including face crop and background removal)",
|
| 473 |
+
elem_id="debug-gallery",
|
| 474 |
+
)
|
| 475 |
+
|
| 476 |
+
# Examples panel - using a Group div with custom class instead of Box
|
| 477 |
+
with gr.Group(elem_id="examples-panel", elem_classes="panel-box"):
|
| 478 |
+
gr.Markdown("## 📚 Examples")
|
| 479 |
+
example_inps = [
|
| 480 |
+
[
|
| 481 |
+
'example_inputs/choi.jpg',
|
| 482 |
+
None,
|
| 483 |
+
'ip',
|
| 484 |
+
'ip',
|
| 485 |
+
'a woman sitting on the cloud, playing guitar',
|
| 486 |
+
1206523688721442817,
|
| 487 |
+
],
|
| 488 |
+
[
|
| 489 |
+
'example_inputs/choi.jpg',
|
| 490 |
+
None,
|
| 491 |
+
'id',
|
| 492 |
+
'ip',
|
| 493 |
+
'a woman holding a sign saying "TOP", on the mountain',
|
| 494 |
+
10441727852953907380,
|
| 495 |
+
],
|
| 496 |
+
[
|
| 497 |
+
'example_inputs/perfume.png',
|
| 498 |
+
None,
|
| 499 |
+
'ip',
|
| 500 |
+
'ip',
|
| 501 |
+
'a perfume under spotlight',
|
| 502 |
+
116150031980664704,
|
| 503 |
+
],
|
| 504 |
+
[
|
| 505 |
+
'example_inputs/choi.jpg',
|
| 506 |
+
None,
|
| 507 |
+
'id',
|
| 508 |
+
'ip',
|
| 509 |
+
'portrait, in alps',
|
| 510 |
+
5443415087540486371,
|
| 511 |
+
],
|
| 512 |
+
[
|
| 513 |
+
'example_inputs/mickey.png',
|
| 514 |
+
None,
|
| 515 |
+
'style',
|
| 516 |
+
'ip',
|
| 517 |
+
'generate a same style image. A rooster wearing overalls.',
|
| 518 |
+
6245580464677124951,
|
| 519 |
+
],
|
| 520 |
+
[
|
| 521 |
+
'example_inputs/mountain.png',
|
| 522 |
+
None,
|
| 523 |
+
'style',
|
| 524 |
+
'ip',
|
| 525 |
+
'generate a same style image. A pavilion by the river, and the distant mountains are endless',
|
| 526 |
+
5248066378927500767,
|
| 527 |
+
],
|
| 528 |
+
[
|
| 529 |
+
'example_inputs/shirt.png',
|
| 530 |
+
'example_inputs/skirt.jpeg',
|
| 531 |
+
'ip',
|
| 532 |
+
'ip',
|
| 533 |
+
'A girl is wearing a short-sleeved shirt and a short skirt on the beach.',
|
| 534 |
+
9514069256241143615,
|
| 535 |
+
],
|
| 536 |
+
[
|
| 537 |
+
'example_inputs/woman2.png',
|
| 538 |
+
'example_inputs/dress.png',
|
| 539 |
+
'id',
|
| 540 |
+
'ip',
|
| 541 |
+
'the woman wearing a dress, In the banquet hall',
|
| 542 |
+
7698454872441022867,
|
| 543 |
+
],
|
| 544 |
+
[
|
| 545 |
+
'example_inputs/dog1.png',
|
| 546 |
+
'example_inputs/dog2.png',
|
| 547 |
+
'ip',
|
| 548 |
+
'ip',
|
| 549 |
+
'two dogs in the jungle',
|
| 550 |
+
6187006025405083344,
|
| 551 |
+
],
|
| 552 |
+
]
|
| 553 |
+
gr.Examples(
|
| 554 |
+
examples=example_inps,
|
| 555 |
+
inputs=[ref_image1, ref_image2, ref_task1, ref_task2, prompt, seed],
|
| 556 |
+
label='Examples by category: IP task (rows 1-4), ID task (row 5), Style task (rows 6-7), Try-On task (rows 8-9)',
|
| 557 |
+
cache_examples='lazy',
|
| 558 |
+
outputs=[output_image, debug_image, seed_output],
|
| 559 |
+
fn=generate_image,
|
| 560 |
+
)
|
| 561 |
+
|
| 562 |
+
generate_btn.click(
|
| 563 |
+
fn=generate_image,
|
| 564 |
+
inputs=[
|
| 565 |
+
ref_image1,
|
| 566 |
+
ref_image2,
|
| 567 |
+
ref_task1,
|
| 568 |
+
ref_task2,
|
| 569 |
+
prompt,
|
| 570 |
+
seed,
|
| 571 |
+
width,
|
| 572 |
+
height,
|
| 573 |
+
ref_res,
|
| 574 |
+
num_steps,
|
| 575 |
+
guidance,
|
| 576 |
+
true_cfg,
|
| 577 |
+
cfg_start_step,
|
| 578 |
+
cfg_end_step,
|
| 579 |
+
neg_prompt,
|
| 580 |
+
neg_guidance,
|
| 581 |
+
first_step_guidance,
|
| 582 |
+
],
|
| 583 |
+
outputs=[output_image, debug_image, seed_output],
|
| 584 |
+
)
|
| 585 |
+
|
| 586 |
+
return demo
|
| 587 |
+
|
| 588 |
+
|
| 589 |
+
if __name__ == '__main__':
|
| 590 |
+
demo = create_demo()
|
| 591 |
+
demo.launch()
|