rahul7star commited on
Commit
9d9e3d4
·
verified ·
1 Parent(s): 9a2334f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -114
app.py CHANGED
@@ -2,41 +2,88 @@ import torch
2
  import spaces
3
  import gradio as gr
4
  from diffusers import DiffusionPipeline
5
-
6
- import spaces
7
- from dataclasses import dataclass
8
- import json
9
- import logging
10
- import os
11
- import random
12
- import re
13
  import sys
14
- import warnings
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- print("Loading Z-Image-Turbo pipeline...")
17
  pipe = DiffusionPipeline.from_pretrained(
18
- "Tongyi-MAI/Z-Image-Turbo",#"T5B/Z-Image-Turbo-FP8",
19
  torch_dtype=torch.bfloat16,
20
  low_cpu_mem_usage=False,
21
  attn_implementation="kernels-community/vllm-flash-attn3",
22
  )
23
 
24
- #pipe.transformer.layers._repeated_blocks = ["ZImageTransformerBlock"]
25
- #spaces.aoti_blocks_load(pipe.transformer.layers, "zerogpu-aoti/Z-Image", variant="fa3")
26
-
27
  pipe.to("cuda")
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  @spaces.GPU
30
  def generate_image(prompt, height, width, num_inference_steps, seed, randomize_seed, num_images):
31
- """Generate multiple images from the given prompt."""
32
- print(prompt)
 
 
 
 
 
 
 
33
  if randomize_seed:
34
  seed = torch.randint(0, 2**32 - 1, (1,)).item()
 
 
 
35
 
36
- # Clamp num_images to max 3
37
  num_images = min(max(1, int(num_images)), 3)
38
-
 
 
 
39
  generator = torch.Generator("cuda").manual_seed(int(seed))
 
 
40
  result = pipe(
41
  prompt=prompt,
42
  height=int(height),
@@ -45,117 +92,73 @@ def generate_image(prompt, height, width, num_inference_steps, seed, randomize_s
45
  guidance_scale=0.0,
46
  generator=generator,
47
  max_sequence_length=1024,
48
- num_images_per_prompt=num_images
 
49
  )
50
-
51
- return result.images, seed
52
 
53
- # Example prompts
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  examples = [
55
- ["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."],
56
- ["A majestic dragon soaring through clouds at sunset, scales shimmering with iridescent colors, detailed fantasy art style"],
57
- ["Cozy coffee shop interior, warm lighting, rain on windows, plants on shelves, vintage aesthetic, photorealistic"],
58
- ["Astronaut riding a horse on Mars, cinematic lighting, sci-fi concept art, highly detailed"],
59
- ["Portrait of a wise old wizard with a long white beard, holding a glowing crystal staff, magical forest background"],
60
  ]
61
 
62
- # Build the Gradio interface
63
- with gr.Blocks(title="Z-Image-Turbo Demo") as demo:
64
- gr.Markdown(
65
- """
66
- # 🎨 Z-Image-Turbo Multi Image Demo
67
-
68
- Generate high-quality images using the [Tongyi-MAI/Z-Image-Turbo](https://huggingface.co/Tongyi-MAI/Z-Image-Turbo) model.
69
- This turbo model generates images in just 8 inference steps!
70
- """
71
- )
72
-
73
  with gr.Row():
74
  with gr.Column(scale=1):
75
- prompt = gr.Textbox(
76
- label="Prompt",
77
- placeholder="Enter your image description...",
78
- lines=4,
79
- )
80
-
81
- with gr.Row():
82
- height = gr.Slider(
83
- minimum=512,
84
- maximum=2048,
85
- value=1024,
86
- step=64,
87
- label="Height",
88
- )
89
- width = gr.Slider(
90
- minimum=512,
91
- maximum=2048,
92
- value=1024,
93
- step=64,
94
- label="Width",
95
- )
96
- with gr.Row():
97
- num_images = gr.Slider(
98
- minimum=1,
99
- maximum=3,
100
- value=2,
101
- step=1,
102
- label="Number of Images",
103
- )
104
 
105
  with gr.Row():
106
- num_inference_steps = gr.Slider(
107
- minimum=1,
108
- maximum=20,
109
- value=9,
110
- step=1,
111
- label="Inference Steps",
112
- info="9 steps results in 8 DiT forwards",
113
- )
114
-
 
115
  with gr.Row():
116
- seed = gr.Number(
117
- label="Seed",
118
- value=42,
119
- precision=0,
120
- )
121
- randomize_seed = gr.Checkbox(
122
- label="Randomize Seed",
123
- value=False,
124
- )
125
-
126
- generate_btn = gr.Button("🚀 Generate", variant="primary", size="lg")
127
-
128
  with gr.Column(scale=1):
129
- output_images = gr.Gallery(
130
- label="Generated Image",
131
- type="pil",
 
 
 
132
  )
133
- used_seed = gr.Number(
134
- label="Seed Used",
135
- interactive=False,
136
- )
137
-
138
- gr.Markdown("### 💡 Example Prompts")
139
- gr.Examples(
140
- examples=examples,
141
- inputs=[prompt],
142
- cache_examples=False,
143
- )
144
 
145
-
146
- # Connect the generate button
147
  generate_btn.click(
148
  fn=generate_image,
149
  inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed, num_images],
150
- outputs=[output_images, used_seed],
151
- )
152
- prompt.submit(
153
- fn=generate_image,
154
- inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed, num_images],
155
- outputs=[output_images, used_seed],
156
- )
157
-
158
-
159
 
160
  if __name__ == "__main__":
161
- demo.launch()
 
2
  import spaces
3
  import gradio as gr
4
  from diffusers import DiffusionPipeline
5
+ import diffusers
6
+ import io
 
 
 
 
 
 
7
  import sys
8
+ import logging
9
+
10
+ # ------------------------
11
+ # GLOBAL LOG BUFFER
12
+ # ------------------------
13
+ log_buffer = io.StringIO()
14
+
15
+ def log(msg):
16
+ print(msg)
17
+ log_buffer.write(msg + "\n")
18
+
19
+ # Enable diffusers debug logs
20
+ diffusers.utils.logging.set_verbosity_info()
21
+
22
+ log("Loading Z-Image-Turbo pipeline...")
23
 
 
24
  pipe = DiffusionPipeline.from_pretrained(
25
+ "Tongyi-MAI/Z-Image-Turbo",
26
  torch_dtype=torch.bfloat16,
27
  low_cpu_mem_usage=False,
28
  attn_implementation="kernels-community/vllm-flash-attn3",
29
  )
30
 
 
 
 
31
  pipe.to("cuda")
32
 
33
+
34
+ #pipe.transformer.layers._repeated_blocks = ["ZImageTransformerBlock"] #spaces.aoti_blocks_load(pipe.transformer.layers, "zerogpu-aoti/Z-Image", variant="fa3")
35
+
36
+ # ------------------------
37
+ # ATTENTION + PIPE INFO
38
+ # ------------------------
39
+ def pipeline_debug_info():
40
+ info = []
41
+ info.append("=== PIPELINE DEBUG INFO ===")
42
+ info.append(f"UNet attention backend: {pipe.unet.config.attn_implementation}")
43
+ info.append(f"Transformer attention backend: {pipe.transformer.config.attn_implementation}")
44
+
45
+ # Processor classes
46
+ try:
47
+ info.append(f"UNet mid-block processor: {type(pipe.unet.mid_block.attentions[0].processor)}")
48
+ except:
49
+ info.append("UNet mid-block processor: <not found>")
50
+
51
+ try:
52
+ info.append(f"Transformer block processor: {type(pipe.transformer.blocks[0].attn.processor)}")
53
+ except:
54
+ info.append("Transformer block processor: <not found>")
55
+
56
+ return "\n".join(info)
57
+
58
+ # ------------------------
59
+ # IMAGE GENERATOR
60
+ # ------------------------
61
  @spaces.GPU
62
  def generate_image(prompt, height, width, num_inference_steps, seed, randomize_seed, num_images):
63
+ log_buffer.truncate(0)
64
+ log_buffer.seek(0)
65
+
66
+ log("=== NEW GENERATION REQUEST ===")
67
+ log(f"Prompt: {prompt}")
68
+ log(f"Height: {height}, Width: {width}")
69
+ log(f"Inference Steps: {num_inference_steps}")
70
+ log(f"Num Images: {num_images}")
71
+
72
  if randomize_seed:
73
  seed = torch.randint(0, 2**32 - 1, (1,)).item()
74
+ log(f"Randomized Seed → {seed}")
75
+ else:
76
+ log(f"Seed: {seed}")
77
 
78
+ # Clamp images
79
  num_images = min(max(1, int(num_images)), 3)
80
+
81
+ # Debug pipe info
82
+ log(pipeline_debug_info())
83
+
84
  generator = torch.Generator("cuda").manual_seed(int(seed))
85
+
86
+ log("Running pipeline forward()...")
87
  result = pipe(
88
  prompt=prompt,
89
  height=int(height),
 
92
  guidance_scale=0.0,
93
  generator=generator,
94
  max_sequence_length=1024,
95
+ num_images_per_prompt=num_images,
96
+ output_type="pil",
97
  )
 
 
98
 
99
+ # Tensor diagnostics (shapes only)
100
+ try:
101
+ latent_shape = pipe.unet.config.sample_size
102
+ log(f"UNet latent resolution (approx): {latent_shape}")
103
+ except:
104
+ pass
105
+
106
+ log("Pipeline finished.")
107
+ log("Returning images...")
108
+
109
+ return result.images, seed, log_buffer.getvalue()
110
+
111
+ # ------------------------
112
+ # GRADIO UI
113
+ # ------------------------
114
  examples = [
115
+ ["Young Chinese woman in red Hanfu, intricate embroidery..."],
116
+ ["A majestic dragon soaring through clouds at sunset..."],
117
+ ["Cozy coffee shop interior, warm lighting, rain on windows..."],
118
+ ["Astronaut riding a horse on Mars, cinematic lighting..."],
119
+ ["Portrait of a wise old wizard..."],
120
  ]
121
 
122
+ with gr.Blocks(title="Z-Image-Turbo Debug Demo") as demo:
123
+ gr.Markdown("# 🎨 Z-Image-Turbo — Multi Image + Full Debug Logs")
124
+
 
 
 
 
 
 
 
 
125
  with gr.Row():
126
  with gr.Column(scale=1):
127
+ prompt = gr.Textbox(label="Prompt", lines=4)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
  with gr.Row():
130
+ height = gr.Slider(512, 2048, 1024, step=64, label="Height")
131
+ width = gr.Slider(512, 2048, 1024, step=64, label="Width")
132
+
133
+ num_images = gr.Slider(1, 3, 2, step=1, label="Number of Images")
134
+
135
+ num_inference_steps = gr.Slider(
136
+ 1, 20, 9, step=1, label="Inference Steps",
137
+ info="9 steps = 8 DiT forward passes",
138
+ )
139
+
140
  with gr.Row():
141
+ seed = gr.Number(label="Seed", value=42, precision=0)
142
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value=False)
143
+
144
+ generate_btn = gr.Button("🚀 Generate", variant="primary")
145
+
 
 
 
 
 
 
 
146
  with gr.Column(scale=1):
147
+ output_images = gr.Gallery(label="Generated Images")
148
+ used_seed = gr.Number(label="Seed Used", interactive=False)
149
+ debug_log = gr.Textbox(
150
+ label="Debug Log Output",
151
+ lines=25,
152
+ interactive=False
153
  )
 
 
 
 
 
 
 
 
 
 
 
154
 
155
+ gr.Examples(examples=examples, inputs=[prompt], cache_examples=False)
156
+
157
  generate_btn.click(
158
  fn=generate_image,
159
  inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed, num_images],
160
+ outputs=[output_images, used_seed, debug_log],
161
+ )
 
 
 
 
 
 
 
162
 
163
  if __name__ == "__main__":
164
+ demo.launch()