burak12321 commited on
Commit
4294d6b
·
verified ·
1 Parent(s): 9f3d895

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +233 -129
app.py CHANGED
@@ -1,154 +1,258 @@
 
 
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
+ # Load the pipeline once at startup
7
+ print("Loading Z-Image-Turbo pipeline...")
8
+ pipe = DiffusionPipeline.from_pretrained(
9
+ "Tongyi-MAI/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,
 
34
  generator=generator,
35
  ).images[0]
36
+
37
  return image, seed
38
 
39
+ # Example prompts
40
  examples = [
41
+ ["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."],
42
+ ["A majestic dragon soaring through clouds at sunset, scales shimmering with iridescent colors, detailed fantasy art style"],
43
+ ["Cozy coffee shop interior, warm lighting, rain on windows, plants on shelves, vintage aesthetic, photorealistic"],
44
+ ["Astronaut riding a horse on Mars, cinematic lighting, sci-fi concept art, highly detailed"],
45
+ ["Portrait of a wise old wizard with a long white beard, holding a glowing crystal staff, magical forest background"],
46
  ]
47
 
48
+ # Custom theme with modern aesthetics (Gradio 6)
49
+ custom_theme = gr.themes.Soft(
50
+ primary_hue="yellow",
51
+ secondary_hue="amber",
52
+ neutral_hue="slate",
53
+ font=gr.themes.GoogleFont("Inter"),
54
+ text_size="lg",
55
+ spacing_size="md",
56
+ radius_size="lg"
57
+ ).set(
58
+ button_primary_background_fill="*primary_500",
59
+ button_primary_background_fill_hover="*primary_600",
60
+ block_title_text_weight="600",
61
+ )
62
 
63
+ # Build the Gradio interface
64
+ with gr.Blocks(fill_height=True) as demo:
65
+ # Header
66
+ gr.Markdown(
67
+ """
68
+ # burak image
69
+ **Ultra-fast AI image generation** • Generate stunning images in just 8 steps
70
+ """,
71
+ elem_classes="header-text"
72
+ )
73
+
74
+ with gr.Row(equal_height=False):
75
+ # Left column - Input controls
76
+ with gr.Column(scale=1, min_width=320):
77
+ prompt = gr.Textbox(
78
+ label="✨ Your Prompt",
79
+ placeholder="Describe the image you want to create...",
80
+ lines=5,
81
+ max_lines=10,
82
+ autofocus=True,
 
 
 
 
 
 
 
 
 
 
 
83
  )
84
+
85
+ with gr.Accordion("⚙️ Advanced Settings", open=False):
86
+ with gr.Row():
87
+ height = gr.Slider(
88
+ minimum=512,
89
+ maximum=2048,
90
+ value=1024,
91
+ step=64,
92
+ label="Height",
93
+ info="Image height in pixels"
94
+ )
95
+ width = gr.Slider(
96
+ minimum=512,
97
+ maximum=2048,
98
+ value=1024,
99
+ step=64,
100
+ label="Width",
101
+ info="Image width in pixels"
102
+ )
103
+
 
 
 
 
 
 
 
 
 
104
  num_inference_steps = gr.Slider(
 
105
  minimum=1,
106
+ maximum=20,
107
+ value=9,
108
  step=1,
109
+ label="Inference Steps",
110
+ info="9 steps = 8 DiT forwards (recommended)"
111
  )
112
+
113
+ with gr.Row():
114
+ randomize_seed = gr.Checkbox(
115
+ label="🎲 Random Seed",
116
+ value=True,
117
+ )
118
+ seed = gr.Number(
119
+ label="Seed",
120
+ value=42,
121
+ precision=0,
122
+ visible=False,
123
+ )
124
+
125
+ def toggle_seed(randomize):
126
+ return gr.Number(visible=not randomize)
127
+
128
+ randomize_seed.change(
129
+ toggle_seed,
130
+ inputs=[randomize_seed],
131
+ outputs=[seed]
132
+ )
133
+
134
+ generate_btn = gr.Button(
135
+ "🚀 Generate Image",
136
+ variant="primary",
137
+ size="lg",
138
+ scale=1
139
+ )
140
+
141
+ # Example prompts
142
+ gr.Examples(
143
+ examples=examples,
144
+ inputs=[prompt],
145
+ label="💡 Try these prompts",
146
+ examples_per_page=5,
147
+ )
148
+
149
+ # Right column - Output
150
+ with gr.Column(scale=1, min_width=320):
151
+ output_image = gr.Image(
152
+ label="Generated Image",
153
+ type="pil",
154
+ show_label=False,
155
+ height=600,
156
+ buttons=["download", "share"],
157
+ )
158
+
159
+ used_seed = gr.Number(
160
+ label="🎲 Seed Used",
161
+ interactive=False,
162
+ container=True,
163
+ )
164
+
165
+ # Footer credits
166
+ gr.Markdown(
167
+ """
168
+ ---
169
+ <div style="text-align: center; opacity: 0.7; font-size: 0.9em; margin-top: 1rem;">
170
+ <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) •
171
+ <strong>Demo by:</strong> <a href="https://x.com/realmrfakename" target="_blank">@mrfakename</a> •
172
+ <strong>Redesign by:</strong> AnyCoder •
173
+ <strong>Optimizations:</strong> <a href="https://huggingface.co/multimodalart" target="_blank">@multimodalart</a> (FA3 + AoTI)
174
+ </div>
175
+ """,
176
+ elem_classes="footer-text"
177
+ )
178
+
179
+ # Connect the generate button
180
+ generate_btn.click(
181
+ fn=generate_image,
182
+ inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed],
183
+ outputs=[output_image, used_seed],
184
+ )
185
+
186
+ # Also allow generating by pressing Enter in the prompt box
187
+ prompt.submit(
188
+ fn=generate_image,
189
+ inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed],
190
+ outputs=[output_image, used_seed],
191
  )
192
 
193
  if __name__ == "__main__":
194
+ demo.launch(
195
+ theme=custom_theme,
196
+ css="""
197
+ .header-text h1 {
198
+ font-size: 2.5rem !important;
199
+ font-weight: 700 !important;
200
+ margin-bottom: 0.5rem !important;
201
+ background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
202
+ -webkit-background-clip: text;
203
+ -webkit-text-fill-color: transparent;
204
+ background-clip: text;
205
+ }
206
+
207
+ .header-text p {
208
+ font-size: 1.1rem !important;
209
+ color: #64748b !important;
210
+ margin-top: 0 !important;
211
+ }
212
+
213
+ .footer-text {
214
+ padding: 1rem 0;
215
+ }
216
+
217
+ .footer-text a {
218
+ color: #f59e0b !important;
219
+ text-decoration: none !important;
220
+ font-weight: 500;
221
+ }
222
+
223
+ .footer-text a:hover {
224
+ text-decoration: underline !important;
225
+ }
226
+
227
+ /* Mobile optimizations */
228
+ @media (max-width: 768px) {
229
+ .header-text h1 {
230
+ font-size: 1.8rem !important;
231
+ }
232
+
233
+ .header-text p {
234
+ font-size: 1rem !important;
235
+ }
236
+ }
237
+
238
+ /* Smooth transitions */
239
+ button, .gr-button {
240
+ transition: all 0.2s ease !important;
241
+ }
242
+
243
+ button:hover, .gr-button:hover {
244
+ transform: translateY(-1px);
245
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15) !important;
246
+ }
247
+
248
+ /* Better spacing */
249
+ .gradio-container {
250
+ max-width: 1400px !important;
251
+ margin: 0 auto !important;
252
+ }
253
+ """,
254
+ footer_links=[
255
+ "api",
256
+ "gradio"
257
+ ]
258
+ )