rahul7star commited on
Commit
ef8cd2a
Β·
verified Β·
1 Parent(s): 2efafc4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import spaces
3
+ import gradio as gr
4
+ from diffusers import DiffusionPipeline
5
+
6
+ # Load the pipeline once at startup
7
+ print("Loading Z-Image-Turbo pipeline...")
8
+ pipe = DiffusionPipeline.from_pretrained(
9
+ "rahul7star/Z-Image-Turbo",
10
+ torch_dtype=torch.bfloat16,
11
+ low_cpu_mem_usage=False,
12
+ )
13
+ pipe.to("cuda")
14
+
15
+ # ======== AoTI compilation + FA3 ========
16
+ pipe.transformer.layers._repeated_blocks = ["ZImageTransformerBlock"]
17
+ spaces.aoti_blocks_load(pipe.transformer.layers, "zerogpu-aoti/Z-Image", variant="fa3")
18
+
19
+ print("Pipeline loaded!")
20
+
21
+ @spaces.GPU
22
+ def generate_image(prompt, height, width, num_inference_steps, seed, randomize_seed, progress=gr.Progress(track_tqdm=True)):
23
+ """Generate an image from the given prompt."""
24
+ if randomize_seed:
25
+ seed = torch.randint(0, 2**32 - 1, (1,)).item()
26
+
27
+ generator = torch.Generator("cuda").manual_seed(int(seed))
28
+ image = pipe(
29
+ prompt=prompt,
30
+ height=int(height),
31
+ width=int(width),
32
+ num_inference_steps=int(num_inference_steps),
33
+ guidance_scale=0.0, # Guidance should be 0 for Turbo models
34
+ generator=generator,
35
+ ).images[0]
36
+
37
+ return image, seed
38
+
39
+
40
+ # Example prompts
41
+ examples = [
42
+ ["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."],
43
+ ["A majestic dragon soaring through clouds at sunset, scales shimmering with iridescent colors, detailed fantasy art style"],
44
+ ["Cozy coffee shop interior, warm lighting, rain on windows, plants on shelves, vintage aesthetic, photorealistic"],
45
+ ["Astronaut riding a horse on Mars, cinematic lighting, sci-fi concept art, highly detailed"],
46
+ ["Portrait of a wise old wizard with a long white beard, holding a glowing crystal staff, magical forest background"],
47
+ ]
48
+
49
+ # Build the Gradio interface
50
+ with gr.Blocks(title="Z-Image-Turbo Demo") as demo:
51
+ gr.Markdown(
52
+ """
53
+ # 🎨 Z-Image-Turbo Demo
54
+
55
+ Generate high-quality images using the [Tongyi-MAI/Z-Image-Turbo](https://huggingface.co/Tongyi-MAI/Z-Image-Turbo) model.
56
+ This turbo model generates images in just 8 inference steps!
57
+ """
58
+ )
59
+
60
+ with gr.Row():
61
+ with gr.Column(scale=1):
62
+ prompt = gr.Textbox(
63
+ label="Prompt",
64
+ placeholder="Enter your image description...",
65
+ lines=4,
66
+ )
67
+
68
+ with gr.Row():
69
+ height = gr.Slider(
70
+ minimum=512,
71
+ maximum=2048,
72
+ value=1024,
73
+ step=64,
74
+ label="Height",
75
+ )
76
+ width = gr.Slider(
77
+ minimum=512,
78
+ maximum=2048,
79
+ value=1024,
80
+ step=64,
81
+ label="Width",
82
+ )
83
+
84
+ with gr.Row():
85
+ num_inference_steps = gr.Slider(
86
+ minimum=1,
87
+ maximum=20,
88
+ value=9,
89
+ step=1,
90
+ label="Inference Steps",
91
+ info="9 steps results in 8 DiT forwards",
92
+ )
93
+
94
+ with gr.Row():
95
+ seed = gr.Number(
96
+ label="Seed",
97
+ value=42,
98
+ precision=0,
99
+ )
100
+ randomize_seed = gr.Checkbox(
101
+ label="Randomize Seed",
102
+ value=False,
103
+ )
104
+
105
+ generate_btn = gr.Button("πŸš€ Generate", variant="primary", size="lg")
106
+
107
+ with gr.Column(scale=1):
108
+ output_image = gr.Image(
109
+ label="Generated Image",
110
+ type="pil",
111
+ )
112
+ used_seed = gr.Number(
113
+ label="Seed Used",
114
+ interactive=False,
115
+ )
116
+
117
+ gr.Markdown("### πŸ’‘ Example Prompts")
118
+ gr.Examples(
119
+ examples=examples,
120
+ inputs=[prompt],
121
+ cache_examples=False,
122
+ )
123
+
124
+ 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")
125
+
126
+ # Connect the generate button
127
+ generate_btn.click(
128
+ fn=generate_image,
129
+ inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed],
130
+ outputs=[output_image, used_seed],
131
+ )
132
+
133
+ # Also allow generating by pressing Enter in the prompt box
134
+ prompt.submit(
135
+ fn=generate_image,
136
+ inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed],
137
+ outputs=[output_image, used_seed],
138
+ )
139
+
140
+ if __name__ == "__main__":
141
+ demo.launch(mcp_server=True)