simapeng commited on
Commit
f8367e5
·
1 Parent(s): 82a1efa

1st version

Browse files
Files changed (3) hide show
  1. app.py +141 -114
  2. requirements.txt +4 -5
  3. zimage_client.py +59 -0
app.py CHANGED
@@ -1,154 +1,181 @@
 
 
1
  import gradio as gr
2
- import numpy as np
3
- import random
4
-
5
- # import spaces #[uncomment to use ZeroGPU]
6
  from diffusers import DiffusionPipeline
7
- import torch
8
 
9
- device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
11
 
12
- if torch.cuda.is_available():
13
- torch_dtype = torch.float16
14
- else:
15
- torch_dtype = torch.float32
 
 
 
 
16
 
17
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
- pipe = pipe.to(device)
 
19
 
20
- MAX_SEED = np.iinfo(np.int32).max
21
- MAX_IMAGE_SIZE = 1024
22
 
23
-
24
- # @spaces.GPU #[uncomment to use ZeroGPU]
25
- def infer(
26
  prompt,
27
  negative_prompt,
28
- seed,
29
- randomize_seed,
30
- width,
31
  height,
32
- guidance_scale,
33
  num_inference_steps,
 
 
 
34
  progress=gr.Progress(track_tqdm=True),
35
  ):
 
36
  if randomize_seed:
37
- seed = random.randint(0, MAX_SEED)
38
 
39
- generator = torch.Generator().manual_seed(seed)
 
 
40
 
41
- image = pipe(
42
- prompt=prompt,
43
- negative_prompt=negative_prompt,
44
- guidance_scale=guidance_scale,
45
- num_inference_steps=num_inference_steps,
46
- width=width,
47
- height=height,
48
- generator=generator,
49
- ).images[0]
50
 
51
- return image, seed
 
 
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
- examples = [
55
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
56
- "An astronaut riding a green horse",
57
- "A delicious ceviche cheesecake slice",
58
- ]
59
 
60
- css = """
61
- #col-container {
62
- margin: 0 auto;
63
- max-width: 640px;
64
- }
65
- """
66
 
67
- with gr.Blocks(css=css) as demo:
68
- with gr.Column(elem_id="col-container"):
69
- gr.Markdown(" # Text-to-Image Gradio Template")
 
 
70
 
71
- with gr.Row():
72
- prompt = gr.Text(
 
 
 
 
 
 
 
 
 
 
 
 
73
  label="Prompt",
74
- show_label=False,
75
- max_lines=1,
76
- placeholder="Enter your prompt",
77
- container=False,
78
- )
79
-
80
- run_button = gr.Button("Run", scale=0, variant="primary")
81
-
82
- result = gr.Image(label="Result", show_label=False)
83
-
84
- with gr.Accordion("Advanced Settings", open=False):
85
- negative_prompt = gr.Text(
86
- label="Negative prompt",
87
- max_lines=1,
88
- placeholder="Enter a negative prompt",
89
- visible=False,
90
  )
91
 
92
- seed = gr.Slider(
93
- label="Seed",
94
- minimum=0,
95
- maximum=MAX_SEED,
96
- step=1,
97
- value=0,
98
  )
99
 
100
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
101
-
102
  with gr.Row():
103
- width = gr.Slider(
104
- label="Width",
105
- minimum=256,
106
- maximum=MAX_IMAGE_SIZE,
107
- step=32,
108
- value=1024, # Replace with defaults that work for your model
109
- )
110
-
111
  height = gr.Slider(
 
 
 
 
112
  label="Height",
113
- minimum=256,
114
- maximum=MAX_IMAGE_SIZE,
115
- step=32,
116
- value=1024, # Replace with defaults that work for your model
117
  )
118
-
119
- with gr.Row():
120
- guidance_scale = gr.Slider(
121
- label="Guidance scale",
122
- minimum=0.0,
123
- maximum=10.0,
124
- step=0.1,
125
- value=0.0, # Replace with defaults that work for your model
126
  )
127
-
 
128
  num_inference_steps = gr.Slider(
129
- label="Number of inference steps",
130
  minimum=1,
131
- maximum=50,
 
132
  step=1,
133
- value=2, # Replace with defaults that work for your model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
- gr.Examples(examples=examples, inputs=[prompt])
137
- gr.on(
138
- triggers=[run_button.click, prompt.submit],
139
- fn=infer,
140
- inputs=[
141
- prompt,
142
- negative_prompt,
143
- seed,
144
- randomize_seed,
145
- width,
146
- height,
147
- guidance_scale,
148
- num_inference_steps,
149
- ],
150
- outputs=[result, seed],
151
  )
152
 
153
  if __name__ == "__main__":
154
- demo.launch()
 
1
+ import torch
2
+ import spaces
3
  import gradio as gr
 
 
 
 
4
  from diffusers import DiffusionPipeline
 
5
 
6
+ MAX_SEED = 2**32 - 1
 
7
 
8
+ # Load the pipeline once at startup
9
+ print("Loading Z-Image-Turbo pipeline...")
10
+ pipe = DiffusionPipeline.from_pretrained(
11
+ "Tongyi-MAI/Z-Image-Turbo",
12
+ torch_dtype=torch.bfloat16,
13
+ low_cpu_mem_usage=False,
14
+ )
15
+ pipe.to("cuda")
16
 
17
+ '# ======== AoTI compilation + FA3 ======== (disabled on HF to avoid outdated AOTI/FA3 package errors)'
18
+ # pipe.transformer.layers._repeated_blocks = ["ZImageTransformerBlock"]
19
+ # spaces.aoti_blocks_load(pipe.transformer.layers, "zerogpu-aoti/Z-Image", variant="fa3")
20
 
21
+ print("Pipeline loaded!")
 
22
 
23
+ @spaces.GPU
24
+ def generate_image(
 
25
  prompt,
26
  negative_prompt,
 
 
 
27
  height,
28
+ width,
29
  num_inference_steps,
30
+ guidance_scale,
31
+ seed,
32
+ randomize_seed,
33
  progress=gr.Progress(track_tqdm=True),
34
  ):
35
+ """Generate 4 images with seeds: seed, 2x, 3x, 4x (mod MAX_SEED)."""
36
  if randomize_seed:
37
+ seed = torch.randint(0, MAX_SEED, (1,)).item()
38
 
39
+ base_seed = int(seed) % MAX_SEED
40
+ if base_seed < 0:
41
+ base_seed += MAX_SEED
42
 
43
+ seeds = [(base_seed * i) % MAX_SEED for i in range(1, 5)]
 
 
 
 
 
 
 
 
44
 
45
+ neg_prompt = None
46
+ if isinstance(negative_prompt, str) and negative_prompt.strip():
47
+ neg_prompt = negative_prompt
48
 
49
+ images = []
50
+ for s in seeds:
51
+ generator = torch.Generator("cuda").manual_seed(int(s))
52
+ image = pipe(
53
+ prompt=prompt,
54
+ negative_prompt=neg_prompt,
55
+ height=int(height),
56
+ width=int(width),
57
+ num_inference_steps=int(num_inference_steps),
58
+ guidance_scale=float(guidance_scale), # 0.0 is recommended default for Turbo
59
+ generator=generator,
60
+ ).images[0]
61
+ images.append(image)
62
 
63
+ return images, ", ".join(str(s) for s in seeds)
 
 
 
 
64
 
 
 
 
 
 
 
65
 
66
+ # Example prompts
67
+ examples = [
68
+ ["Astronaut riding a horse on Mars, cinematic lighting, sci-fi concept art, highly detailed"],
69
+ ["Portrait of a wise old wizard with a long white beard, holding a glowing crystal staff, magical forest background"],
70
+ ]
71
 
72
+ # Build the Gradio interface
73
+ with gr.Blocks(title="Z-Image-Turbo Demo") as demo:
74
+ gr.Markdown(
75
+ """
76
+ # 🎨 Z-Image-Turbo Demo
77
+
78
+ Generate high-quality images using the [Tongyi-MAI/Z-Image-Turbo](https://huggingface.co/Tongyi-MAI/Z-Image-Turbo) model.
79
+ This turbo model generates images in just 8 inference steps!
80
+ """
81
+ )
82
+
83
+ with gr.Row():
84
+ with gr.Column(scale=1):
85
+ prompt = gr.Textbox(
86
  label="Prompt",
87
+ placeholder="Enter your image description...",
88
+ lines=4,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  )
90
 
91
+ negative_prompt = gr.Textbox(
92
+ label="Negative Prompt",
93
+ placeholder="Things you don't want in the image...",
94
+ lines=3,
 
 
95
  )
96
 
 
 
97
  with gr.Row():
 
 
 
 
 
 
 
 
98
  height = gr.Slider(
99
+ minimum=512,
100
+ maximum=2048,
101
+ value=1024,
102
+ step=64,
103
  label="Height",
 
 
 
 
104
  )
105
+ width = gr.Slider(
106
+ minimum=512,
107
+ maximum=2048,
108
+ value=1024,
109
+ step=64,
110
+ label="Width",
 
 
111
  )
112
+
113
+ with gr.Row():
114
  num_inference_steps = gr.Slider(
 
115
  minimum=1,
116
+ maximum=20,
117
+ value=9,
118
  step=1,
119
+ label="Inference Steps",
120
+ info="9 steps results in 8 DiT forwards",
121
+ )
122
+
123
+ guidance_scale = gr.Slider(
124
+ minimum=0.0,
125
+ maximum=7.0,
126
+ value=0.0,
127
+ step=0.1,
128
+ label="CFG Guidance Scale",
129
+ info="0 = no CFG (recommended for Turbo models)",
130
+ )
131
+
132
+ with gr.Row():
133
+ seed = gr.Number(
134
+ label="Seed",
135
+ value=42,
136
+ precision=0,
137
  )
138
+ randomize_seed = gr.Checkbox(
139
+ label="Randomize Seed",
140
+ value=False,
141
+ )
142
+
143
+ generate_btn = gr.Button("🚀 Generate", variant="primary", size="lg")
144
+
145
+ with gr.Column(scale=1):
146
+ output_images = gr.Gallery(
147
+ label="Generated Images",
148
+ columns=2,
149
+ rows=2,
150
+ preview=True,
151
+ )
152
+ used_seeds = gr.Textbox(
153
+ label="Seeds Used (base, 2x, 3x, 4x)",
154
+ interactive=False,
155
+ )
156
+
157
+ gr.Markdown("### 💡 Example Prompts")
158
+ gr.Examples(
159
+ examples=examples,
160
+ inputs=[prompt],
161
+ cache_examples=False,
162
+ )
163
 
164
+ gr.Markdown("Demo by [mrfakename](https://x.com/realmrfakename). Model by Alibaba. The model is licensed under Apache 2.0, you can use generated images commercially! Thanks to [multimodalart](https://huggingface.co/multimodalart) for the FA3 + AoTI enhancements/speedups")
165
+
166
+ # Connect the generate button
167
+ generate_btn.click(
168
+ fn=generate_image,
169
+ inputs=[prompt, negative_prompt, height, width, num_inference_steps, guidance_scale, seed, randomize_seed],
170
+ outputs=[output_images, used_seeds],
171
+ )
172
+
173
+ # Also allow generating by pressing Enter in the prompt box
174
+ prompt.submit(
175
+ fn=generate_image,
176
+ inputs=[prompt, negative_prompt, height, width, num_inference_steps, guidance_scale, seed, randomize_seed],
177
+ outputs=[output_images, used_seeds],
 
178
  )
179
 
180
  if __name__ == "__main__":
181
+ demo.launch(mcp_server=True, show_error=True)
requirements.txt CHANGED
@@ -1,6 +1,5 @@
1
- accelerate
2
- diffusers
3
- invisible_watermark
4
- torch
5
  transformers
6
- xformers
 
 
1
+ gradio
2
+ git+https://github.com/huggingface/diffusers
 
 
3
  transformers
4
+ kernels
5
+ gradio[mcp]
zimage_client.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from datetime import datetime
3
+
4
+ from gradio_client import Client
5
+
6
+
7
+ def save_images(images, used_seeds, output_dir="outputs"):
8
+ os.makedirs(output_dir, exist_ok=True)
9
+ timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
10
+
11
+ for index, image in enumerate(images):
12
+ filename = f"{timestamp}_img{index + 1}.png"
13
+ path = os.path.join(output_dir, filename)
14
+
15
+ # `image` may be a PIL.Image or a path-like object depending on Gradio.
16
+ if hasattr(image, "save"):
17
+ image.save(path)
18
+ else:
19
+ from PIL import Image
20
+
21
+ Image.open(image).save(path)
22
+
23
+ print(f"Saved: {path}")
24
+
25
+ print(f"Seeds used: {used_seeds}")
26
+
27
+
28
+ def main():
29
+ # Space that runs the Z-Image UI; change if needed.
30
+ space_id = "xiaopeng/Z_Image_Turbo_Free"
31
+
32
+ client = Client(space_id)
33
+
34
+ prompt = "A beautiful portrait photo of a woman, 4k, highly detailed"
35
+ negative_prompt = ""
36
+ height = 1024
37
+ width = 1024
38
+ num_inference_steps = 9
39
+ guidance_scale = 0.0 # 0 = no CFG, as recommended for Turbo
40
+ seed = 42
41
+ randomize_seed = False
42
+
43
+ images, used_seeds = client.predict(
44
+ prompt,
45
+ negative_prompt,
46
+ height,
47
+ width,
48
+ num_inference_steps,
49
+ guidance_scale,
50
+ seed,
51
+ randomize_seed,
52
+ api_name="/generate_image",
53
+ )
54
+
55
+ save_images(images, used_seeds)
56
+
57
+
58
+ if __name__ == "__main__":
59
+ main()