Spaces:
Running
on
Zero
Running
on
Zero
File size: 8,706 Bytes
55fe803 62594be 55fe803 eee0c8d 55fe803 62594be eee0c8d 55fe803 e28279b 55fe803 81688cf e28279b 55fe803 e28279b 55fe803 e28279b 55fe803 e28279b 55fe803 e28279b 55fe803 e28279b 55fe803 e28279b 55fe803 e28279b 55fe803 e28279b 55fe803 e28279b 55fe803 e28279b 55fe803 e28279b 55fe803 e28279b 55fe803 e28279b 55fe803 e28279b 190e9bc e28279b 55fe803 e28279b 4789a7f e28279b |
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 |
import torch
import spaces
import gradio as gr
from diffusers import DiffusionPipeline
# Load the pipeline once at startup
print("Loading Z-Image-Turbo pipeline...")
pipe = DiffusionPipeline.from_pretrained(
"Tongyi-MAI/Z-Image-Turbo",
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=False,
)
pipe.to("cuda")
# ======== AoTI compilation + FA3 ========
pipe.transformer.layers._repeated_blocks = ["ZImageTransformerBlock"]
spaces.aoti_blocks_load(pipe.transformer.layers, "zerogpu-aoti/Z-Image", variant="fa3")
print("Pipeline loaded!")
@spaces.GPU
def generate_image(prompt, height, width, num_inference_steps, seed, randomize_seed, progress=gr.Progress(track_tqdm=True)):
"""Generate an image from the given prompt."""
if randomize_seed:
seed = torch.randint(0, 2**32 - 1, (1,)).item()
generator = torch.Generator("cuda").manual_seed(int(seed))
image = pipe(
prompt=prompt,
height=int(height),
width=int(width),
num_inference_steps=int(num_inference_steps),
guidance_scale=0.0,
generator=generator,
).images[0]
return image, seed
# Example prompts
examples = [
["Young Chinese woman in red Hanfu, intricate embroidery. Impeccable makeup, red floral forehead pattern. Elaborate high bun, golden phoenix headdress, red flowers, beads. Holds round folding fan with lady, trees, bird. Neon lightning-bolt lamp, bright yellow glow, above extended left palm. Soft-lit outdoor night background, silhouetted tiered pagoda, blurred colorful distant lights."],
["A majestic dragon soaring through clouds at sunset, scales shimmering with iridescent colors, detailed fantasy art style"],
["Cozy coffee shop interior, warm lighting, rain on windows, plants on shelves, vintage aesthetic, photorealistic"],
["Astronaut riding a horse on Mars, cinematic lighting, sci-fi concept art, highly detailed"],
["Portrait of a wise old wizard with a long white beard, holding a glowing crystal staff, magical forest background"],
]
# Custom theme with modern aesthetics (Gradio 6)
custom_theme = gr.themes.Soft(
primary_hue="yellow",
secondary_hue="amber",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter"),
text_size="lg",
spacing_size="md",
radius_size="lg"
).set(
button_primary_background_fill="*primary_500",
button_primary_background_fill_hover="*primary_600",
block_title_text_weight="600",
)
# Build the Gradio interface
with gr.Blocks(fill_height=True) as demo:
# Header
gr.Markdown(
"""
# 🎨 Z-Image-Turbo
**Ultra-fast AI image generation** • Generate stunning images in just 8 steps
""",
elem_classes="header-text"
)
with gr.Row(equal_height=False):
# Left column - Input controls
with gr.Column(scale=1, min_width=320):
prompt = gr.Textbox(
label="✨ Your Prompt",
placeholder="Describe the image you want to create...",
lines=5,
max_lines=10,
autofocus=True,
)
with gr.Accordion("⚙️ Advanced Settings", open=False):
with gr.Row():
height = gr.Slider(
minimum=512,
maximum=2048,
value=1024,
step=64,
label="Height",
info="Image height in pixels"
)
width = gr.Slider(
minimum=512,
maximum=2048,
value=1024,
step=64,
label="Width",
info="Image width in pixels"
)
num_inference_steps = gr.Slider(
minimum=1,
maximum=20,
value=9,
step=1,
label="Inference Steps",
info="9 steps = 8 DiT forwards (recommended)"
)
with gr.Row():
randomize_seed = gr.Checkbox(
label="🎲 Random Seed",
value=True,
)
seed = gr.Number(
label="Seed",
value=42,
precision=0,
visible=False,
)
def toggle_seed(randomize):
return gr.Number(visible=not randomize)
randomize_seed.change(
toggle_seed,
inputs=[randomize_seed],
outputs=[seed]
)
generate_btn = gr.Button(
"🚀 Generate Image",
variant="primary",
size="lg",
scale=1
)
# Example prompts
gr.Examples(
examples=examples,
inputs=[prompt],
label="💡 Try these prompts",
examples_per_page=5,
)
# Right column - Output
with gr.Column(scale=1, min_width=320):
output_image = gr.Image(
label="Generated Image",
type="pil",
show_label=False,
height=600,
buttons=["download", "share"],
)
used_seed = gr.Number(
label="🎲 Seed Used",
interactive=False,
container=True,
)
# Footer credits
gr.Markdown(
"""
---
<div style="text-align: center; opacity: 0.7; font-size: 0.9em; margin-top: 1rem;">
<strong>Model:</strong> <a href="https://huggingface.co/Tongyi-MAI/Z-Image-Turbo" target="_blank">Tongyi-MAI/Z-Image-Turbo</a> (Apache 2.0 License) •
<strong>Demo by:</strong> <a href="https://x.com/realmrfakename" target="_blank">@mrfakename</a> •
<strong>Redesign by:</strong> AnyCoder •
<strong>Optimizations:</strong> <a href="https://huggingface.co/multimodalart" target="_blank">@multimodalart</a> (FA3 + AoTI)
</div>
""",
elem_classes="footer-text"
)
# Connect the generate button
generate_btn.click(
fn=generate_image,
inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed],
outputs=[output_image, used_seed],
)
# Also allow generating by pressing Enter in the prompt box
prompt.submit(
fn=generate_image,
inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed],
outputs=[output_image, used_seed],
)
if __name__ == "__main__":
demo.launch(
theme=custom_theme,
css="""
.header-text h1 {
font-size: 2.5rem !important;
font-weight: 700 !important;
margin-bottom: 0.5rem !important;
background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
.header-text p {
font-size: 1.1rem !important;
color: #64748b !important;
margin-top: 0 !important;
}
.footer-text {
padding: 1rem 0;
}
.footer-text a {
color: #f59e0b !important;
text-decoration: none !important;
font-weight: 500;
}
.footer-text a:hover {
text-decoration: underline !important;
}
/* Mobile optimizations */
@media (max-width: 768px) {
.header-text h1 {
font-size: 1.8rem !important;
}
.header-text p {
font-size: 1rem !important;
}
}
/* Smooth transitions */
button, .gr-button {
transition: all 0.2s ease !important;
}
button:hover, .gr-button:hover {
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15) !important;
}
/* Better spacing */
.gradio-container {
max-width: 1400px !important;
margin: 0 auto !important;
}
""",
footer_links=[
"api",
"gradio"
]
) |