Spaces:
Running
on
Zero
Running
on
Zero
File size: 19,038 Bytes
506ccf8 bb2d84c 5af6243 371c0ec 16bea72 506ccf8 16bea72 371c0ec bb2d84c 5af6243 bb2d84c 371c0ec f2125e1 371c0ec 16bea72 371c0ec bb2d84c ad4cf92 507a133 ad4cf92 f2125e1 c9fb392 1afdb64 f2125e1 bb2d84c 5af6243 d6ca753 bb2d84c 5af6243 b5bc414 5af6243 bb2d84c d6ca753 bf66885 7d86fa0 371c0ec 7d86fa0 371c0ec f2125e1 371c0ec 5d56951 c92f2b8 5af6243 bb2d84c f0f736a bb2d84c 5af6243 00842ed 5af6243 00842ed 5af6243 00842ed 5af6243 f2125e1 d107352 f2125e1 a2cd504 f2125e1 de09a62 a2cd504 f2125e1 a2cd504 f2125e1 5af6243 bb2d84c 5b2aa57 bb2d84c 5b2aa57 efb7244 5b2aa57 efb7244 bb2d84c 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 bb2d84c efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 5d56951 f0f736a 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 bb2d84c 5af6243 7c35e56 efb7244 49b3b0d bb2d84c 5b2aa57 efb7244 5b2aa57 efb7244 bb2d84c 5b2aa57 5af6243 5b2aa57 00842ed efb7244 16bea72 5b2aa57 16bea72 5b2aa57 5af6243 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 efb7244 5b2aa57 b4f488e 5b2aa57 bb2d84c 5b2aa57 bb2d84c b4f488e 5b2aa57 efb7244 5b2aa57 b4f488e 5b2aa57 00842ed 5b2aa57 00842ed f2125e1 bb2d84c 49b3b0d b4f488e 00842ed 5b2aa57 00842ed b4f488e 00842ed 5b2aa57 00842ed 5b2aa57 bb2d84c b4f488e efb7244 5b2aa57 efb7244 bb2d84c efb7244 5b2aa57 efb7244 bb2d84c 5af6243 bb2d84c 5af6243 bb2d84c 6936a77 efb7244 00842ed f2125e1 efb7244 bb2d84c efb7244 49b3b0d efb7244 5b2aa57 efb7244 5b2aa57 efb7244 49b3b0d |
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 |
import gradio as gr
import numpy as np
import torch, random, json, spaces, time
from ulid import ULID
from diffsynth.pipelines.qwen_image import (
QwenImagePipeline, ModelConfig,
QwenImageUnit_Image2LoRAEncode, QwenImageUnit_Image2LoRADecode
)
from safetensors.torch import save_file
import torch
from PIL import Image
from utils import repo_utils, image_utils, prompt_utils
# repo_utils.clone_repo_if_not_exists("git clone https://huggingface.co/DiffSynth-Studio/General-Image-Encoders", "app/repos")
# repo_utils.clone_repo_if_not_exists("https://huggingface.co/apple/starflow", "app/models")
URL_PUBLIC = "https://huggingface.co/spaces/AiSudo/Qwen-Image-to-LoRA/blob/main"
DTYPE = torch.bfloat16
MAX_SEED = np.iinfo(np.int32).max
vram_config_disk_offload = {
"offload_dtype": "disk",
"offload_device": "disk",
"onload_dtype": "disk",
"onload_device": "disk",
"preparing_dtype": torch.bfloat16,
"preparing_device": "cuda",
"computation_dtype": torch.bfloat16,
"computation_device": "cuda",
}
# Load models
pipe_lora = QwenImagePipeline.from_pretrained(
torch_dtype=torch.bfloat16,
device="cuda",
model_configs=[
ModelConfig(
download_source="huggingface",
model_id="DiffSynth-Studio/General-Image-Encoders",
origin_file_pattern="SigLIP2-G384/model.safetensors",
**vram_config_disk_offload
),
ModelConfig(
download_source="huggingface",
model_id="DiffSynth-Studio/General-Image-Encoders",
origin_file_pattern="DINOv3-7B/model.safetensors",
**vram_config_disk_offload
),
ModelConfig(
download_source="huggingface",
model_id="DiffSynth-Studio/Qwen-Image-i2L",
origin_file_pattern="Qwen-Image-i2L-Style.safetensors",
**vram_config_disk_offload
),
],
processor_config=ModelConfig(model_id="Qwen/Qwen-Image-Edit", origin_file_pattern="processor/"),
vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 0.5,
)
vram_config = {
"offload_dtype": "disk",
"offload_device": "disk",
"onload_dtype": torch.bfloat16,
"onload_device": "cuda",
"preparing_dtype": torch.bfloat16,
"preparing_device": "cuda",
"computation_dtype": torch.bfloat16,
"computation_device": "cuda",
}
pipe_imagen = QwenImagePipeline.from_pretrained(
torch_dtype=torch.bfloat16,
device="cuda",
model_configs=[
ModelConfig(download_source="huggingface", model_id="Qwen/Qwen-Image", origin_file_pattern="transformer/diffusion_pytorch_model*.safetensors", **vram_config),
ModelConfig(download_source="huggingface", model_id="Qwen/Qwen-Image", origin_file_pattern="text_encoder/model*.safetensors", **vram_config),
ModelConfig(download_source="huggingface", model_id="Qwen/Qwen-Image", origin_file_pattern="vae/diffusion_pytorch_model.safetensors", **vram_config),
],
tokenizer_config=ModelConfig(download_source="huggingface", model_id="Qwen/Qwen-Image", origin_file_pattern="tokenizer/"),
vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 0.5,
)
@spaces.GPU
def generate_lora(
input_images,
progress=gr.Progress(track_tqdm=True),
):
ulid = str(ULID()).lower()[:12]
print(f"ulid: {ulid}")
if not input_images:
print("images are empty.")
return False
input_images = [Image.open(filepath).convert("RGB") for filepath, _ in input_images]
# Model inference
with torch.no_grad():
embs = QwenImageUnit_Image2LoRAEncode().process(pipe_lora, image2lora_images=input_images)
lora = QwenImageUnit_Image2LoRADecode().process(pipe_lora, **embs)["lora"]
lora_name = f"{ulid}.safetensors"
lora_path = f"loras/{lora_name}"
save_file(lora, lora_path)
return lora_name, gr.update(interactive=True, value=lora_path), gr.update(interactive=True)
@spaces.GPU
def generate_image(
lora_name,
prompt,
negative_prompt="blurry ugly bad",
width=1024,
height=1024,
seed=42,
randomize_seed=True,
guidance_scale=3.5,
num_inference_steps=8,
progress=gr.Progress(track_tqdm=True),
):
lora_path = f"loras/{lora_name}"
pipe_imagen.clear_lora()
pipe_imagen.load_lora(pipe_imagen.dit, lora_path)
if randomize_seed:
seed = random.randint(0, MAX_SEED)
generator = torch.Generator().manual_seed(seed)
output_image = pipe_imagen(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=num_inference_steps,
width=width,
height=height,
# generator=generator,
# true_cfg_scale=guidance_scale,
# guidance_scale=1.0 # Use a fixed default for distilled guidance
)
return output_image, seed
return True
def read_file(path: str) -> str:
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
return content
# Enhanced Apple-style CSS - more minimalist and clean
css = """
/* Pure Apple Design System */
.gradio-container {
font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", "Helvetica Neue", Helvetica, Arial, sans-serif !important;
background: #ffffff !important;
color: #1d1d1f !important;
line-height: 1.47059 !important;
font-weight: 400 !important;
letter-spacing: -.022em !important;
}
#col-container {
margin: 0 auto;
max-width: 980px;
padding: 40px 20px;
}
/* Ultra-minimalist header */
.gradio-container .gr-block-header {
background: transparent !important;
border: none !important;
padding: 0 !important;
margin-bottom: 60px !important;
box-shadow: none !important;
}
.gradio-container h1 {
font-weight: 600 !important;
font-size: 3rem !important;
color: #1d1d1f !important;
text-align: center !important;
margin-bottom: 16px !important;
letter-spacing: -.003em !important;
}
.gradio-container .subtitle {
font-size: 1.25rem !important;
font-weight: 400 !important;
color: #6e6e73 !important;
text-align: center !important;
margin-bottom: 8px !important;
line-height: 1.4 !important;
}
/* Clean card sections */
.section-card {
background: #f2f2f7 !important;
border-radius: 18px !important;
padding: 32px !important;
margin-bottom: 32px !important;
border: none !important;
box-shadow: none !important;
}
/* Apple-style buttons */
.gradio-container .gr-button {
background: #007aff !important;
border: none !important;
border-radius: 8px !important;
color: white !important;
font-weight: 500 !important;
font-size: 17px !important;
padding: 16px 32px !important;
min-height: 44px !important;
transition: all 0.15s ease !important;
box-shadow: none !important;
letter-spacing: -.022em !important;
}
.gradio-container .gr-button:hover {
background: #0051d5 !important;
transform: none !important;
box-shadow: none !important;
}
.gradio-container .gr-button:active {
background: #004bb8 !important;
transform: scale(0.98) !important;
}
/* Clean input fields */
.gradio-container .gr-textbox,
.gradio-container .gr-slider {
background: #ffffff !important;
border: 1px solid #d2d2d7 !important;
border-radius: 10px !important;
padding: 12px 16px !important;
font-size: 17px !important;
color: #1d1d1f !important;
transition: all 0.15s ease !important;
min-height: 44px !important;
}
.gradio-container .gr-textbox:focus,
.gradio-container .gr-slider:focus {
border-color: #007aff !important;
box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.1) !important;
outline: none !important;
}
/* Gallery styling */
.gradio-container .gr-gallery {
border-radius: 12px !important;
border: 1px solid #d2d2d7 !important;
background: #ffffff !important;
overflow: hidden !important;
}
/* Image output */
.gradio-container .gr-image {
border-radius: 12px !important;
border: 1px solid #d2d2d7 !important;
background: #ffffff !important;
overflow: hidden !important;
}
/* Accordion - Apple style */
.gradio-container .gr-accordion {
background: #f2f2f7 !important;
border: none !important;
border-radius: 12px !important;
padding: 0 !important;
margin-top: 24px !important;
}
.gradio-container .gr-accordion .gr-accordion-button {
background: transparent !important;
border: none !important;
padding: 16px !important;
font-weight: 500 !important;
color: #1d1d1f !important;
}
/* Download button */
.gradio-container .gr-download-button {
background: #34c759 !important;
border: none !important;
border-radius: 8px !important;
color: white !important;
font-weight: 500 !important;
font-size: 17px !important;
padding: 16px 32px !important;
min-height: 44px !important;
}
.gradio-container .gr-download-button:hover {
background: #30a14a !important;
}
/* Checkbox styling */
.gradio-container .gr-checkbox {
background: #ffffff !important;
border: 2px solid #d2d2d7 !important;
border-radius: 6px !important;
width: 24px !important;
height: 24px !important;
}
.gradio-container .gr-checkbox.checked {
background: #007aff !important;
border-color: #007aff !important;
}
/* Examples section */
.gradio-container .gr-examples {
background: #f2f2f7 !important;
border-radius: 18px !important;
padding: 24px !important;
border: none !important;
}
/* Label styling */
.gradio-container .gr-label {
font-weight: 600 !important;
color: #1d1d1f !important;
font-size: 17px !important;
margin-bottom: 8px !important;
letter-spacing: -.022em !important;
}
/* Mobile responsiveness */
@media (max-width: 768px) {
#col-container {
padding: 20px 16px !important;
max-width: 100% !important;
}
.gradio-container h1 {
font-size: 2rem !important;
margin-bottom: 12px !important;
}
.gradio-container .subtitle {
font-size: 1.1rem !important;
}
.section-card {
padding: 24px !important;
margin-bottom: 24px !important;
}
.gradio-container .gr-button {
padding: 14px 28px !important;
font-size: 16px !important;
}
.gradio-container .gr-gallery {
height: 200px !important;
columns: 2 !important;
}
.gradio-container .gr-row {
flex-direction: column !important;
gap: 20px !important;
}
}
@media (max-width: 480px) {
.gradio-container h1 {
font-size: 1.75rem !important;
}
.section-card {
padding: 20px !important;
}
.gradio-container .gr-gallery {
height: 180px !important;
columns: 1 !important;
}
}
/* Remove all animations for Apple-like snappiness */
.gradio-container * {
transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease !important;
}
/* Hide unnecessary elements */
.gradio-container .gr-footer,
.gradio-container .gr-header {
display: none !important;
}
/* Clean scrollbar */
.gradio-container ::-webkit-scrollbar {
width: 3px !important;
}
.gradio-container ::-webkit-scrollbar-track {
background: transparent !important;
}
.gradio-container ::-webkit-scrollbar-thumb {
background: #d2d2d7 !important;
border-radius: 3px !important;
}
.gradio-container ::-webkit-scrollbar-thumb:hover {
background: #007aff !important;
}
"""
with open('examples/0_examples.json', 'r') as file: examples = json.load(file)
print(examples)
# Gradio 6 syntax - no parameters in Blocks()
with gr.Blocks() as demo:
with gr.Column(elem_id="col-container"):
with gr.Column():
# Ultra-minimalist Apple-style header
gr.HTML("""
<div style="text-align: center; max-width: 600px; margin: 0 auto;">
<h1>Qwen Image to LoRA</h1>
<p class="subtitle">Generate custom LoRA models from your images</p>
<p style="font-size: 14px; color: #86868b; margin-top: 16px;">
Demo by <a href="https://aisudo.com/" target="_blank" style="color: #007aff; text-decoration: none;">AiSudo</a> •
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #007aff; text-decoration: none;">Built with anycoder</a>
</p>
</div>
""")
with gr.Row():
with gr.Column(elem_classes=["section-card"]):
input_images = gr.Gallery(
label="Input Images",
file_types=["image"],
show_label=True,
columns=2,
object_fit="cover",
height=250)
lora_button = gr.Button("Generate LoRA", size="lg")
with gr.Column(elem_classes=["section-card"]):
lora_name = gr.Textbox(
label="Generated LoRA",
lines=2,
interactive=False,
placeholder="Your LoRA will appear here..."
)
lora_download = gr.DownloadButton(
label="Download LoRA",
interactive=False,
size="lg"
)
with gr.Column(elem_classes=["section-card"]) as imagen_container:
gr.Markdown("### Generate Images")
with gr.Row():
with gr.Column():
prompt = gr.Textbox(
label="Prompt",
lines=2,
placeholder="Describe what you want to generate...",
value="a person in a fishing boat.",
)
imagen_button = gr.Button("Generate Image", interactive=False, size="lg")
with gr.Accordion("Settings", open=False):
negative_prompt = gr.Textbox(
label="Negative Prompt",
lines=1,
placeholder="What to avoid...",
value="blurry, low quality"
)
num_inference_steps = gr.Slider(
label="Steps",
minimum=1,
maximum=50,
step=1,
value=25,
)
guidance_scale = gr.Slider(
label="Guidance Scale",
minimum=1.0,
maximum=10.0,
step=0.1,
value=3.5,
)
with gr.Row():
width = gr.Slider(
label="Width",
minimum=512,
maximum=1280,
step=32,
value=768,
)
height = gr.Slider(
label="Height",
minimum=512,
maximum=1280,
step=32,
value=1024,
)
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=42,
)
randomize_seed = gr.Checkbox(label="Randomize Seed", value=False)
with gr.Column():
output_image = gr.Image(
label="Generated Image",
height=350
)
gr.Examples(
examples=examples,
inputs=[input_images],
label="Examples"
)
gr.Markdown(read_file("static/footer.md"))
lora_button.click(
fn=generate_lora,
inputs=[
input_images
],
outputs=[lora_name, lora_download, imagen_button],
api_visibility="public"
)
imagen_button.click(
fn=generate_image,
inputs=[
lora_name,
prompt,
negative_prompt,
width,
height,
seed,
randomize_seed,
guidance_scale,
num_inference_steps,
],
outputs=[output_image, seed],
api_visibility="public"
)
if __name__ == "__main__":
# Gradio 6 syntax - all launch parameters go here
demo.launch(
css=css, # Moved from Blocks() to launch() - Gradio 6 syntax
mcp_server=True,
theme=gr.themes.Base(
primary_hue="blue",
secondary_hue="gray",
neutral_hue="gray",
font=[
gr.themes.GoogleFont("Inter"),
"ui-sans-serif",
"system-ui",
"sans-serif"
],
font_mono=[
gr.themes.GoogleFont("JetBrains Mono"),
"ui-monospace",
"Consolas",
"monospace"
]
).set(
body_background_fill="white",
body_background_fill_dark="#000000",
button_primary_background_fill="#007aff",
button_primary_background_fill_hover="#0051d5",
button_primary_background_fill_active="#004bb8",
button_primary_text_color="white",
button_secondary_background_fill="#f2f2f7",
button_secondary_background_fill_hover="#e5e5ea",
block_background_fill="white",
block_background_fill_dark="#1c1c1e",
block_border_width="0px",
block_border_color="#d2d2d7",
block_border_color_dark="#38383a",
block_radius="18px",
block_shadow="none",
block_label_text_color="#1d1d1f",
block_label_text_color_dark="#f2f2f2",
block_title_text_color="#1d1d1f",
block_title_text_color_dark="#f2f2f2",
input_background_fill="white",
input_background_fill_dark="#1c1c1e",
input_border_color="#d2d2d7",
input_border_color_dark="#38383a",
input_border_width="1px",
input_radius="10px",
input_shadow="none",
input_text_color="#1d1d1f",
input_text_color_dark="#f2f2f2",
),
footer_links=[
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
]
) |