Spaces:
Running
on
Zero
Running
on
Zero
File size: 16,500 Bytes
1cb89a8 2a111d5 9ac11e6 2a111d5 9ac11e6 1cb89a8 20fed2a 3638b57 2a111d5 1cb89a8 2a111d5 20fed2a 2a111d5 e28e511 cf16bb0 2a111d5 6fd4fd2 e28e511 2a111d5 6fd4fd2 cf16bb0 e28e511 2a111d5 3638b57 2a111d5 dd441ec 2a111d5 9ac11e6 2a111d5 9ac11e6 2a111d5 9ac11e6 2a111d5 9ac11e6 6fd4fd2 96e3744 cf16bb0 bdba128 cf16bb0 9ac11e6 2a111d5 6fd4fd2 9ac11e6 2a111d5 9ac11e6 6fd4fd2 cf16bb0 2a111d5 bdba128 9ac11e6 96e3744 6fd4fd2 3fe273f 2a111d5 cf16bb0 2a111d5 cf16bb0 2a111d5 cf16bb0 2a111d5 cf16bb0 2a111d5 fd25dcd 2a111d5 cf16bb0 2a111d5 9ac11e6 2a111d5 fd25dcd cf16bb0 2a111d5 cf16bb0 2a111d5 9ac11e6 2a111d5 9ac11e6 2a111d5 9ac11e6 2a111d5 9ac11e6 2a111d5 9ac11e6 2a111d5 9ac11e6 2a111d5 9ac11e6 2a111d5 9ac11e6 2a111d5 9ac11e6 0427012 9ac11e6 2a111d5 3638b57 20fed2a 2a111d5 fd25dcd fc3e76d |
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 |
"""
UniPic-3 DMD Multi-Image Composition
Hugging Face Space - ZeroGPU 优化版本 V5
关键策略:
1. 全局只加载不需要 GPU 的组件(scheduler, tokenizer, processor)
2. 需要 GPU 的模型在 @spaces.GPU 内部加载,显式指定 device='cuda'
3. 不使用 device_map='auto',因为它可能在 ZeroGPU 外部被错误地分配
"""
import gradio as gr
import torch
from PIL import Image
import os
import sys
# Hugging Face Spaces GPU decorator
try:
import spaces
HF_SPACES = True
print("✅ Running in Hugging Face Spaces with ZeroGPU")
except ImportError:
HF_SPACES = False
print("⚠️ Running locally (no ZeroGPU)")
class spaces:
@staticmethod
def GPU(duration=60):
def decorator(func):
return func
return decorator
# Local pipeline import
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# Model configuration
MODEL_NAME = os.environ.get("MODEL_NAME", "Skywork/Unipic3-DMD")
TRANSFORMER_PATH = os.environ.get("TRANSFORMER_PATH", "Skywork/Unipic3-DMD/ema_transformer")
dtype = torch.bfloat16
# ============================================================
# 全局加载轻量级组件(不需要 GPU)
# ============================================================
print("🚀 Loading lightweight components (CPU)...")
from diffusers import (
FlowMatchEulerDiscreteScheduler,
QwenImageTransformer2DModel,
AutoencoderKLQwenImage
)
from transformers import AutoModel, AutoTokenizer, Qwen2VLProcessor
try:
from pipeline_qwenimage_edit import QwenImageEditPipeline
except ImportError:
from diffusers import QwenImageEditPipeline
# 这些组件不需要 GPU,可以在全局加载
scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(
MODEL_NAME, subfolder='scheduler'
)
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, subfolder='tokenizer')
processor = Qwen2VLProcessor.from_pretrained(MODEL_NAME, subfolder='processor')
print("✅ Lightweight components loaded!")
# ============================================================
# Pipeline 状态
# ============================================================
pipe = None
_models_loaded = False
# ============================================================
# GPU 推理函数 - 模型在这里加载
# ============================================================
@spaces.GPU(duration=180)
def generate_image(
images: list[Image.Image],
prompt: str,
true_cfg_scale: float,
seed: int,
num_steps: int
) -> Image.Image:
"""
GPU 推理函数
关键:所有需要 GPU 的模型都在这里加载,确保在真实 GPU 环境中
"""
global pipe, _models_loaded
print(f"🎨 Generating with {len(images)} image(s)...")
print(f" Prompt: {prompt[:50]}...")
print(f" Steps: {num_steps}, CFG: {true_cfg_scale}, Seed: {seed}")
# 在真实 GPU 环境中加载模型(首次调用时)
if not _models_loaded:
print(" [INIT] Loading models on real GPU...")
device = 'cuda'
# 加载 text_encoder 到 GPU
print(" [INIT] Loading text_encoder...")
text_encoder = AutoModel.from_pretrained(
MODEL_NAME,
subfolder='text_encoder',
torch_dtype=dtype,
).to(device).eval()
# 加载 transformer 到 GPU
print(" [INIT] Loading transformer...")
if os.path.exists(TRANSFORMER_PATH) and os.path.isdir(TRANSFORMER_PATH):
config_path = os.path.join(TRANSFORMER_PATH, "config.json")
if os.path.exists(config_path):
transformer = QwenImageTransformer2DModel.from_pretrained(
TRANSFORMER_PATH,
torch_dtype=dtype,
use_safetensors=False
).to(device).eval()
else:
transformer = QwenImageTransformer2DModel.from_pretrained(
TRANSFORMER_PATH,
subfolder='transformer',
torch_dtype=dtype,
use_safetensors=False
).to(device).eval()
else:
path_parts = TRANSFORMER_PATH.split('/')
if len(path_parts) >= 3:
repo_id = '/'.join(path_parts[:2])
subfolder = '/'.join(path_parts[2:])
transformer = QwenImageTransformer2DModel.from_pretrained(
repo_id,
subfolder=subfolder,
torch_dtype=dtype,
use_safetensors=False
).to(device).eval()
else:
transformer = QwenImageTransformer2DModel.from_pretrained(
TRANSFORMER_PATH,
subfolder='transformer',
torch_dtype=dtype,
use_safetensors=False
).to(device).eval()
# 加载 VAE 到 GPU
print(" [INIT] Loading VAE...")
vae = AutoencoderKLQwenImage.from_pretrained(
MODEL_NAME,
subfolder='vae',
torch_dtype=dtype,
).to(device).eval()
# 创建 Pipeline
print(" [INIT] Creating pipeline...")
pipe = QwenImageEditPipeline(
scheduler=scheduler,
vae=vae,
text_encoder=text_encoder,
tokenizer=tokenizer,
processor=processor,
transformer=transformer
)
_models_loaded = True
print(" [INIT] ✅ Models loaded successfully!")
# 验证设备
print(f" [DEBUG] text_encoder device: {next(pipe.text_encoder.parameters()).device}")
print(f" [DEBUG] transformer device: {next(pipe.transformer.parameters()).device}")
print(f" [DEBUG] vae device: {next(pipe.vae.parameters()).device}")
# Generate
with torch.no_grad():
generator = torch.Generator(device='cuda').manual_seed(int(seed))
if len(images) == 1:
result = pipe(
images[0],
prompt=prompt,
height=1024,
width=1024,
negative_prompt=' ',
num_inference_steps=num_steps,
true_cfg_scale=true_cfg_scale,
generator=generator
).images[0]
else:
result = pipe(
images=images,
prompt=prompt,
height=1024,
width=1024,
negative_prompt=' ',
num_inference_steps=num_steps,
true_cfg_scale=true_cfg_scale,
generator=generator
).images[0]
print("✅ Generation complete!")
return result
# ============================================================
# UI 逻辑(CPU,始终可用)
# ============================================================
def process_images(
img1, img2, img3, img4, img5, img6,
prompt: str,
cfg_scale: float,
seed: int,
num_steps: int
):
"""处理图像 - 验证输入后调用 GPU 函数"""
images = [img for img in [img1, img2, img3, img4, img5, img6] if img is not None]
if len(images) == 0:
return None, "❌ Please upload at least one image"
if len(images) > 6:
return None, f"❌ Maximum 6 images allowed (got {len(images)})"
if not prompt or prompt.strip() == "":
return None, "❌ Please enter an editing instruction"
try:
images = [img.convert("RGB") for img in images]
result = generate_image(
images=images,
prompt=prompt,
true_cfg_scale=cfg_scale,
seed=seed,
num_steps=num_steps
)
return result, f"✅ Generated from {len(images)} image(s) in {num_steps} steps"
except Exception as e:
import traceback
traceback.print_exc()
return None, f"❌ Error: {str(e)}"
def update_image_visibility(num):
return [gr.update(visible=(i < num)) for i in range(6)]
# ============================================================
# 自定义 CSS
# ============================================================
CUSTOM_CSS = """
@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap');
:root {
--primary: #6366f1;
--primary-dark: #4f46e5;
--accent: #f472b6;
--surface: #0f0f23;
--surface-light: #1a1a3e;
--surface-elevated: #252552;
--text: #e2e8f0;
--text-muted: #94a3b8;
--border: #334155;
--success: #10b981;
--error: #ef4444;
--gradient-1: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
--gradient-hero: linear-gradient(135deg, #0f0f23 0%, #1a1a3e 50%, #252552 100%);
}
.gradio-container {
font-family: 'Outfit', sans-serif !important;
background: var(--gradient-hero) !important;
min-height: 100vh;
}
.main-header {
text-align: center;
padding: 2rem 1rem;
background: linear-gradient(180deg, rgba(99, 102, 241, 0.1) 0%, transparent 100%);
border-radius: 24px;
margin-bottom: 2rem;
border: 1px solid rgba(99, 102, 241, 0.2);
}
.main-header h1 {
font-size: 2.5rem;
font-weight: 700;
background: linear-gradient(135deg, #fff 0%, #a5b4fc 50%, #f472b6 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
margin-bottom: 0.5rem;
}
.main-header p {
color: var(--text-muted);
font-size: 1.1rem;
max-width: 600px;
margin: 0 auto;
}
.feature-badges {
display: flex;
gap: 1rem;
justify-content: center;
flex-wrap: wrap;
margin-top: 1.5rem;
}
.badge {
display: inline-flex;
align-items: center;
gap: 0.5rem;
padding: 0.5rem 1rem;
background: rgba(99, 102, 241, 0.15);
border: 1px solid rgba(99, 102, 241, 0.3);
border-radius: 9999px;
color: #a5b4fc;
font-size: 0.875rem;
font-weight: 500;
}
.section-header {
display: flex;
align-items: center;
gap: 0.75rem;
margin-bottom: 1rem;
padding-bottom: 0.75rem;
border-bottom: 1px solid var(--border);
}
.section-header h3 {
font-size: 1.125rem;
font-weight: 600;
color: var(--text);
margin: 0;
}
.generate-btn {
background: var(--gradient-1) !important;
border: none !important;
border-radius: 12px !important;
padding: 1rem 2rem !important;
font-size: 1.1rem !important;
font-weight: 600 !important;
color: white !important;
cursor: pointer !important;
transition: all 0.3s ease !important;
box-shadow: 0 4px 15px rgba(99, 102, 241, 0.4) !important;
}
.generate-btn:hover {
transform: translateY(-2px) !important;
box-shadow: 0 6px 20px rgba(99, 102, 241, 0.5) !important;
}
.output-image {
border-radius: 16px;
overflow: hidden;
border: 2px solid transparent;
background: linear-gradient(var(--surface-light), var(--surface-light)) padding-box,
var(--gradient-1) border-box;
}
@media (max-width: 768px) {
.main-header h1 { font-size: 1.75rem; }
.feature-badges { flex-direction: column; align-items: center; }
}
"""
# ============================================================
# 构建 Gradio 界面
# ============================================================
def create_demo():
with gr.Blocks(
title="UniPic-3 DMD",
theme=gr.themes.Base(
primary_hue="indigo",
secondary_hue="pink",
neutral_hue="slate",
font=("Outfit", "sans-serif"),
),
css=CUSTOM_CSS
) as demo:
gr.HTML("""
<div class="main-header">
<h1>🎨 UniPic-3 DMD</h1>
<p>Multi-Image Composition with Distribution-Matching Distillation</p>
<div class="feature-badges">
<span class="badge">⚡ 8-Step Fast Inference</span>
<span class="badge">🖼️ Up to 6 Images</span>
<span class="badge">🚀 12.5× Speedup</span>
</div>
</div>
""")
with gr.Row(equal_height=True):
with gr.Column(scale=1):
gr.HTML('<div class="section-header"><span>📸</span><h3>Upload Images</h3></div>')
num_images = gr.Slider(minimum=1, maximum=6, value=2, step=1,
label="Number of Images", info="Select how many images to compose")
with gr.Row():
img1 = gr.Image(type="pil", label="Image 1", visible=True)
img2 = gr.Image(type="pil", label="Image 2", visible=True)
with gr.Row():
img3 = gr.Image(type="pil", label="Image 3", visible=False)
img4 = gr.Image(type="pil", label="Image 4", visible=False)
with gr.Row():
img5 = gr.Image(type="pil", label="Image 5", visible=False)
img6 = gr.Image(type="pil", label="Image 6", visible=False)
image_inputs = [img1, img2, img3, img4, img5, img6]
num_images.change(fn=update_image_visibility, inputs=num_images, outputs=image_inputs)
gr.HTML('<div class="section-header"><span>✍️</span><h3>Editing Instruction</h3></div>')
prompt_input = gr.Textbox(
label="Prompt",
placeholder="e.g., A man from Image1 standing on a surfboard from Image2...",
lines=3,
value="Combine the reference images to generate the final result."
)
with gr.Accordion("⚙️ Advanced Settings", open=False):
cfg_scale = gr.Slider(minimum=1.0, maximum=10.0, value=4.0, step=0.5,
label="CFG Scale", info="Higher = more prompt alignment")
with gr.Row():
seed = gr.Number(value=42, label="Seed", info="For reproducibility", precision=0)
num_steps = gr.Slider(minimum=1, maximum=8, value=8, step=1,
label="Steps", info="8 recommended for DMD")
generate_btn = gr.Button("🚀 Generate Image", variant="primary", size="lg",
elem_classes=["generate-btn"])
with gr.Column(scale=1):
gr.HTML('<div class="section-header"><span>🎨</span><h3>Generated Result</h3></div>')
output_image = gr.Image(type="pil", label="Output", elem_classes=["output-image"])
status_text = gr.Textbox(
label="Status",
value="✨ Ready! First run takes ~60s to load models.",
interactive=False,
)
gr.HTML("""
<div style="margin-top: 1.5rem; padding: 1rem; background: rgba(99, 102, 241, 0.1);
border-radius: 12px; border: 1px solid rgba(99, 102, 241, 0.2);">
<p style="color: #ffffff; font-weight: 600; margin-bottom: 0.5rem;">💡 Tips</p>
<ul style="color: #ffffff; font-size: 0.9rem; margin: 0; padding-left: 1.25rem;">
<li>Reference images as "Image1", "Image2", etc.</li>
<li>First run loads models (~60s)</li>
</ul>
</div>
""")
generate_btn.click(
fn=process_images,
inputs=[*image_inputs, prompt_input, cfg_scale, seed, num_steps],
outputs=[output_image, status_text]
)
gr.HTML('<div class="section-header" style="margin-top: 2rem;"><span>📚</span><h3>Example Prompts</h3></div>')
gr.Examples(
examples=[
["A person from Image1 wearing the outfit from Image2"],
["Combine Image1 and Image2 into a single cohesive scene"],
["The object from Image1 placed in the environment from Image2"],
],
inputs=[prompt_input],
label=""
)
return demo
demo = create_demo()
if __name__ == "__main__":
demo.launch() |