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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import spaces
3
+ import gradio as gr
4
+ from diffusers import DiffusionPipeline
5
+
6
+ # ============================================================
7
+ # Load the pipeline once at startup
8
+ # ============================================================
9
+
10
+ print("Loading Z-Image-Turbo pipeline...")
11
+
12
+ pipe = DiffusionPipeline.from_pretrained(
13
+ "Tongyi-MAI/Z-Image-Turbo",
14
+ torch_dtype=torch.bfloat16,
15
+ low_cpu_mem_usage=False,
16
+ )
17
+
18
+ pipe.to("cuda")
19
+
20
+ # ============================================================
21
+ # Optional AoTI + Flash Attention 3
22
+ # ============================================================
23
+
24
+ # pipe.transformer.layers._repeated_blocks = ["ZImageTransformerBlock"]
25
+ # spaces.aoti_blocks_load(
26
+ # pipe.transformer.layers,
27
+ # "zerogpu-aoti/Z-Image",
28
+ # variant="fa3"
29
+ # )
30
+
31
+ print("Pipeline loaded successfully!")
32
+
33
+ # ============================================================
34
+ # Image Generation Function
35
+ # ============================================================
36
+
37
+ @spaces.GPU
38
+ def generate_image(
39
+ prompt,
40
+ height,
41
+ width,
42
+ num_inference_steps,
43
+ seed,
44
+ randomize_seed,
45
+ progress=gr.Progress(track_tqdm=True),
46
+ ):
47
+ """Generate an AI image from a text prompt."""
48
+
49
+ if randomize_seed:
50
+ seed = torch.randint(0, 2**32 - 1, (1,)).item()
51
+
52
+ generator = torch.Generator("cuda").manual_seed(int(seed))
53
+
54
+ image = pipe(
55
+ prompt=prompt,
56
+ height=int(height),
57
+ width=int(width),
58
+ num_inference_steps=int(num_inference_steps),
59
+ guidance_scale=0.0,
60
+ generator=generator,
61
+ ).images[0]
62
+
63
+ return image, seed
64
+
65
+
66
+ # ============================================================
67
+ # Example Prompts
68
+ # ============================================================
69
+
70
+ examples = [
71
+ [
72
+ "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 glowing above her hand. Night background with pagoda and colorful lights."
73
+ ],
74
+ [
75
+ "A majestic dragon soaring through clouds at sunset, scales shimmering with iridescent colors, highly detailed fantasy artwork."
76
+ ],
77
+ [
78
+ "Cozy coffee shop interior with warm lighting, rain on the windows, plants on wooden shelves, vintage aesthetic, photorealistic."
79
+ ],
80
+ [
81
+ "Astronaut riding a horse on Mars, cinematic lighting, science fiction concept art, ultra detailed."
82
+ ],
83
+ [
84
+ "Portrait of a wise old wizard with a long white beard holding a glowing crystal staff inside a magical forest."
85
+ ],
86
+ ]
87
+
88
+ # ============================================================
89
+ # Build the Gradio Interface
90
+ # ============================================================
91
+
92
+ with gr.Blocks(
93
+ theme=gr.themes.Ocean(),
94
+ fill_height=True,
95
+ ) as demo:
96
+
97
+ # --------------------------------------------------------
98
+ # Header
99
+ # --------------------------------------------------------
100
+
101
+ gr.Markdown(
102
+ """
103
+ # 🌊 Z-Image-Turbo
104
+
105
+ ### Ultra-fast AI image generation powered by **Tongyi-MAI/Z-Image-Turbo**
106
+
107
+ Create stunning AI artwork in only **8 inference steps**.
108
+ """,
109
+ elem_classes="header-text",
110
+ )
111
+
112
+ with gr.Row(equal_height=False):
113
+
114
+ # ====================================================
115
+ # LEFT COLUMN
116
+ # ====================================================
117
+
118
+ with gr.Column(scale=1, min_width=320):
119
+
120
+ prompt = gr.Textbox(
121
+ label="✨ Prompt",
122
+ placeholder="Describe the image you'd like to generate...",
123
+ lines=5,
124
+ max_lines=10,
125
+ autofocus=True,
126
+ )
127
+
128
+ with gr.Accordion(
129
+ "⚙️ Advanced Settings",
130
+ open=False,
131
+ ):
132
+
133
+ with gr.Row():
134
+
135
+ height = gr.Slider(
136
+ minimum=512,
137
+ maximum=2048,
138
+ value=1024,
139
+ step=64,
140
+ label="Height",
141
+ info="Image height in pixels",
142
+ )
143
+
144
+ width = gr.Slider(
145
+ minimum=512,
146
+ maximum=2048,
147
+ value=1024,
148
+ step=64,
149
+ label="Width",
150
+ info="Image width in pixels",
151
+ )
152
+
153
+ num_inference_steps = gr.Slider(
154
+ minimum=1,
155
+ maximum=20,
156
+ value=9,
157
+ step=1,
158
+ label="Inference Steps",
159
+ info="Recommended: 9",
160
+ )
161
+
162
+ with gr.Row():
163
+
164
+ randomize_seed = gr.Checkbox(
165
+ label="🎲 Random Seed",
166
+ value=True,
167
+ )
168
+
169
+ seed = gr.Number(
170
+ label="Seed",
171
+ value=42,
172
+ precision=0,
173
+ visible=False,
174
+ )
175
+
176
+ def toggle_seed(randomize):
177
+ return gr.Number(
178
+ visible=not randomize
179
+ )
180
+
181
+ randomize_seed.change(
182
+ toggle_seed,
183
+ inputs=randomize_seed,
184
+ outputs=seed,
185
+ )
186
+
187
+ generate_btn = gr.Button(
188
+ "🚀 Generate Image",
189
+ variant="primary",
190
+ size="lg",
191
+ )
192
+
193
+ gr.Examples(
194
+ examples=examples,
195
+ inputs=prompt,
196
+ label="💡 Example Prompts",
197
+ examples_per_page=5,
198
+ )
199
+
200
+ # ====================================================
201
+ # RIGHT COLUMN
202
+ # ====================================================
203
+
204
+ with gr.Column(scale=1, min_width=320):
205
+
206
+ output_image = gr.Image(
207
+ label="Generated Image",
208
+ type="pil",
209
+ format="png",
210
+ show_label=False,
211
+ height=600,
212
+ )
213
+
214
+ used_seed = gr.Number(
215
+ label="🎲 Seed Used",
216
+ interactive=False,
217
+ container=True,
218
+ )
219
+
220
+
221
+ # ========================================================
222
+ # Footer
223
+ # ========================================================
224
+
225
+ gr.Markdown(
226
+ """
227
+ ---
228
+ <div class="footer-text">
229
+
230
+ <strong>Model:</strong>
231
+ <a href="https://huggingface.co/Tongyi-MAI/Z-Image-Turbo" target="_blank">
232
+ Tongyi-MAI/Z-Image-Turbo
233
+ </a>
234
+
235
+ <br>
236
+
237
+ <strong>Demo:</strong>
238
+ <a href="https://x.com/realmrfakename" target="_blank">
239
+ @mrfakename
240
+ </a>
241
+
242
+ <br>
243
+
244
+ <strong>Optimizations:</strong>
245
+ <a href="https://huggingface.co/multimodalart" target="_blank">
246
+ @multimodalart
247
+ </a>
248
+
249
+ </div>
250
+ """,
251
+ elem_classes="footer-text",
252
+ )
253
+
254
+
255
+ # ========================================================
256
+ # Button Event
257
+ # ========================================================
258
+
259
+ generate_btn.click(
260
+ fn=generate_image,
261
+ inputs=[
262
+ prompt,
263
+ height,
264
+ width,
265
+ num_inference_steps,
266
+ seed,
267
+ randomize_seed,
268
+ ],
269
+ outputs=[
270
+ output_image,
271
+ used_seed,
272
+ ],
273
+ )
274
+
275
+
276
+ # ========================================================
277
+ # Press Enter To Generate
278
+ # ========================================================
279
+
280
+ prompt.submit(
281
+ fn=generate_image,
282
+ inputs=[
283
+ prompt,
284
+ height,
285
+ width,
286
+ num_inference_steps,
287
+ seed,
288
+ randomize_seed,
289
+ ],
290
+ outputs=[
291
+ output_image,
292
+ used_seed,
293
+ ],
294
+ )
295
+
296
+
297
+ # ============================================================
298
+ # Launch Application
299
+ # ============================================================
300
+
301
+ if __name__ == "__main__":
302
+
303
+ demo.launch(
304
+ css="""
305
+
306
+ /* ==============================================
307
+ Header
308
+ ============================================== */
309
+
310
+ .header-text h1 {
311
+
312
+ font-size: 2.5rem !important;
313
+
314
+ font-weight: 800 !important;
315
+
316
+ background:
317
+ linear-gradient(
318
+ 135deg,
319
+ #0ea5e9,
320
+ #2563eb
321
+ );
322
+
323
+ -webkit-background-clip: text;
324
+
325
+ -webkit-text-fill-color: transparent;
326
+
327
+ background-clip: text;
328
+
329
+ }
330
+
331
+
332
+ .header-text h3 {
333
+
334
+ color:
335
+ #64748b !important;
336
+
337
+ }
338
+
339
+
340
+
341
+ /* ==============================================
342
+ Generate Button
343
+ ============================================== */
344
+
345
+
346
+ button.primary {
347
+
348
+ background:
349
+ linear-gradient(
350
+ 135deg,
351
+ #0ea5e9,
352
+ #2563eb
353
+ ) !important;
354
+
355
+ border-radius:
356
+ 16px !important;
357
+
358
+ font-weight:
359
+ 700 !important;
360
+
361
+ transition:
362
+ all .25s ease !important;
363
+
364
+ }
365
+
366
+
367
+
368
+ button.primary:hover {
369
+
370
+ transform:
371
+ translateY(-3px);
372
+
373
+ box-shadow:
374
+ 0 10px 30px
375
+ rgba(
376
+ 37,
377
+ 99,
378
+ 235,
379
+ .35
380
+ ) !important;
381
+
382
+ }
383
+
384
+
385
+
386
+ /* ==============================================
387
+ Textbox Styling
388
+ ============================================== */
389
+
390
+
391
+ textarea {
392
+
393
+ border-radius:
394
+ 16px !important;
395
+
396
+ }
397
+
398
+
399
+
400
+ /* ==============================================
401
+ Image Output
402
+ ============================================== */
403
+
404
+
405
+ .image-container {
406
+
407
+ border-radius:
408
+ 20px !important;
409
+
410
+ overflow:
411
+ hidden !important;
412
+
413
+ }
414
+
415
+
416
+
417
+ /* ==============================================
418
+ Footer
419
+ ============================================== */
420
+
421
+
422
+ .footer-text {
423
+
424
+ text-align:
425
+ center;
426
+
427
+ opacity:
428
+ .75;
429
+
430
+ padding:
431
+ 1rem;
432
+
433
+ }
434
+
435
+
436
+
437
+ .footer-text a {
438
+
439
+ color:
440
+ #2563eb !important;
441
+
442
+ text-decoration:
443
+ none !important;
444
+
445
+ font-weight:
446
+ 600;
447
+
448
+ }
449
+
450
+
451
+
452
+ .footer-text a:hover {
453
+
454
+ text-decoration:
455
+ underline !important;
456
+
457
+ }
458
+
459
+
460
+
461
+ /* ==============================================
462
+ Container Width
463
+ ============================================== */
464
+
465
+
466
+ .gradio-container {
467
+
468
+ max-width:
469
+ 1400px !important;
470
+
471
+ margin:
472
+ auto !important;
473
+
474
+ }
475
+
476
+
477
+
478
+ /* ==============================================
479
+ Mobile
480
+ ============================================== */
481
+
482
+
483
+ @media(max-width:768px){
484
+
485
+ .header-text h1 {
486
+
487
+ font-size:
488
+ 1.8rem !important;
489
+
490
+ }
491
+
492
+ }
493
+
494
+ """,
495
+
496
+ footer_links=[
497
+ "api",
498
+ "gradio"
499
+ ],
500
+
501
+ mcp_server=True,
502
+ )