Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
-
import torch, random, json, spaces
|
| 4 |
from ulid import ULID
|
| 5 |
from diffsynth.pipelines.qwen_image import (
|
| 6 |
QwenImagePipeline, ModelConfig,
|
| 7 |
QwenImageUnit_Image2LoRAEncode, QwenImageUnit_Image2LoRADecode
|
| 8 |
)
|
| 9 |
from safetensors.torch import save_file
|
| 10 |
-
import torch
|
| 11 |
from PIL import Image
|
| 12 |
-
from utils import repo_utils, image_utils, prompt_utils
|
| 13 |
-
|
| 14 |
|
|
|
|
| 15 |
# repo_utils.clone_repo_if_not_exists("git clone https://huggingface.co/DiffSynth-Studio/General-Image-Encoders", "app/repos")
|
| 16 |
# repo_utils.clone_repo_if_not_exists("https://huggingface.co/apple/starflow", "app/models")
|
| 17 |
|
|
@@ -19,6 +17,7 @@ URL_PUBLIC = "https://huggingface.co/spaces/AiSudo/Qwen-Image-to-LoRA/blob/main"
|
|
| 19 |
DTYPE = torch.bfloat16
|
| 20 |
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
|
|
|
|
| 22 |
vram_config_disk_offload = {
|
| 23 |
"offload_dtype": "disk",
|
| 24 |
"offload_device": "disk",
|
|
@@ -30,28 +29,28 @@ vram_config_disk_offload = {
|
|
| 30 |
"computation_device": "cuda",
|
| 31 |
}
|
| 32 |
|
| 33 |
-
# Load models
|
| 34 |
pipe_lora = QwenImagePipeline.from_pretrained(
|
| 35 |
torch_dtype=torch.bfloat16,
|
| 36 |
device="cuda",
|
| 37 |
model_configs=[
|
| 38 |
ModelConfig(
|
| 39 |
-
download_source="huggingface",
|
| 40 |
-
model_id="DiffSynth-Studio/General-Image-Encoders",
|
| 41 |
-
origin_file_pattern="SigLIP2-G384/model.safetensors",
|
| 42 |
-
**vram_config_disk_offload
|
| 43 |
),
|
| 44 |
ModelConfig(
|
| 45 |
-
download_source="huggingface",
|
| 46 |
-
model_id="DiffSynth-Studio/General-Image-Encoders",
|
| 47 |
-
origin_file_pattern="DINOv3-7B/model.safetensors",
|
| 48 |
-
**vram_config_disk_offload
|
| 49 |
),
|
| 50 |
ModelConfig(
|
| 51 |
-
download_source="huggingface",
|
| 52 |
-
model_id="DiffSynth-Studio/Qwen-Image-i2L",
|
| 53 |
-
origin_file_pattern="Qwen-Image-i2L-Style.safetensors",
|
| 54 |
-
**vram_config_disk_offload
|
| 55 |
),
|
| 56 |
],
|
| 57 |
processor_config=ModelConfig(model_id="Qwen/Qwen-Image-Edit", origin_file_pattern="processor/"),
|
|
@@ -69,6 +68,7 @@ vram_config = {
|
|
| 69 |
"computation_device": "cuda",
|
| 70 |
}
|
| 71 |
|
|
|
|
| 72 |
pipe_imagen = QwenImagePipeline.from_pretrained(
|
| 73 |
torch_dtype=torch.bfloat16,
|
| 74 |
device="cuda",
|
|
@@ -82,32 +82,101 @@ pipe_imagen = QwenImagePipeline.from_pretrained(
|
|
| 82 |
)
|
| 83 |
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
@spaces.GPU
|
| 86 |
def generate_lora(
|
| 87 |
input_images,
|
|
|
|
|
|
|
| 88 |
progress=gr.Progress(track_tqdm=True),
|
| 89 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
ulid = str(ULID()).lower()[:12]
|
| 92 |
print(f"ulid: {ulid}")
|
| 93 |
|
| 94 |
if not input_images:
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
|
|
|
| 98 |
input_images = [Image.open(filepath).convert("RGB") for filepath, _ in input_images]
|
| 99 |
-
|
| 100 |
# Model inference
|
| 101 |
with torch.no_grad():
|
| 102 |
embs = QwenImageUnit_Image2LoRAEncode().process(pipe_lora, image2lora_images=input_images)
|
| 103 |
lora = QwenImageUnit_Image2LoRADecode().process(pipe_lora, **embs)["lora"]
|
| 104 |
|
| 105 |
lora_name = f"{ulid}.safetensors"
|
|
|
|
| 106 |
lora_path = f"loras/{lora_name}"
|
| 107 |
-
|
| 108 |
save_file(lora, lora_path)
|
| 109 |
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
@spaces.GPU
|
| 113 |
def generate_image(
|
|
@@ -122,14 +191,18 @@ def generate_image(
|
|
| 122 |
num_inference_steps=8,
|
| 123 |
progress=gr.Progress(track_tqdm=True),
|
| 124 |
):
|
|
|
|
|
|
|
|
|
|
| 125 |
lora_path = f"loras/{lora_name}"
|
|
|
|
| 126 |
pipe_imagen.clear_lora()
|
| 127 |
pipe_imagen.load_lora(pipe_imagen.dit, lora_path)
|
| 128 |
|
| 129 |
if randomize_seed:
|
| 130 |
seed = random.randint(0, MAX_SEED)
|
| 131 |
|
| 132 |
-
generator = torch.Generator().manual_seed(seed)
|
| 133 |
|
| 134 |
output_image = pipe_imagen(
|
| 135 |
prompt=prompt,
|
|
@@ -144,13 +217,6 @@ def generate_image(
|
|
| 144 |
|
| 145 |
return output_image, seed
|
| 146 |
|
| 147 |
-
return True
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
def read_file(path: str) -> str:
|
| 151 |
-
with open(path, 'r', encoding='utf-8') as f:
|
| 152 |
-
content = f.read()
|
| 153 |
-
return content
|
| 154 |
|
| 155 |
# Enhanced Apple-style CSS - more minimalist and clean
|
| 156 |
css = """
|
|
@@ -302,20 +368,6 @@ css = """
|
|
| 302 |
background: #30a14a !important;
|
| 303 |
}
|
| 304 |
|
| 305 |
-
/* Checkbox styling */
|
| 306 |
-
.gradio-container .gr-checkbox {
|
| 307 |
-
background: #ffffff !important;
|
| 308 |
-
border: 2px solid #d2d2d7 !important;
|
| 309 |
-
border-radius: 6px !important;
|
| 310 |
-
width: 24px !important;
|
| 311 |
-
height: 24px !important;
|
| 312 |
-
}
|
| 313 |
-
|
| 314 |
-
.gradio-container .gr-checkbox.checked {
|
| 315 |
-
background: #007aff !important;
|
| 316 |
-
border-color: #007aff !important;
|
| 317 |
-
}
|
| 318 |
-
|
| 319 |
/* Examples section */
|
| 320 |
.gradio-container .gr-examples {
|
| 321 |
background: #f2f2f7 !important;
|
|
@@ -324,143 +376,94 @@ css = """
|
|
| 324 |
border: none !important;
|
| 325 |
}
|
| 326 |
|
| 327 |
-
/* Label styling */
|
| 328 |
-
.gradio-container .gr-label {
|
| 329 |
-
font-weight: 600 !important;
|
| 330 |
-
color: #1d1d1f !important;
|
| 331 |
-
font-size: 17px !important;
|
| 332 |
-
margin-bottom: 8px !important;
|
| 333 |
-
letter-spacing: -.022em !important;
|
| 334 |
-
}
|
| 335 |
-
|
| 336 |
/* Mobile responsiveness */
|
| 337 |
@media (max-width: 768px) {
|
| 338 |
-
#col-container {
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
}
|
| 342 |
-
|
| 343 |
-
.gradio-container
|
| 344 |
-
|
| 345 |
-
margin-bottom: 12px !important;
|
| 346 |
-
}
|
| 347 |
-
|
| 348 |
-
.gradio-container .subtitle {
|
| 349 |
-
font-size: 1.1rem !important;
|
| 350 |
-
}
|
| 351 |
-
|
| 352 |
-
.section-card {
|
| 353 |
-
padding: 24px !important;
|
| 354 |
-
margin-bottom: 24px !important;
|
| 355 |
-
}
|
| 356 |
-
|
| 357 |
-
.gradio-container .gr-button {
|
| 358 |
-
padding: 14px 28px !important;
|
| 359 |
-
font-size: 16px !important;
|
| 360 |
-
}
|
| 361 |
-
|
| 362 |
-
.gradio-container .gr-gallery {
|
| 363 |
-
height: 200px !important;
|
| 364 |
-
columns: 2 !important;
|
| 365 |
-
}
|
| 366 |
-
|
| 367 |
-
.gradio-container .gr-row {
|
| 368 |
-
flex-direction: column !important;
|
| 369 |
-
gap: 20px !important;
|
| 370 |
-
}
|
| 371 |
}
|
| 372 |
|
| 373 |
@media (max-width: 480px) {
|
| 374 |
-
.gradio-container h1 {
|
| 375 |
-
|
| 376 |
-
}
|
| 377 |
-
|
| 378 |
-
.section-card {
|
| 379 |
-
padding: 20px !important;
|
| 380 |
-
}
|
| 381 |
-
|
| 382 |
-
.gradio-container .gr-gallery {
|
| 383 |
-
height: 180px !important;
|
| 384 |
-
columns: 1 !important;
|
| 385 |
-
}
|
| 386 |
}
|
| 387 |
|
| 388 |
-
/*
|
| 389 |
-
.gradio-container * {
|
| 390 |
-
transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease !important;
|
| 391 |
-
}
|
| 392 |
-
|
| 393 |
-
/* Hide unnecessary elements */
|
| 394 |
.gradio-container .gr-footer,
|
| 395 |
.gradio-container .gr-header {
|
| 396 |
display: none !important;
|
| 397 |
}
|
| 398 |
-
|
| 399 |
-
/* Clean scrollbar */
|
| 400 |
-
.gradio-container ::-webkit-scrollbar {
|
| 401 |
-
width: 3px !important;
|
| 402 |
-
}
|
| 403 |
-
|
| 404 |
-
.gradio-container ::-webkit-scrollbar-track {
|
| 405 |
-
background: transparent !important;
|
| 406 |
-
}
|
| 407 |
-
|
| 408 |
-
.gradio-container ::-webkit-scrollbar-thumb {
|
| 409 |
-
background: #d2d2d7 !important;
|
| 410 |
-
border-radius: 3px !important;
|
| 411 |
-
}
|
| 412 |
-
|
| 413 |
-
.gradio-container ::-webkit-scrollbar-thumb:hover {
|
| 414 |
-
background: #007aff !important;
|
| 415 |
-
}
|
| 416 |
"""
|
| 417 |
|
| 418 |
|
| 419 |
-
|
| 420 |
-
with open(
|
|
|
|
| 421 |
print(examples)
|
| 422 |
|
| 423 |
-
|
| 424 |
with gr.Blocks() as demo:
|
| 425 |
with gr.Column(elem_id="col-container"):
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
<div style="text-align: center; max-width:
|
| 430 |
<h1>Qwen Image to LoRA</h1>
|
| 431 |
<p class="subtitle">Generate custom LoRA models from your images</p>
|
| 432 |
<p style="font-size: 14px; color: #86868b; margin-top: 16px;">
|
| 433 |
-
Demo by <a href="https://aisudo.com/" target="_blank" style="color: #007aff; text-decoration: none;">AiSudo</a> •
|
| 434 |
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #007aff; text-decoration: none;">Built with anycoder</a>
|
| 435 |
</p>
|
| 436 |
</div>
|
| 437 |
-
"""
|
| 438 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
with gr.Row():
|
| 440 |
with gr.Column(elem_classes=["section-card"]):
|
| 441 |
input_images = gr.Gallery(
|
| 442 |
-
label="Input Images",
|
| 443 |
file_types=["image"],
|
| 444 |
-
show_label=True,
|
| 445 |
-
columns=2,
|
| 446 |
-
object_fit="cover",
|
| 447 |
-
height=250
|
| 448 |
-
|
| 449 |
lora_button = gr.Button("Generate LoRA", size="lg")
|
| 450 |
|
| 451 |
with gr.Column(elem_classes=["section-card"]):
|
| 452 |
lora_name = gr.Textbox(
|
| 453 |
-
label="Generated LoRA",
|
| 454 |
-
lines=2,
|
| 455 |
interactive=False,
|
| 456 |
-
placeholder="Your LoRA will appear here..."
|
| 457 |
)
|
| 458 |
-
|
| 459 |
-
|
|
|
|
|
|
|
|
|
|
| 460 |
interactive=False,
|
| 461 |
-
size="lg"
|
|
|
|
| 462 |
)
|
| 463 |
-
|
| 464 |
with gr.Column(elem_classes=["section-card"]) as imagen_container:
|
| 465 |
gr.Markdown("### Generate Images")
|
| 466 |
with gr.Row():
|
|
@@ -471,15 +474,15 @@ with gr.Blocks() as demo:
|
|
| 471 |
placeholder="Describe what you want to generate...",
|
| 472 |
value="a person in a fishing boat.",
|
| 473 |
)
|
| 474 |
-
|
| 475 |
-
imagen_button = gr.Button("Generate Image", interactive=False, size="lg")
|
| 476 |
-
|
| 477 |
with gr.Accordion("Settings", open=False):
|
| 478 |
negative_prompt = gr.Textbox(
|
| 479 |
label="Negative Prompt",
|
| 480 |
lines=1,
|
| 481 |
placeholder="What to avoid...",
|
| 482 |
-
value="blurry, low quality"
|
| 483 |
)
|
| 484 |
num_inference_steps = gr.Slider(
|
| 485 |
label="Steps",
|
|
@@ -501,7 +504,7 @@ with gr.Blocks() as demo:
|
|
| 501 |
minimum=512,
|
| 502 |
maximum=1280,
|
| 503 |
step=32,
|
| 504 |
-
value=768,
|
| 505 |
)
|
| 506 |
height = gr.Slider(
|
| 507 |
label="Height",
|
|
@@ -520,26 +523,23 @@ with gr.Blocks() as demo:
|
|
| 520 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=False)
|
| 521 |
|
| 522 |
with gr.Column():
|
| 523 |
-
output_image = gr.Image(
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
)
|
| 527 |
-
|
| 528 |
-
gr.Examples(
|
| 529 |
-
examples=examples,
|
| 530 |
-
inputs=[input_images],
|
| 531 |
-
label="Examples"
|
| 532 |
-
)
|
| 533 |
gr.Markdown(read_file("static/footer.md"))
|
| 534 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
lora_button.click(
|
| 536 |
fn=generate_lora,
|
| 537 |
-
inputs=[
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
outputs=[lora_name, lora_download, imagen_button],
|
| 541 |
-
api_visibility="public"
|
| 542 |
)
|
|
|
|
|
|
|
| 543 |
imagen_button.click(
|
| 544 |
fn=generate_image,
|
| 545 |
inputs=[
|
|
@@ -554,33 +554,20 @@ with gr.Blocks() as demo:
|
|
| 554 |
num_inference_steps,
|
| 555 |
],
|
| 556 |
outputs=[output_image, seed],
|
| 557 |
-
api_visibility="public"
|
| 558 |
)
|
| 559 |
|
| 560 |
|
| 561 |
if __name__ == "__main__":
|
| 562 |
-
# Gradio 6 syntax - all launch parameters go here
|
| 563 |
demo.launch(
|
| 564 |
-
css=css, #
|
| 565 |
-
mcp_server=True,
|
| 566 |
theme=gr.themes.Base(
|
| 567 |
primary_hue="blue",
|
| 568 |
secondary_hue="gray",
|
| 569 |
neutral_hue="gray",
|
| 570 |
-
font=[
|
| 571 |
-
|
| 572 |
-
"ui-sans-serif",
|
| 573 |
-
"system-ui",
|
| 574 |
-
"sans-serif"
|
| 575 |
-
],
|
| 576 |
-
font_mono=[
|
| 577 |
-
gr.themes.GoogleFont("JetBrains Mono"),
|
| 578 |
-
"ui-monospace",
|
| 579 |
-
"Consolas",
|
| 580 |
-
"monospace"
|
| 581 |
-
]
|
| 582 |
),
|
| 583 |
-
footer_links=[
|
| 584 |
-
|
| 585 |
-
]
|
| 586 |
-
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
+
import torch, random, json, spaces
|
| 4 |
from ulid import ULID
|
| 5 |
from diffsynth.pipelines.qwen_image import (
|
| 6 |
QwenImagePipeline, ModelConfig,
|
| 7 |
QwenImageUnit_Image2LoRAEncode, QwenImageUnit_Image2LoRADecode
|
| 8 |
)
|
| 9 |
from safetensors.torch import save_file
|
|
|
|
| 10 |
from PIL import Image
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# from utils import repo_utils, image_utils, prompt_utils
|
| 13 |
# repo_utils.clone_repo_if_not_exists("git clone https://huggingface.co/DiffSynth-Studio/General-Image-Encoders", "app/repos")
|
| 14 |
# repo_utils.clone_repo_if_not_exists("https://huggingface.co/apple/starflow", "app/models")
|
| 15 |
|
|
|
|
| 17 |
DTYPE = torch.bfloat16
|
| 18 |
MAX_SEED = np.iinfo(np.int32).max
|
| 19 |
|
| 20 |
+
|
| 21 |
vram_config_disk_offload = {
|
| 22 |
"offload_dtype": "disk",
|
| 23 |
"offload_device": "disk",
|
|
|
|
| 29 |
"computation_device": "cuda",
|
| 30 |
}
|
| 31 |
|
| 32 |
+
# Load models (LoRA encoder/decoder)
|
| 33 |
pipe_lora = QwenImagePipeline.from_pretrained(
|
| 34 |
torch_dtype=torch.bfloat16,
|
| 35 |
device="cuda",
|
| 36 |
model_configs=[
|
| 37 |
ModelConfig(
|
| 38 |
+
download_source="huggingface",
|
| 39 |
+
model_id="DiffSynth-Studio/General-Image-Encoders",
|
| 40 |
+
origin_file_pattern="SigLIP2-G384/model.safetensors",
|
| 41 |
+
**vram_config_disk_offload,
|
| 42 |
),
|
| 43 |
ModelConfig(
|
| 44 |
+
download_source="huggingface",
|
| 45 |
+
model_id="DiffSynth-Studio/General-Image-Encoders",
|
| 46 |
+
origin_file_pattern="DINOv3-7B/model.safetensors",
|
| 47 |
+
**vram_config_disk_offload,
|
| 48 |
),
|
| 49 |
ModelConfig(
|
| 50 |
+
download_source="huggingface",
|
| 51 |
+
model_id="DiffSynth-Studio/Qwen-Image-i2L",
|
| 52 |
+
origin_file_pattern="Qwen-Image-i2L-Style.safetensors",
|
| 53 |
+
**vram_config_disk_offload,
|
| 54 |
),
|
| 55 |
],
|
| 56 |
processor_config=ModelConfig(model_id="Qwen/Qwen-Image-Edit", origin_file_pattern="processor/"),
|
|
|
|
| 68 |
"computation_device": "cuda",
|
| 69 |
}
|
| 70 |
|
| 71 |
+
# Load image generation pipeline
|
| 72 |
pipe_imagen = QwenImagePipeline.from_pretrained(
|
| 73 |
torch_dtype=torch.bfloat16,
|
| 74 |
device="cuda",
|
|
|
|
| 82 |
)
|
| 83 |
|
| 84 |
|
| 85 |
+
def read_file(path: str) -> str:
|
| 86 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 87 |
+
return f.read()
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def show_user(profile: gr.OAuthProfile | None):
|
| 91 |
+
"""
|
| 92 |
+
Displays who is logged in (or nothing if not logged in).
|
| 93 |
+
Works in Spaces OAuth; locally uses hf auth login if available.
|
| 94 |
+
"""
|
| 95 |
+
if profile is None:
|
| 96 |
+
return ""
|
| 97 |
+
username = getattr(profile, "username", None) or "unknown"
|
| 98 |
+
return f"✅ Signed in as **{username}**"
|
| 99 |
+
|
| 100 |
+
|
| 101 |
@spaces.GPU
|
| 102 |
def generate_lora(
|
| 103 |
input_images,
|
| 104 |
+
profile: gr.OAuthProfile | None = None,
|
| 105 |
+
oauth_token: gr.OAuthToken | None = None,
|
| 106 |
progress=gr.Progress(track_tqdm=True),
|
| 107 |
):
|
| 108 |
+
"""
|
| 109 |
+
- Always generates and saves LoRA locally under ./loras/
|
| 110 |
+
- If user is signed in (OAuth), also uploads to the user's *own* Hub repo.
|
| 111 |
+
"""
|
| 112 |
+
import os
|
| 113 |
+
from huggingface_hub import HfApi
|
| 114 |
|
| 115 |
ulid = str(ULID()).lower()[:12]
|
| 116 |
print(f"ulid: {ulid}")
|
| 117 |
|
| 118 |
if not input_images:
|
| 119 |
+
return (
|
| 120 |
+
"",
|
| 121 |
+
gr.update(value="⚠️ Please upload at least 1 image."),
|
| 122 |
+
gr.update(interactive=False),
|
| 123 |
+
gr.update(interactive=False, link=""),
|
| 124 |
+
)
|
| 125 |
|
| 126 |
+
# Gradio Gallery returns list of (filepath, metadata)
|
| 127 |
input_images = [Image.open(filepath).convert("RGB") for filepath, _ in input_images]
|
| 128 |
+
|
| 129 |
# Model inference
|
| 130 |
with torch.no_grad():
|
| 131 |
embs = QwenImageUnit_Image2LoRAEncode().process(pipe_lora, image2lora_images=input_images)
|
| 132 |
lora = QwenImageUnit_Image2LoRADecode().process(pipe_lora, **embs)["lora"]
|
| 133 |
|
| 134 |
lora_name = f"{ulid}.safetensors"
|
| 135 |
+
os.makedirs("loras", exist_ok=True)
|
| 136 |
lora_path = f"loras/{lora_name}"
|
|
|
|
| 137 |
save_file(lora, lora_path)
|
| 138 |
|
| 139 |
+
# Default: local-only message (still lets user generate images from local LoRA)
|
| 140 |
+
hub_url = ""
|
| 141 |
+
hub_markdown = "✅ LoRA generated locally. Sign in to upload it to your Hugging Face account."
|
| 142 |
+
|
| 143 |
+
# Upload to the signed-in user's own account if available
|
| 144 |
+
if profile is not None and oauth_token is not None and getattr(oauth_token, "token", None):
|
| 145 |
+
try:
|
| 146 |
+
username = getattr(profile, "username", None) or ""
|
| 147 |
+
if not username:
|
| 148 |
+
raise ValueError("Could not read username from OAuth profile.")
|
| 149 |
+
|
| 150 |
+
api = HfApi(token=oauth_token.token)
|
| 151 |
+
|
| 152 |
+
# Create / reuse a user repo (model repo recommended for LoRAs)
|
| 153 |
+
# Change name if you want:
|
| 154 |
+
repo_id = f"{username}/qwen-image-loras"
|
| 155 |
+
api.create_repo(repo_id=repo_id, repo_type="model", exist_ok=True)
|
| 156 |
+
|
| 157 |
+
api.upload_file(
|
| 158 |
+
path_or_fileobj=lora_path,
|
| 159 |
+
path_in_repo=f"loras/{lora_name}",
|
| 160 |
+
repo_id=repo_id,
|
| 161 |
+
repo_type="model",
|
| 162 |
+
commit_message=f"Add LoRA: {lora_name}",
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
hub_url = f"https://huggingface.co/{repo_id}/blob/main/loras/{lora_name}"
|
| 166 |
+
hub_markdown = f"✅ **Uploaded to your account:** {hub_url}"
|
| 167 |
+
|
| 168 |
+
except Exception as e:
|
| 169 |
+
print(f"Error uploading to user repo: {e}")
|
| 170 |
+
hub_markdown = f"⚠️ Upload failed (still saved locally): `{str(e)}`"
|
| 171 |
+
|
| 172 |
+
# Enable image generation button regardless
|
| 173 |
+
return (
|
| 174 |
+
lora_name,
|
| 175 |
+
gr.update(value=hub_markdown),
|
| 176 |
+
gr.update(interactive=True),
|
| 177 |
+
gr.update(interactive=bool(hub_url), link=hub_url),
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
|
| 181 |
@spaces.GPU
|
| 182 |
def generate_image(
|
|
|
|
| 191 |
num_inference_steps=8,
|
| 192 |
progress=gr.Progress(track_tqdm=True),
|
| 193 |
):
|
| 194 |
+
if not lora_name:
|
| 195 |
+
return None, seed
|
| 196 |
+
|
| 197 |
lora_path = f"loras/{lora_name}"
|
| 198 |
+
|
| 199 |
pipe_imagen.clear_lora()
|
| 200 |
pipe_imagen.load_lora(pipe_imagen.dit, lora_path)
|
| 201 |
|
| 202 |
if randomize_seed:
|
| 203 |
seed = random.randint(0, MAX_SEED)
|
| 204 |
|
| 205 |
+
# generator = torch.Generator().manual_seed(seed)
|
| 206 |
|
| 207 |
output_image = pipe_imagen(
|
| 208 |
prompt=prompt,
|
|
|
|
| 217 |
|
| 218 |
return output_image, seed
|
| 219 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
# Enhanced Apple-style CSS - more minimalist and clean
|
| 222 |
css = """
|
|
|
|
| 368 |
background: #30a14a !important;
|
| 369 |
}
|
| 370 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
/* Examples section */
|
| 372 |
.gradio-container .gr-examples {
|
| 373 |
background: #f2f2f7 !important;
|
|
|
|
| 376 |
border: none !important;
|
| 377 |
}
|
| 378 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
/* Mobile responsiveness */
|
| 380 |
@media (max-width: 768px) {
|
| 381 |
+
#col-container { padding: 20px 16px !important; max-width: 100% !important; }
|
| 382 |
+
.gradio-container h1 { font-size: 2rem !important; margin-bottom: 12px !important; }
|
| 383 |
+
.gradio-container .subtitle { font-size: 1.1rem !important; }
|
| 384 |
+
.section-card { padding: 24px !important; margin-bottom: 24px !important; }
|
| 385 |
+
.gradio-container .gr-button { padding: 14px 28px !important; font-size: 16px !important; }
|
| 386 |
+
.gradio-container .gr-gallery { height: 200px !important; columns: 2 !important; }
|
| 387 |
+
.gradio-container .gr-row { flex-direction: column !important; gap: 20px !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
}
|
| 389 |
|
| 390 |
@media (max-width: 480px) {
|
| 391 |
+
.gradio-container h1 { font-size: 1.75rem !important; }
|
| 392 |
+
.section-card { padding: 20px !important; }
|
| 393 |
+
.gradio-container .gr-gallery { height: 180px !important; columns: 1 !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
}
|
| 395 |
|
| 396 |
+
/* Hide gradio header/footer */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
.gradio-container .gr-footer,
|
| 398 |
.gradio-container .gr-header {
|
| 399 |
display: none !important;
|
| 400 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
"""
|
| 402 |
|
| 403 |
|
| 404 |
+
# Load examples
|
| 405 |
+
with open("examples/0_examples.json", "r") as file:
|
| 406 |
+
examples = json.load(file)
|
| 407 |
print(examples)
|
| 408 |
|
| 409 |
+
|
| 410 |
with gr.Blocks() as demo:
|
| 411 |
with gr.Column(elem_id="col-container"):
|
| 412 |
+
# Header
|
| 413 |
+
gr.HTML(
|
| 414 |
+
"""
|
| 415 |
+
<div style="text-align: center; max-width: 700px; margin: 0 auto;">
|
| 416 |
<h1>Qwen Image to LoRA</h1>
|
| 417 |
<p class="subtitle">Generate custom LoRA models from your images</p>
|
| 418 |
<p style="font-size: 14px; color: #86868b; margin-top: 16px;">
|
| 419 |
+
Demo by <a href="https://aisudo.com/" target="_blank" style="color: #007aff; text-decoration: none;">AiSudo</a> •
|
| 420 |
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #007aff; text-decoration: none;">Built with anycoder</a>
|
| 421 |
</p>
|
| 422 |
</div>
|
| 423 |
+
"""
|
| 424 |
+
)
|
| 425 |
+
|
| 426 |
+
# ✅ Hugging Face Login Button
|
| 427 |
+
with gr.Row():
|
| 428 |
+
with gr.Column(scale=1):
|
| 429 |
+
login_btn = gr.LoginButton(
|
| 430 |
+
value="Sign in with Hugging Face",
|
| 431 |
+
logout_value="Logout ({})",
|
| 432 |
+
variant="huggingface",
|
| 433 |
+
size="lg",
|
| 434 |
+
)
|
| 435 |
+
with gr.Column(scale=3):
|
| 436 |
+
whoami = gr.Markdown(value="", elem_id="whoami")
|
| 437 |
+
|
| 438 |
with gr.Row():
|
| 439 |
with gr.Column(elem_classes=["section-card"]):
|
| 440 |
input_images = gr.Gallery(
|
| 441 |
+
label="Input Images",
|
| 442 |
file_types=["image"],
|
| 443 |
+
show_label=True,
|
| 444 |
+
columns=2,
|
| 445 |
+
object_fit="cover",
|
| 446 |
+
height=250,
|
| 447 |
+
)
|
| 448 |
lora_button = gr.Button("Generate LoRA", size="lg")
|
| 449 |
|
| 450 |
with gr.Column(elem_classes=["section-card"]):
|
| 451 |
lora_name = gr.Textbox(
|
| 452 |
+
label="Generated LoRA",
|
| 453 |
+
lines=2,
|
| 454 |
interactive=False,
|
| 455 |
+
placeholder="Your LoRA will appear here...",
|
| 456 |
)
|
| 457 |
+
hub_link = gr.Markdown(value="", label="Hub Link")
|
| 458 |
+
|
| 459 |
+
# This becomes clickable only after upload
|
| 460 |
+
lora_download = gr.Button(
|
| 461 |
+
value="View on Hub",
|
| 462 |
interactive=False,
|
| 463 |
+
size="lg",
|
| 464 |
+
link="",
|
| 465 |
)
|
| 466 |
+
|
| 467 |
with gr.Column(elem_classes=["section-card"]) as imagen_container:
|
| 468 |
gr.Markdown("### Generate Images")
|
| 469 |
with gr.Row():
|
|
|
|
| 474 |
placeholder="Describe what you want to generate...",
|
| 475 |
value="a person in a fishing boat.",
|
| 476 |
)
|
| 477 |
+
|
| 478 |
+
imagen_button = gr.Button("Generate Image", interactive=False, size="lg")
|
| 479 |
+
|
| 480 |
with gr.Accordion("Settings", open=False):
|
| 481 |
negative_prompt = gr.Textbox(
|
| 482 |
label="Negative Prompt",
|
| 483 |
lines=1,
|
| 484 |
placeholder="What to avoid...",
|
| 485 |
+
value="blurry, low quality",
|
| 486 |
)
|
| 487 |
num_inference_steps = gr.Slider(
|
| 488 |
label="Steps",
|
|
|
|
| 504 |
minimum=512,
|
| 505 |
maximum=1280,
|
| 506 |
step=32,
|
| 507 |
+
value=768,
|
| 508 |
)
|
| 509 |
height = gr.Slider(
|
| 510 |
label="Height",
|
|
|
|
| 523 |
randomize_seed = gr.Checkbox(label="Randomize Seed", value=False)
|
| 524 |
|
| 525 |
with gr.Column():
|
| 526 |
+
output_image = gr.Image(label="Generated Image", height=350)
|
| 527 |
+
|
| 528 |
+
gr.Examples(examples=examples, inputs=[input_images], label="Examples")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
gr.Markdown(read_file("static/footer.md"))
|
| 530 |
|
| 531 |
+
# Login click shows the user
|
| 532 |
+
login_btn.click(fn=show_user, inputs=[login_btn], outputs=[whoami], api_visibility="public")
|
| 533 |
+
|
| 534 |
+
# Generate LoRA (auto-uploads to user account if signed in)
|
| 535 |
lora_button.click(
|
| 536 |
fn=generate_lora,
|
| 537 |
+
inputs=[input_images],
|
| 538 |
+
outputs=[lora_name, hub_link, imagen_button, lora_download],
|
| 539 |
+
api_visibility="public",
|
|
|
|
|
|
|
| 540 |
)
|
| 541 |
+
|
| 542 |
+
# Generate Image
|
| 543 |
imagen_button.click(
|
| 544 |
fn=generate_image,
|
| 545 |
inputs=[
|
|
|
|
| 554 |
num_inference_steps,
|
| 555 |
],
|
| 556 |
outputs=[output_image, seed],
|
| 557 |
+
api_visibility="public",
|
| 558 |
)
|
| 559 |
|
| 560 |
|
| 561 |
if __name__ == "__main__":
|
|
|
|
| 562 |
demo.launch(
|
| 563 |
+
css=css, # Gradio 6: pass css here
|
| 564 |
+
mcp_server=True,
|
| 565 |
theme=gr.themes.Base(
|
| 566 |
primary_hue="blue",
|
| 567 |
secondary_hue="gray",
|
| 568 |
neutral_hue="gray",
|
| 569 |
+
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 570 |
+
font_mono=[gr.themes.GoogleFont("JetBrains Mono"), "ui-monospace", "Consolas", "monospace"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 571 |
),
|
| 572 |
+
footer_links=[{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}],
|
| 573 |
+
)
|
|
|
|
|
|