Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,35 @@
|
|
| 1 |
import torch
|
| 2 |
-
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
from diffusers import DiffusionPipeline
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
| 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 |
-
|
| 22 |
-
def generate_image(
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
if randomize_seed:
|
| 25 |
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 26 |
-
|
| 27 |
-
generator = torch.Generator(
|
|
|
|
| 28 |
image = pipe(
|
| 29 |
prompt=prompt,
|
| 30 |
height=int(height),
|
|
@@ -33,19 +38,20 @@ def generate_image(prompt, height, width, num_inference_steps, seed, randomize_s
|
|
| 33 |
guidance_scale=0.0,
|
| 34 |
generator=generator,
|
| 35 |
).images[0]
|
| 36 |
-
|
| 37 |
return image, seed
|
| 38 |
|
| 39 |
-
|
|
|
|
| 40 |
examples = [
|
| 41 |
-
["Young Chinese woman in red Hanfu, intricate embroidery
|
| 42 |
-
["A majestic dragon soaring through clouds at sunset,
|
| 43 |
-
["Cozy coffee shop interior,
|
| 44 |
-
["Astronaut riding a horse on Mars, cinematic
|
| 45 |
-
["Portrait of
|
| 46 |
]
|
| 47 |
|
| 48 |
-
#
|
| 49 |
custom_theme = gr.themes.Soft(
|
| 50 |
primary_hue="yellow",
|
| 51 |
secondary_hue="amber",
|
|
@@ -53,206 +59,85 @@ custom_theme = gr.themes.Soft(
|
|
| 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 |
-
#
|
| 64 |
-
with gr.Blocks(fill_height=True) as demo:
|
| 65 |
-
# Header
|
| 66 |
gr.Markdown(
|
| 67 |
"""
|
| 68 |
-
#
|
| 69 |
-
**Ultra-fast AI image generation** •
|
| 70 |
-
"""
|
| 71 |
-
elem_classes="header-text"
|
| 72 |
)
|
| 73 |
-
|
| 74 |
-
with gr.Row(
|
| 75 |
-
# Left column - Input controls
|
| 76 |
with gr.Column(scale=1, min_width=320):
|
| 77 |
prompt = gr.Textbox(
|
| 78 |
-
label="✨
|
| 79 |
-
placeholder="Describe the image you want
|
| 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 |
-
|
| 89 |
-
|
| 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 |
-
|
| 106 |
-
maximum=20,
|
| 107 |
-
value=9,
|
| 108 |
-
step=1,
|
| 109 |
-
label="Inference Steps",
|
| 110 |
-
info="9 steps = 8 DiT forwards (recommended)"
|
| 111 |
)
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 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 |
-
|
| 130 |
-
|
| 131 |
-
|
| 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="💡
|
| 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 |
-
|
| 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 |
-
|
| 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 |
-
)
|
|
|
|
| 1 |
import torch
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from diffusers import DiffusionPipeline
|
| 4 |
|
| 5 |
+
# ================== DEVICE ==================
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
print(f"Using device: {device}")
|
| 8 |
+
|
| 9 |
+
# ================== LOAD PIPELINE ==================
|
| 10 |
print("Loading Z-Image-Turbo pipeline...")
|
| 11 |
pipe = DiffusionPipeline.from_pretrained(
|
| 12 |
"Tongyi-MAI/Z-Image-Turbo",
|
|
|
|
| 13 |
low_cpu_mem_usage=False,
|
| 14 |
)
|
| 15 |
+
pipe = pipe.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
print("Pipeline loaded!")
|
| 17 |
|
| 18 |
+
# ================== IMAGE GENERATION ==================
|
| 19 |
+
def generate_image(
|
| 20 |
+
prompt,
|
| 21 |
+
height,
|
| 22 |
+
width,
|
| 23 |
+
num_inference_steps,
|
| 24 |
+
seed,
|
| 25 |
+
randomize_seed,
|
| 26 |
+
progress=gr.Progress(track_tqdm=True),
|
| 27 |
+
):
|
| 28 |
if randomize_seed:
|
| 29 |
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 30 |
+
|
| 31 |
+
generator = torch.Generator(device).manual_seed(int(seed))
|
| 32 |
+
|
| 33 |
image = pipe(
|
| 34 |
prompt=prompt,
|
| 35 |
height=int(height),
|
|
|
|
| 38 |
guidance_scale=0.0,
|
| 39 |
generator=generator,
|
| 40 |
).images[0]
|
| 41 |
+
|
| 42 |
return image, seed
|
| 43 |
|
| 44 |
+
|
| 45 |
+
# ================== EXAMPLES ==================
|
| 46 |
examples = [
|
| 47 |
+
["Young Chinese woman in red Hanfu, intricate embroidery, neon lightning lamp floating above palm, cinematic lighting"],
|
| 48 |
+
["A majestic dragon soaring through clouds at sunset, fantasy art, ultra detailed"],
|
| 49 |
+
["Cozy coffee shop interior, rain on windows, warm light, photorealistic"],
|
| 50 |
+
["Astronaut riding a horse on Mars, cinematic sci-fi concept art"],
|
| 51 |
+
["Portrait of an old wizard with glowing staff, magical forest"],
|
| 52 |
]
|
| 53 |
|
| 54 |
+
# ================== THEME ==================
|
| 55 |
custom_theme = gr.themes.Soft(
|
| 56 |
primary_hue="yellow",
|
| 57 |
secondary_hue="amber",
|
|
|
|
| 59 |
font=gr.themes.GoogleFont("Inter"),
|
| 60 |
text_size="lg",
|
| 61 |
spacing_size="md",
|
| 62 |
+
radius_size="lg",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
+
# ================== UI ==================
|
| 66 |
+
with gr.Blocks(fill_height=True, theme=custom_theme) as demo:
|
|
|
|
| 67 |
gr.Markdown(
|
| 68 |
"""
|
| 69 |
+
# 🤖 Burak Image
|
| 70 |
+
**Ultra-fast AI image generation** • CPU / GPU Auto
|
| 71 |
+
"""
|
|
|
|
| 72 |
)
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
|
|
|
| 75 |
with gr.Column(scale=1, min_width=320):
|
| 76 |
prompt = gr.Textbox(
|
| 77 |
+
label="✨ Prompt",
|
| 78 |
+
placeholder="Describe the image you want...",
|
| 79 |
lines=5,
|
|
|
|
|
|
|
| 80 |
)
|
| 81 |
+
|
| 82 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 83 |
with gr.Row():
|
| 84 |
+
height = gr.Slider(512, 2048, 1024, step=64, label="Height")
|
| 85 |
+
width = gr.Slider(512, 2048, 1024, step=64, label="Width")
|
| 86 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
num_inference_steps = gr.Slider(
|
| 88 |
+
1, 20, 9, step=1, label="Inference Steps"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
)
|
| 90 |
+
|
| 91 |
+
randomize_seed = gr.Checkbox(
|
| 92 |
+
label="🎲 Random Seed", value=True
|
| 93 |
+
)
|
| 94 |
+
seed = gr.Number(
|
| 95 |
+
label="Seed", value=42, precision=0, visible=False
|
| 96 |
+
)
|
| 97 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
randomize_seed.change(
|
| 99 |
+
lambda x: gr.Number(visible=not x),
|
| 100 |
+
randomize_seed,
|
| 101 |
+
seed,
|
| 102 |
)
|
| 103 |
+
|
| 104 |
generate_btn = gr.Button(
|
| 105 |
"🚀 Generate Image",
|
| 106 |
variant="primary",
|
| 107 |
size="lg",
|
|
|
|
| 108 |
)
|
| 109 |
+
|
|
|
|
| 110 |
gr.Examples(
|
| 111 |
examples=examples,
|
| 112 |
inputs=[prompt],
|
| 113 |
+
label="💡 Example Prompts",
|
|
|
|
| 114 |
)
|
| 115 |
+
|
|
|
|
| 116 |
with gr.Column(scale=1, min_width=320):
|
| 117 |
output_image = gr.Image(
|
| 118 |
label="Generated Image",
|
| 119 |
type="pil",
|
|
|
|
| 120 |
height=600,
|
| 121 |
buttons=["download", "share"],
|
| 122 |
)
|
| 123 |
+
|
| 124 |
used_seed = gr.Number(
|
| 125 |
label="🎲 Seed Used",
|
| 126 |
interactive=False,
|
|
|
|
| 127 |
)
|
| 128 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
generate_btn.click(
|
| 130 |
+
generate_image,
|
| 131 |
inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed],
|
| 132 |
outputs=[output_image, used_seed],
|
| 133 |
)
|
| 134 |
+
|
|
|
|
| 135 |
prompt.submit(
|
| 136 |
+
generate_image,
|
| 137 |
inputs=[prompt, height, width, num_inference_steps, seed, randomize_seed],
|
| 138 |
outputs=[output_image, used_seed],
|
| 139 |
)
|
| 140 |
|
| 141 |
+
# ================== LAUNCH ==================
|
| 142 |
if __name__ == "__main__":
|
| 143 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|